Skip to content
Closed
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
70 changes: 61 additions & 9 deletions astrai/extension/backend/linear.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Inference-only dispatch for AstrAI linear layers.

The CUDA GEMV path is deliberately narrow: automatic selection is enabled
only for single-row BF16 shapes measured to beat ``F.linear`` on a supported
architecture. Every training, prefill, unsupported-layout, and unmeasured
call falls back to PyTorch.
only for small decode batches and BF16 shapes measured to beat ``F.linear``
on a supported architecture. Every training, prefill, unsupported-layout,
and unmeasured call falls back to PyTorch.
"""

import logging
Expand All @@ -30,20 +30,72 @@
logger = logging.getLogger(__name__)

# Shape keys are (N, K) for Y[M, N] = X[M, K] @ W[N, K].T. A band is
# automatic only after both the per-shape >=5% and end-to-end decode >=3%
# gates pass and checkpoint greedy output remains stable. M=1 and M=8 remain
# empty on SM89; the safe M=2/4 bands improve real-engine throughput by
# 11.8-14.0%.
# automatic only after both the per-shape >=5% and projection-chain/engine
# >=3% gates pass and output argmax remains stable. M=1 is limited to OPT 1.3B;
# M=8 remains empty because at least one projection in each measured family
# misses the per-shape gate even when its aggregate chain result is positive.
_COMMON_TRANSFORMER_SM89_SHAPES = frozenset(
{
(1024, 4096), # LLaMA 3 8B K/V
(4096, 4096), # LLaMA 2/3 7B/8B Q/O
(11008, 4096), # LLaMA 2 7B gate/up
(4096, 11008), # LLaMA 2 7B down
(14336, 4096), # LLaMA 3 8B gate/up
(4096, 14336), # LLaMA 3 8B down
(5120, 5120), # LLaMA 2 13B Q/K/V/O
(13824, 5120), # LLaMA 2 13B gate/up
(5120, 13824), # LLaMA 2 13B down
(16384, 4096), # GPT-NeoX MLP up
(4096, 16384), # GPT-NeoX MLP down
}
)
_COMMON_TRANSFORMER_SM89_M4_SHAPES = _COMMON_TRANSFORMER_SM89_SHAPES - {
(4096, 4096),
(11008, 4096),
(4096, 11008),
}
_QWEN2_7B_SM89_SHAPES = frozenset(
{
(512, 3584), # K/V
(3584, 3584), # Q/O
(18944, 3584), # gate/up
(3584, 18944), # down
}
)
_LLAMA3_70B_SM89_SHAPES = frozenset(
{
(1024, 8192), # K/V
(8192, 8192), # Q/O
(28672, 8192), # gate/up
(8192, 28672), # down
}
)
_OPT_1_3B_SM89_SHAPES = frozenset(
{
(2048, 2048), # Q/K/V/O
(8192, 2048), # MLP up
(2048, 8192), # MLP down
}
)

_AUTO_GEMV_SHAPES: dict[tuple[int, int], dict[int, frozenset[tuple[int, int]]]] = {
(8, 9): {
2: frozenset(
1: _OPT_1_3B_SM89_SHAPES,
2: _COMMON_TRANSFORMER_SM89_SHAPES
| _QWEN2_7B_SM89_SHAPES
| _LLAMA3_70B_SM89_SHAPES
| _OPT_1_3B_SM89_SHAPES
| frozenset(
{
(256, 1536),
(1536, 1536),
(100000, 1536),
}
),
4: frozenset({(256, 1536), (1536, 1536)}),
4: _COMMON_TRANSFORMER_SM89_M4_SHAPES
| _QWEN2_7B_SM89_SHAPES
| _LLAMA3_70B_SM89_SHAPES
| frozenset({(256, 1536), (1536, 1536)}),
}
}
_AUTO_GEMV_M = frozenset(
Expand Down
39 changes: 39 additions & 0 deletions benchmarks/infraswe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# InfraSWE Draft: common-shape BF16 GEMV

This directory binds the common LLaMA/GPT-NeoX GEMV benchmark to AstrAI as an
explicit repository target. AstrAI is not one of InfraSWE v0.5's built-in
projects, so selecting a built-in default would score the change against the
wrong host contract. The Draft remains `D3-contract-proposed`; it does not
claim maintainer review, sealing, hidden-probe completion, or an official
ProjectFit.

The Draft was validated and resolved before opening the PR with InfraSWE commit
`811bc775ed5b3a6ec853219245f3469f78818020`:

```bash
PYTHONPATH=src .venv/bin/infraswe draft validate \
/path/to/AstrAI/benchmarks/infraswe/astrai-gemv-common-draft.json

PYTHONPATH=src .venv/bin/infraswe draft resolve \
--local-draft \
/path/to/AstrAI/benchmarks/infraswe/astrai-gemv-common-draft.json \
--output /tmp/astrai-gemv-common-draft-resolution.json
```

The candidate and contract digests bind the ordered source, tests,
documentation, and raw benchmark evidence. The required comparison cell is one
NVIDIA L20 (`sm_89`); optional A100 and H100 cells are explicitly untested.
Compilation happens before timed cases. The checked-in kernel suite uses 21
paired/interleaved samples, while the distinct-weight synthetic chains include
the guarded Python dispatcher. The extended portfolio adds Qwen2-7B, LLaMA 3
70B, and OPT-1.3B shapes at M=1/2/4/8, plus an interleaved
128-thread-versus-256-thread CTA comparison. Neither suite is presented as
whole-model throughput.

Applying InfraSWE's frozen `project-fit-kernel-v0.5` formula to the visible
evidence yields a diagnostic ProjectFit of **92.64/100** and BenchmarkTrust of
**95.87/100**. The machine-readable rationale is
`benchmarks/results/gemv_common_l20_sm89_infraswe_score.json`. Both numbers are
non-official: official ProjectFit remains unresolved until the Draft is sealed,
five or more fresh-process replays and system traces exist, hidden probes are
complete, and the evidence manifest is verified.
103 changes: 103 additions & 0 deletions benchmarks/infraswe/astrai-gemv-common-draft.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"schema_version": "0.5",
"draft": {
"id": "astrai-common-shape-bf16-gemv-v1",
"revision": 2,
"state": "D3-contract-proposed",
"created_by": "0z5a"
},
"target": {
"mode": "repository",
"repository": "https://github.com/ViperEkura/AstrAI",
"revision": "sha256:59c45de7cb0356e9661cfb73d38bd11c888a8b8d564e9123a90c9fa3910cc498",
"project_profile_sha256": "sha256:953067c47f5298f06846c8fe873db55afae49b6588a81926e49d9ef6b0994e08"
},
"candidate": {
"kind": "git-diff",
"revision": "sha256:d9ae3993caf937517b121d4fe855f2c5172cf38f69aa4bda10bbb8daad52a730",
"intent": "add-fastpath",
"implementation_kind": "cuda-native",
"entrypoints": [
"astrai.extension.bf16_gemv",
"astrai.extension.backend.linear",
"scripts/tools/benchmark_gemv_common.py"
],
"operator_family": "dense-gemm",
"phase": "inference",
"backend": "cuda",
"primary_host_candidate": "astrai"
},
"baseline": {
"mode": "target-head",
"revision": "sha256:59c45de7cb0356e9661cfb73d38bd11c888a8b8d564e9123a90c9fa3910cc498"
},
"deployment": {
"workload_portfolio": {
"id": "astrai-common-shape-gemv-chains-v1",
"sha256": "sha256:c3cf734200844d7b8308a4803f456cd9a3f0d7e19839e7d3e74778f6d1307e4d",
"path": "docs/benchmarks/gemv_common_extended_chain_l20_sm89.json"
},
"required_cells": [
"cuda-sm89-nvidia-l20"
],
"optional_cells": [
"cuda-sm80-a100",
"cuda-sm90-h100"
],
"request_or_step_protocol": {
"id": "astrai-gemv-common-benchmark-v1",
"sha256": "sha256:74808a77e551ffa5f77e3256930f2647ebd080828cfdbdcc2770f9d1fa298ff6",
"path": "docs/developer/decode_linear_benchmark.md"
}
},
"retrieval": {
"enabled": true,
"corpus_cutoff": "2026-09-02T09:45:00Z",
"sources": [
"target-code",
"merged-prs",
"rejected-prs",
"review-comments",
"ci-failures"
],
"precedent_set_sha256": "sha256:9034271948fd791fb53d5099c2bc246acfd4e27e45926857220b48fd841389b9"
},
"acceptance_contract": {
"status": "proposed",
"path": "tests/extension/test_gemv.py,tests/extension/test_linear_dispatch.py",
"sha256": "sha256:15ffa9e1378d7cb1b06f018f9e1aac16bf56f14446271d51129f86c13f3827bb",
"probe_set_sha256": "sha256:beafc95a2a4aaa587079f1938db8c0fb115d63c2b0c3b185debc9a6132397653",
"hidden_probe_policy_sha256": "sha256:e6bdf6b5a307c9ebdaf643d18ae88b51c6e554d7d9df4427c2f7ba0eb5cc8e93"
},
"project_objectives": {
"edge_ecosystem_policy": "experimental",
"profile_set_sha256": "sha256:22da954f941ba912f60d79b7a58121436d4ba97e64f0182f66b8274080c3a75a"
},
"benchmark_loop": {
"fast_stage_max_official_fraction": 0.05,
"affected_stage_max_official_fraction": 0.2,
"official_replays": 7,
"early_exit_on_hard_gate": true,
"affected_case_selection": "required",
"benchmark_budget_policy_id": "draft-staged-budget-v0.5",
"evidence_policy_id": "v0.4-evidence-ladder-plus-seal-v0.5",
"precompile": {
"mode": "auto",
"trigger": "when-compilation-required",
"cache_policy": "content-addressed-evidence-identity",
"cache_miss_action": "precompile-before-timed-cases",
"timing_phases": [
"precompile",
"cold-start",
"steady-state"
],
"steady_state_compile_allowed": false
}
},
"scoring": {
"formula_template_id": "project-fit-kernel-v0.5",
"provisional_scoring_allowed": true,
"official_scoring_requires_seal": true,
"project_season": "astrai-2026q3"
}
}
122 changes: 122 additions & 0 deletions benchmarks/results/gemv_common_l20_sm89_infraswe_score.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"schema_version": "0.5",
"score_kind": "diagnostic-project-fit",
"score_is_official": false,
"draft_id": "astrai-common-shape-bf16-gemv-v1",
"draft_state": "D3-contract-proposed",
"formula_template_id": "project-fit-kernel-v0.5",
"diagnostic_project_fit_100": 92.6415162827573,
"component_values": {
"evolutionary_maintainability": 0.8835232637338853,
"project_contract_fit": 1.0,
"performance_reuse_utilization": 0.9515978740065268,
"operational_fit": 0.8438741102892859
},
"component_floors": {
"evolutionary_maintainability": 0.6,
"project_contract_fit": 0.6,
"performance_reuse_utilization": 0.4,
"operational_fit": 0.6
},
"subcomponent_inputs": {
"evolutionary_maintainability": {
"evolution": 0.78,
"locality": 0.9,
"tests": 1.0,
"failure": 1.0,
"contract": 0.9
},
"project_contract_fit": {
"integration": 1.0,
"interface": 1.0,
"lifecycle": 1.0,
"buildtest": 1.0,
"policy": 1.0
},
"performance_reuse_utilization": {
"attainment": 1.0,
"coverage": 0.82,
"retention": 1.0,
"family": 1.0,
"compile": 1.0
},
"operational_fit": {
"replay": 0.9,
"load": 0.82,
"resource": 0.9,
"coldsteady": 0.75
}
},
"input_rationale": {
"evolution": "The candidate extends the merged AstrAI GEMV stack with a rejected broad half-CTA experiment and an evidence-gated exact selector, but has no upstream maintenance history yet.",
"locality": "The CUDA change remains inside the GEMV launcher and one cooperative kernel template; dispatcher, benchmark, tests, and documentation are separately scoped.",
"tests": "The complete CPU-capable AstrAI suite passed (625 passed, 185 skipped) and the built L20 extension passed 140 focused GPU tests.",
"failure": "Unaligned K, unsupported M, training/autograd, unmeasured shapes, other architectures, losing half-CTA cells, and sub-threshold automatic bands fail closed to the existing kernel or PyTorch.",
"contract": "The benchmark and tests are digest-bound, but the D3 acceptance contract has not received maintainer review or a seal.",
"integration": "The fast path remains behind AstrAI's existing bf16_gemv primitive and linear backend.",
"interface": "No public signature or environment-variable contract changes; ASTRAI_GEMV modes remain compatible.",
"lifecycle": "The path remains inference-only, uses the current CUDA stream, fuses optional bias, and is CUDA Graph safe.",
"buildtest": "The SM89 extension built all 128/256-thread row specializations without spills, Ruff format/import checks passed, and both local and L20 tests passed.",
"policy": "The candidate adds no dependency and enables automatic dispatch only for measured architecture/M/(N,K) cells that clear per-shape and chain gates.",
"attainment": "Every newly enabled Qwen2, LLaMA 3 70B, and OPT chain exceeds the 3% gate; losing M/family combinations are explicitly excluded.",
"coverage": "Twenty-two traditional shapes and seven model families were measured in the required NVIDIA L20/SM89 cell; optional A100/SM80 and H100/SM90 cells remain untested and fall back in auto mode.",
"retention": "The exact row/shape half-CTA selector preserves the 256-thread fallback and existing warp-tiled bands; direct A/B evidence and the regression suite passed.",
"family": "Twenty-two LLaMA, Qwen2, OPT, and GPT-NeoX matrix shapes plus seven dependent synthetic chains cover M=1/2/4/8, without claiming a whole-model benchmark.",
"compile": "AOT CUDA compilation completed before timed cases; no compilation is permitted in steady-state timing.",
"replay": "Paired order alternation, warmup, 21 samples, median and p90 are captured, but not seven fresh processes.",
"load": "Distinct-weight dependent chains cover seven model families and include Python dispatch; a real end-to-end service workload was not run.",
"resource": "GPU5 memory and utilization were checked before and after; the co-resident idle AstrAI service remained allocated and is disclosed.",
"coldsteady": "Precompile and steady-state phases are separated, but cold-start latency is not part of the checked-in result."
},
"benchmark_trust": {
"formula_version": "benchmark-trust-v0.5",
"status": "scored",
"score_100": 95.87315155141826,
"components": {
"reproducibility": 1.0,
"evidence": 1.0,
"statistics": 0.9,
"environment": 0.9
},
"failure_codes": [
"DRAFT_UNSEALED",
"FRESH_PROCESS_REPLAY_INCOMPLETE",
"OPTIONAL_CELLS_UNTESTED"
]
},
"official_project_fit": {
"status": "unresolved",
"score_100": null,
"failure_codes": [
"DRAFT_SEAL_MISSING",
"FRESH_PROCESS_REPLAYS_BELOW_MINIMUM",
"SYSTEM_TRACE_EVIDENCE_MISSING",
"HIDDEN_PROBES_INCOMPLETE",
"EVIDENCE_MANIFEST_UNVERIFIED"
]
},
"comparison_cell": {
"target_project_profile_sha256": "sha256:953067c47f5298f06846c8fe873db55afae49b6588a81926e49d9ef6b0994e08",
"target_repository_or_baseline_sha256": "sha256:59c45de7cb0356e9661cfb73d38bd11c888a8b8d564e9123a90c9fa3910cc498",
"change_intent": "add-fastpath",
"semantic_contract_sha256": "sha256:74808a77e551ffa5f77e3256930f2647ebd080828cfdbdcc2770f9d1fa298ff6",
"acceptance_contract_sha256": "sha256:15ffa9e1378d7cb1b06f018f9e1aac16bf56f14446271d51129f86c13f3827bb",
"probe_set_sha256": "sha256:beafc95a2a4aaa587079f1938db8c0fb115d63c2b0c3b185debc9a6132397653",
"workload_portfolio_sha256": "sha256:c3cf734200844d7b8308a4803f456cd9a3f0d7e19839e7d3e74778f6d1307e4d",
"performance_target_sha256": "sha256:22da954f941ba912f60d79b7a58121436d4ba97e64f0182f66b8274080c3a75a",
"required_deployment_cell_set_sha256": "sha256:6c00c84e931a5ad6fdaa5b4f7d4497c872530c75f8510431aff883006a4a2779",
"formula_template_id": "project-fit-kernel-v0.5",
"evidence_policy_id": "v0.4-evidence-ladder-plus-seal-v0.5",
"project_season": "astrai-2026q3",
"cross_project_ranking_allowed": false
},
"execution": {
"infraswe_commit": "811bc775ed5b3a6ec853219245f3469f78818020",
"draft_resolution_sha256": "952ee658e669126582b8eb9aeedede9f70dae6348ea0f1119d64a36d5da100fe",
"infraswe_draft_engine_tests": "53 passed",
"astrai_tests": "625 passed, 185 skipped",
"astrai_l20_gpu_tests": "140 passed",
"astrai_lint": "ruff format and import-order checks passed",
"sm89_build": "bf16_gemv target built successfully; 128/256-thread cooperative specializations used 26-52 registers, one barrier, and zero spills; M=4 warp tiling used 42 registers, zero barriers, and zero spills"
}
}
Loading
Loading