Skip to content

Eval forward: remove host syncs so it compiles as one graph - #73

Open
Thunderblok wants to merge 3 commits into
david-thrower:71-bug-fixes-and-cleanup-of-br-70-kfrom
Thunderblok:perf/canonical-eval-no-host-sync
Open

Thunderblok wants to merge 3 commits into
david-thrower:71-bug-fixes-and-cleanup-of-br-70-kfrom
Thunderblok:perf/canonical-eval-no-host-sync

Conversation

@Thunderblok

@Thunderblok Thunderblok commented Nov 17, 2026 •

Copy link
Copy Markdown

Follow-up to the int(n.max()) graph break you found in _canonical_eval_hidden.

Changes

  • hf_model.py: n <= L always holds, so the length check needs data only when L > seq_len. For L <= seq_len (every generation step inside the training context), it is now a shape-only branch: no device sync and no graph break. The data-dependent check stays for the rare L > seq_len case.
  • recurrent.py: the ACT early exit (halted.all()) was a second data-dependent graph break in the eval forward. It now runs in eager mode only (not torch.compiler.is_compiling()). Under compile, the fixed n_loops run. Loops after all tokens halt add weight 0, so the output does not change.
  • test_eval_compile.py: compiles the eval forward with fullgraph=True and checks it against eager. Also checks the raw-layout path for rows longer than seq_len. This test fails on 7e4dd59 and passes here.

Checks

  • Eager outputs are bit-identical to 7e4dd59. Checked lengths: L = 1, 7, 64, 255, 256, 300 and 400, with left and right padding, and with L > seq_len on both the packed and the raw paths.
  • torch._dynamo.explain reports 0 graph breaks on the eval forward. Before this change: 2 (Tensor.item(), then halted.all()).
  • Our patch test suite gives the same result on 7e4dd59 and on this branch (61 passed; the 4 failures are environment-only and also fail on 7e4dd59).

Not changed

  • The per-call cost of the canonical 2048-token layout is unchanged. That cost is what makes eval match training.
  • The training path is unchanged.

🤖 Generated with Claude Code

_canonical_eval_hidden checked int(n.max()) > seq_len on every call. Since
n <= L, the check needs the data only when L > seq_len; for L <= seq_len it
is now a shape-only branch (no device sync, no torch.compile graph break).

The ACT early exit in the recurrent loop (`halted.all()`) was the other
data-dependent break. It now runs only in eager mode; under torch.compile
the fixed n_loops run. Skipped loops add weight 0, so outputs are unchanged.

Eager outputs are bit-identical to before (checked for L = 1..400, left
and right padding, and L > seq_len packed and raw paths). The eval forward
now compiles with fullgraph=True and matches eager. The 2048-token canonical
layout cost per call is unchanged; it is what makes eval match training.

test_eval_compile.py fails on 7e4dd59 and passes here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RmsBBBg4SD8FZFAJnebLt9
Copilot AI balanced review requested due to automatic review settings November 17, 2026 00:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

🔵 Needs a closer look

The change impacts torch.compile capture behavior (SymInt guards and ACT early-exit under compile), and the evidence indicates it still needs validation on the target backend/hardware to confirm single-graph fullgraph capture, avoid recompiles, and maintain acceptable performance.

No open issues 🎉

What changed in this PR

Updates the eval/generation forward path to eliminate host sync–triggered torch.compile(fullgraph=True) graph breaks so eval can compile as a single graph in the common case.

Changes:

  • Refactors canonical eval length handling to keep the L <= seq_len path shape-only (no device→host sync), preserving the data-dependent check for L > seq_len.
  • Disables ACT’s data-dependent early-exit during compilation to avoid graph breaks, while keeping eager-mode behavior.
  • Adds a regression test that compiles eval forward with fullgraph=True and checks compiled vs eager equivalence, including coverage for the raw-layout long-row path.
File Description
test_eval_compile.py Adds unit coverage that compiles the eval forward with fullgraph=True and asserts compiled outputs match eager, including a L > seq_len raw-layout regression check.
helix_lm/​recurrent.py Gates ACT’s halted.all() early-exit so it only runs in eager mode, preventing a data-dependent graph break when the eval forward is compiled.
helix_lm/​hf_model.py Adjusts canonical eval length logic to avoid host-syncing length checks for the common L <= seq_len case while retaining the data-dependent fallback for L > seq_len.

Review effort: Balanced

@david-thrower

Copy link
Copy Markdown
Owner

Verdict: technically sound, ship it, but not into 72

Both graph breaks are real; I saw Tensor.item() at hf_model.py:361 in the log myself, and the halted.all() sync is the same class of defect. The reasoning for each fix is correct:
n ≤ L ≤ T. valid.sum(dim=1) can never exceed the row length, so when L ≤ seq_len the data-dependent test is genuinely dead code. That's a sound elimination, not a shortcut.
ACT under compile. Inactive loops contribute weight = rem * still = 0, so h_out + 0*h is exact. Skipping the early exit under compile is output-preserving.
Why it should not be diverted to br-72

The training path is untouched, and the run has generated_example_length=0 — it never generates. There is literally nothing for this PR to improve in the job that is queued right now.
Branch 72 has to stay frozen. The launcher resolves the branch to a SHA via ls-remote at startup and records it in the model card. If the branch moves under a running job, the recorded commit stops meaning "the code that trained this model" — that is precisely the provenance failure that cost us a day (branch-70 checkpoint that no longer loads under its own code). Freeze 72, land this as 73-eval-compile-no-syncs, merge after the run.

Three things I'd want before merge

(a) "0 graph breaks" ≠ "0 recompiles." The branch still specialises on L ≤ T vs L > T, and generation grows L one token at a time (1, 2, 3 … T). That's a shape guard firing per length. Count them with TORCH_LOGS=recompiles over a 100-token generation — if it thrashes, the fix has traded a sync for a recompile storm, which is worse. If so, torch._dynamo.mark_dynamic(input_ids, 1) is the usual remedy.
(b) The "output does not change" claim has exactly one hole: NaN. h_out + 0 * h is exact for finite h, but 0 * NaN = NaN. In eager the loop is skipped, so a non-finite h never reaches h_out; under compile the loop runs and poisons it. That's the one case where compiled and eager diverge. Given this codebase already carries strict_nan_check and the _safe_attention NaN guards, either state the caveat explicitly ("numerically identical for finite activations") or mask it — torch.where(weight > 0, weight * h, 0).
(c) The claim needs a bit-identical test against compiled, not just against 7e4dd59. Comparing eager-vs-7e4dd59 proves the refactor is neutral; it doesn't prove fullgraph compilation is. Add torch.equal(compiled_out, eager_out) for the same length sweep, plus one case where a later loop would produce a non-finite value.
The Copilot note's point about backend dependence is fair and worth honouring: fullgraph capture and recompile behaviour on CPU don't establish L40S/A100. One run of the new test on the actual target settles it.

One expectation to set

This removes per-step syncs and recompiles; it does not reduce the per-call cost of the canonical 2048-token layout the PR says so, and it's the right call. So generative throughput will barely move. If the on-device story is the goal, the KV cache is still the item that matters, and this PR is a prerequisite for it rather than a substitute.
Operationally: land it as 73, leave it out of the running job, and after the 5B finishes I'll re-run the mixed generation check (br71_generation_check.py pattern — branch-73 code on trained weights) to confirm the output is byte-identical to what 72 produces. That's a five-minute job and it's the only evidence that matters for a change to the eval path.

@david-thrower

Copy link
Copy Markdown
Owner

TLDR: hold it. Merge after the run, not before. It cannot improve the training, and it can only add risk to a job that hasn't started yet.

The timing fact that decides it: the queued job hasn't been placed — 31m so far — and the launcher downloads the branch when the container starts, not when you submitted. So merging 73 into 72 right now wouldn't be a neutral act; it would silently change what the queued job pulls and records as its code_commit.

merge into 72 now hold to its own branch
effect on training numerics none — both changes are eval-only (_canonical_eval_hidden runs only when not self.training; the ACT break already has and not self.training, so compile-guarding it is a no-op in training) none
effect on the recorded commit commit becomes code that no end-to-end run has exercised commit is exactly what was verified: SFT, push, reload, bf16
risk to the queued job a typo or import error in 73 kills it at startup — cheap in money, ~30–50 min lost in queue zero
provenance a branch that moves under a queued job is the exact failure from branch 70 (checkpoint whose code no longer matches) 72 stays frozen, attributable, reproducible
cost of holding 73 sits unmerged; eval path stays uncompiled none that affects the model — lighteval is eager and never compiles

Why "it can't affect training" isn't a good enough reason to merge now: that sentence is precisely the shape of every bug in this project. "Training always uses autocast, so bf16 can't matter." "The config is fine, save_pretrained can't fail." Both were true until they weren't. The difference here is that I can afford to be wrong — the fix is pure performance, so the run loses nothing by not having it.

What I'd do:

  1. Let the queued job start on 72 exactly as verified.
  2. Land 73 as its own branch, get the three things from my earlier review: a recompile count over a 100-token generation, an explicit accepted-answer on the 0 * NaN case, and one run on L40S/A100 rather than CPU.
  3. After the 5B finishes, run the mixed check — branch-73 code over the trained weights — and confirm output is byte-identical to what 72 produces. That's ~$0.05 and it's the only evidence that matters for an eval-path change.

If you'd rather merge now anyway, the safe sequence is: cancel the queued job first, merge, then submit a 2-minute startup-only run (imports, token check, bf16 gate, one optimizer step) to prove 73 doesn't break anything, then fire the real run. That costs the queue position and buys the provenance — but it buys nothing for the model.

Copilot AI balanced review requested due to automatic review settings November 17, 2026 01:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

🟡 Changes recommended

The workflow step in .github/workflows/python-app.yml has a critical command-continuation error where a trailing backslash on the final unittest line causes the subsequent python pretrain_data_court.py line to be appended to the unittest invocation, breaking the CI step until the backslash is removed.

1 open issue

Review effort: Balanced

Comment thread .github/workflows/python-app.yml Resolved
Added a new test for evaluating graph equivalence to the CI workflow.
Copilot AI balanced review requested due to automatic review settings November 17, 2026 01:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

🔵 Needs a closer look

A hidden review note indicates the test’s determinism setup is potentially misleading because torch.manual_seed(1) may be overridden by model construction auto-seeding, coupling generated ids to internal initialization behavior and warranting human scrutiny before approval.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@david-thrower

Copy link
Copy Markdown
Owner

All checks pass. Note this branch is 1 bug fix behind branch #72 so the integration plan:

  1. Proceed to Merge this into bug-fixes-and-cleanup-of-br-70-k #71
  2. Create a staging branch from bug-fixes-and-cleanup-of-br-70-k #71 (with this merged in).
  3. Merge patch-sft-bug-fix-from-71 #72 into the staging branch. (probably will be main, as patch-sft-bug-fix-from-71 #72 is a candidate to merge into main once https://huggingface.co/david-thrower/HelixLM-53.9M-20261116-br72-d512-h8-nl3-s2048-5.0BT-wsd is done training)
  4. Repeat the battery of small ablation tests we ran before training the br72 base model on a model built from the staging branch (Tests pre-training, generation from the pre-train, saving pretrain, push pretrian to hub, load it from the hub, verify continued pre-training still works, verify generate still works, verify liteval works on the pre-train model, verify we can perform SFT on the downloaded pretrain, verify that saving the SFT model works, generation works on the SFT, and liteval works on the SFT model).
  5. Train the next base model from a model built from the staging branch.
  6. Merge the staging branch into main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants