Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
557 changes: 59 additions & 498 deletions batchgen/batchgen_worker.py

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions batchgen/config/cuda_graph_flag_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,59 @@ def glm5_whole_model_cuda_graph_requested(
model_name,
enable_cuda_graph=enable_cuda_graph,
)


def any_cuda_graph_requested(
model_name: str | None,
*,
enable_cuda_graph: bool,
) -> bool:
"""True when ``--enable-cuda-graph`` turns on any decode CUDA graph for ``model_name``."""
return bool(
glm5_any_cuda_graph_requested(
model_name,
enable_cuda_graph=enable_cuda_graph,
)
or cli_enables_whole_model_graph(
model_name,
enable_cuda_graph=enable_cuda_graph,
)
)


def whole_model_cuda_graph_requested(
model_name: str | None,
*,
enable_cuda_graph: bool,
) -> bool:
"""True when ``--enable-cuda-graph`` requests a whole-model decode CUDA graph."""
return bool(
glm5_whole_model_cuda_graph_requested(
model_name,
enable_cuda_graph=enable_cuda_graph,
)
or cli_enables_whole_model_graph(
model_name,
enable_cuda_graph=enable_cuda_graph,
)
)


def should_warmup_decode_graphs_before_decode(
*,
graph_manager_is_initialized: bool,
global_batch_has_queueing: bool,
model_name: str | None,
enable_cuda_graph: bool,
) -> bool:
"""Delegate pre-decode CUDA graph warmup decision from the config layer."""
from batchgen.models.glm.glm5.cuda_graph_policy import (
should_warmup_cuda_graphs_before_decode,
)

return should_warmup_cuda_graphs_before_decode(
graph_manager_is_initialized=graph_manager_is_initialized,
global_batch_has_queueing=global_batch_has_queueing,
model_name=model_name,
enable_cuda_graph=enable_cuda_graph,
)
76 changes: 73 additions & 3 deletions batchgen/cuda_graph/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class BatchState:
decode_iter: int
input_ids: Optional[torch.Tensor] = None
device: Optional[torch.device] = None
attention_mask: Optional[torch.Tensor] = None
graph_required: bool = False
debug_values: Optional[Dict[str, Any]] = None


# ---------------------------------------------------------------------------
Expand All @@ -90,14 +93,17 @@ class GraphDecision:
"""Result of `adapter.eligibility(batch_state)`.

`reason` is a debug breadcrumb, not a control-flow input — the worker
branches on `mode` alone. `fail_on_eager` is set when the developer has
requested `BATCHGEN_DECODE_GRAPH_COMPARE_FAIL=1` and the adapter detects
a mode unexpectedly fell back to EAGER.
branches on `mode` alone. `fail_on_eager` is set when a required graph
is unexpectedly unavailable (fail-loud). `span_exceeded` carries
``(current_max_seqlen, captured_max_seqlen)`` when an eager fallback is
an expected long-context span overrun that should trigger the rate-limited
span warning instead of raising.
"""
mode: GraphMode
bucket: Optional[int]
reason: str = ""
fail_on_eager: bool = False
span_exceeded: Optional[Tuple[int, int]] = None


# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -454,6 +460,70 @@ def post_capture_finalize(

# ---- Step-time ------------------------------------------------------

def validate_decode_batch_admission(
self,
decode_batch: Sequence[int],
global_batch: Any,
) -> None:
"""Validate that admitted decode sequences fit this graph lifetime.

Default is a no-op. Models with span-restricted captures (e.g. an
``all_short`` DSA graph lifetime) override to fail loud before decode
starts.
"""
return

def needs_capture_for_batch(
self,
existing_manager: Optional[CUDAGraphManager],
*,
max_rank_bsz: int,
gpu_kv_manager: Any,
log_state_change: bool = False,
rank: int = 0,
) -> bool:
"""Return True if this decode batch requires a (re-)capture before decoding.

Default returns False (the generic warmup trigger suffices). Models
that check per-bucket capture coverage and page-table signatures
override this method.
"""
return False

def on_decode_step_pre_forward(
self,
*,
manager: Optional[CUDAGraphManager],
local_bsz: int,
max_rank_bsz: int,
rank_counts: Any,
gpu_kv_manager: Any,
decode_iter: int,
rank: int,
graph_required: bool = False,
) -> None:
"""Optional model-specific pre-forward diagnostics before replay/eager.

MUST be a cheap cached-flag check in steady decode with zero host sync.
"""
return

def decode_eager_forward(
self,
model: Any,
new_tokens: torch.Tensor,
*,
attention_mask: Any,
position_ids: Any,
) -> Any:
"""Run eager decode forward when no CUDA graph replays this step."""
return model(
new_tokens,
attention_mask=attention_mask,
position_ids=position_ids,
use_cache=False,
)

@abstractmethod
def eligibility(self, batch_state: BatchState) -> GraphDecision:
"""Pick the best mode for this step.
Expand Down
Loading
Loading