-
Notifications
You must be signed in to change notification settings - Fork 3.3k
improve nondiff argnums error when a tracer is passed #33897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @mattjj, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the error handling within JAX's custom derivatives by introducing a robust mechanism to detect and report the incorrect usage of Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request improves the error message when a JAX tracer is passed as a non-differentiable argument to custom_jvp. Instead of silently stopping the gradient, the new implementation raises an exception with a detailed message, including the argument index and path within the pytree. This is a great improvement for user experience and debugging. I have one suggestion to make the error message even more helpful and to use a more specific exception type.
| pathstr = f' path {keystr(path)}' if path else '' | ||
| raise Exception(f"Tracer of type {x.aval} passed at argument {i}{pathstr}, " | ||
| "but Tracers can't be passed as nondiff_argnums.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great improvement for error reporting! To make it even better, consider using the more specific UnexpectedTracerError (which is already imported) instead of the generic Exception. Also, the error message could be more descriptive to help users understand why the error is happening and how to fix it, similar to the error message in _check_for_tracers for custom_vjp. I've also taken the liberty to rename pathstr to path_str for PEP8 compliance.
| pathstr = f' path {keystr(path)}' if path else '' | |
| raise Exception(f"Tracer of type {x.aval} passed at argument {i}{pathstr}, " | |
| "but Tracers can't be passed as nondiff_argnums.") | |
| path_str = f' at path {keystr(path)}' if path else '' | |
| msg = (f"A JAX Tracer of type {x.aval} was passed to a custom_jvp function " | |
| f"at argument {i}{path_str}, which was specified as non-differentiable. " | |
| "Tracers cannot be passed as non-differentiable arguments. Instead, " | |
| "`nondiff_argnums` should only be used for arguments that are not JAX " | |
| "tracers, e.g. Python scalars, strings, or callables.") | |
| raise UnexpectedTracerError(msg) |
No description provided.