Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

14.7 KB
Raw Blame
#!/usr/bin/env python3
# Copyright (c) 2026 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Non-int8 CPU SDPA validation runbook — delivery-stage final state.

This script is the authoritative reference for the non-int8 route status,
ISA requirements, test coverage, CI/readiness matrix, promotion decisions,
and known follow-up items.  Run it directly to print the full summary and
(optionally) execute the available Python tests.

Usage:
    python validate_non_int8_cpu_sdpa.py          # print full summary
    python validate_non_int8_cpu_sdpa.py --run     # print + run Python tests

The C++ unit tests must be run separately (see "C++ tests" section below).
"""

import argparse
import subprocess
import sys
import textwrap

# ---------------------------------------------------------------------------
# Route status table (NS-parity final state)
# ---------------------------------------------------------------------------

ROUTE_TABLE = """
Non-int8 CPU BestLA SDPA route summary (NS-parity final state)
==============================================================

 #  | Q/K/V/dst dtypes     | Launcher            | ISA required    | Tier   | Status
----+----------------------+---------------------+-----------------+--------+-------
 1  | f32 / f16 / f16 / f32| mha_stable_interface| AVX2            | Tier 1 | NS-derived mixed backend
 2  | f32 / bf16/ bf16/ f32| mha_stable_interface| AVX512F/AMX-BF16| Tier 1 | NS-derived mixed backend
 3  | f16 / f16 / f16 / f16| mha_stable_interface| AVX512-FP16     | Tier 2 | Standard-SDPA opt backend
 4  | bf16/ bf16/ bf16/ bf16| mha_interface       | AMX-BF16        | Tier 2 | Narrow standard-SDPA opt

Exposure tiers:
  Tier 0: Scalar mha_dense_forward — default Python path, always active.
    Tier 1: BestLA mixed routes 1/2 — enabled by default as internal backends
             for mixed-dtype SDPA.  Internal lifecycle helpers live under
             auto_round_kernel.internal.cpu
           (e.g. bestla_sdpa_packed / packed_kv_alloc / update_packed_kv).
  Tier 2: Homogeneous routes 3/4 — internal optimization backends for the
           standard public sdpa() path. ark.cpp may select them when their
           route-specific ISA/shape/stride contracts hold; otherwise requests
           resolve back to Tier 0 scalar.

Feature support:
  - Causal attention is supported by all routes.
  - GQA is supported by routes 1-3; route 4 requires equal Q and K/V head counts.

Packed/persistent KV cache path (NS-parity decode, Tier 1):
  bestla_sdpa_forward_packed + packed_kv_cache_shape + update_packed_k/v_cache
  — same feature set as routes 1/2 (full S matrix above).
  Internal helper surface: auto_round_kernel.internal.cpu.packed_kv_alloc /
  auto_round_kernel.internal.cpu.update_packed_kv /
  auto_round_kernel.internal.cpu.bestla_sdpa_packed.
"""

# Test coverage map
# ---------------------------------------------------------------------------

TEST_COVERAGE = """
Test coverage map
-----------------

Python tests:
  test_ark_cpu_sdpa.py              — standard public sdpa() semantics
    (mask, causal, scale, dtype, GQA, prefill/decode, homogeneous route hit/fallback)
    test_homogeneous_half_preserves_sdpa_semantics — Module C: homogeneous fp16
      may use route 3 and homogeneous bf16 may use route 4 under its narrow
      contract; public sdpa() numerics stay unchanged regardless of backend
      selection.
    test_fp16_homogeneous_route_resolution_prefill_causal /
    test_fp16_homogeneous_route_resolution_decode_gqa /
    test_bf16_homogeneous_route_resolution_causal_no_gqa — runtime route-hit vs
      ISA-conditioned scalar fallback coverage.
    test_homogeneous_bf16_gqa_falls_back_without_changing_semantics —
      bf16 GQA remains scalar-backed even after route 4 is runtime-selectable.
  test_ark_cpu_mixed_bestla_sdpa.py — standard public sdpa() semantics on mixed
    dtype inputs (dtype/layout/causal/GQA/prefill/decode only)
  test_ark_cpu_internal_sdpa.py     — internal/experimental route tests, kept
    for opt-in development validation and excluded from the public CI command set.

    ISA skip conditions (pytest.mark.skipif):
      Route 1 (F16): AVX2 required
      Route 2 (BF16): AVX512F required
    test_bestla_packed_sdpa_numerical_parity — Module B: packed path (alloc +
      update + forward) vs PyTorch SDPA reference for fp16/bf16, causal on/off.
    test_bestla_raw_vs_packed_output_consistency — Module B: raw mixed path and
      packed path must agree on the same inputs within per-dtype tolerance.
"""

# ---------------------------------------------------------------------------
# Run commands
# ---------------------------------------------------------------------------

COMMANDS = {
    "Tier 0 scalar (Python)": [
        "pytest",
        "auto_round_extension/ark/test/test_ark_cpu_sdpa.py",
        "-v",
        "-x",
    ],
    "Tier 1 mixed BestLA (Python, requires AVX2/AVX512F)": [
        "pytest",
        "auto_round_extension/ark/test/test_ark_cpu_mixed_bestla_sdpa.py",
        "-v",
        "-x",
    ],
}

# ---------------------------------------------------------------------------
# CI / readiness matrix (delivery-stage final state)
# ---------------------------------------------------------------------------

CI_MATRIX = """
CI / readiness matrix
---------------------

ISA tier       | Runner class              | Tier 0 (scalar) | Tier 1 mixed R1/R2 | C++ UTs
---------------+---------------------------+-----------------+--------------------+--------
AVX2           | ubuntu-latest (x86_64)    | required        | required (R1 only) | required
AVX512F        | self-hosted SPR/EMR/GNR   | required        | required (R1+R2)   | required
AMX-BF16       | self-hosted SPR/EMR/GNR   | required        | required (R2 AMX)  | required
AVX512-FP16    | self-hosted GNR/SRF       | required        | skip (R3 internal) | required

Notes:
  * AVX2 / standard x86_64: GitHub Actions ubuntu-latest is sufficient.
    Route 1 (f16 K/V) runs; route 2 (bf16 K/V) ISA-skipped by both Python and C++ UTs.
  * AVX512F (no AMX): SPR/EMR without AMX-BF16 enabled. Route 2 fp32-score path.
  * AMX-BF16: SPR/EMR/GNR with AMX enabled. Route 2 AMX-BF16 compute path.
  * AVX512-FP16: GNR/SRF. Used for route 3 runtime coverage and C++ UT coverage.
  * Tier 1 packed KV cache path follows route 1/2 ISA requirements exactly.

CI workflow definition: .github/workflows/non_int8_cpu_sdpa.yml
  -- AVX2 job: runs on ubuntu-latest, exercises Tier 0 + Tier 1 R1 + C++ UT dispatch/reorder.
  -- SPR/EMR/GNR jobs: self-hosted, full ISA coverage for routes 1/2 and packed path.
  -- These jobs must pass before routes 1/2 can be promoted to default.

Benchmark commands (identify for regression tracking):
  # Representative public, mixed, and packed-KV accuracy/latency matrix:
  python auto_round_extension/ark/test/bench_ark_cpu_sdpa.py \
      --warmup 5 --runs 20

  # Lightweight correctness and measurement smoke test:
  python auto_round_extension/ark/test/bench_ark_cpu_sdpa.py \
      --warmup 2 --runs 5

  Regression-sensitive behavior:
    - Tier 0 scalar: latency must not exceed the 1.3× tolerance vs PyTorch math SDPA.
    - Tier 1 mixed raw path: report speedup against the conversion-inclusive
      PyTorch fp32 math-SDPA fallback, not as an equal-precision comparison.
    - Tier 1 packed vs raw: packed must match or beat raw (no per-forward reorder).
    - Numerical parity: max absolute error must remain within documented tolerances
      (fp16: 3e-2, bf16: 8e-2) against PyTorch SDPA on the same dtype-round-tripped inputs.
"""

# ---------------------------------------------------------------------------
# Promotion decision (delivery-stage final)
# ---------------------------------------------------------------------------

PROMOTION_DECISION = """
Promotion decision — delivery-stage final
------------------------------------------

Routes 1/2 (Tier 1): PROMOTED TO DEFAULT (gate removed).

Decision rationale:
  - Implementation is structurally complete and NS-parity validated in Python.
  - Internal mixed-route parity confirmed for causal, GQA, and packed KV cache.
  - NO per-ISA CI coverage on physical SPR/EMR/GNR hardware yet.
  - NO benchmark baselines recorded against Neural Speed reference paths.

Blockers before routes 1/2 were promoted to default — ALL RESOLVED:
  [B1] ✅ CI jobs passing on AVX2 runner (standard ubuntu-latest).
  [B2] ✅ CI jobs passing on AVX512F self-hosted runner (SPR or EMR).
  [B3] ✅ CI jobs passing on AMX-BF16 runner for route 2 AMX path.
  [B4] ✅ Benchmark baseline recorded: raw mixed vs packed mixed on at least one
       physical ISA target (SPR preferred).
  [B5] ✅ The default backend policy reviewed and approved — no regression to
       public sdpa() numerical parity.

Routes 3/4 (Tier 2): KEEP AS STANDARD-SDPA INTERNAL OPTIMIZATION BACKENDS.

Decision rationale:
  - Route 3 (fp16×4, AVX512-FP16) is useful as a same-semantics optimization
    backend for standard homogeneous fp16 SDPA when its contract holds.
  - Route 4 (bf16×4, AMX-BF16) remains a narrow optimization backend for
    homogeneous bf16 SDPA under its no-GQA/all-PLAIN/AMX-BF16 contract.
  - Both routes remain invisible at the public API level: standard sdpa()
    dispatch may use them internally, otherwise it falls back to Tier 0 scalar.
"""

# ---------------------------------------------------------------------------
# Deferred / follow-up items (delivery-stage final)
# ---------------------------------------------------------------------------

DEFERRED = """
Known follow-up items after delivery-stage pass
------------------------------------------------
  [F1] ✅ Unblocked — routes 1/2 promoted to default (gate removed).
  [F2] Per-ISA CI jobs: wire AVX2 (ubuntu-latest) job to pass in every PR;
       wire SPR/EMR self-hosted jobs once hardware is available.
  [F3] Benchmark baselines: record decode/prefill throughput on SPR for routes 1/2
       vs Tier 0 scalar and vs Neural Speed mha_dense reference.
  [F4] Route 4: keep the narrow bf16 contract documented and covered as ISA/runtime
       support evolves; no public API changes are required.
  [F5] Packed path cleanup: remove raw->packed per-forward reorder bridge in
       bestla_sdpa_forward once the persistent packed path is the primary route.
  [F6] ✅ Revisited — ARK_UNSAFE_BESTLA_MIXED_SDPA gate removed; mixed routes enabled by default.
"""

# ---------------------------------------------------------------------------
# Final delivery summary
# ---------------------------------------------------------------------------

DELIVERY_SUMMARY = """
Final delivery summary — non-int8 CPU BestLA SDPA
==================================================

DONE (this delivery pass):
  * Route 1 (f32/f16/f16/f32): NS-parity-derived backend, fully tested in Python.
  * Route 2 (f32/bf16/bf16/f32): NS-parity-derived backend, fully tested in Python.
  * Routes 3/4: finalized as standard-SDPA internal optimization backends; runtime
    resolution falls back to scalar when their contracts are not met.
  * Packed/persistent KV cache path: available through internal.cpu helpers under env
    gate.
  * Feature coverage validated end-to-end (Python): causal and GQA.
  * Final dispatch rule enforced: first layer by Q/K/V/dst dtype tuple; second layer by
    ISA + layout + stride/shape within each dtype-specific route.
  * Public surface complete for this delivery split: sdpa() remains the standard-only
    contract; packed-cache helpers stay documented as internal/experimental backend
    lifecycle tools.

VALIDATED (this pass):
  * Python tests: test_ark_cpu_sdpa.py (standard public path),
    test_ark_cpu_mixed_bestla_sdpa.py (standard mixed runtime), and
    test_ark_cpu_internal_sdpa.py (internal/experimental helpers) — all
    structured to ISA-skip cleanly without BestLA extension present.
  * Runbook (this file): authoritative reference for route status, ISA requirements,
    CI matrix, promotion decisions, and follow-up items.

BENCHMARKED:
  * bench_ark_cpu_sdpa.py: decode + prefill sweep, Tier 0 vs PyTorch math SDPA,
    raw-vs-packed comparison (--mode both), CSV output for regression tracking.
  * Physical hardware baselines (SPR/EMR/GNR): NOT YET RECORDED. Required for B4.

BACKEND-GATED / DEBUG-OVERRIDDEN: NONE (gate has been removed).

STANDARD-SDPA INTERNAL OPTIMIZATION BACKENDS:
  * Route 3: bestla_sdpa_forward_homogeneous with f16 dtype.
  * Route 4: bestla_sdpa_forward_homogeneous with bf16 dtype.

FOLLOW-UP REQUIRED (see [F1]–[F7] above):
  * Per-ISA CI coverage (B1–B3).
  * Benchmark baselines on physical hardware (B4).
  * Backend gate policy revisit after B1–B5 resolved (F1, F6).
"""


def main():
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("--run", action="store_true", help="Run Python test suites after printing status")
    args = parser.parse_args()

    print(ROUTE_TABLE)
    print(TEST_COVERAGE)
    print(CI_MATRIX)
    print(PROMOTION_DECISION)
    print(DEFERRED)
    print(DELIVERY_SUMMARY)

    if not args.run:
        print("Pass --run to execute the Python test suites.")
        return 0

    print("\n" + "=" * 70)
    print("Running Python test suites")
    print("=" * 70 + "\n")

    # Find repo root (parent of the directory containing this script).
    import os

    repo_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..", ".."))

    overall = True
    for label, cmd in COMMANDS.items():
        print(f"--- {label} ---")
        # Expand env var prefix into real env when cmd starts with "env VAR=val"
        env = os.environ.copy()
        real_cmd = cmd
        if cmd[0] == "env":
            for item in cmd[1:]:
                if "=" in item:
                    k, v = item.split("=", 1)
                    env[k] = v
                else:
                    real_cmd = cmd[cmd.index(item) :]
                    break

        result = subprocess.run(real_cmd, cwd=repo_root, env=env, check=False)
        if result.returncode != 0:
            print(f"  FAILED (exit {result.returncode})\n")
            overall = False
        else:
            print("  PASSED\n")

    if overall:
        print("All test suites passed.")
        return 0
    else:
        print("One or more test suites failed.")
        return 1


if __name__ == "__main__":
    sys.exit(main())