agent commited on
Commit
bb51fdf
·
1 Parent(s): 884986c

debug_backend: operator-requested cuDNN SDPA NaN diagnostics (GPU id, seq%128, zero-token repro with cudnn sdp disabled, both-backend grad finiteness)

Browse files
app.py CHANGED
@@ -271,6 +271,7 @@ ALLOWED_CMDS = {"train_selfplay.py", "inspect_trace.py", "tests/test_phase1.py",
271
  "experiments/gemma_decoder/run_pilot.py",
272
  "experiments/gemma_decoder/debug_d.py",
273
  "experiments/gemma_decoder/debug_nan.py",
 
274
  "experiments/gemma_decoder/tests_gemma.py"}
275
 
276
 
 
271
  "experiments/gemma_decoder/run_pilot.py",
272
  "experiments/gemma_decoder/debug_d.py",
273
  "experiments/gemma_decoder/debug_nan.py",
274
+ "experiments/gemma_decoder/debug_backend.py",
275
  "experiments/gemma_decoder/tests_gemma.py"}
276
 
277
 
experiments/gemma_decoder/debug_backend.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Operator-requested diagnostics (steps 1-4) for the cuDNN SDPA NaN bug:
2
+ 1) GPU + cudnn version + backend context
3
+ 2) failing seq_len % 128 check
4
+ 3) A-const-with-zero-token repro with cuDNN SDPA disabled
5
+ 4) per-param grad finiteness both backends
6
+ """
7
+ import os, sys
8
+ sys.path.insert(0, "/app")
9
+ import torch
10
+ import torch.nn.functional as F
11
+ print("torch", torch.__version__, flush=True)
12
+ print("cuda available:", torch.cuda.is_available(), flush=True)
13
+ if torch.cuda.is_available():
14
+ print("GPU:", torch.cuda.get_device_name(0), flush=True)
15
+ print("capability:", torch.cuda.get_device_capability(0), flush=True)
16
+ try:
17
+ import torch.backends.cudnn as _c
18
+ print("cudnn version:", _c.version(), "enabled:", _c.is_enabled(), flush=True)
19
+ except Exception as e:
20
+ print("cudnn import fail:", e, flush=True)
21
+
22
+ from experiments.gemma_decoder import run_pilot as rp
23
+ tok, gemma = rp.load_gemma("google/gemma-3-270m", "cpu")
24
+ text = ("Some passes barely move the representation while others rework it "
25
+ "substantly.")
26
+
27
+ def const_with_zero_token(zero=True):
28
+ cp = torch.nn.Parameter(torch.randn(1, 48, 640) * 0.02)
29
+ if zero:
30
+ cp.data[:, 0, :] = 0.0
31
+ def f():
32
+ return cp * rp.GEMMA_SCALE
33
+ return cp, f
34
+
35
+ def run(tag, zero=True, disable_cudnn=False):
36
+ if disable_cudnn and torch.cuda.is_available():
37
+ torch.backends.cuda.enable_cudnn_sdp(False)
38
+ try:
39
+ cp, f = const_with_zero_token(zero)
40
+ ids = tok(text, return_tensors="pt").input_ids
41
+ emb = gemma.model.embed_tokens(ids) * rp.GEMMA_SCALE
42
+ prefix = f()
43
+ full = torch.cat([prefix, emb], 1)
44
+ L = full.shape[1]
45
+ out = gemma(inputs_embeds=full)
46
+ logits = out.logits[0, prefix.shape[1]-1:-1]
47
+ tgt = ids[0, 1:]
48
+ loss = F.cross_entropy(logits, tgt)
49
+ loss.backward()
50
+ gnorm = float(cp.grad.abs().sum())
51
+ import math
52
+ print(f"{tag}: seq_len={L} (mod128={L%128}) loss={float(loss):.3f} "
53
+ f"finite={math.isfinite(gnorm)} gnorm={gnorm:.4e}", flush=True)
54
+ return math.isfinite(gnorm)
55
+ finally:
56
+ if torch.cuda.is_available():
57
+ torch.backends.cuda.enable_cudnn_sdp(True)
58
+
59
+ r1 = run("zero-token DEFAULT backend", zero=True, disable_cudnn=False)
60
+ r2 = run("zero-token NO-cuDNN-SDPA", zero=True, disable_cudnn=True)
61
+ r3 = run("nonzero-token DEFAULT backend", zero=False, disable_cudnn=False)
62
+ print(f"RESULTS: default+zero={r1} nocudnn+zero={r2} default+nonzero={r3}", flush=True)
63
+ print("BACKEND DIAG COMPLETE", flush=True)