Server Virtualization

2026-05-03 04:43:08

How to Implement Gradient-Based Long-Horizon Planning with World Models Using GRASP

Step-by-step guide to implementing GRASP gradient-based long-horizon planning with world models, covering virtual states, stochastic exploration, and gradient reshaping to overcome optimization challenges.

Introduction

Modern world models can predict long sequences of future observations in high-dimensional visual spaces, making them look like general-purpose simulators. However, using these models for long-horizon planning remains fragile due to ill-conditioned optimization, non-greedy local minima, and subtle gradient issues through high-dimensional latent spaces. The GRASP method tackles these challenges with three key innovations: lifting trajectories into virtual states for parallel optimization, injecting stochasticity into state iterates for exploration, and reshaping gradients to give action signals while avoiding brittle state-input gradients. This guide walks you through implementing a GRASP-style planner step by step.

How to Implement Gradient-Based Long-Horizon Planning with World Models Using GRASP
Source: bair.berkeley.edu

What You Need

  • Learned world model – a predictive model that given current state and action sequence produces next state distribution. Can be latent dynamics (e.g., RSSM) or pixel-level model.
  • Action space – continuous or discrete actions, with bounds if applicable.
  • Latent state space – high-dimensional vector representation (typically 64–512 dimensions).
  • Optimizer – Adam or SGD with momentum, for optimizing action sequences.
  • Gradient computation pipeline – auto-diff framework (PyTorch, JAX) that can handle unrolled trajectories.
  • Planning horizon – number of time steps $H$ you want to plan ahead (e.g., 50–200).
  • Virtual state schedule – number of virtual states per real step (typically 1–5).
  • Exploration noise scale – standard deviation for stochastic perturbations in state space (e.g., 0.1).

Step-by-Step Implementation

Step 1: Set Up the World Model and Define Latent Dynamics

Train or load a world model that maps current state (image, latent vector) and action to next latent state. Ensure it outputs a distribution (e.g., Gaussian mean/logvar) so you can sample. For planning, you'll need to differentiate through the model's forward pass. Typical architecture: encoder → recurrent latent dynamics → decoder.

# Pseudocode: world_model(state, action) -> next_state_distribution
next_state_mean, next_state_logvar = world_model(current_latent, action)

Step 2: Lift Trajectory into Virtual States for Parallel Optimization

GRASP's first trick: instead of unrolling a single trajectory step by step, you maintain virtual states at each time step. For a horizon of $H$ real steps, define $K$ virtual states per step. The action sequence is shared across all virtual states at the same time. This allows gradient computation to be parallel across the $K$ copies, smoothing the loss landscape and avoiding local minima.

Initialize virtual state sequences: for $t=0..H-1$, create $K$ latent vectors. At time 0, all $K$ states are the same (current real state). At later times, they evolve independently but with the same action.

# K virtual states, H steps
virtual_states = [torch.randn(K, latent_dim)] * H

Step 3: Introduce Stochasticity into State Iterates for Exploration

During optimization, add random perturbations to the virtual states at each iteration. This acts as a form of exploration in the latent space, preventing the planner from getting stuck in poor regions. The noise scale should be adjusted: too low = no benefit, too high = unstable.

# At each optimization iteration, add noise
for t in range(H):
    virtual_states[t] += noise_scale * torch.randn_like(virtual_states[t])

This stochasticity is applied before computing the dynamics for the next virtual state, giving a stochastic gradient estimate that helps escape saddle points.

Step 4: Reshape Gradients to Avoid Brittle State-Input Gradients

Naive gradient-based planning differentiates the loss with respect to actions through the world model. This creates two problems: (1) gradients from high-dimensional visual states to actions are noisy and poorly conditioned; (2) gradients vanish/explode over long horizons. GRASP solves this by not backpropagating through the state-transition function for the action gradient. Instead, it computes an approximate gradient that treats each time step's state as independent of earlier actions (using the virtual states). This gives a clean signal directly from the reward/objective to the action, bypassing the fragile state dynamics.

How to Implement Gradient-Based Long-Horizon Planning with World Models Using GRASP
Source: bair.berkeley.edu

Implement the gradient reshaping by stopping gradients on the virtual states when they are used as input to the world model for action gradient computation. Only allow gradients to flow from the reward to the action via a simplified path.

Step 5: Optimize Action Sequences Over Long Horizons

With the setup above, you can now perform gradient descent on the action sequence. Define a loss function (e.g., negative cumulative reward, distance to goal, or value function). At each iteration:

  • Forward pass: For each virtual state at time $t$, apply action $a_t$ in the world model to get next virtual state distribution. Sample one realization.
  • Compute loss from the final virtual states (or aggregated over time).
  • Backward pass: compute gradients w.r.t. actions, using the reshaped gradient path.
  • Update actions via optimizer.
  • Optionally, update virtual states with noise (Step 3).

Repeat for a fixed number of iterations (e.g., 100–500). The actions will converge to a plan that maximizes the objective.

Step 6: Evaluate and Deploy the Planner

After optimization, take the first action of the planned sequence and execute in the real environment. Then re-plan with new observations. This is standard MPC (Model Predictive Control). Evaluate performance on long-horizon tasks (e.g., 100-step robotic tasks, video game levels). Compare to baselines like random shooting, cross-entropy method, or naive gradient methods to confirm improvement.

Tips for Success

  • Start with a low number of virtual states (e.g., $K=2$). Increase gradually if you see optimization stagnation.
  • Noise scale tuning: Begin with 0.05 and adjust based on loss landscape smoothness. If updates become unstable, reduce noise.
  • Learning rate matters: Use a learning rate around 1e-2 to 1e-1 for action parameters. Lower if gradients are large.
  • Monitor gradient norms: If action gradients explode, clip them. GRASP's reshaping helps but isn't foolproof.
  • Use a warmup phase: Run a few iterations without noise to get close to a reasonable region, then add stochasticity.
  • Validate on simple short-horizon tasks first (e.g., 10-step) to ensure your pipeline works before scaling to long horizons.
  • Consider ensemble virtual states: Instead of just $K$ samples, use a small ensemble of independent noise realizations to average gradients.
  • For vision-based models: Pretrain the world model separately; do not backprop through the encoder during planning (use latent states only).

By following these steps, you can make gradient-based planning with learned world models practical for horizons that were previously intractable. The GRASP techniques reduce fragility and enable robust long-term planning.