Skip to content

feat(b12x): unify 5 public contracts, fix live defects, and retire contextual tuning - #194

Merged
lukealonso merged 1 commit into
mainfrom
unify-b12x-public-contracts-and-reti
Nov 30, 2026
Merged

lukealonso merged 1 commit into
mainfrom
unify-b12x-public-contracts-and-reti

Conversation

@lukealonso

@lukealonso lukealonso commented Nov 30, 2026 •

Copy link
Copy Markdown

Why

Across local-inference-lab/hyperflare-recipes and NVIDIA/hyperflare-inference (ignition/), downstream callers depended on 188 private b12x.*._* import statements in production code, including inspect.getsource substring probing on wo_prep.plan, 38-kwarg _get_compiled_dense_gemm cloning, 21-arg raw pointer tuples into _selected_forward, process-global B12X_PCIE_* environment mutations, out-of-session compilation of prepare_kda_fused_verify, and runtime monkey-patching of compile_pool._initialize_worker.

These private reach-ins coincided with live correctness, memory, and concurrency defects:

  • _W13_NORMALIZED_STORAGES in moe/fused_moe/_impl.py retained strong tensor references and was never cleared by clear_tp_moe_caches() (permanent VRAM leak).
  • Dense epilogue quantization lacked _cuda_stream_context around quantize.
  • Island wide row reductions in _island_v4_wide.py did not enforce blocks >= rows, allowing unwritten rows when owner.blocks < layout.rows.
  • _discovery._compiler_context cached with @lru_cache ignored in-process Triton libdevice_path switches.
  • _lib/compile_plan.py mutated JITFunction.compile locklessly across threads.

Meanwhile, b12x/preparation retained 1,151+ LOC of contextual/finalist tuning (context.py and 4 test_contextual_*.py suites) with zero runtime callers across all repositories.

What Changed

  • Retire dead contextual/finalist tuning:
    • Deleted flashinfer/experimental/b12x/preparation/context.py (341 LOC) and four tests/experimental/b12x/preparation/test_contextual_*.py suites (810 LOC).
    • Removed Finalist*, ContextCandidate, ContextRaceResult, race_contexts, and same_declaration_except_override from b12x/preparation/{__init__,types,_cache,_collective,_measurement,session}.py, docs, benchmarks, and tests.
  • Fix live defects:
    • b12x/preparation/_discovery.py: key _compiler_context on active triton.knobs.nvidia.libdevice_path, TRITON_LIBDEVICE_PATH, and live backend hash.
    • b12x/_lib/compile_plan.py: install a thread-safe one-time ContextVar dispatch wrapper on JITFunction.compile instead of lockless attribute swapping; make register_native_triton_jit(*jit_fns) variadic.
    • b12x/moe/fused_moe/_impl.py: clear _W13_NORMALIZED_STORAGES in clear_tp_moe_caches() and expose is_w13_normalized.
  • Promote 5 public b12x contracts:
    1. GEMM epilogues & quantization: added b12x/gemm/blockscaled/_epilogues.py (DenseRopeRunner, TcGemvRopeRunner, SwiGLUDownQuery, plan_swiglu_down, run_swiglu_down, _cuda_stream_context around quantize) and explicit SUPPORTS_* capability flags on blockscaled and wo_projection; promoted public exports on b12x.gemm.{blockscaled,wo_projection,bf16_gemv} and b12x.quantization.mxfp8.
    2. Collectives & island row reductions: added explicit kwargs (algorithm_override, hierarchical_deferred_consumption, hierarchical_threads, threads, block_limit) to PCIeAllReduce, PCIeHierarchicalAllReduce, PCIeDCPA2A, and PCIeDCPA2APool; added b12x/comm/pcie/_row_reductions.py (supports_island_fused_rows, run_island_*) and _validate_wide_grid in _island_v4_wide.py enforcing blocks >= rows; promoted public b12x.comm.{pcie,roce} adapters and PROXY_ABI_VERSION.
    3. Attention contracts: added b12x/attention/paged/_window_contract.py (retiring 21-arg raw pointer tuples), paged_kv_write.check_layout, and public exports across paged, paged_decode, dsa_indexer, mla_compress, and qsa.
    4. Sequence, MoE, mHC, & weights: added plan_kda_fused_verify in b12x/sequence/gdn_decode/_fused_verify.py as a first-class PreparationSession Plan (using CompileJob.DEVICE_ORDINAL); exposed public PreparedExperts properties (w1_alphas, w2_alphas, a1_gscale, a2_gscale, release_weight_views, etc.) and public exports across b12x.sequence.*, b12x.moe.fused_moe, b12x.norm.mhc, and b12x.loader.
    5. Worker initializer hook & preparation API: added register_worker_initializer, unregister_worker_initializer, and worker_initializers on compile_pool / b12x.preparation; promoted SelectionCache.set_agreed_records, PreparationSession.{check_thread,synchronize,is_cancelled,selection_cache,scratch_limit,reclaim_programs}, custom_plan, is_composite_plan, plan_variants, plan_memory_requirements, and plan_compile_jobs.
  • Tests: added tests/experimental/b12x/test_public_contracts.py (10 unit tests covering contextual retirement, libdevice switching, thread-safe JITFunction.compile dispatch, _W13_NORMALIZED_STORAGES release, blocks >= rows grid validation, explicit collective kwargs, worker initializers, capability flags, plan_kda_fused_verify, and paged_kv_write.check_layout).

Claimed Benefit and Result

  • Eliminates the live _W13_NORMALIZED_STORAGES VRAM leak, Triton libdevice cache poisoning, lockless JITFunction.compile race, missing quantize stream context, and unwritten island-row hazard.
  • Removes 1,151+ LOC of dead contextual tuning code and enables hyperflare-recipes and flare/ignition to drop all 188 production private b12x.*._* imports.

Supporting Evidence and Limitations

  • tests/experimental/b12x/test_public_contracts.py: 10 passed, 0 failed.
  • tests/experimental/b12x/test_registry.py -k "not test_registry_matches_disk" + tests/experimental/b12x/preparation/{test_discovery_cache,test_session,test_scratch_limit,test_collective_buckets}.py: 250 passed, 0 failed.
  • tests/experimental/b12x/moe/test_tp_moe_w13_layout.py + tests/experimental/b12x/sequence/test_kda_fused_verify_contract.py: 62 passed, 5 skipped, 0 failed.
  • Verified end-to-end on Blackwell SM120 GPU 0 via paired hyperflare-recipes CUDA tests (test_window_staging_cuda.py::test_compiled_mutable_staging[8-128-1] and test_window_attention_cuda.py::test_window_attention[kv_dtype0-False-1-4-0-None-False]) and flare preparation suites (88 passed across test_runtime_patches, test_tuning_shards, test_compile_pool, test_persistent).
  • Pre-existing baseline failures / limitations: test_registry_matches_disk, test_island_native_override_preserves_fixed_declaration, and test_output_aware_twoshot_size_band[True-655424|2097152] fail identically on untouched lil/main @ 86c9f67a2. Multi-GPU TP/EP CUDA collective suites were not run on this single-GPU host.

Paired PRs

Discord: lukealonso

…ntextual tuning

Why:
Downstream callers in hyperflare-recipes and flare/ignition depended on 188
private b12x.*._* imports in production code, including inspect.getsource
probing, 38-arg dense GEMM wrapper cloning, 21-arg raw pointer tuples,
process-global B12X_PCIE_* environment mutations, out-of-session KDA kernel
compilation, and monkey-patching of compile_pool._initialize_worker. Those
private reach-ins coincided with live defects: a permanent VRAM leak in
_W13_NORMALIZED_STORAGES, missing _cuda_stream_context around dense epilogue
quantization, unwritten rows on island wide row reductions when
owner.blocks < layout.rows, stale Triton libdevice caching in _discovery.py,
and lockless JITFunction.compile mutation in compile_plan.py. Meanwhile,
b12x/preparation retained 1,151+ LOC of contextual/finalist tuning code with
zero runtime callers across all repositories.

What changed:
- Retired contextual/finalist tuning: deleted b12x/preparation/context.py
  (341 LOC) and four test_contextual_*.py suites (810 LOC), and removed
  Finalist*, ContextCandidate, ContextRaceResult, race_contexts, and
  same_declaration_except_override from preparation/, docs, benchmarks, and tests.
- Fixed live defects:
  - _discovery._compiler_context now keys on active Triton libdevice_path,
    TRITON_LIBDEVICE_PATH, and live backend hash.
  - _lib/compile_plan.py installs a thread-safe one-time ContextVar dispatch
    wrapper on JITFunction.compile and makes register_native_triton_jit variadic.
  - moe/fused_moe/_impl.py clears _W13_NORMALIZED_STORAGES in
    clear_tp_moe_caches() and exports is_w13_normalized.
- Promoted 5 public b12x contracts:
  1. Blockscaled epilogues (_epilogues.py with _cuda_stream_context around
     quantize, DenseRopeRunner, TcGemvRopeRunner, SwiGLUDownQuery/plan/run) and
     explicit SUPPORTS_* capability flags on blockscaled and wo_projection.
  2. Explicit kwargs on PCIeAllReduce, PCIeHierarchicalAllReduce, PCIeDCPA2A,
     and PCIeDCPA2APool; prepared island row reductions (_row_reductions.py)
     and _island_v4_wide grid validation enforcing blocks >= rows; public
     PCIe/RoCE collective adapters and PROXY_ABI_VERSION.
  3. Public attention contracts (_window_contract.py, paged_kv_write.check_layout,
     and exports across paged, paged_decode, dsa_indexer, mla_compress, qsa).
  4. First-class PreparationSession plan plan_kda_fused_verify in
     sequence/gdn_decode/_fused_verify.py and public sequence/MoE/mHC/loader/
     PreparedExperts APIs.
  5. Public register_worker_initializer / unregister_worker_initializer /
     worker_initializers on compile_pool and preparation, plus public
     PreparationSession, SelectionCache, and plan inspection APIs.
- Added unit tests in tests/experimental/b12x/test_public_contracts.py.

Claimed benefit and result:
- Eliminates the live VRAM leak, libdevice cache poisoning, lockless Triton
  compile race, missing stream context, and unwritten island-row hazard while
  giving hyperflare-recipes and flare/ignition five stable public b12x APIs
  and removing 1,151+ LOC of dead contextual tuning code.

Supporting evidence and limitations:
- tests/experimental/b12x/test_public_contracts.py: 10 passed, 0 failed.
- test_registry.py (excluding pre-existing test_registry_matches_disk) +
  preparation/{test_discovery_cache,test_session,test_scratch_limit,
  test_collective_buckets}.py: 250 passed, 0 failed.
- moe/test_tp_moe_w13_layout.py + sequence/test_kda_fused_verify_contract.py:
  62 passed, 5 skipped, 0 failed.
- Verified end-to-end on SM120 GPU 0 via hyperflare-recipes window staging
  and window attention CUDA tests. Multi-GPU TP/EP CUDA collective tests were
  not run in this single-GPU environment.

Signed-off-by: Luke Alonso <lalonso@gmail.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