# Different api for scalar input in Enzyme.jl

**URL:** <https://discourse.julialang.org/t/different-api-for-scalar-input-in-enzyme-jl/107882>\
**Category:** General Usage\
**Tags:** enzyme\
**Created:** [December 20, 2023, 11:00pm UTC](https://discourse.julialang.org/t/different-api-for-scalar-input-in-enzyme-jl/107882 "2023-12-20T23:00:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwalker](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@jwalker](https://discourse.julialang.org/u/jwalker)\
**Post date:** [December 20, 2023, 11:00pm UTC](https://discourse.julialang.org/t/different-api-for-scalar-input-in-enzyme-jl/107882/1 "2023-12-20T23:00:08Z")

</div>

I’m puzzled by a difference in the API of Reverse mode with functions of scalar inputs versus those of other (e.g. vector inputs). It seems to be the case that for a scalar function of a scalar input, you must use Reverse mode with Active arguments, or wrap them as an array.

Take this simple example. It seems like the correct way to use Duplicated arguments for a scalar inputs is to wrap them in an array structure as I’ve done in the fourth call below.

```julia
using Enzyme

g(x) = sum(x.^2)

y = 1.;
dy = 0.;
@show autodiff(ReverseWithPrimal, g, Active, Duplicated(y,dy))
@show y,dy

y = [1., 2.];
dy = [0.,0.];
@show autodiff(ReverseWithPrimal, g, Active, Duplicated(y,dy))
@show y,dy

y = 1.;
@show dy=autodiff(Reverse, g, Active, Active(y))[1][1]
@show y,dy

y = [1.];
dy = [0.];
@show autodiff(ReverseWithPrimal, g, Active, Duplicated(y,dy))
@show y,dy

```

Output:

```julia
autodiff(ReverseWithPrimal, g, Active, Duplicated(y, dy)) = ((nothing,), 1.0)
(y, dy) = (1.0, 0.0)
autodiff(ReverseWithPrimal, g, Active, Duplicated(y, dy)) = ((nothing,), 5.0)
(y, dy) = ([1.0, 2.0], [2.0, 4.0])
dy = ((autodiff(Reverse, g, Active, Active(y)))[1])[1] = 2.0
(y, dy) = (1.0, 2.0)
autodiff(ReverseWithPrimal, g, Active, Duplicated(y, dy)) = ((nothing,), 1.0)
(y, dy) = ([1.0], [2.0])

```

Is there a design reason why Duplicated won’t work with scalars?

---

<div class="post-metadata">

**Author:** ![jgreener64](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgreener64/32/2483_2.png) [@jgreener64](https://discourse.julialang.org/u/jgreener64)\
**Post date:** [December 20, 2023, 11:35pm UTC](https://discourse.julialang.org/t/different-api-for-scalar-input-in-enzyme-jl/107882/2 "2023-12-20T23:35:16Z")

</div>

Scalars are passed by value to `Duplicated` whereas arrays are passed by reference. In order to modify the underlying data during autodiff, it needs to be passed by reference.

---

<div class="post-metadata">

**Author:** ![jwalker](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@jwalker](https://discourse.julialang.org/u/jwalker)\
**Post date:** [December 20, 2023, 11:50pm UTC](https://discourse.julialang.org/t/different-api-for-scalar-input-in-enzyme-jl/107882/3 "2023-12-20T23:50:56Z")

</div>

Got it. Thanks!
