jax.lax.stop_gradient() passes a value through unchanged while blocking gradient flow through that path. It is useful when only part of a computation should affect autodiff, such as excluding one branch from an update. section 4.2.3
Applied to an input, it behaves like an identity in the forward pass, but reverse-mode and forward-mode differentiation treat it as non-differentiable. The result is zero gradient for that path. section 4.2.3
Blocking one branch
For f(x, y) = x**2 + stop_gradient(y**2), differentiating at (1.0, 1.0) gives gradients (2.0, 0.0). The x branch still contributes; the y branch is ignored by autodiff. section 4.2.3
Use it when gradient control matters, especially in these settings:
- Exclude variables from one loss while optimizing another. section 4.2.3
- Prevent a branch in a computational graph from influencing updates. section 4.2.3
- Handle cases common in reinforcement learning. section 4.2.3
The computational-graph figure makes the branch-selective gradient behavior easy to see. section 4.2.3
