Files
prime-rl/tools
carstenandClaude Opus 5.5 699342d3dc Merge upstream/main into trinity-dsv41; port V4.1 engram onto shared n-gram module
Merges upstream/main (eed0e44c) into trinity-dsv41 (PR #4057 + trinity LoRA warm
start, adapter reload timeouts, renderers fork). Submodule pins for verifiers,
prime-envs and prime-kernels follow upstream/main; renderers stays on the
trinity fork.

Following b417ea31 (#4293), the three n-gram files take upstream's side and
DeepSeek-V4.1's engram is ported onto the shared module:

- engram.py: DeepseekV41NGramHasher(NGramHasher) replaces EngramHasher. It
  maps tokens through the compressed token map, cuts look-back at document
  starts only (no EOS reset), pads with compressed_token_map[pad_token_id],
  takes consecutive_primes_from(engram_vocab_size, skip=24*table, count=24)
  as head sizes, and uses odd PCG64 multipliers seeded with
  10007 * engram_layer_ids[table]. Table rows come from the config
  (384006168 / 384016682) and are checked to hold every bucket.
  DeepseekV41Engram is now only the gate (wkv, q/k weights).
- modeling: DeepseekV41Model.ngram = NGramEmbeddings(hasher, {1: 0, 14: 1},
  head_dim=256, non-persistent layout, deduplicating lookup). The row is
  hashed once and looked up outside the layers. layers[i].engram runs the
  gate inside the AC-wrapped compiled block. PackedContext carries
  cu_seqlens.
- conversion: HF layers.{i}.engram.embed.weight <-> model.ngram.tables.{i}.weight.
  The gate keeps its HF name, so vLLM-side names are unchanged.
- layers/ngram.py: NGramTable/NGramEmbeddings gain persistent_layout and
  deduplicate_lookup options.
- ngram_lookup.py: RowShardedLookup(deduplicate=True) sends unique ids
  (torch.unique) and sums repeated-row grads in fp32. ngram_runtime warms up
  the all-to-all before training.
- model.py: drops AllToAllEmbeddingParallel, the FSDP-ignored table path, the
  hand-applied divide factor and the dp_replicate guard. freeze_engram_tables
  is keyed on NGramTable. optim/__init__.py drops the "engrams." Muon rule.
- transports: the fp8 row-chunk NCCL export is keyed on NGramTables that no
  ChunkedConvOp converts (utils.weights.get_row_chunked_tables). It handles
  plain, 1-D Shard(0) and multi-dim meshes (replicate or size-1 FSDP dims).
  filesystem.py rejects such tables by the same rule instead of
  hasattr(rows_per_rank).
- dsv41_indexer.py: upstream moved fp8_indexer to DeepGEMM and removed
  _triton_fp8_indexer_kernel, so V4.1's two-level indexer now vendors that
  Triton kernel (KEY_ALIGNMENT 16) to keep scoring unchanged.

Equivalence against the pre-merge tree (HEAD 282af805):
- Hashed ids are bit-identical for two synthetic configs with packed
  multi-document rows (incl. 1-token docs, 24 heads/table, many collisions).
  Primes, offsets, multipliers and pad id are identical. The real config
  gives identical 2x24 primes, multipliers and 384006168/384016682 rows.
- The table lookup + gate matches the old DeepseekV41Engram bitwise
  (outputs and grads of the table, wkv, q/k and the streams), fp32 and bf16,
  CPU and CUDA.
- A 6-layer V4.1 stack (attention/MoE stubbed), loaded from the same HF
  state dict, matches bitwise in its output and all 58 HF-named grads.
- RowShardedLookup(deduplicate) on gloo with 2 and 3 ranks and uneven rows:
  outputs bitwise equal to a local lookup and to the old _AllToAllLookup.
  fp32 grads are bitwise equal to the old path. bf16 grads differ by about
  1 bf16 ulp, because the old path accumulated into an fp32 master.
- The fp8 export on gloo (plain, 1-D, replicate x shard, size-1 x shard)
  reassembles byte-identical to quantize_rows_ue8m0 of the full table.

Untested here (no SM90 GPUs, one free GPU): multi-GPU FSDP+RowShardedLookup
training, the NCCL export through PyNccl into vLLM, the vLLM RowChunkWriter,
and the GPU-only test_deepseek_v41.py (skipped on SM86).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 17:55:47 +00:00
..