Skip to content
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

[Capture] Add execution support for broadcast_in_dim_p and iota_p #6865

Open
wants to merge 10 commits into
base: dynamic-capture-hop-2
Choose a base branch
from

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Jan 20, 2025

Context:

While we can capture the creation of arrays with a dynamic shape like jnp.ones, jnp.zeros, and jnp.arange, jax does not support executing these equations:

def f(n):
    return jax.numpy.ones((n,))

jaxpr = jax.make_jaxpr(f)(2)
jax.core.eval_jaxpr(jaxpr.jaxpr, jaxpr.consts, 4)
...
XlaRuntimeError: UNKNOWN: [/var/folders/k1/0v_kvphn55lgf_45kntf1hqm0000gq/T/ipykernel_16612/1590642475.py:2:11](http://localhost:8888/var/folders/k1/0v_kvphn55lgf_45kntf1hqm0000gq/T/ipykernel_16612/1590642475.py#line=1): error: 'mhlo.dynamic_broadcast_in_dim' op can't be translated to XLA HLO
...

I think jax.core.eval_jaxpr is trying to jit and lower the broadcast_in_dim primitive, but can't. This problem propogates to executing any jaxpr with a PlxprInterpreter

Description of the Change:

Registers special handling for broadcast_in_dim and iota.

Benefits:

We can execute the creation of dynamically shaped arrays.

Possible Drawbacks:

Sensitive to the location of jax.lax.broadcast_in_dim_p and jax.lax.iota_p. So this may break if jax moves things around.

Related GitHub Issues:

[sc-82653] [sc-82656]

Copy link

codecov bot commented Jan 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (dynamic-capture-hop-2@ab090bf). Learn more about missing BASE report.

Additional details and impacted files
@@                   Coverage Diff                    @@
##             dynamic-capture-hop-2    #6865   +/-   ##
========================================================
  Coverage                         ?   99.60%           
========================================================
  Files                            ?      477           
  Lines                            ?    45263           
  Branches                         ?        0           
========================================================
  Hits                             ?    45083           
  Misses                           ?      180           
  Partials                         ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@albi3ro albi3ro changed the title [Capture] Add execution support for broadcast_in_dim_p [Capture] Add execution support for broadcast_in_dim_p and iota_p Jan 21, 2025
@mudit2812 mudit2812 self-requested a review January 21, 2025 19:07
Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

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

Looks good. I'd suggest expanding the docstring of _fill_in_shape_with_dyn_shape, and also adding some more inline comments to the primitive registrations about the changes. I had to look at the jaxpr of a dummy function locally to convince myself why this works 😅 . Maybe it's worth including some info in the intro_to_dynamic_shapes.md file.

pennylane/capture/base_interpreter.py Outdated Show resolved Hide resolved
doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
tests/capture/test_base_interpreter.py Outdated Show resolved Hide resolved
tests/devices/qubit/test_dq_interpreter.py Outdated Show resolved Hide resolved
Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

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

Looks good. I think a few more dev comments should be added, otherwise this looks approval ready to me.

tests/capture/test_base_interpreter.py Outdated Show resolved Hide resolved
tests/devices/qubit/test_dq_interpreter.py Outdated Show resolved Hide resolved
pennylane/capture/base_interpreter.py Outdated Show resolved Hide resolved
Copy link
Contributor

@PietropaoloFrisoni PietropaoloFrisoni left a comment

Choose a reason for hiding this comment

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

Looks excellent, I just don't understand the examples in the docstrings. If you could clarify that it would be great 👍

pennylane/capture/base_interpreter.py Outdated Show resolved Hide resolved
Copy link
Contributor

@PietropaoloFrisoni PietropaoloFrisoni left a comment

Choose a reason for hiding this comment

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

🚀

pennylane/capture/base_interpreter.py Show resolved Hide resolved
pennylane/capture/base_interpreter.py Outdated Show resolved Hide resolved
pennylane/capture/base_interpreter.py Show resolved Hide resolved
pennylane/capture/base_interpreter.py Show resolved Hide resolved
Co-authored-by: Pietropaolo Frisoni <[email protected]>
Co-authored-by: Mudit Pandey <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants