Skip to content

Conversation

@mattjj
Copy link
Collaborator

@mattjj mattjj commented Dec 11, 2025

No description provided.

@mattjj mattjj self-assigned this Dec 11, 2025
@mattjj mattjj added the pull ready Ready for copybara import and testing label Dec 11, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 Tracer objects with nondiff_argnums. The change ensures that users receive immediate and clear feedback when attempting to mark a differentiable argument (represented by a Tracer) as non-differentiable, thereby improving the API's predictability and user experience.

Highlights

  • Improved Error Handling: The system now explicitly raises an error when a JAX Tracer object is passed to an argument specified in nondiff_argnums, preventing silent misbehavior.
  • Detailed Error Messages: The new error message provides precise information, including the argument index and the specific path within the argument where the Tracer was detected, aiding in quicker debugging.
  • Refactored Tracer Validation: The previous _stop_gradient utility has been replaced with dedicated _check_tracer functions, allowing for more targeted and informative validation of nondiff_argnums.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines +81 to +83
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.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pull ready Ready for copybara import and testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant