Code repository for Test-Time Sequential Steering of Diffusion Models via Preconditioned Crank-Nicolson. Implementations for Residual preconditioned Crank-Nicolson (RpCN) and other test-time guidance methods are provided as an installable Python package (see Usage).
The code for replicating the paper's experiments are on different branches of this Git repository:
- toy: Code for Section 5.1: Synthetic Expermiment.
- image_gen: Code for Section 5.2: Conditional Image Generation.
- dynamical-systems: Code for Section 5.3: Dynamical Systems.
- inverse_problems: Code for Section 5.4: Inverse Problems.
Clone the repository and install using pip or uv:
pip install .Warning
The dependency versions for PyTorch and Jax have not been specified to maximize compatibility potential without verification of compatibility. We hope it is compatible with the versions of PyTorch or Jax that your project is using.
Test-time guidance methods for diffusion models, including FK steering, DAS, CREPE and RpCN, written against the Array API standard (2025.12). It runs on JAX, PyTorch (through array-api-compat) and array_api_strict.
Every method calls your model through one evaluate function. Build it from a noise-prediction network
eps_net(x [B, *D], t [B]) -> eps and a reward reward(x0 [B, *D]) -> [B], both PyTorch callables
(make_evaluate_jax and JaxBackend are the JAX equivalents). The builders take
reward(x0, x, t) -> [B], so a reward may also read the noisy state and time; on_x0 adapts a reward of
the estimate alone:
from ttg import (CREPEConfig, DDIMStep, Direction, ESSBelow, EulerMaruyama, EvenOdd, Every,
FKDiff, Guided, Langevin, LangevinMove, Never, Noising, PCN, PosteriorStep,
SequentialSweep, ReverseSDE, VPLinear, constant, crepe, crepe_sample,
das_lambda, ladder, level_lambda, make_evaluate_torch, on_x0, smc, ssp, tilt,
x0_from_eps)
from ttg.backend import TorchBackend
backend = TorchBackend()
xp = backend.xp # array_api_compat.torch
sch = VPLinear()
D = (3, 32, 32) # event shape of one sample
I, N = 4, 16 # independent instances x particles
evaluate = make_evaluate_torch(eps_net, lambda x, t, eps, s: (x0_from_eps(x, t, eps, s), eps), sch,
on_x0(reward))
u = xp.linspace(1.0, 0.0, 100, dtype=xp.float32) # schedule coordinate, noise (1) to data (0)
P = PosteriorStep(sch)
start = sch.prior((I, N), D, xp=xp, dtype=xp.float32)
key = backend.key(0)FK steering
fk = smc(start, u, backend=backend, evaluate=evaluate, schedule=sch, target=P, proposal=P,
potential=FKDiff(constant(10.0)), policy=Every(5), key=key)
x0, log_weights = fk.final.x0, fk.log_weights # [I, N, *D] samples, [I, N] log-weightsDAS
lam = das_lambda(sch.time(u), growth=0.008, alpha_kl=0.01)
das = smc(start, u, backend=backend, evaluate=evaluate, schedule=sch, target=P,
proposal=Guided(P, tilt(lam)), potential=FKDiff(lam), policy=ESSBelow(0.5), sampler=ssp, key=key)RpCN — the particle axis is a temperature ladder 0 = \beta_0 < \dots < \beta_K = \beta; there is no resampling.
K, beta = 3, 10.0
betas = xp.linspace(0.0, beta, K + 1, dtype=xp.float32)
P = DDIMStep(sch, eta=1.0)
rpcn = smc(sch.prior((I, K + 1), D, xp=xp, dtype=xp.float32), u, backend=backend, evaluate=evaluate,
schedule=sch, target=P, proposal=P, potential=FKDiff(ladder(betas)), policy=Never(),
move=PCN(n_proposals=20, step_size=constant(0.95), pairs=SequentialSweep()),
key=key)
coldest = rpcn.final.x0[:, K] # [I, *D]: the beta_K = beta replicaCREPE — replica exchange across noise levels 0 = u_0 < \dots < u_M = 1.
u_levels = (0.0, 0.25, 0.5, 0.75, 1.0)
lam = level_lambda(sch.time(xp.asarray(u_levels, dtype=xp.float32)), beta=10.0, rho=1.0)
noising = Noising(sch) # one object for both: the forward terms cancel statically
denoising = EulerMaruyama(ReverseSDE(sch), Direction.BACKWARD)
langevin = Langevin(step_size=lambda t: 0.01 * sch.std(t) ** 2)
cfg = CREPEConfig(u_levels=u_levels, steps_per_gap=10, model_noising=noising, model_denoising=denoising,
proposal_noising=noising, proposal_denoising=Guided(denoising, tilt(lam)), lam=lam,
local_move=LangevinMove(Guided(langevin, tilt(lam, 0.5))), local_levels=(0, 1, 2, 3))
samples, replicas = crepe(cfg, I, D, 100, lambda r: r.x0, backend=backend, evaluate=evaluate, schedule=sch,
dtype=xp.float32, key=key) # samples: [100, period, I, 1, *D]
more, replicas = crepe(cfg, I, D, 100, lambda r: r.x0, backend=backend, evaluate=evaluate, schedule=sch,
dtype=xp.float32, key=backend.key(1),
initial_ladder=replicas) # continue: no re-initialisationcrepe_sample runs CREPE end to end and returns an SMCResult with one instance per chain. With the lowest
level above the data end (u_0 > 0), it also finishes the level-0 samples the rest of the way to
u = 0, weighted (exact=True) or as plain guided denoising (exact=False):
cfg = CREPEConfig(u_levels=(0.25, 0.5, 0.75, 1.0), ...) # as above, with lam on these levels
out, replicas = crepe_sample(cfg, I, D, 80, n_tail=10, exact=False, backend=backend, evaluate=evaluate,
schedule=sch, dtype=xp.float32, key=key, thin=4,
burn_in=20) # 80 samples, 1 per 4 sweeps
x0, log_weights = out.final.x0, out.log_weights # [I, 80, *D], [I, 80]crepe, pool_samples and crepe_finish remain available for custom emit functions.
thin keeps every thin-th sweep's sample.
burn_in blocks run first and store nothing.
The results are torch.Tensor objects. On JAX, the same calls can be wrapped in jax.jit.