# Pushforward in Enzyme.jl

**URL:** https://discourse.julialang.org/t/pushforward-in-enzyme-jl/83921
**Category:** Specific Domains
**Tags:** autodiff
**Created:** [July 7, 2022, 9:01pm UTC](https://discourse.julialang.org/t/pushforward-in-enzyme-jl/83921 "2022-07-07T21:01:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)
#### Post date: [July 7, 2022, 9:01pm UTC](https://discourse.julialang.org/t/pushforward-in-enzyme-jl/83921/1 "2022-07-07T21:01:22Z")

</div>

Hello is there a way to obtain the pushforward of a function using `Enzyme.jl`?

Concretely, given the function `foo` below, I want the `enzyme` to produce the function `dfoo`:

```julia
#output in d, mutated
function foo(d,c)
    d.=c.^2
    return nothing
end
#output in d, mutated
function dfoo(dd,c,dc)
    dd.=2.0.*c.*dc
    return nothing
end

```

Which is the Frechet derivative at `c` applied on `dc`. I tried:

```julia
c=2*ones(3)
dc=rand(3)
d=zeros(3)
dd=zeros(3)
Enzyme.autodiff(bla,Dupl(d,dd),Duplicated(c,dc))
dd # all zeros!

```

which does not work, I appreciate any help 🙂

---

<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: [July 13, 2022, 11:06pm UTC](https://discourse.julialang.org/t/pushforward-in-enzyme-jl/83921/2 "2022-07-13T23:06:44Z")

</div>

You’re looking for forward mode AD. The default with Enzyme is to do reverse mode (which propagates derivatives from outputs to inputs, hence when dd remains zeros). Forward mode will do as you desire (propagate derivatives from inputs to outputs).

Try `autodiff(Forward, foo, Duplicated(d,dd), Duplicated(c,dc))`

---

<div class="post-metadata">

### Author: ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)
#### Post date: [July 14, 2022, 4:16pm UTC](https://discourse.julialang.org/t/pushforward-in-enzyme-jl/83921/3 "2022-07-14T16:16:19Z")

</div>

> [@wsmoses](#):
>
> autodiff(Forward, foo, Duplicated(d,dd), Duplicated(c,dc))

Thank you! I have one more question, does this do the right thing and compute the derivative at almost the cost of 2 applications of `foo` or does it scale like the dimensions of the input (size of `c`)?

---

<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: [July 14, 2022, 4:37pm UTC](https://discourse.julialang.org/t/pushforward-in-enzyme-jl/83921/4 "2022-07-14T16:37:54Z")

</div>

Yes forward mode in Enzyme should be approximately 2 times the runtime of the original function, or faster.
