# Is there a way to define custom Zygote adjoints for only some arguments of a function?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-define-custom-zygote-adjoints-for-only-some-arguments-of-a-function/31884>\
**Category:** General Usage\
**Created:** [December 5, 2019, 2:44am UTC](https://discourse.julialang.org/t/is-there-a-way-to-define-custom-zygote-adjoints-for-only-some-arguments-of-a-function/31884 "2019-12-05T02:44:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [December 5, 2019, 2:44am UTC](https://discourse.julialang.org/t/is-there-a-way-to-define-custom-zygote-adjoints-for-only-some-arguments-of-a-function/31884/1 "2019-12-05T02:44:16Z")

</div>

Suppose I have some function for which I define custom Zygote adjoints, but the gradient for some of the arguments I don’t need yet and I’d like to leave to implement later. Currently, I could define that adjoint to return `nothing` for those arguments and this will work, but if I accidentally take a gradient w.r.t. to them, it will silently fail and give me `0`/`nothing`. Instead, is there a way to do something kind of like the following?

```julia
f(x,y) = x*y
# suppose the gradient w.r.t. x is easy but I haven't coded up y yet
@adjoint f(x,y) = f(x,y), Δ->(Δ*y, error("not implemented yet"))

gradient(x->f(x,1), 1) # should work 
gradient(y->f(1,y), 1) # should error "not implemented yet"

```

The above does not currently work since the error is always evaluated, even we don’t need the gradient w.r.t. to `y`, but could something similar to this work?

---

<div class="post-metadata">

**Author:** ![MikeInnes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikeinnes/32/3656_2.png) [@MikeInnes](https://discourse.julialang.org/u/MikeInnes)\
**Post date:** [December 5, 2019, 11:08am UTC](https://discourse.julialang.org/t/is-there-a-way-to-define-custom-zygote-adjoints-for-only-some-arguments-of-a-function/31884/2 "2019-12-05T11:08:35Z")

</div>

Basically, no, but this would be a great reason to have thunks (i.e. returning a zero-argument closure that can lazily evaluate to the gradient when needed; or in this case throw an error). We’ve discussed adding that a fair bit, ChainRules already has a design, so it’ll happen eventually.
