# Enzyme - Incorrect (?) Forward results on function that returns a literal value within a tuple

**URL:** <https://discourse.julialang.org/t/enzyme-incorrect-forward-results-on-function-that-returns-a-literal-value-within-a-tuple/126443>\
**Category:** Specific Domains\
**Tags:** enzyme\
**Created:** [March 1, 2025, 2:13am UTC](https://discourse.julialang.org/t/enzyme-incorrect-forward-results-on-function-that-returns-a-literal-value-within-a-tuple/126443 "2025-03-01T02:13:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jl\_enthusiast](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@jl\_enthusiast](https://discourse.julialang.org/u/jl_enthusiast)\
**Post date:** [March 1, 2025, 2:13am UTC](https://discourse.julialang.org/t/enzyme-incorrect-forward-results-on-function-that-returns-a-literal-value-within-a-tuple/126443/1 "2025-03-01T02:13:25Z")

</div>

The issue can be reduced to the following semi-MWE:

Consider a function that return a literal number, and some vector(s):

```julia
using Enzyme
function tester(x::AbstractVector{T}) where T
     return (T(pi), sum(x), x.^2)
end
xi = [0.0, 2.0, 0.0]
derivs = Enzyme.autodiff(Forward,tester, Duplicated, BatchDuplicated(xi, onehot(xi)))

```

Which yields:

```julia
(var"1" = (var"1" = (3.141592653589793, 1.0, [0.0, 0.0, 0.0]), var"2" = (3.141592653589793, 1.0, [0.0, 4.0, 0.0]), var"3" = (3.141592653589793, 1.0, [0.0, 0.0, 0.0])),)

```

I.e. the derivate of the 1st element of the tupple (the literal value) is incorrect.

Note that a very crude “fix” is:

```julia
function tester1(x::AbstractVector{T}) where T
      return (T(x[begin]*pi/x[begin]), sum(x), x.^2)
end
derivs1 = Enzyme.autodiff(Forward,tester1, Duplicated, BatchDuplicated(xi, onehot(xi)))
#(var"1" = (var"1" = (0.0, 1.0, [0.0, 0.0, 0.0]), var"2" = (0.0, 1.0, [0.0, 4.0, 0.0]), var"3" = (0.0, 1.0, [0.0, 0.0, 0.0])),)

```

I am not sure if this is a bug or just a limitation of the forward mode (could not find anything in the docs regarding such).

---

<div class="post-metadata">

**Author:** ![wsmoses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsmoses/32/26497_2.png) [@wsmoses](https://discourse.julialang.org/u/wsmoses)\
**Post date:** [March 1, 2025, 2:21am UTC](https://discourse.julialang.org/t/enzyme-incorrect-forward-results-on-function-that-returns-a-literal-value-within-a-tuple/126443/2 "2025-03-01T02:21:08Z")

</div>

seems like a bug, open an issue?

---

<div class="post-metadata">

**Author:** ![jl\_enthusiast](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@jl\_enthusiast](https://discourse.julialang.org/u/jl_enthusiast)\
**Post date:** [March 1, 2025, 2:30am UTC](https://discourse.julialang.org/t/enzyme-incorrect-forward-results-on-function-that-returns-a-literal-value-within-a-tuple/126443/3 "2025-03-01T02:30:17Z")

</div>

Is this MWE ok or should I try to reduce it further?

---

<div class="post-metadata">

**Author:** ![wsmoses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsmoses/32/26497_2.png) [@wsmoses](https://discourse.julialang.org/u/wsmoses)\
**Post date:** [March 1, 2025, 2:35am UTC](https://discourse.julialang.org/t/enzyme-incorrect-forward-results-on-function-that-returns-a-literal-value-within-a-tuple/126443/4 "2025-03-01T02:35:45Z")

</div>

I think that’s fine
