Skip to content

HWM ​

Zhang et al., Hierarchical Planning with Latent World Models, 2026. Paper · Code · Notebook

HWM plans over long horizons with two action-conditioned world models that share one latent space. The low level predicts the next latent from a primitive action. The high level predicts the latent stride steps later, the next waypoint, from a latent macro-action lk=Aψ(atk:tk+1−1) that encodes the actions in between. Here the low level is a LeWM, so the encoder learns from pixels with SIGReg. The high level reads the low level's latents through a stop-gradient, as HWM trains it on a frozen encoder. Each level has HWM's ℓ1 teacher-forcing loss. The low level also has the ℓ1 multi-step rollout loss ∑j‖F(1)(z1,a1:j)−zj+1‖1:

L=Ltf(1)+γLroll(1)+λSIGReg(z)+γtf(2)Ltf(2)+γroll(2)Lroll(2)

Planning is top-down, and the plan is redone after every executed step:

  1. The high level searches macro-actions l1:H that minimize ‖F(2)(z1,l1:H)−zg‖1.
  2. Its first predicted waypoint z~1 becomes the subgoal.
  3. The low level searches the next stride actions that minimize ‖F(1)(z1,a1:h)−z~1‖1.

Usage ​

python
from torch_world.models import HWM, LeWM
from torch_world.nn import CLSEncoder
from torch_world.planning import CEM, HierarchicalPlanner

low = LeWM(CLSEncoder(img_size=64, patch_size=8, embed_dim=128, depth=4, num_heads=4), action_dim=2, embed_dim=128,
           history=3, depth=4, num_heads=4, head_dim=32, mlp_dim=512, proj_dim=512, lamb=0.05)
model = HWM(low, action_dim=2, stride=4, macro_dim=8, embed_dim=128, history=3, depth=4, num_heads=4, head_dim=32,
            mlp_dim=512)

out = model(pixels, actions)        # (B, 3 * 4 + 1, C, H, W), (B, 3 * 4 + 1, A): 4 waypoints
# out: {"loss", "low_loss", "rollout_loss", "sigreg_loss", "high_loss"}

planner = HierarchicalPlanner(
    CEM(horizon=5, action_dim=8, bounds=(-2, 2)),      # macro-actions toward the goal
    CEM(horizon=4, action_dim=2, bounds=(-1, 1)),      # primitive actions toward the first waypoint
)
plan = planner(model, model.encode(obs[:, None]), model.encode(goal_images[:, None])[:, 0])  # (B, 4, 2)

The macro-action encoder ends with a BatchNorm without affine parameters, so training macro-actions are standardized. The high-level planner can then sample them from N(0,I) and clip them to ±2. The paper initializes from, and clips to, the 2–98% quantiles of training macro-actions. With shrink_every, HierarchicalPlanner shortens the high horizon as the episode runs, as the paper does.

The high level is trained with teacher forcing by default, as in the paper's Franka and Push-T setups. On Diverse Maze, where the paper measures its largest gain over flat planning, the high level is instead trained on its own autoregressive rollouts (γtf=0, γroll=1, Table 8). That is the setting the planner uses: it rolls the high level out over several macro-actions from one waypoint, so a teacher-forced high level compounds its errors. Pass gamma_tf_high=0.0, gamma_roll_high=1.0 for the maze recipe; setting gamma_tf_high=0.0 skips the teacher-forcing forward pass, so training windows can span more macro steps than high.history:

python
model = HWM(low, action_dim=2, stride=4, macro_dim=8, gamma_tf_high=0.0, gamma_roll_high=1.0, ...)
# out has "high_rollout_loss" instead of "high_loss"

At small training budgets on a jointly trained SIGReg encoder, keeping both terms (gamma_tf_high=1.0, gamma_roll_high=1.0) can outperform pure rollout (45% against 27% on Two-Rooms opposite room; see benchmark results).

The paper also shortens the high horizon by one every stride low-level steps, down to 3 on Diverse Maze: HierarchicalPlanner(high, low, shrink_every=4, min_horizon=3) when replanning after every step.

The paper differs from this implementation in a few ways:

  • Encoder: frozen pre-trained encoders (V-JEPA 2, DINOv2) or PLDM's, where here a LeWM encoder learns from pixels.
  • Waypoints: variable spacing with a transformer macro-action encoder on Franka and Push-T. The fixed stride and MLP encoder used here are the paper's maze setup.
  • Planner: MPPI in the maze, where this implementation runs CEM at both levels. The maze's low-level cost averages the last three predicted latents against the subgoal, where HierarchicalPlanner scores the last one; for the final 15 steps the paper drops the high level and plans flat toward the goal with a horizon of 50.

Call recompute_batchnorm before planning, as for LeWM.

Reference ​

Arguments and methods: HWM and HierarchicalPlanner in the API reference.