Skip to content

refactor(runtime): make ForwardContext tensor-free (restore "Do not contain Tensor")'#745

Draft
rjzhb wants to merge 2 commits into
lightseekorg:mainfrom
rjzhb:rjzhb/refactor/ctx-drop-draft-tensors
Draft

refactor(runtime): make ForwardContext tensor-free (restore "Do not contain Tensor")'#745
rjzhb wants to merge 2 commits into
lightseekorg:mainfrom
rjzhb:rjzhb/refactor/ctx-drop-draft-tensors

Conversation

@rjzhb

@rjzhb rjzhb commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

Goal

ForwardContext documents itself as "Do not contain Tensor", yet five
loose torch.Tensor fields have crept in over time: req_to_page,
gather_ids, draft_seq_lens_buf, accept_lengths, dsa_swa_slot_mapping.
This (draft) PR is the umbrella for making the context tensor-free again —
routing each tensor through a persistent buffer or an object reference
so nothing loose rides on the per-forward context.

Not a bugfix: today these are only read on eager paths or are persistent
buffers, so CUDA-graph replay is unaffected. This is invariant / hygiene
cleanup — keeping the context from silently becoming a tensor grab-bag.

Approach

Draft-related tensors are bundled into a drafter-owned SpecForwardState,
referenced once via ctx.spec (an object ref, same category as attn_backend
/ token_to_kv_pool). The rest move to their natural owner (backend / input
buffers) or have their read site restructured.

Status

  • draft_seq_lens_bufctx.spec.draft_seq_lens
  • accept_lengthsctx.spec.accept_lengths
  • req_to_page — persistent page-table buffer; only 1 ctx read left (glm5),
    the rest already flow as explicit args. Small.
  • gather_ids — per-forward; ~16 reads across 6 models + logits. Needs
    deciding where the first-step row-narrowing lives. Structural.
  • dsa_swa_slot_mapping — deepseek_v4 intra-forward scratch (set/cleared
    within one forward); different mechanism.

@rjzhb
rjzhb force-pushed the rjzhb/refactor/ctx-drop-draft-tensors branch from 32c9be9 to 166ae35 Compare July 22, 2026 04:31
Signed-off-by: rjzhb <rjzhb222@163.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@rjzhb
rjzhb force-pushed the rjzhb/refactor/ctx-drop-draft-tensors branch from 166ae35 to 337bfb2 Compare July 22, 2026 04:34
Pass the drafter's mutable draft_seq_lens_buf via the existing seq_lens
forward kwarg instead of a separate draft_seq_lens parameter. Only forward
seq_lens into the model when accept_lengths is present so target models
are unchanged.

Signed-off-by: rjzhb <rjzhb222@163.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant