PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
#!/usr/bin/env python
#
# 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.
"""
Unit tests for sagev1_varlen: correctness and performance.
Tests both kernel variants:
- v1_pvhalf: PV in half precision
- v1_pvi8: PV in INT8 precision
SAGE uses INT8 block quantization for Q/K, so numerical error is larger
(~2e-2 vs ~0.0005 for non-quantized SDPA).
Usage:
python test/test_sagev1_varlen.py
"""
import math
import sys
import time
import pytest
import torch
from ut_utils import get_ark, is_xpu_available, print_top_diffs, reference_sdpa_varlen
def has_sagev1():
"""Check if the SAGE varlen kernel is available."""
try:
ark_instance = get_ark()
except Exception:
return False
if ark_instance.xpu_lib is None:
return False
return hasattr(ark_instance.xpu_lib, "sagev1_varlen")
pytestmark = [
pytest.mark.skipif(not is_xpu_available(), reason="XPU not available"),
pytest.mark.skipif(not has_sagev1(), reason="SAGE v1 kernel not built (need ARK_SYCL_TLA=ON)"),
]
# Supported SAGE kernel variants
KERNEL_VARIANTS = ["v1_pvhalf", "v1_pvi8"]
# B=1 single-sequence cases
VARLEN_TEST_CASES = [
(1, 512, 512, 8, 8, 64, torch.float16, False, "single-seq-nc"),
(1, 512, 512, 8, 8, 64, torch.float16, True, "single-seq-c"),
(1, 1024, 1024, 16, 4, 128, torch.float16, False, "gqa-nc"),
(1, 1024, 1024, 16, 4, 128, torch.float16, True, "gqa-c"),
(1, 2048, 4096, 8, 8, 64, torch.float16, False, "kv-longer-nc"),
(1, 2048, 4096, 8, 8, 64, torch.float16, True, "kv-longer-c"),
(1, 1024, 1024, 8, 8, 128, torch.bfloat16, False, "bf16-nc"),
(1, 1024, 1024, 8, 8, 128, torch.bfloat16, True, "bf16-c"),
(1, 4096, 4096, 32, 8, 128, torch.float16, False, "large-gqa-nc"),
(1, 4096, 4096, 32, 8, 128, torch.float16, True, "large-gqa-c"),
# Multi-batch cases (batch>1). Block quantization across batch boundaries
# in the flat varlen tensor introduces slightly larger mean error, so
# thresholds are relaxed accordingly.
(2, 1024, 1024, 16, 4, 128, torch.float16, False, "multi-2-nc"),
(2, 1024, 1024, 16, 4, 128, torch.float16, True, "multi-2-c"),
(4, 1024, 1024, 8, 8, 64, torch.float16, False, "multi-4-nc"),
(4, 1024, 1024, 8, 8, 64, torch.float16, True, "multi-4-c"),
]
def build_varlen_problem(
batch: int,
total_q: int,
total_kv: int,
h_q: int,
h_kv: int,
head_dim: int,
dtype: torch.dtype = torch.float16,
device: str = "xpu",
min_seq_q: int = 1,
min_seq_kv: int = 1,
):
"""Create random varlen Q/K/V tensors and cumulative-length arrays.
Returns:
(q, k, v, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k,
seq_lens_q, seq_lens_k)
"""
# Heuristic: align total_q/kv up to quant_block_size (64) for SAGE
total_q = ((total_q + 63) // 64) * 64
total_kv = ((total_kv + 63) // 64) * 64
def _random_seq_lens(batch, total, min_len=1):
cuts = sorted(
torch.randint(min_len, max(min_len + 1, total - (batch - 1) * min_len + 1), (batch - 1,)).tolist()
)
seq_lens = []
prev = 0
for c in cuts:
seq_lens.append(c - prev)
prev = c
seq_lens.append(total - prev)
seq_lens = [max(min_len, min(s, total - (batch - 1) * min_len)) for s in seq_lens]
diff = total - sum(seq_lens)
if diff != 0:
for i in range(abs(diff)):
idx = i % batch
if diff > 0:
seq_lens[idx] += 1
elif seq_lens[idx] > min_len:
seq_lens[idx] -= 1
return seq_lens
seq_lens_q = _random_seq_lens(batch, total_q, min_seq_q)
ratio = total_kv / max(total_q, 1)
seq_lens_k = [max(min_seq_kv, round(l * ratio)) for l in seq_lens_q]
diff = total_kv - sum(seq_lens_k)
if diff != 0:
for i in range(abs(diff)):
idx = i % batch
if diff > 0:
seq_lens_k[idx] += 1
elif seq_lens_k[idx] > min_seq_kv:
seq_lens_k[idx] -= 1
cu_seqlens_q = torch.zeros(batch + 1, dtype=torch.int32, device=device)
cu_seqlens_k = torch.zeros(batch + 1, dtype=torch.int32, device=device)
for i in range(batch):
cu_seqlens_q[i + 1] = cu_seqlens_q[i] + seq_lens_q[i]
cu_seqlens_k[i + 1] = cu_seqlens_k[i] + seq_lens_k[i]
total_q_actual = int(cu_seqlens_q[-1].item())
total_kv_actual = int(cu_seqlens_k[-1].item())
q = torch.randn(total_q_actual, h_q, head_dim, dtype=dtype, device=device)
k = torch.randn(total_kv_actual, h_kv, head_dim, dtype=dtype, device=device)
v = torch.randn(total_kv_actual, h_kv, head_dim, dtype=dtype, device=device)
max_seqlen_q = max(seq_lens_q)
max_seqlen_k = max(seq_lens_k)
return q, k, v, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, seq_lens_q, seq_lens_k
# ========================================================================
# Correctness tests
# ========================================================================
def _check_sagev1_varlen_correctness(
batch=2,
total_q=1024,
total_kv=1024,
h_q=16,
h_kv=4,
head_dim=128,
dtype=torch.float16,
is_causal=False,
device="xpu",
kernel="v1_pvhalf",
quant_block_size=64,
max_diff_threshold: float | None = None,
mean_diff_threshold: float | None = None,
verbose: bool = True,
) -> dict:
"""Run one correctness case for a given kernel variant. Returns metrics dict.
Note: this is NOT a pytest test (underscore-prefixed). Use the
``test_sagev1_varlen_correctness`` wrapper for pytest discovery.
"""
# SAGE quantization error tolerance
if max_diff_threshold is None:
# v1_pvi8 has larger quantization error (PV path also INT8)
max_diff_threshold = 3.0 if kernel == "v1_pvi8" else 1.5
if mean_diff_threshold is None:
mean_diff_threshold = 0.01 if kernel == "v1_pvi8" else 0.005
# Multi-batch: block quantization boundaries across batch splits
# in the flat varlen tensor compound the mean error (per-batch
# max values are unrelated, making boundary-block scales a worse
# fit for tokens on both sides of the split).
if batch > 1:
mean_diff_threshold *= batch
q, k, v, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, seq_lens_q, seq_lens_k = build_varlen_problem(
batch, total_q, total_kv, h_q, h_kv, head_dim, dtype, device
)
scale = 1.0 / math.sqrt(head_dim)
# Torch reference (per-sequence, no padding)
ref = reference_sdpa_varlen(
q,
k,
v,
cu_seqlens_q,
cu_seqlens_k,
max_seqlen_q,
max_seqlen_k,
is_causal=is_causal,
scale=scale,
device=device,
)
# SAGE varlen kernel
out = get_ark().sageattn_varlen(
q,
k,
v,
cu_seqlens_q,
cu_seqlens_k,
max_seqlen_q,
max_seqlen_k,
is_causal=is_causal,
sm_scale=scale,
kernel=kernel,
quant_block_size=quant_block_size,
)
dff = (ref - out).abs()
max_diff = float(dff.max().item())
mean_diff = float(dff.mean().item())
has_nan = torch.isnan(out).any().item() or torch.isinf(out).any().item()
if has_nan:
nan_count = int(torch.isnan(out).sum().item() + torch.isinf(out).sum().item())
total_elems = out.numel()
print(f" [NaN/Inf] {nan_count}/{total_elems} abnormal elements — KERNEL BUG")
if verbose:
print(f" seq_lens_q={seq_lens_q}, seq_lens_k={seq_lens_k}")
print(f" max_seqlen_q={max_seqlen_q}, max_seqlen_k={max_seqlen_k}")
print_top_diffs(dff, ref, out, topk=4, threshold=0.5)
passed = not has_nan and (max_diff < max_diff_threshold) and (mean_diff < mean_diff_threshold)
assert passed, (
f"max_diff={max_diff:.4f} exceeds threshold={max_diff_threshold}, "
f"mean_diff={mean_diff:.6f} exceeds threshold={mean_diff_threshold}"
+ (f", has_nan={has_nan}" if has_nan else "")
)
return {
"passed": passed,
"max_diff": max_diff,
"mean_diff": mean_diff,
"max_diff_threshold": max_diff_threshold,
"mean_diff_threshold": mean_diff_threshold,
"has_nan": has_nan,
}
def test_sagev1_varlen_correctness():
"""Pytest entry point — asserts correctness, returns None."""
_check_sagev1_varlen_correctness()
def run_all_correctness_tests(device="xpu", kernel="v1_pvhalf", quant_block_size=64):
"""Run all VARLEN_TEST_CASES for one kernel variant."""
passed = 0
failed = 0
print(f"\n--- Kernel variant: {kernel} (block_size={quant_block_size}) ---")
for case in VARLEN_TEST_CASES:
batch, total_q, total_kv, h_q, h_kv, head_dim, dtype, is_causal, label = case
print(
f"\n[{label}] B={batch} total_q={total_q} total_kv={total_kv} "
f"Hq={h_q} Hkv={h_kv} D={head_dim} causal={is_causal} dtype={dtype}"
)
try:
result = _check_sagev1_varlen_correctness(
batch=batch,
total_q=total_q,
total_kv=total_kv,
h_q=h_q,
h_kv=h_kv,
head_dim=head_dim,
dtype=dtype,
is_causal=is_causal,
device=device,
kernel=kernel,
quant_block_size=quant_block_size,
)
status = "PASS" if result["passed"] else "FAIL"
extra = " (NaN)" if result.get("has_nan") else ""
print(f" -> {status} max_diff={result['max_diff']:.4f} mean_diff={result['mean_diff']:.6f}{extra}")
if result["passed"]:
passed += 1
else:
failed += 1
except Exception as e:
print(f" -> ERROR: {e}")
failed += 1
total = len(VARLEN_TEST_CASES)
print(f"\n{kernel}: {passed} passed, {failed} failed out of {total}")
return failed == 0
# ========================================================================
# Performance benchmarks
# ========================================================================
VARLEN_BENCH_CASES = [
(1, 4096, 4096, 32, 8, 128, torch.float16, False, "prefill-gqa-nc"),
(1, 4096, 4096, 32, 8, 128, torch.float16, True, "prefill-gqa-c"),
(1, 8192, 8192, 16, 4, 128, torch.float16, False, "prefill-long-nc"),
(1, 8192, 8192, 16, 4, 128, torch.float16, True, "prefill-long-c"),
(1, 2048, 8192, 32, 8, 128, torch.float16, False, "asym-kv-long-nc"),
(1, 2048, 8192, 32, 8, 128, torch.float16, True, "asym-kv-long-c"),
(1, 4096, 4096, 16, 4, 128, torch.bfloat16, False, "bf16-nc"),
(1, 4096, 4096, 16, 4, 128, torch.bfloat16, True, "bf16-c"),
(1, 4096, 4096, 16, 4, 64, torch.float16, False, "dim64-nc"),
(1, 4096, 4096, 16, 4, 64, torch.float16, True, "dim64-c"),
]
def benchmark_sagev1_varlen_case(
batch=2,
total_q=2048,
total_kv=2048,
h_q=16,
h_kv=4,
head_dim=128,
dtype=torch.float16,
is_causal=False,
device="xpu",
kernel="v1_pvhalf",
quant_block_size=64,
warmup_runs=10,
benchmark_runs=100,
verbose=True,
) -> dict:
"""Build a varlen problem and benchmark the SAGE kernel."""
q, k, v, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, seq_lens_q, seq_lens_k = build_varlen_problem(
batch,
total_q,
total_kv,
h_q,
h_kv,
head_dim,
dtype,
device,
min_seq_q=1,
min_seq_kv=1,
)
scale = 1.0 / math.sqrt(head_dim)
# Warmup
for _ in range(warmup_runs):
get_ark().sageattn_varlen(
q,
k,
v,
cu_seqlens_q,
cu_seqlens_k,
max_seqlen_q,
max_seqlen_k,
is_causal=is_causal,
sm_scale=scale,
kernel=kernel,
quant_block_size=quant_block_size,
)
if device == "xpu":
torch.xpu.synchronize()
# Benchmark
st = time.time()
for _ in range(benchmark_runs):
get_ark().sageattn_varlen(
q,
k,
v,
cu_seqlens_q,
cu_seqlens_k,
max_seqlen_q,
max_seqlen_k,
is_causal=is_causal,
sm_scale=scale,
kernel=kernel,
quant_block_size=quant_block_size,
)
if device == "xpu":
torch.xpu.synchronize()
et = time.time()
dur = (et - st) / benchmark_runs
# One more call to get output for diff
out = get_ark().sageattn_varlen(
q,
k,
v,
cu_seqlens_q,
cu_seqlens_k,
max_seqlen_q,
max_seqlen_k,
is_causal=is_causal,
sm_scale=scale,
kernel=kernel,
quant_block_size=quant_block_size,
)
# Build per-batch sagev1 reference to isolate varlen offset logic
cuq = cu_seqlens_q.cpu().tolist()
cuk = cu_seqlens_k.cpu().tolist()
batch_refs = []
for i in range(batch):
q_i = q[cuq[i] : cuq[i + 1]]
k_i = k[cuk[i] : cuk[i + 1]]
v_i = v[cuk[i] : cuk[i + 1]]
n_q, n_k = q_i.shape[0], k_i.shape[0]
q_4d = torch.empty(1, h_q, n_q, head_dim, dtype=q_i.dtype, device=q_i.device)
q_4d[0] = q_i.permute(1, 0, 2)
k_4d = torch.empty(1, h_kv, n_k, head_dim, dtype=k_i.dtype, device=k_i.device)
k_4d[0] = k_i.permute(1, 0, 2)
v_4d = torch.empty(1, h_kv, n_k, head_dim, dtype=v_i.dtype, device=v_i.device)
v_4d[0] = v_i.permute(1, 0, 2)
# Use sagev1 (batched, same quantization) for bit-exact comparison
o_4d = get_ark().sagev1(
q_4d,
k_4d,
v_4d,
is_causal=is_causal,
scale=scale,
quant_block_size=quant_block_size,
)
batch_refs.append(o_4d.squeeze(0).permute(1, 0, 2))
ref = torch.cat(batch_refs, dim=0)
dff = (ref - out).abs()
max_diff = float(dff.max().item())
mean_diff = float(dff.mean().item())
if math.isnan(max_diff):
has_nan = True
max_diff = 999.0
mean_diff = 999.0
else:
has_nan = False
# FLOPs / memory estimate
group = h_q // h_kv
avg_seq_q = total_q / batch
avg_seq_kv = total_kv / batch
ops_per_seq = group * h_kv * avg_seq_q * head_dim * avg_seq_kv * 2 # QK
ops_per_seq += group * h_kv * avg_seq_q * avg_seq_kv * 2 # softmax
ops_per_seq += group * h_kv * avg_seq_q * head_dim * avg_seq_kv * 2 # PV
total_ops = ops_per_seq * batch
bytes_per_seq = (
h_q * avg_seq_q * head_dim + h_kv * avg_seq_kv * head_dim + h_kv * avg_seq_kv * head_dim
) * dtype.itemsize
total_bytes = bytes_per_seq * batch
tflops = total_ops / dur / 1e12
gbps = total_bytes / dur / 1e9
if verbose:
print(f" seq_lens_q={seq_lens_q}")
print(f" seq_lens_kv={seq_lens_k}")
print(f" max_seqlen_q={max_seqlen_q}, max_seqlen_kv={max_seqlen_k}")
print(f" (diff vs per-batch {kernel} (same kernel): bit-exact)")
print_top_diffs(dff, ref, out, topk=4, threshold=0.5)
print(f" Time: {dur*1e3:.3f} ms TFLOPS: {tflops:.2f} GB/s: {gbps:.1f}")
return {
"dur_ms": dur * 1e3,
"tflops": tflops,
"gbps": gbps,
"max_diff": max_diff,
"mean_diff": mean_diff,
"batch": batch,
"total_q": total_q,
"total_kv": total_kv,
"h_q": h_q,
"h_kv": h_kv,
"head_dim": head_dim,
"dtype": "fp16" if dtype == torch.float16 else ("bf16" if dtype == torch.bfloat16 else str(dtype)),
"is_causal": is_causal,
"max_seqlen_q": max_seqlen_q,
"max_seqlen_k": max_seqlen_k,
}
def run_all_benchmarks(device="xpu", kernel="v1_pvhalf", quant_block_size=64, warmup_runs=5, benchmark_runs=50):
"""Run all benchmark cases and print a summary table."""
results = []
print(f"\n--- [Bench] kernel={kernel} block_size={quant_block_size} ---")
for case in VARLEN_BENCH_CASES:
batch, total_q, total_kv, h_q, h_kv, head_dim, dtype, is_causal, label = case
print(f"\n--- [Bench] {label} ---")
try:
m = benchmark_sagev1_varlen_case(
batch=batch,
total_q=total_q,
total_kv=total_kv,
h_q=h_q,
h_kv=h_kv,
head_dim=head_dim,
dtype=dtype,
is_causal=is_causal,
device=device,
kernel=kernel,
quant_block_size=quant_block_size,
warmup_runs=warmup_runs,
benchmark_runs=benchmark_runs,
)
m["label"] = label
results.append(m)
except Exception as e:
print(f" ERROR: {e}")
results.append({"label": label, "dur_ms": None, "tflops": None, "error": str(e)})
# Summary table
print(f"\n{'=' * 120}")
print(
f"{'Label':<25s} {'Batch':>5s} {'TotQ':>6s} {'TotKV':>6s} {'Hq':>4s} {'Hkv':>4s} {'D':>4s} "
f"{'Causal':>7s} {'dtype':>6s} {'MaxSq':>6s} {'MaxSk':>6s} "
f"{'Time(ms)':>9s} {'TFLOPS':>8s} {'GB/s':>8s} {'vs-batch':>9s}"
)
print(f"{'-' * 120}")
for r in results:
dur = f"{r['dur_ms']:.3f}" if r.get("dur_ms") is not None else " N/A "
tflops = f"{r['tflops']:.2f}" if r.get("tflops") is not None else " N/A "
gbps = f"{r['gbps']:.1f}" if r.get("gbps") is not None else " N/A "
md = f"{r['max_diff']:.4f}" if r.get("max_diff") is not None else " N/A "
causal = "causal" if r.get("is_causal") else "non-causal"
dtype_str = r.get("dtype", "?")
hk = r.get("h_kv", "?")
sq = r.get("max_seqlen_q", "?")
sk = r.get("max_seqlen_k", "?")
print(
f"{r['label']:<25s} {r.get('batch', '?'):>5} {r.get('total_q', '?'):>6} "
f"{r.get('total_kv', '?'):>6} {r.get('h_q', '?'):>4} {hk:>4} "
f"{r.get('head_dim', '?'):>4} {causal:>7s} {dtype_str:>6s} "
f"{sq:>6} {sk:>6} "
f"{dur:>9s} {tflops:>8s} {gbps:>8s} {md:>9s}"
)
print(f"{'=' * 120}")
return results
# ========================================================================
# Diagnostic: varlen vs per-batch sagev1 (same kernel math)
# ========================================================================
def compare_varlen_vs_batched_sagev1(device="xpu", kernel="v1_pvhalf", quant_block_size=64):
"""Compare ``sageattn_varlen`` with per-batch ``sagev1``.
Both use the same quantized kernel; this isolates the varlen
offset/striding logic from SAGE quantization error.
"""
batch, h_q, h_kv, head_dim = 4, 16, 4, 128
dtype = torch.float16
total_q, total_kv = 2048, 2048
scale = 1.0 / math.sqrt(head_dim)
q, k, v, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, seq_lens_q, seq_lens_k = build_varlen_problem(
batch, total_q, total_kv, h_q, h_kv, head_dim, dtype, device
)
# Varlen
out_varlen = get_ark().sageattn_varlen(
q,
k,
v,
cu_seqlens_q,
cu_seqlens_k,
max_seqlen_q,
max_seqlen_k,
is_causal=False,
sm_scale=scale,
kernel=kernel,
quant_block_size=quant_block_size,
)
# Per-batch sagev1
cuq = cu_seqlens_q.cpu().tolist()
cuk = cu_seqlens_k.cpu().tolist()
outputs = []
for i in range(batch):
q_i = q[cuq[i] : cuq[i + 1]]
k_i = k[cuk[i] : cuk[i + 1]]
v_i = v[cuk[i] : cuk[i + 1]]
n_q, n_k = q_i.shape[0], k_i.shape[0]
q_4d = torch.empty(1, h_q, n_q, head_dim, dtype=q_i.dtype, device=q_i.device)
q_4d[0] = q_i.permute(1, 0, 2)
k_4d = torch.empty(1, h_kv, n_k, head_dim, dtype=k_i.dtype, device=k_i.device)
k_4d[0] = k_i.permute(1, 0, 2)
v_4d = torch.empty(1, h_kv, n_k, head_dim, dtype=v_i.dtype, device=v_i.device)
v_4d[0] = v_i.permute(1, 0, 2)
o_4d = get_ark().sagev1(
q_4d,
k_4d,
v_4d,
is_causal=False,
scale=scale,
quant_block_size=quant_block_size,
)
outputs.append(o_4d.squeeze(0).permute(1, 0, 2))
ref = torch.cat(outputs, dim=0)
dff = (out_varlen - ref).abs()
md = float(dff.max().item())
mean_d = float(dff.mean().item())
print(f"\n=== Varlen vs Per-Batch Batched {kernel} ===")
print(f" max_diff = {md:.6f}, mean_diff = {mean_d:.8f}")
if md > 0.5:
print_top_diffs(dff, out_varlen, ref, topk=6, threshold=0.1)
return md < 0.5
# ========================================================================
# Entry point
# ========================================================================
def main():
if not is_xpu_available():
print("XPU not available, skipping tests.")
return
all_ok = True
for kernel in KERNEL_VARIANTS:
print("=" * 60)
print(f"sagev1_varlen ({kernel}) — Correctness Tests")
print("=" * 60)
ok = run_all_correctness_tests(kernel=kernel)
all_ok = all_ok and ok
print(f"\n{'=' * 60}")
print(f"sagev1_varlen ({kernel}) — Performance Benchmarks")
print("=" * 60)
run_all_benchmarks(kernel=kernel, warmup_runs=5, benchmark_runs=50)
if not all_ok:
print("\nSome tests FAILED!")
sys.exit(1)
print("\nAll tests passed.")
if __name__ == "__main__":
main()