Deduplicate the GLM-5.2 and DSV4 exact LM heads onto shared machinery - #75
Merged
qywu merged 1 commit intoAug 20, 2026
Merged
Conversation
Second consolidation tranche for #71: the two exact heads ran the same program skeleton in near-duplicate. New xorl/models/transformers/exact_lm_head_shared.py owns the skeleton; each family keeps only its contract-bearing pieces (base projection kernel, temperature dtype boundary, native selected-score kernel, surrogate formulation, geometry validation). - ONE autograd boundary (ExactLmHeadFunction) replaces the three per-family Functions. Row ownership is an injected ExactHeadRowPlan: replicated (GLM's local head), equal rank-order blocks (GLM TP16), or ragged padded blocks (DSV4 TP8). Row-gather closures stay family-side so their monkeypatch points and collectives semantics are unchanged. - ONE filtered surrogate-VJP chunk loop and ONE unfiltered reference-grad plumbing, parameterized by exact-score/reference-logits closures. - Shared LoRA A/B SGEMM choreography (exact_lora_local_logits) and single-adapter LoRABatchInfo builder; families keep their pinned base GEMM. - Shared rank-order collectives (vocab gather with dtype/world parameters, equal and ragged row gathers, row counts, fp32 sum) and one TP-group geometry checker with per-family program names, preserving the pinned error substrings. - Both filtered forwards now score through score_with_sampling_transforms instead of hand-rolled support/identity/partitioned sequences. - gather_vocab_shards moves from the loss module to xorl.utils.dist_utils (review placement finding on #74). Net -575 lines. Forward value paths are byte-preserved: the same kernels run in the same order with the same dtype boundaries; only the plumbing moved.
Broly Security ScanNote ✅ Clean scan Note Re-scan this PR anytime with
|
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #74 (base branch
issue-71-consolidate-scoring-paths; retarget tomainafter #74 merges). Second consolidation tranche for #71: the GLM-5.2 (glm5/exact_lm_head_qlora.py) and DSV4 (deepseek_v4/exact_lm_head.py) exact heads ran the same program skeleton in near-duplicate — this PR extracts it once and leaves each family holding only its contract.Design rule: share the machinery, inject the contract
New
xorl/models/transformers/exact_lm_head_shared.pyowns the skeleton. The families keep the byte-bearing pieces as closures and constants:ExactLmHeadFunction— the one autograd boundary (gather rows → exact value → local slice; backward: gather grads → surrogate VJP → local slice). Replaces 3 per-family Functions.head_v2_full_logits_with_lseFP32 vsF.linearBF16)ExactHeadRowPlan— row ownership as an injected plan: replicated (GLM local head), equal rank-order blocks (GLM TP16), ragged padded blocks (DSV4 TP8)filtered_surrogate_local_grad_logits/surrogate_local_grad_logits— the chunked support+reference-grad loops, parameterized by exact-score/reference-logits closureshead_v2_selected_logprob_from_logitsvs BIlog_softmax)exact_lora_local_logits+single_adapter_lora_batch_info— the literal serving A/B SGEMM choreography and its invariants_local_qlora_surrogate_vjpvs_local_surrogate_vjp— different validated dtype boundaries, deliberately NOT merged)check_exact_head_tp_group— one TP-group geometry checker with per-family program namessource_ordinal)Both filtered forwards now also score through the shared
score_with_sampling_transformsinstead of hand-rolled support/identity/partitioned sequences, andgather_vocab_shardsmoved from the loss module toxorl.utils.dist_utils(the placement finding from #74's review).Byte-safety
Forward value paths are byte-preserved: the same kernels run in the same order with the same dtype boundaries — only plumbing moved. The pieces where the two families genuinely differ in validated numerics (the straight-through surrogate formulations, the base GEMMs, the temperature stores) were deliberately left family-side rather than force-unified. Test-pinned error substrings (
requires TP16,gather order,shard/group rank mismatch,must use NCCL,equal source-row counts,collective rank order) survive via theprogramparameter.Numbers
Net −575 lines (
glm5/exact_lm_head_qlora.py1298→847,deepseek_v4/exact_lm_head.py836→612, +369-line shared module). Combined with #74, the exact-head files are down ~40% while gaining the filter capability.Testing
test_glm52_exact_lm_head_qlora_collectives.py), the ragged plan + monkeypatched collectives (test_dsv4_exact_lm_head_temperature.py), the replicated plan + Hopper byte test (test_glm52_exact_lm_head_qlora.py), and the TP-group validation matrix. Test collection counts are identical before/after.test_lm_head_tp_fsdp_e2e.py,test_lm_head_tp_parallel_state.py,test_glm52_exact_qlora_fsdp2.py(17 tests, 2×H100, ~8.5 min).