david-thrower commited on
Commit
ec71cb2
Β·
verified Β·
1 Parent(s): d0475a7

Upload GITHUB_ISSUE.md

Browse files
Files changed (1) hide show
  1. GITHUB_ISSUE.md +169 -0
GITHUB_ISSUE.md ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Bug Report: 4 Critical Issues Found in NAS / GPU Path (branch `do-not-merge-gpu-tests-from-23`)
2
+
3
+ **Reporter:** ML Intern
4
+ **Branch audited:** `do-not-merge-gpu-tests-from-23`
5
+ **Fix branch:** `fix-gpu-tests-from-23-v2`
6
+ **Severity:** High β€” NAS trials have been wasted on dead parameters; GPU training fails with OOM / NaN / compile crashes.
7
+
8
+ ---
9
+
10
+ ## Summary
11
+
12
+ During a neural-architecture-search audit of `nas_helixlm.py` v2.3 on the `do-not-merge-gpu-tests-from-23` branch, four interrelated bugs were found in `graph.py`, `mamba2.py`, `nodes.py`, `trainer.py`, and `nas_helixlm.py`. All four have been fixed in branch `fix-gpu-tests-from-23-v2` (see commit `d6619f4`).
13
+
14
+ | # | Bug | File(s) | Impact |
15
+ |---|-----|---------|--------|
16
+ | 1 | `nodes_per_column` is ignored by `_build_node_spec()` | `graph.py` | NAS permutes a dead parameter; topology is not reproducible by `seed` alone |
17
+ | 2 | Mamba2 / SSM scan builds a 256-step autograd chain β†’ OOM | `mamba2.py`, `nodes.py` | GPU OOM on SSM configs; ~1.1 GB saved tensors per mamba2 node |
18
+ | 3 | `fp16` AMP forced for `seq_len > 128` β†’ NaN | `nas_helixlm.py`, `trainer.py` | Immediate NaN on small models where there is no memory pressure |
19
+ | 4 | `torch.compile` crashes on custom Python loops | `mamba2.py`, `nodes.py`, `nas_helixlm.py` | Inductor graph-breaks / invalid kernels on GPU; compile silently skipped for SSM/Titans configs |
20
+
21
+ ---
22
+
23
+ ## 1. `nodes_per_column` is dead β€” `_build_node_spec()` ignores it
24
+
25
+ ### Evidence
26
+
27
+ `config.py` docstrings and presets (lines 22-28, 311-380) advertise `nodes_per_column` tuples like `(2,2)`, `(2,3,2)`, `(3,4,4,3)`. Validation logic (lines 249-256) even pads/truncates the tuple to match `n_columns`.
28
+
29
+ **However**, `graph.py` lines 88-148 hardcode every column to:
30
+
31
+ ```python
32
+ column = [
33
+ ("linear_attn" | "full_attn", {…}),
34
+ ("swiglu", {…}),
35
+ ("mamba2" | "ssm", {…}) # if use_ssm
36
+ ("titans", {…}) # if use_titans and ci==0
37
+ ("gate", {…}) # always appended
38
+ ]
39
+ ```
40
+
41
+ `nodes_per_column` is **never read** after validation.
42
+
43
+ ### Fix
44
+
45
+ Wire `nodes_per_column` into `_build_node_spec()` by repeating the `[attention, swiglu]` base pattern until the target count is reached. Optional SSM/Titans nodes consume one slot each. A gate is appended when there are multiple compute nodes or when `ci > 0`.
46
+
47
+ ```python
48
+ for ci in range(cfg.n_columns):
49
+ target = cfg.nodes_per_column[ci] # e.g. 3
50
+ # Build base: attn + swiglu
51
+ # Insert optional SSM/Titans if room
52
+ # Repeat [attn, swiglu] to fill remaining slots
53
+ # Append gate for aggregation
54
+ ```
55
+
56
+ The RNG seed already controls lateral/vertical wiring; with this fix the **node count** is also deterministic and reproducible.
57
+
58
+ ---
59
+
60
+ ## 2. OOM from mamba2 scan's 256-step autograd chain
61
+
62
+ ### Evidence
63
+
64
+ `_ssd_chunked_scan` (and `_ssm_chunked_scan` in `nodes.py`) keeps every intermediate `h` tensor for backward:
65
+
66
+ ```python
67
+ h = A_c[:, t] * h + B_c[:, t] * x_c[:, t].unsqueeze(-1)
68
+ # h is (B, d_inner, d_state) β€” kept for ALL timesteps
69
+ ```
70
+
71
+ For `B=32, d_inner=768, d_state=64`, each mamba2 node alone stores **~1.1 GB** of saved tensors. Multiple columns and loops multiply this.
72
+
73
+ ### Fix
74
+
75
+ Wrap each chunk's inner loop in `torch.utils.checkpoint`:
76
+
77
+ ```python
78
+ def _chunk_scan(h_in, A_c_in, ...):
79
+ h = h_in
80
+ for t in range(chunk_size):
81
+ h = ...
82
+ ys_c.append(y_t)
83
+ return h, torch.stack(ys_c, dim=1)
84
+
85
+ h, ys_chunk = torch.utils.checkpoint.checkpoint(
86
+ _chunk_scan, h, A_c, B_c, x_c, C_c,
87
+ use_reentrant=False,
88
+ )
89
+ ```
90
+
91
+ Only the **chunk boundary** `h` states are materialised for backward. Trade ~10–20 % extra compute for an order-of-magnitude memory reduction.
92
+
93
+ ---
94
+
95
+ ## 3. fp16 AMP causes NaN on `seq_len β‰₯ 256`
96
+
97
+ ### Evidence
98
+
99
+ `nas_helixlm.py` line 395 forced `dtype_str = "float32"` everywhere because fp16 caused immediate NaN on `d β‰₯ 256` with `LR=3e-3`. The root cause is not fp16 universally, but this architecture's SSM scan, Titans memory updates, and `ELU+1.0` feature maps β€” all of which underflow in fp16's narrow dynamic range.
100
+
101
+ ### Fix
102
+
103
+ Use **bfloat16** instead of fp16 on GPU. bf16 shares the same 8-bit exponent range as fp32, so it does not underflow on the scan or memory updates. It is natively supported on Ampere+ (A100, H100, L4) and is typically as fast as fp16.
104
+
105
+ ```python
106
+ if torch.cuda.is_available():
107
+ dtype_str = "bfloat16"
108
+ use_amp = True
109
+ else:
110
+ dtype_str = "float32"
111
+ use_amp = False
112
+ ```
113
+
114
+ The `Trainer` was also updated to skip `GradScaler` when the AMP dtype is bf16 (no loss-scaling needed) and to pass `torch.bfloat16` to `torch.amp.autocast`.
115
+
116
+ ---
117
+
118
+ ## 4. `torch.compile` breaks on custom Python loops
119
+
120
+ ### Evidence
121
+
122
+ The inductor backend cannot compile the `for t in range(chunk_size)` loops with in-place `h` mutations inside `_ssd_chunked_scan` and `TitansMemoryNode.forward`. The old workaround in `nas_helixlm.py` simply **skipped compilation entirely** for any SSM/Titans config:
123
+
124
+ ```python
125
+ if use_ssm or use_titans:
126
+ return model, False, "skipped: SSM/Titans autograd not compile-safe"
127
+ ```
128
+
129
+ ### Fix
130
+
131
+ Decorate the loop functions with `@torch.compiler.disable`. This tells the inductor backend to treat them as opaque ops β€” the rest of the model (embeddings, linear projections, attention, SwiGLU) still gets compiled.
132
+
133
+ Decorated functions:
134
+ - `mamba2._ssd_chunked_scan`
135
+ - `nodes._ssm_chunked_scan`
136
+ - `nodes.TitansMemoryNode.forward`
137
+
138
+ `try_compile_model` in `nas_helixlm.py` now removes the SSM/Titans skip and attempts compilation for all configs.
139
+
140
+ ---
141
+
142
+ ## Reproducing the Baseline
143
+
144
+ The sacred CPU baseline (1000 samples, seq_len=96, 14 epochs) should yield:
145
+ - Train perplexity **~ 23**
146
+ - Val perplexity **~ 85–86**
147
+ - Throughput **~ 1,892 tok/s**
148
+
149
+ With these fixes applied, GPU configs should hit the same numbers (or better, thanks to bf16 + compile) without NaN or OOM.
150
+
151
+ ---
152
+
153
+ ## Patched Files
154
+
155
+ | File | Lines changed | Nature of change |
156
+ |------|---------------|------------------|
157
+ | `helix_lm/graph.py` | +56 / βˆ’20 | `_build_node_spec()` now reads `nodes_per_column` |
158
+ | `helix_lm/mamba2.py` | +25 / βˆ’6 | `@torch.compiler.disable` + `checkpoint` on chunk scan |
159
+ | `helix_lm/nodes.py` | +23 / βˆ’7 | Same for `_ssm_chunked_scan` + `TitansMemoryNode.forward` |
160
+ | `helix_lm/trainer.py` | +29 / βˆ’11 | bf16 autocast, conditional GradScaler |
161
+ | `nas_helixlm.py` | +25 / βˆ’12 | bf16 dtype selection, remove compile skip for SSM/Titans |
162
+
163
+ ---
164
+
165
+ ## Branch
166
+
167
+ `fix-gpu-tests-from-23-v2` (forked from `do-not-merge-gpu-tests-from-23`)
168
+
169
+ **Note:** I do not have write access to push this branch to GitHub. The commit `d6619f4` is ready in the local workspace; please pull / cherry-pick / review before merging to `main`.