Skip to content

usp: differentiable operators for training (Ulysses/Ring/local attention)#1

Open
zhihengy wants to merge 3 commits into
feat/wan-rollout-optimizationfrom
feat/wan-usp-ops
Open

usp: differentiable operators for training (Ulysses/Ring/local attention)#1
zhihengy wants to merge 3 commits into
feat/wan-rollout-optimizationfrom
feat/wan-usp-ops

Conversation

@zhihengy

@zhihengy zhihengy commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Status update (2026-07-09): miles_diffusion no longer depends on these patches — the trainer now owns its differentiable USP operators (radixark/miles_diffusion#21, sp_ops.py). This PR remains open as standalone bugfixes worth having / upstreaming: anyone running training-style autograd through sglang's USP operators hits these silent-gradient bugs.

What these fix

All three follow the same pattern: a training-only branch guarded by torch.is_grad_enabled() / requires_grad; the inference path is byte-identical.

  1. 642026e31e Ulysses all-to-all autogradft_c.all_to_all_single has no autograd kernel, so backward silently stops there (q/k/v projection grads come out None, with no error). An even-split all-to-all is an involution, so the adjoint is the same collective. Restores the differentiability the FastVideo source had.
  2. a928d729c5 differentiable ring attentionring_attn called _templated_ring_attention (forward template) directly; the KV ring rotation is not autograd-aware, so dK/dV missed the reverse-ring pass (~0.59 rel error). Adds _RingFlashAttention (fwd/bwd templates, aten flash op); inference path unchanged.
  3. a606a3d0ff differentiable local attention — the jit FA kernels are forward-only on Hopper (fa3 via sgl_kernel::fwd has no autograd registration; fa4 on Blackwell has backward). Grad-enabled path routes through torch SDPA.

Invasiveness

2 files, ~120 insertions / 4 deletions. No API changes, no new deps. Validated by miles' SP parity suite before the dependency was removed (forward bitwise vs full-sequence reference, 6 bands incl. ring × grad-ckpt; details in radixark/miles_diffusion#21).

If upstreaming to sgl-project: main has since rewritten the USP all-to-all (sgl-project#24366, sgl-project#27143), so patch 1's wrap point needs re-adaptation there.

🤖 Generated with Claude Code

root and others added 3 commits July 6, 2026 22:25
ft_c.all_to_all_single has no registered autograd kernel, so backward silently
stops there — fine for inference (no_grad) but breaks training (q/k/v projection
grads come out None). Wrap it in an autograd Function; an even-split all-to-all is
an involution, so its adjoint is the same collective on the upstream gradient.
Restores the differentiability the FastVideo source had. Transparent to rollout.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
ring_attn called _templated_ring_attention (forward template) directly; the KV
ring rotation isn't autograd-aware, so dK/dV never get the reverse-ring gradient
pass that torch keeps in _templated_ring_attention_backward (normally wired by
torch's context_parallel autograd.Function). dQ survived (Q isn't rotated) but
to_k/to_v weight grads were ~0.59 rel error. Add _RingFlashAttention autograd
Function (fwd=forward template, bwd=backward template, op=aten flash) and route
ring_attn through it when grad is enabled; inference path unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The jit FA kernels (sgl_kernel::fwd) have no autograd registration, so
backward through USPAttention's local attention silently produces no
q/k/v grads (fa4 on Blackwell has backward; fa3 on Hopper does not).
Route the grad-enabled path through torch SDPA; inference is unchanged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant