# How to profile Zygote gradients

**URL:** https://discourse.julialang.org/t/how-to-profile-zygote-gradients/90398
**Category:** Machine Learning
**Created:** [November 17, 2022, 12:59pm UTC](https://discourse.julialang.org/t/how-to-profile-zygote-gradients/90398 "2022-11-17T12:59:54Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![renatobellotti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/renatobellotti/32/38793_2.png) [@renatobellotti](https://discourse.julialang.org/u/renatobellotti)
#### Post date: [November 17, 2022, 12:59pm UTC](https://discourse.julialang.org/t/how-to-profile-zygote-gradients/90398/1 "2022-11-17T12:59:54Z")

</div>

Hi,

I’m currently running an expensive optimisation with a code that uses Zygote for automatic differentiation. The gradient computation seems to dominate the computational cost. Is it possible to profile which parts of the code cause an expensive gradient computation?

---

<div class="post-metadata">

### Author: ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)
#### Post date: [November 17, 2022, 9:00pm UTC](https://discourse.julialang.org/t/how-to-profile-zygote-gradients/90398/2 "2022-11-17T21:00:23Z")

</div>

Any profiler you’re comfortable with should work. There are two main internal functions to look for:

1. `_pullback(::F, args...)`: this is the augmented primal function/foward pass of a particular callable of type `F`. You can expect to see it show up roughly in the same place you’d usually see the same method of `F` when not differentiating.
2. `Pullback{F, ...}` or `∂(F)`: this is the wrapper that handles the backwards pass. It will forward to a ChainRules `rrule` or Zygote `@adjoint` if one exists, and otherwise contains an automatically generated pullback.

See [Changing the Primal · ChainRules](https://juliadiff.org/ChainRulesCore.jl/dev/design/changing_the_primal.html) for more on the terminology above. Note that because Zygote uses generated functions, some line numbers will be fancy. Wherever possible though, Zygote tries to preserve the original source locations of function calls so that you can see which method is being sent through AD.

---

<div class="post-metadata">

### Author: ![renatobellotti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/renatobellotti/32/38793_2.png) [@renatobellotti](https://discourse.julialang.org/u/renatobellotti)
#### Post date: [January 19, 2023, 3:43pm UTC](https://discourse.julialang.org/t/how-to-profile-zygote-gradients/90398/3 "2023-01-19T15:43:46Z")

</div>

Thanks for the answer!
