diff_bridge
cosmic_warp.rl.diff_bridge
Warp <-> torch autograd bridge: differentiable sim steps as torch autograd nodes.
:class:DiffSimStepper exposes one simulator integration step as a
torch.autograd.Function: flat float64 torch tensors in (state x,
actuator command u), flat float64 torch tensor out (next state), with the
step's warp kernels recorded on a per-call wp.Tape whose adjoint runs in
the node's backward. Chaining step() calls under torch autograd yields the
exact interleaved gradient through dynamics and anything torch-side in the
loop (a policy a_t = pi(o_t), a reward, ...) — the foundation for
first-order RL (SHAC).
sim = SpacecraftSimulator(..., differentiable=True, check_bounds=False)
stepper = DiffSimStepper(sim)
x = stepper.read_state() # [E, X_DIM] f64, detached
x.requires_grad_(True)
for t in range(horizon):
u = policy(x) # [E, U_DIM] f64, on-graph
x = stepper.step(x, u) # differentiable sim step
loss = objective(x)
loss.backward() # adjoints through all steps
Each differentiable step() call allocates its own tape and warp buffers
(freed with the autograd graph), so any autograd pattern is safe: repeated
backward with retain_graph=True, torch.autograd.gradcheck's
interleaved numeric and analytic passes, and slot-free window chaining. When
no gradient can flow (torch.no_grad() or detached inputs), step()
switches to an untaped fast path with reused buffers — evaluation and
inference loops pay no taping cost. Buffer pooling for the differentiable
path is a later optimization if per-step allocation ever shows up in profiles.
Each differentiable step runs on a snapshot of the vehicle, thruster and
wheel configurations, so its backward replays the parameters its forward
used: resetting the simulator (which redraws randomized parameters) or
calling set_config between a step and its backward does not change the
gradient. A taped step copies the three arrays; inside
:meth:DiffSimStepper.frozen_params the steps share one copy instead.
Gradients are first order only: the backward pass is a Warp adjoint, not a torch graph, so double backward (Hessian-vector products, gradient penalties) raises instead of treating the step Jacobian as a constant.
Same requirements as :class:~cosmic_warp.core.autodiff.DifferentiableRollout:
the simulator must be built with differentiable=True and termination checks
disabled, everything is float64, and taped steps cannot be CUDA-graph captured.
DiffSimStepper
DiffSimStepper(sim: SpacecraftSimulator)
Differentiable per-step interface to a simulator (torch boundary).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sim
|
SpacecraftSimulator
|
A :class: |
required |
The stepper never mutates the simulator: taped steps use a scratch
episode_steps counter and their own state buffers. Use
:meth:read_state / :meth:write_state to move state between the
simulator (resets, evaluation) and the differentiable chain.
A taped step reads the vehicle, thruster and wheel configurations from a
copy taken at its forward, which its backward reuses; the simulator may
be reset or reconfigured before backward().
Source code in cosmic_warp/rl/diff_bridge.py
step
Advance one step: (x, u) -> x_next, differentiable w.r.t. both.
When no gradient can flow — under torch.no_grad(), or when neither
input requires grad — the step skips taping and per-call allocation
entirely (same kernels, reused buffers), so evaluation and inference
loops run at plain simulation cost.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
State |
required |
u
|
Tensor
|
Actuator commands |
required |
Source code in cosmic_warp/rl/diff_bridge.py
frozen_params
Share one parameter snapshot across the taped steps in the block.
Every taped :meth:step otherwise copies the vehicle, thruster and
wheel configurations for its backward. Inside the block they share
one copy, taken on entry: cheaper for a multi-step rollout, provided
nothing changes the configuration inside the block. Nested blocks
share the outer snapshot.
Source code in cosmic_warp/rl/diff_bridge.py
read_state
The simulator's current state as a detached [E, X_DIM] tensor.
Source code in cosmic_warp/rl/diff_bridge.py
write_state
Write a (detached) [E, X_DIM] tensor into the simulator's state.