Curious if there's a way to get gradients wrt final instead of initial values for mutating functions

24 views
Skip to first unread message

Aaron Kriegman

unread,
May 29, 2025, 3:18:53 PMMay 29
to Enzyme AD
Hi all, thanks for building this awesome project! I have a sorta specific question I'm curious about.

Consider the following example:
```
double f(double& x) {
  x = x * x;
  return x * x;
}
```
Here the gradient of `f` at `x = 1` wrt the initial value of `x` is 4, and the gradient wrt the final value of `x` is 2. Enzyme calculates the former, but what if I'm interested in the latter? I assume there's no api for this in Enzyme, but what would it take to hack this on?

ChatGPT suggested that I could split the function into an outer mutating function and an inner function that takes the final values of `x`, and take the gradient of the inner function. But for my application I don't know the function up front, so I would need a way to make this split automatically. I'm imagining there's a cleaner solution.

I'm still studying the internals of Enzyme and I'll probably find the answer to my question, but I was curious if anyone has any thoughts on this.

Best,
Aaron
Reply all
Reply to author
Forward
0 new messages