-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[no-thunks] Deprecate old form of cond and remove some tracing layers.
#33710
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 @dougalm, 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 refactors the internal JAXPR tracing and constant handling mechanisms within JAX, particularly impacting control flow primitives like 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 is a nice refactoring that deprecates an old form of cond and simplifies some of the internal tracing logic. The changes are consistent across multiple files and the test suite is updated accordingly. I've found a few critical bugs in the new helper functions in jax/_src/lax/control_flow/common.py related to incorrect attribute access on ClosedJaxpr objects, which would lead to AttributeErrors. I also spotted an unpacking error in jax/_src/lax/control_flow/solves.py that would cause a ValueError at runtime. I've provided detailed comments and suggestions to fix these issues. Besides these, the refactoring looks solid.
| def _pad_constvars(jaxpr: core.ClosedJaxpr, num_consts: int, | ||
| left: tuple[core.AvalQDD, ...], | ||
| right: tuple[core.AbstractValue, ...]) -> core.ClosedJaxpr: | ||
| def make_var(aq): | ||
| return core.Var(aq.aval, initial_qdd=aq.qdd, final_qdd=aq.qdd) | ||
| constvars = [*map(make_var, left), *jaxpr.constvars, *map(make_var, right)] | ||
| effs = pe._renumber_effects([*constvars, *jaxpr.invars], | ||
| [*jaxpr.constvars, *jaxpr.invars], jaxpr.effects) | ||
| jaxpr = jaxpr.replace(constvars=constvars, effects=effs) | ||
| config.enable_checks.value and core.check_jaxpr(jaxpr) | ||
| invars = [*map(make_var, left), *jaxpr.invars[:num_consts], | ||
| *map(make_var, right), *jaxpr.invars[num_consts:]] | ||
| effs = pe._renumber_effects(invars, jaxpr.invars, jaxpr.effects) | ||
| jaxpr = jaxpr.replace(jaxpr=jaxpr.jaxpr.replace(invars=invars, effects=effs)) | ||
| config.enable_checks.value and core.check_jaxpr(jaxpr.jaxpr) | ||
| return jaxpr |
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.
The jaxpr argument is a ClosedJaxpr, but you are trying to access attributes like invars and effects which belong to the inner Jaxpr object. This will raise an AttributeError. You should access them via jaxpr.jaxpr.
| def _pad_constvars(jaxpr: core.ClosedJaxpr, num_consts: int, | |
| left: tuple[core.AvalQDD, ...], | |
| right: tuple[core.AbstractValue, ...]) -> core.ClosedJaxpr: | |
| def make_var(aq): | |
| return core.Var(aq.aval, initial_qdd=aq.qdd, final_qdd=aq.qdd) | |
| constvars = [*map(make_var, left), *jaxpr.constvars, *map(make_var, right)] | |
| effs = pe._renumber_effects([*constvars, *jaxpr.invars], | |
| [*jaxpr.constvars, *jaxpr.invars], jaxpr.effects) | |
| jaxpr = jaxpr.replace(constvars=constvars, effects=effs) | |
| config.enable_checks.value and core.check_jaxpr(jaxpr) | |
| invars = [*map(make_var, left), *jaxpr.invars[:num_consts], | |
| *map(make_var, right), *jaxpr.invars[num_consts:]] | |
| effs = pe._renumber_effects(invars, jaxpr.invars, jaxpr.effects) | |
| jaxpr = jaxpr.replace(jaxpr=jaxpr.jaxpr.replace(invars=invars, effects=effs)) | |
| config.enable_checks.value and core.check_jaxpr(jaxpr.jaxpr) | |
| return jaxpr | |
| def _pad_constvars(jaxpr: core.ClosedJaxpr, num_consts: int, | |
| left: tuple[core.AvalQDD, ...], | |
| right: tuple[core.AbstractValue, ...]) -> core.ClosedJaxpr: | |
| def make_var(aq): | |
| return core.Var(aq.aval, initial_qdd=aq.qdd, final_qdd=aq.qdd) | |
| invars = [*map(make_var, left), *jaxpr.jaxpr.invars[:num_consts], | |
| *map(make_var, right), *jaxpr.jaxpr.invars[num_consts:]] | |
| effs = pe._renumber_effects(invars, jaxpr.jaxpr.invars, jaxpr.jaxpr.effects) | |
| jaxpr = jaxpr.replace(jaxpr=jaxpr.jaxpr.replace(invars=invars, effects=effs)) | |
| config.enable_checks.value and core.check_jaxpr(jaxpr.jaxpr) | |
| return jaxpr |
| @weakref_lru_cache | ||
| def _dedup_consts(jaxpr, const_ids): | ||
| def _dedup_consts(jaxpr, num_consts, const_ids): | ||
| newvars = {} | ||
| canonicalize = {v: newvars.setdefault(constid, v) | ||
| for constid, v in zip(const_ids, jaxpr.constvars)} | ||
| for constid, v in zip(const_ids, jaxpr.invars[:num_consts])} | ||
| eqns = [e.replace(invars=[canonicalize.get(x, x) if isinstance(x, core.Var) | ||
| else x for x in e.invars]) for e in jaxpr.eqns] | ||
| outvars = [canonicalize.get(x, x) if isinstance(x, core.Var) else x | ||
| for x in jaxpr.outvars] | ||
| constvars = list(newvars.values()) | ||
| effs = pe._renumber_effects( | ||
| [*constvars, *jaxpr.invars], | ||
| [*map(canonicalize.get, jaxpr.constvars), *jaxpr.invars], jaxpr.effects) | ||
| jaxpr = jaxpr.replace(constvars=constvars, eqns=eqns, outvars=outvars, | ||
| effects=effs) | ||
| invars = [*list(newvars.values()), *jaxpr.invars[num_consts:]] | ||
| effs = pe._renumber_effects(invars, | ||
| [*map(canonicalize.get, jaxpr.invars[:num_consts]), *jaxpr.invars[num_consts:]], | ||
| jaxpr.effects) | ||
| jaxpr = jaxpr.replace(jaxpr=jaxpr.jaxpr.replace(invars=invars, eqns=eqns, outvars=outvars, | ||
| effects=effs)) | ||
| config.enable_checks.value and core.check_jaxpr(jaxpr) | ||
| return jaxpr |
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.
Similar to _pad_constvars, the jaxpr argument is a ClosedJaxpr, but you are accessing attributes of the inner Jaxpr object directly (e.g., jaxpr.invars, jaxpr.eqns). This will raise an AttributeError. You should use jaxpr.jaxpr to access them. Also, check_jaxpr at the end should be called on jaxpr.jaxpr. It would also be good to add type hints to this function for clarity.
| @weakref_lru_cache | |
| def _dedup_consts(jaxpr, const_ids): | |
| def _dedup_consts(jaxpr, num_consts, const_ids): | |
| newvars = {} | |
| canonicalize = {v: newvars.setdefault(constid, v) | |
| for constid, v in zip(const_ids, jaxpr.constvars)} | |
| for constid, v in zip(const_ids, jaxpr.invars[:num_consts])} | |
| eqns = [e.replace(invars=[canonicalize.get(x, x) if isinstance(x, core.Var) | |
| else x for x in e.invars]) for e in jaxpr.eqns] | |
| outvars = [canonicalize.get(x, x) if isinstance(x, core.Var) else x | |
| for x in jaxpr.outvars] | |
| constvars = list(newvars.values()) | |
| effs = pe._renumber_effects( | |
| [*constvars, *jaxpr.invars], | |
| [*map(canonicalize.get, jaxpr.constvars), *jaxpr.invars], jaxpr.effects) | |
| jaxpr = jaxpr.replace(constvars=constvars, eqns=eqns, outvars=outvars, | |
| effects=effs) | |
| invars = [*list(newvars.values()), *jaxpr.invars[num_consts:]] | |
| effs = pe._renumber_effects(invars, | |
| [*map(canonicalize.get, jaxpr.invars[:num_consts]), *jaxpr.invars[num_consts:]], | |
| jaxpr.effects) | |
| jaxpr = jaxpr.replace(jaxpr=jaxpr.jaxpr.replace(invars=invars, eqns=eqns, outvars=outvars, | |
| effects=effs)) | |
| config.enable_checks.value and core.check_jaxpr(jaxpr) | |
| return jaxpr | |
| @weakref_lru_cache | |
| def _dedup_consts(jaxpr: core.ClosedJaxpr, num_consts: int, const_ids: tuple) -> core.ClosedJaxpr: | |
| newvars = {} | |
| canonicalize = {v: newvars.setdefault(constid, v) | |
| for constid, v in zip(const_ids, jaxpr.jaxpr.invars[:num_consts])} | |
| eqns = [e.replace(invars=[canonicalize.get(x, x) if isinstance(x, core.Var) | |
| else x for x in e.invars]) for e in jaxpr.jaxpr.eqns] | |
| outvars = [canonicalize.get(x, x) if isinstance(x, core.Var) else x | |
| for x in jaxpr.jaxpr.outvars] | |
| invars = [*list(newvars.values()), *jaxpr.jaxpr.invars[num_consts:]] | |
| effs = pe._renumber_effects(invars, | |
| [*map(canonicalize.get, jaxpr.jaxpr.invars[:num_consts]), *jaxpr.jaxpr.invars[num_consts:]], | |
| jaxpr.jaxpr.effects) | |
| jaxpr = jaxpr.replace(jaxpr=jaxpr.jaxpr.replace(invars=invars, eqns=eqns, outvars=outvars, | |
| effects=effs)) | |
| config.enable_checks.value and core.check_jaxpr(jaxpr.jaxpr) | |
| return jaxpr |
jax/_src/lax/control_flow/solves.py
Outdated
| solve_jaxpr, solution_tree, solve_consts = pe.trace_to_jaxpr( | ||
| partial(solve, f), in_args_tree, guess_avals, solve_debug) |
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.
pe.trace_to_jaxpr returns a 2-tuple (ClosedJaxpr, PyTreeDef). You are trying to unpack it into three variables, which will cause a ValueError.
| solve_jaxpr, solution_tree, solve_consts = pe.trace_to_jaxpr( | |
| partial(solve, f), in_args_tree, guess_avals, solve_debug) | |
| solve_jaxpr, solution_tree = pe.trace_to_jaxpr( | |
| partial(solve, f), in_args_tree, guess_avals, solve_debug) |
jax/_src/lax/control_flow/common.py
Outdated
| # TODO(dougalm): this seems way too complicated. Why not allow different consts for each | ||
| # branch of a switch? | ||
| def _merge_common_consts( | ||
| jaxprs: Sequence[core.Jaxpr], |
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.
8b7db85 to
ec3ecde
Compare
This reduces the stack depth in `cond` and exposes fewer internals.
ec3ecde to
4cbcbe7
Compare
This reduces the stack depth in
condand exposes fewer internals.