From 6991986ab12180160600eac0f26fd8e16dee9ee6 Mon Sep 17 00:00:00 2001 From: Luke Alonso Date: Wed, 2 Dec 2026 08:50:57 -0800 Subject: [PATCH 1/2] perf(sampling): collapse per-row PreparedSampling loop to 1 row-independent FlashInfer launch Why this change is needed: PreparedSampling previously sampled each decode batch row in a Python loop calling sample_probabilities(probabilities[row:row+1], ...) followed by two single-element .copy_() operations per row to preserve request-independent Philox RNG state. Because FlashInfer's SamplingFromProbKernel previously mixed the thread-block index into curand_init, batching rows would have coupled a request's sampled token to its batch slot. At batch capacity 64, that loop emitted up to 192 sequential CUDA graph nodes (64 sampling launches plus 128 scalar copies) in every decode graph. What changed: - ignition/components/flashinfer_sampling.py: - Before: Exposed only sample_probabilities without row_independent_rng and had no out-tensor wrapper for sampling_from_probs_out. - After: Added row_independent_rng: bool = False to sample_probabilities and added sample_probabilities_out(..., row_independent_rng=False) backed by torch.ops.flashinfer.sampling_from_probs_out (requires local-inference-lab/flashinfer#208, which adds row_independent_rng to SamplingFromProbKernel and sampling_from_probs_out). - ignition/components/sampling.py: - Before: PreparedSampling.__call__ iterated over range(self._rows), launching one single-row sample_probabilities call and copying sampled_tokens[row:row+1] and sample_valid[row:row+1] per row. - After: Issues one batched sample_probabilities_out(probabilities, scratch["sampled_tokens"], scratch["sample_valid"], seed=scratch["packed_seed"], offset=scratch["packed_offset"], row_independent_rng=True) launch writing directly into the prepared scratch buffers. - Compatibility: No Helios wire format, Protobuf, CLI, or runtime ABI changes. Existing exported cores load and serve unchanged. Claimed benefit and result: - Measured on PreparedSampling at C=64, vocab=16384 inside torch.cuda.CUDAGraph(keep_graph=True): collapses the per-row sampling loop from 192 graph nodes to 1 batched sampling launch (23 total nodes for the complete PreparedSampling graph) while preserving bit-exact sampled tokens across batch slots. - Whole-model serving latency improvements are expected on newly exported cores and are not claimed from this standalone sampling graph measurement. Supporting evidence and limitations: - Single-GPU SM120 (RTX PRO 6000 Blackwell) verification against local-inference-lab/flashinfer#208: - PreparedSampling at C=64, vocab=16384 captured in torch.cuda.CUDAGraph(keep_graph=True): bit-exact token parity against single-row launches across 7 distinct slots (0, 1, 7, 15, 31, 48, 63), batched_valid.all() == True, 23 total CUDA graph nodes. - CPU component suite: - pytest tests/components/ -q: 185 passed, 55 skipped (GPU and qualification-feature tests skipped). - Unrun checks / limitations: tests/components/test_sampling_flashinfer.py could not run through GraphReplayWorker because flare_heliopause in the local .venv is built without the qualification Cargo feature; full-model export and multi-GPU serving benchmarks were not run. Dependency merge order: merge local-inference-lab/flashinfer#208 and update the pinned FlashInfer revision before merging this PR. Design-Signed-Off-By: Antigravity (Gemini); design-review=2e5b --- ignition/components/flashinfer_sampling.py | 14 ++++++++++++-- ignition/components/sampling.py | 14 +++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/ignition/components/flashinfer_sampling.py b/ignition/components/flashinfer_sampling.py index 3e655d8b0..44a4106b2 100644 --- a/ignition/components/flashinfer_sampling.py +++ b/ignition/components/flashinfer_sampling.py @@ -32,9 +32,19 @@ def filter_probabilities_out(logits, top_k, top_p, row_states, softmax_workspace @torch.compiler.allow_in_graph -def sample_probabilities(probabilities, seed, offset, return_valid=False): +def sample_probabilities(probabilities, seed, offset, return_valid=False, *, row_independent_rng=False): return sampling.sampling_from_probs(probabilities, seed=seed, offset=offset, - deterministic=True, check_nan=False, return_valid=return_valid) + deterministic=True, check_nan=False, return_valid=return_valid, + row_independent_rng=row_independent_rng) + + +@torch.compiler.allow_in_graph +def sample_probabilities_out(probabilities, samples, valid, seed, offset, *, row_independent_rng=False): + """Sample every row in one launch directly into caller-owned output buffers.""" + sampling.get_sampling_module() + torch.ops.flashinfer.sampling_from_probs_out( + probabilities, samples, valid, None, True, seed, 0, offset, 0, row_independent_rng + ) @torch.compiler.allow_in_graph diff --git a/ignition/components/sampling.py b/ignition/components/sampling.py index ece253a9e..3aa780897 100644 --- a/ignition/components/sampling.py +++ b/ignition/components/sampling.py @@ -21,7 +21,11 @@ from .completion import CompletionOutputs, completion_record_capacity from .logprobs import PreparedLogprobs from .capacity import MAX_CONCURRENCY -from .flashinfer_sampling import filter_probabilities_out, probability_filter_workspace, sample_probabilities +from .flashinfer_sampling import ( + filter_probabilities_out, + probability_filter_workspace, + sample_probabilities_out, +) from ignition.preparation.persistent import persistent_copy, persistent_empty from ignition.preparation.scratch import bind_scratch @@ -589,12 +593,8 @@ def launch(operation): num_warps=4, enable_fp_fusion=False) torch.sum(probabilities, dim=-1, keepdim=True, out=scratch["probability_mass"]) launch(self._normalize) - for row in range(self._rows): - sampled, valid = sample_probabilities(probabilities[row:row + 1], - seed=scratch["packed_seed"][row:row + 1], offset=scratch["packed_offset"][row:row + 1], - return_valid=True) - scratch["sampled_tokens"][row:row + 1].copy_(sampled) - scratch["sample_valid"][row:row + 1].copy_(valid) + sample_probabilities_out(probabilities, scratch["sampled_tokens"], scratch["sample_valid"], + seed=scratch["packed_seed"], offset=scratch["packed_offset"], row_independent_rng=True) def __call__(self) -> None: if not self._prepared: From 1855c8fc708ce3e92984c345616395991a5b65a7 Mon Sep 17 00:00:00 2001 From: Luke Alonso Date: Wed, 2 Dec 2026 10:25:29 -0800 Subject: [PATCH 2/2] build(deps): pin flashinfer-python to 5a8ceed80ccc and drop unused sample_probabilities kwarg Why this change is needed: pyproject.toml tracked flashinfer-python on branch = "main" without a commit pin, leaving CI and fresh environments exposed to moving upstream state even after local-inference-lab/flashinfer#208 merged. In addition, sample_probabilities in ignition/components/flashinfer_sampling.py carried an unused row_independent_rng parameter because all batched callers use sample_probabilities_out. What changed: - pyproject.toml: - Before: flashinfer-python = { git = "https://github.com/local-inference-lab/flashinfer.git", branch = "main" }. - After: Pinned flashinfer-python to rev = "5a8ceed80ccc58e11a4d49ccc910f52967699c09" (merge commit of local-inference-lab/flashinfer#208). - ignition/components/flashinfer_sampling.py: - Before: sample_probabilities accepted an unused keyword-only row_independent_rng=False parameter. - After: Restored sample_probabilities to its prior signature while keeping sample_probabilities_out(..., row_independent_rng=False). Claimed benefit and result: Guarantees that fresh environments and CI resolve the FlashInfer revision containing row_independent_rng on torch.ops.flashinfer.sampling_from_probs_out, and keeps the diff minimal by removing unused API surface. Supporting evidence and limitations: - Single-GPU SM120 verification of PreparedSampling across eager, compile_forward, and aot_forward (serialize + torch.compiler.load_compiled_function + CUDAGraph replay): - Cutoff ties & device parameter changes on vocab 154880 (GLM) and 248320 (Qwen) across eager, compile_forward, and aot_forward with zero allocations during graph replay: 6/6 passed. - RNG invariance across eager, compile_forward, and aot_forward, capacities (1, 2, 4, 64), slot rotations, and 3 (seed, counter) pairs: passed. - Functional PreparedSampling.functional() + run() under compile_forward and aot_forward: passed. - Fresh-process AOT deserialize (torch.compiler.load_compiled_function) + CUDAGraph replay at C=64: passed. - CPU tests: CUDA_VISIBLE_DEVICES="" pytest tests/preparation/test_packaging.py tests/preparation/test_core_boundary.py tests/components/ -q: 192 passed, 55 skipped. - Limitations / unrun checks: GraphReplayWorker execution of tests/components/test_sampling_flashinfer.py remains unrun locally because flare_heliopause in the local .venv lacks the qualification Cargo feature; full-model export and flare run serving benchmarks were not run. Design-Signed-Off-By: Antigravity (Gemini); design-review=2e5b --- ignition/components/flashinfer_sampling.py | 5 ++--- pyproject.toml | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/ignition/components/flashinfer_sampling.py b/ignition/components/flashinfer_sampling.py index 44a4106b2..9f298d4df 100644 --- a/ignition/components/flashinfer_sampling.py +++ b/ignition/components/flashinfer_sampling.py @@ -32,10 +32,9 @@ def filter_probabilities_out(logits, top_k, top_p, row_states, softmax_workspace @torch.compiler.allow_in_graph -def sample_probabilities(probabilities, seed, offset, return_valid=False, *, row_independent_rng=False): +def sample_probabilities(probabilities, seed, offset, return_valid=False): return sampling.sampling_from_probs(probabilities, seed=seed, offset=offset, - deterministic=True, check_nan=False, return_valid=return_valid, - row_independent_rng=row_independent_rng) + deterministic=True, check_nan=False, return_valid=return_valid) @torch.compiler.allow_in_graph diff --git a/pyproject.toml b/pyproject.toml index d8717dbbd..e03a2529c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,7 +44,7 @@ explicit = true torch = { index = "pytorch-cu132" } torchvision = { index = "pytorch-cu132" } flare-heliopause = { path = "heliopause" } -flashinfer-python = { git = "https://github.com/local-inference-lab/flashinfer.git", branch = "main" } +flashinfer-python = { git = "https://github.com/local-inference-lab/flashinfer.git", rev = "5a8ceed80ccc58e11a4d49ccc910f52967699c09" } [tool.setuptools.packages.find] include = ["ignition*"]