-
Notifications
You must be signed in to change notification settings - Fork 47
Fix compilation of Jacobian with for loops and vmap #332
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
Open
rmoyard
wants to merge
47
commits into
main
Choose a base branch
from
fix_forloop_enzyme
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello. You may have forgotten to update the changelog!
|
rmoyard
commented
Mar 19, 2024
dime10
reviewed
Mar 19, 2024
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!
Co-authored-by: David Ittah <[email protected]>
Testing this fix on the following example results in very long (infinite?) compile or runtime: import pennylane as qml
import jax
from jax import numpy as jnp
import catalyst
n_wires = 5
data = jnp.sin(jnp.mgrid[-2:2:0.2].reshape(n_wires, -1)) ** 3
targets = jnp.array([-0.2, 0.4, 0.35, 0.2])
dev_name = "lightning.qubit"
dev = qml.device(dev_name, wires=n_wires)
@qml.qnode(dev)
def circuit(data, weights):
"""Quantum circuit ansatz"""
for i in range(n_wires):
qml.RY(data[i], wires=i)
for i in range(n_wires):
qml.RX(weights[i, 0], wires=i)
qml.RY(weights[i, 1], wires=i)
qml.RX(weights[i, 2], wires=i)
qml.CNOT(wires=[i, (i + 1) % n_wires])
return qml.expval(qml.sum(*[qml.PauliZ(i) for i in range(n_wires)]))
# try broadcasting
jit_circuit = catalyst.qjit(catalyst.vmap(circuit, in_axes = (1, None)))
def my_model(data, weights, bias):
# works with default.qubit
if dev_name == "default.qubit":
return circuit(data, weights) + bias
# works with lightning.qubit, not broadcasted
# return jnp.array([circuit(jnp.array(d), weights) for d in data.T])
# only works with loss_fn, fails at grad step
return jit_circuit(data, weights) + bias
@jax.jit
def loss_fn(params, data, targets):
predictions = my_model(data, params["weights"], params["bias"])
loss = jnp.sum((targets - predictions) ** 2 / len(data))
return loss
weights = jnp.ones([n_wires, 3])
bias = jnp.array(0.)
params = {"weights": weights, "bias": bias}
print(loss_fn(params, data, targets))
print(jax.grad(loss_fn)(params, data, targets)) # runs for > 20 minutes |
[sc-49763] |
Closes #294 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Context:
Some gradients/jacobians do not compile because the insertion point in the lowering is not correctly set.
Description of the Change:
The insertion is the call op and not the body of the function.
Benefits:
We can compile and run more derivatives of vmap and for loop.
Drawback
It compiles gradient acting on vmap but returns wrong results, see xfailed test.
[sc-59758]