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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""A/B benchmark for PR#32: does fla's tilelang GatedDeltaNet backend actually beat the
|
| 3 |
+
pure-PyTorch delta-rule fallback on Hopper (sm90)?
|
| 4 |
+
|
| 5 |
+
Context: on Hopper with Triton>=3.4 fla's gated chunk_bwd Triton kernel is miscomputed and
|
| 6 |
+
HARD-RAISES (fla #640). The worker USED to drop fla -> transformers' pure-PyTorch delta rule
|
| 7 |
+
(correct, slow). PR#32 instead installs fla's **tilelang** backend (correct on Triton>=3.4) and
|
| 8 |
+
keeps fla. transformers gates GDN on is_fla_available(), so:
|
| 9 |
+
|
| 10 |
+
* mode 'off' -> fla physically removed -> pure-PyTorch delta rule (the OLD fallback)
|
| 11 |
+
* mode 'tilelang' -> fla + pinned tilelang present -> fla tilelang GDN (the NEW fast path)
|
| 12 |
+
|
| 13 |
+
Run BOTH as separate processes (fla import state is sticky), then compare ms/step + peak VRAM:
|
| 14 |
+
FLA_MODE=off python gdn_fla_tilelang_bench.py
|
| 15 |
+
FLA_MODE=tilelang python gdn_fla_tilelang_bench.py
|
| 16 |
+
|
| 17 |
+
Higher speedup (off_ms / tilelang_ms) and lower peak mem => PR#32's win is real on this host.
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
from __future__ import annotations
|
| 21 |
+
|
| 22 |
+
import json
|
| 23 |
+
import os
|
| 24 |
+
import subprocess
|
| 25 |
+
import sys
|
| 26 |
+
import time
|
| 27 |
+
|
| 28 |
+
TILELANG_PIN = "0.1.11"
|
| 29 |
+
TVM_FFI_PIN = "0.1.11"
|
| 30 |
+
FLA_GIT = "git+https://github.com/fla-org/flash-linear-attention.git@f0e213dbd8b5fb90c3c7eca869ac1706d5377139"
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _run(*a: str) -> int:
|
| 34 |
+
return subprocess.run([sys.executable, "-m", "pip", *a], check=False).returncode
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _remove_fla() -> None:
|
| 38 |
+
"""Physically remove fla so is_fla_available() is False -> transformers native torch delta."""
|
| 39 |
+
import importlib
|
| 40 |
+
import importlib.util
|
| 41 |
+
import shutil
|
| 42 |
+
|
| 43 |
+
_run("uninstall", "-y", "-q", "flash-linear-attention")
|
| 44 |
+
for _ in range(6):
|
| 45 |
+
importlib.invalidate_caches()
|
| 46 |
+
spec = importlib.util.find_spec("fla")
|
| 47 |
+
if spec is None:
|
| 48 |
+
break
|
| 49 |
+
locs = list(getattr(spec, "submodule_search_locations", []) or [])
|
| 50 |
+
if spec.origin:
|
| 51 |
+
locs.append(os.path.dirname(spec.origin))
|
| 52 |
+
gone = False
|
| 53 |
+
for loc in locs:
|
| 54 |
+
if loc and os.path.isdir(loc) and os.path.basename(loc.rstrip("/")) == "fla":
|
| 55 |
+
shutil.rmtree(loc, ignore_errors=True)
|
| 56 |
+
gone = True
|
| 57 |
+
if not gone:
|
| 58 |
+
break
|
| 59 |
+
importlib.invalidate_caches()
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def _ensure_tilelang() -> None:
|
| 63 |
+
import importlib
|
| 64 |
+
import importlib.metadata as md
|
| 65 |
+
|
| 66 |
+
def ver(d):
|
| 67 |
+
try:
|
| 68 |
+
return md.version(d)
|
| 69 |
+
except Exception:
|
| 70 |
+
return None
|
| 71 |
+
|
| 72 |
+
if ver("tilelang") != TILELANG_PIN:
|
| 73 |
+
_run("install", "-q", f"tilelang=={TILELANG_PIN}")
|
| 74 |
+
if ver("apache-tvm-ffi") != TVM_FFI_PIN:
|
| 75 |
+
_run("install", "-q", f"apache-tvm-ffi=={TVM_FFI_PIN}")
|
| 76 |
+
if importlib.util.find_spec("fla") is None or importlib.util.find_spec("fla.modules") is None:
|
| 77 |
+
_run("install", "-q", "--no-deps", FLA_GIT)
|
| 78 |
+
importlib.invalidate_caches()
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def main() -> None:
|
| 82 |
+
mode = os.environ.get("FLA_MODE", "tilelang")
|
| 83 |
+
model_id = os.environ.get("MODEL", "Qwen/Qwen3.5-0.8B")
|
| 84 |
+
seqs = [int(s) for s in os.environ.get("SEQS", "4096,8192,16384").split(",")]
|
| 85 |
+
warmup, iters = 2, 6
|
| 86 |
+
|
| 87 |
+
if mode == "off":
|
| 88 |
+
_remove_fla()
|
| 89 |
+
else:
|
| 90 |
+
_ensure_tilelang()
|
| 91 |
+
|
| 92 |
+
import torch
|
| 93 |
+
|
| 94 |
+
# Report the resolved backend state so the comparison is auditable.
|
| 95 |
+
try:
|
| 96 |
+
from transformers.utils.import_utils import is_fla_available
|
| 97 |
+
|
| 98 |
+
fla_ok = bool(is_fla_available())
|
| 99 |
+
except Exception:
|
| 100 |
+
import importlib.util as _u
|
| 101 |
+
|
| 102 |
+
fla_ok = _u.find_spec("fla") is not None
|
| 103 |
+
tl = None
|
| 104 |
+
try:
|
| 105 |
+
import importlib.metadata as md
|
| 106 |
+
|
| 107 |
+
import tilelang # noqa: F401
|
| 108 |
+
|
| 109 |
+
tl = md.version("tilelang")
|
| 110 |
+
except Exception:
|
| 111 |
+
tl = None
|
| 112 |
+
|
| 113 |
+
name = torch.cuda.get_device_name(0)
|
| 114 |
+
cap = torch.cuda.get_device_capability(0)
|
| 115 |
+
print(
|
| 116 |
+
f"[gdn-bench] mode={mode} gpu={name} sm{cap[0]}{cap[1]} "
|
| 117 |
+
f"is_fla_available={fla_ok} tilelang={tl} torch={torch.__version__}",
|
| 118 |
+
flush=True,
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
from peft import LoraConfig, get_peft_model
|
| 122 |
+
from transformers import AutoModelForCausalLM
|
| 123 |
+
|
| 124 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 125 |
+
model_id, dtype=torch.bfloat16, attn_implementation="sdpa"
|
| 126 |
+
).cuda()
|
| 127 |
+
model = get_peft_model(
|
| 128 |
+
model,
|
| 129 |
+
LoraConfig(r=16, lora_alpha=32, target_modules="all-linear", task_type="CAUSAL_LM"),
|
| 130 |
+
)
|
| 131 |
+
model.train()
|
| 132 |
+
model.gradient_checkpointing_enable()
|
| 133 |
+
vocab = model.config.vocab_size
|
| 134 |
+
|
| 135 |
+
results = []
|
| 136 |
+
g = torch.Generator(device="cpu").manual_seed(0)
|
| 137 |
+
for L in seqs:
|
| 138 |
+
rec = {"seq": L}
|
| 139 |
+
try:
|
| 140 |
+
ids = torch.randint(0, vocab, (1, L), generator=g).cuda()
|
| 141 |
+
torch.cuda.reset_peak_memory_stats()
|
| 142 |
+
|
| 143 |
+
def step(ids=ids):
|
| 144 |
+
out = model(input_ids=ids, labels=ids)
|
| 145 |
+
out.loss.backward()
|
| 146 |
+
model.zero_grad(set_to_none=True)
|
| 147 |
+
return float(out.loss.detach())
|
| 148 |
+
|
| 149 |
+
loss = 0.0
|
| 150 |
+
for _ in range(warmup):
|
| 151 |
+
loss = step()
|
| 152 |
+
torch.cuda.synchronize()
|
| 153 |
+
t0 = time.perf_counter()
|
| 154 |
+
for _ in range(iters):
|
| 155 |
+
loss = step()
|
| 156 |
+
torch.cuda.synchronize()
|
| 157 |
+
ms = (time.perf_counter() - t0) / iters * 1000.0
|
| 158 |
+
rec.update(
|
| 159 |
+
ms_per_step=round(ms, 1),
|
| 160 |
+
peak_gb=round(torch.cuda.max_memory_allocated() / 1e9, 2),
|
| 161 |
+
loss=round(loss, 5),
|
| 162 |
+
)
|
| 163 |
+
print(f"[gdn-bench] seq={L}: {rec['ms_per_step']} ms/step "
|
| 164 |
+
f"peak={rec['peak_gb']} GB loss={rec['loss']}", flush=True)
|
| 165 |
+
except Exception as e: # capture (e.g. fla #640 hard-raise) instead of aborting the sweep
|
| 166 |
+
rec["error"] = f"{type(e).__name__}: {e}"
|
| 167 |
+
print(f"[gdn-bench] seq={L}: ERROR {rec['error']}", flush=True)
|
| 168 |
+
torch.cuda.empty_cache()
|
| 169 |
+
results.append(rec)
|
| 170 |
+
|
| 171 |
+
print("RESULT_JSON " + json.dumps({"mode": mode, "model": model_id, "gpu": name,
|
| 172 |
+
"sm": f"{cap[0]}{cap[1]}", "is_fla_available": fla_ok,
|
| 173 |
+
"tilelang": tl, "results": results}), flush=True)
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
if __name__ == "__main__":
|
| 177 |
+
main()
|