Gradient Fields and Directional Derivatives


From computation graphs to LLM, VLM, and agent gradients

Vishnu Boddeti

Example 1

Reverse-mode autodiff turns a program into reusable local derivatives

A program becomes a computation graph

A computer program represented as a directed acyclic computation graph, with a forward evaluation and a backward gradient sweep

Forward: compute and save intermediate values.   Backward: revisit the same operations in reverse and apply the chain rule.

Reverse mode is local message passing

A computation graph node receiving gradient messages from its children and sending messages to its parents
  • Each child sends one contribution to the adjoint of \(z\).
  • The node sums all paths that depend on \(z\).
  • Local vector–Jacobian products carry the result to the parents.

\[ \bar z=\sum_{s\in \mathrm{Child}(z)} \left(\frac{\partial s}{\partial z}\right)^{\!\top}\bar s \]

The gradient answers every local direction question

● Gradient \(\nabla f\) ● Direction \(u\) ● Projection \(D_u f\)
  • \(\nabla f(x)\) collects the partial derivatives.
  • \(D_u f(x)=\nabla f(x)^\top u\) is the slope along unit direction \(u\).
  • The projection is largest when \(u\) aligns with the gradient.

\[ |D_u f(x)|\le \|\nabla f(x)\|_2 \]

Move the point, then align the direction with the gradient

point \((x,y)\) — drag it \(\nabla f\) \(u(\theta)\)

For \(f(x,y)=x^2+2y^2\), the gradient is \((2x,4y)\).

The directional derivative is largest uphill, zero across the contour, and negative downhill.

Example 2

LLM pretraining clips rare gradient spikes

LLM pretraining sees brief, extreme gradient spikes

HLAT gradient norm over roughly 1.8 trillion training tokens, showing many sharp spikes

The baseline is small; occasional norms jump by orders of magnitude.

Histogram of the number of steps required for gradient spikes to stabilize

Most sustained spikes disappear after one step.

Clipping is a guardrail for the rare spike—not a replacement for the ordinary gradient.

Global-norm clipping caps length without changing direction

radius c raw g clipped g Same direction If ‖g‖₂≤ c: leave g alone If ‖g‖₂>c: set length to c

\[ g_{\mathrm{clip}}=g\min\!\left(1,\frac{c}{\|g\|_2}\right) \]

Increase the raw gradient and watch the update saturate

Move the control to compare the two update rules.
Ordinary gradients pass through. Only the oversized step is rescaled.

Example 3

Input gradients can steer VLM outputs

A real gradient attack changes a VLM caption step by step

Chain of Attack paper example: learned pixel perturbations progressively steer a bird image caption toward a baseball target caption

Each noisy square is a learned pixel perturbation. The bird still looks unchanged, while its caption moves toward the baseball target.

Increase the pixel step and test the local prediction

Move the control away from the linearization point.
Near zero, the gradient is predictive. Farther away, curvature and discrete preprocessing matter.

Example 4

Agent training must assign credit across tool calls

One reward must travel back through an agent trajectory

Task analyzesales Plan choosenext step Search retrieveschema SQL querydatabase Answer writeconclusion Eval R=1 success credit assignment: which decision made the reward possible?

Gradients train the model inside a step. Policy gradients or another credit-assignment method connect discrete tool calls across steps.

Lengthen the trajectory and watch credit become ambiguous

Move the control to add more tool calls before the reward.
A final score says whether the run worked. Credit assignment estimates which earlier actions should change.

Practice problems

Each scenario hides a mathematical structure from this lecture. Identify the structure, justify it, and work through the resulting problem.

Problem 1 Adversarial examples

Which perturbation increases loss fastest?

A red team cannot perturb an input arbitrarily; the hardware allows movement only along one physical sensor direction. Before launching an attack, can you predict which way along that line makes the loss rise fastest?

Model evidence

$L(x_1,x_2)=x_1^2+2x_1x_2+3x_2$, the current input is $x=(1,-1)$, and the feasible unit direction is $v=(1,1)/\sqrt2$.

  1. What first-order quantity measures the loss change along the feasible direction?
  2. Should the attacker move along $v$ or $-v$ initially?
  3. How does this constrained attack compare with unconstrained steepest ascent?
Problem 2 Backpropagation

Differentiate through an encoder and prediction head

A model first builds nonlinear features and then scores them. Instead of differentiating the whole expression from scratch, you have the local derivative of each stage. How do the two pieces snap together?

Model evidence

$h(x_1,x_2)=(x_1+x_2,x_1x_2)$, $g(z_1,z_2)=z_1^2+z_2$, and $x=(1,2)$.

  1. What are the encoder's local linear map and the head's local sensitivity?
  2. What derivative do they give for the full model $g\circ h$?
  3. Does direct differentiation of one output coordinate agree?
Problem 3 Interpretability

Separate two feature effects in a response surface

A response surface predicts model error from data imbalance and label noise. You want to nudge both experimental settings and estimate the effect before running another expensive training job.

Model evidence

$E(a,b)=a^2e^b+ab$, evaluated at $(a,b)=(1,0)$.

  1. What is the isolated local effect of each variable?
  2. What linear approximation describes a simultaneous change $(\Delta a,\Delta b)$?
  3. What error change does it predict for $(0.02,-0.01)$?