I’m running into an issue using ReverseDiff. It seems to be evaluating my function incorrectly when I prerecord a gradient tape:

```
using ReverseDiff: GradientTape, GradientConfig, gradient!, compile
using DiffResults: value, GradientResult
# Compute the gradient using a gradient tape and return the value
function reversediff_tape_eval(func, val)
f_tape = GradientTape(func, rand(length(val)))
compiled_f_tape = compile(f_tape)
results = GradientResult(similar(val))
gradient!(results, compiled_f_tape, val)
return DiffResults.value(results)
end
# Compute the gradient without a gradient tape and return the value
function reversediff_eval(func, val)
results = GradientResult(similar(val))
gradient!(results, func, val)
return DiffResults.value(results)
end
# Simple Example
function f1(xs)
function f2(p)
s = 0.0
for x in xs
if x[1] > p[1]
s += x[1]
else
s += x[2]
end
end
return s
end
return f2
end
# Example input
xs = [rand(2) for i=1:5] # e.g. [[0.698, 0.035], [0.828, 0.666], [0.209, 0.384], [0.661, 0.089], [0.394, 0.812]]
a = rand(2) # e.g. [0.705, 0.092]
f = f1(xs)
f(a) # 2.149 Correct
reversediff_eval(f, a) # 2.149 Correct
reversediff_tape_eval(f, a) # 2.966 Incorrect
```

What’s even more bizarre is that I get a different results every time I evaluate reversediff_tape_eval…

Anyone have any idea what’s going on?