Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Add support to AutoGraph for #769
Add support to AutoGraph for #769
Changes from all commits
c893f17
ded9e81
6add6c5
7981c3c
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Nice example! Maybe we could also include the function output to show what it does:
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.
I wonder if it would be more maintainable to have a single function which dispatches to each operator, say in case we wanted to expand the functionality a bit more, or to have each operator have a separate function. What is your opinion on it?
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.
I did try to use a helper to generate the different functions, similar to
_logical_op
, but I couldn't figure out a nice way to put+=
and friends in a lambda, so this seemed like the next best option.A possibly more maintainable alternative would be to have a single
update_item_with_op
function that takes an extra parameter and uses that at runtime to determine which JAX function / operator to dispatch to, something likeWhen generating the call in
operator_update.transform
we would just stick in the appropriate constant. I do see two cons to this approach: first, it relies on a set of magic constants, and second, it adds extra compares and branches in the output that could negatively impact performance. Those may not be big concerns though; the calls are only generated from one place and this function isn't exposed to the user (right?) so magic constants aren't that problematic IMO, and if the JAX compiler is smart enough with inlining / constant propagation / dead code elimination it will get rid of the extra compares and branches.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.
Thanks Spencer, that's a good analysis. The single
update_item_with_op
is what I had in mind, but your reasoning is sound so I'm happy to leave the functions as they are :)Something that might be interesting to you is that JAX is not capable of compiling most Python code. Instead, it will compile an instruction if either:
jax.numpy.abs(x)
)jax.numpy.array(2) * 3
)Consequently, the comparisons and branches all happen before the JAX compiler kicks in (when Python executes the code). There are however special functions that allow you compile a conditional (if statement) for example.
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.
Looks great 👍
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.