I really find this method of training policies really interesting: instead of treating the simulator as a black box, you backpropagate through the physics.
Say you want a policy
You want
Zeroth-order: the simulator is a black box
This is what PPO and every REINFORCE-flavoured method do. You never look inside
It's called zeroth-order because it only ever uses values of the reward, never its derivative. That makes it incredibly robust:
First-order: the simulator is just another layer
If
Now the gradient isn't "this random action happened to work", it's "if your knee torque were a bit higher, your body would have been 2 mm further forward". One rollout gives you an actual derivative, not a noisy estimate of one. In theory that's orders of magnitude more sample-efficient.
So why isn't everybody doing this? Three reasons.
Problem 1: long horizons explode
Look at the recursion above:
The fix that made this practical is SHAC (Short-Horizon Actor-Critic, Xu et al. 2022): only backprop through a short window of
The window starts from a state with stop_gradient on it, so gradients never travel further back than
Problem 2: contacts have no gradient
This is the big one for legged robots. Inside the contact solver there's a line that decides whether a contact exists at all. In MJX it's literally:
active = pos < 0 # is the foot below the floor?That's a step function. Its derivative is zero everywhere (and undefined at the edge). So if a foot is 2 cm above the ground, the gradient has no idea that pushing it down would give it traction. The policy can't learn "put your foot down" from a signal that is exactly zero.
A toy version, a foot 2 cm above the floor that only gets friction if it touches:
import jax
import jax.numpy as jnp
def traction(a, kappa=None):
foot_z = 0.02 - a # action pushes the foot down
if kappa is None:
active = jnp.where(foot_z < 0, 1.0, 0.0) # hard switch
else:
active = jax.nn.sigmoid(-foot_z * kappa) # soft switch
return active * 0.8 * 1.0 * 9.81 # mu * m * g, only while touching
jax.grad(traction)(0.0) # 0.0 -> learns nothing
jax.grad(lambda a: traction(a, kappa=300.0))(0.0) # 5.81 -> "push the foot down"The fix from Schwarke et al. is exactly that: replace the step with a sigmoid of sharpness
The catch is that the soft version also changes the physics. With
You can also get the best of both worlds with a straight-through estimator: the hard switch in the forward pass, the sigmoid's derivative in the backward pass:
soft = jax.nn.sigmoid(-pos * kappa)
active = soft + jax.lax.stop_gradient((pos < 0) - soft)
# forward: exactly (pos < 0) backward: d(soft)/d(pos)The simulation stays bit-identical to the real one, and only the gradient is "lying". It's the same trick people use to backprop through quantization.
Problem 3: NaNs
With PPO, a NaN somewhere in the physics just means a weird episode. Here it goes straight into your weights. Two that bit me:
jnp.linalg.norm gave NaN gradients, even though the forward value was a perfectly fine 0.0. jax.grad(jnp.linalg.norm)(jnp.zeros(3)) returns [nan nan nan]. Wrapping the command in stop_gradient fixed it (it's an input, it should never have had a gradient anyway).nan_to_nums whatever exploded. The forward pass looks fine, but the backward pass still runs through the explosion, and nan * 0 is still nan. The rule: stop_gradient the state on every reset, so nothing flows back through a fall.Is it worth it?
The gradients you get are not perfect. In my MJX reimplementation the smoothed gradients only had a cosine similarity of ~0.37 with finite differences, and I needed 32-step windows where the paper uses 12 to make up for it. But a noisy first-order gradient is still a lot better than a zeroth-order one, and Schwarke et al. show it's good enough to train locomotion that runs on a real ANYmal. Suh et al. also have a great paper on when the first-order gradient is actually worse (spoiler: stiff contacts and discontinuities, exactly the stuff above).
References