Skip to content

Some fixes/improvements around synchronous bookkeeping operations - #83

Merged
epwalsh merged 17 commits into
mainfrom
epwalsh/speed-test
Nov 7, 2024
Merged

epwalsh merged 17 commits into
mainfrom
epwalsh/speed-test

Conversation

@epwalsh

@epwalsh epwalsh commented Nov 6, 2024

Copy link
Copy Markdown
Contributor

No description provided.

@epwalsh
epwalsh merged commit 83db5f7 into main Nov 7, 2024
@epwalsh
epwalsh deleted the epwalsh/speed-test branch November 7, 2024 00:09
dhgottesman pushed a commit to dhgottesman/OLMo-core that referenced this pull request Dec 30, 2025
dhgottesman pushed a commit to dhgottesman/OLMo-core that referenced this pull request Dec 30, 2025
jason718 added a commit that referenced this pull request Nov 7, 2026
Stacked on #916. This prepares an ablation PT asked for: Run 2 (the
"Ablation 2" config, row 34 of the midtraining results sheet: 256K,
`lm1e-4` alignment parent, MT+ with agentic data, vision v6), rerun with
EMo turned **on with causal pools** during mid-training instead of EMo
off. The hero recipe currently runs every downstream stage with EMo off.

## Why

From Akshita's write-up and MaxText work (oe-planning #86–#88, #97):
- Whole-document EMo picks each document's pool from router scores
summed over the **entire** document, future tokens included. A
document's first tokens are therefore routed with information they won't
have at inference.
- The cost in her tests:
  - First-16-token loss is 1.3–1.9 nats worse.
- On a bare prompt, the model ends the document immediately in 13 of 16
cases.
- Causal pools fix this and keep modularity on her tiny-model decay
(step 9,499):

| Decay | First-16 CE (all experts) | Own 64-expert pool CE | Bare
prompt ends immediately |
|---|---|---|---|
| EMo off | 3.33 | 2.135 | 0/16 |
| EMo on | 5.20 | 2.092 | 13/16 |
| Causal | 3.34 | 2.067 | 0/16 |

Causal pools haven't been tested in mid-training yet, which is what this
run is for. The bar is "no regression against Run 2" on our evals. PT
runs the modularity checks on the checkpoint.

## What changes

### 1. `EmoRouterConfig.causal_pool`
Off by default; the default path is unchanged. This is the same rule as
the MaxText `emo_causal_pool` flag (AkshitaB/maxtext@7b2380e):
- MaxText's `csum_end - csum_before` (whole document) becomes `csum -
csum_before`: the sum of the document's scores up to and including each
token, in fp32 on the softmax scores. Here that's `ops.doc_prefix_sum`,
used in place of `ops.doc_sum_scatter`.
- Each token's pool is the top-P experts by those sums. A document's
first token keeps its own top experts, the pool narrows as the document
goes on, and the last token's pool equals the whole-document pool.
- In causal mode the env-gated fused `document_pool_keep_mask` profiling
kernel is bypassed.

### 2. EMo document boundaries in `MultimodalLM` (no MaxText
counterpart)
- Until now the LM derived EMo documents from EOS tokens
(`segment_ids_from_eos`). That matched attention in text pretraining.
- In mixed mid-training batches, attention documents aren't
EOS-delimited:
- The text side is 32 unrelated 8K chunks per 256K instance, with no EOS
at chunk ends.
  - Packed vision examples need not end in EOS.
- Without this change, a pool would span unrelated chunks or examples.
- `MultimodalLM` now passes its attention documents to
`Transformer._prepare_inputs` as `emo_segment_ids`. These come from
`example_ids`, or `document_ids` when set: the text stage's EOS +
8K-chunk segments. A padding run counts as one document.
- Pools therefore restart exactly where attention and KDA state restart.
- Nothing is passed when the LM has no EMo routers, and plain
`Transformer` callers are unchanged.

## Pool sizes: same as pretraining

When a stage keeps EMo (`apply_stage_routing(use_emo=True)` in
scaling-ladders), it reuses the source checkpoint's router config
unchanged. I checked both source checkpoints
(`olmo35-small-hero-20260907/emo/14T` and
`olmo35-small-14t-plus600bdecay-20261008-emo/step870229`):
- 512 routed experts, top-k 16.
- `min_document_expert_pool=16`, `max_document_expert_pool=512`: each
document draws one pool size P uniformly from 16–512, independently in
each MoE layer. Each token picks its 16 experts from inside its pool.
  - P = 16: every token in the document uses the same 16 experts.
  - P = 512: no restriction.
- `eval_document_expert_pool=512`: eval and HF export use all experts,
so EMo (causal or not) is a no-op for our standard evals. Only PT's
modularity checks use small pools.

The {32, 64, 256} sizes (probabilities 1/8, 1/8, 3/4) in the write-up
are MaxText's tiny-model "r3" recipe. The write-up finds those sizes
aren't special.

## Tests

`src/test/nn/moe/emo_router_v2_test.py`. The two router tests mirror
MaxText's `EmoEvalPoolsTest` cases:
- Same setup: router weight = identity so expert logits are given
directly; pools sampled in [2, 8] of 8 experts.
- Same documents: 6 + 10 tokens in row 0 and 16 tokens in row 1.
- The pool mask is read from the router's `pool_keep_mask` call.

| Test | Checks |
|---|---|
| `test_causal_pool_ignores_later_tokens` | Noise added to tokens 9
onward leaves the pool masks of tokens 0–8 unchanged and changes later
ones (as MaxText's `test_causal_pool_ignores_later_tokens`) |
| `test_causal_pool_first_token_keeps_its_own_top_experts` | Each
document's first token keeps exactly its own top-P logits, and its last
token's mask equals the whole-document one (as MaxText's test of the
same name) |
| `test_doc_prefix_sum_matches_per_document_cumsum` | Prefix sums match
a brute-force per-document cumsum over 4 segment layouts, and the last
token of each document matches `doc_sum_scatter` |
| `test_multimodal_emo_segments_follow_example_ids` | Document numbering
from `example_ids`, including the padding run |

Differences from MaxText's tests:
- Segment ids are 0-based, as OLMo-core's are.
- The seed is chosen so every first-token pool actually restricts its
document (pools of 3, 7 and 6 experts). The test asserts this, so it
can't pass trivially with a full pool.

Results (CPU):
- EMo tests (`emo_router_v2_test.py` + `emo_document_pool_test.py`): 16
passed, 5 skipped (GPU-only kernel tests).
- With causal mode disabled in the router, both causal tests fail, as
they should.
- `src/test/nn/vision/multimodal_test.py`: 46 passed.
- isort, black and ruff are clean. mypy reports nothing in the changed
files (the 9 errors it prints are pre-existing, in `data/multimodal/*`
and `multimodal_train_module.py`).

## Not in this PR / next
- The scaling-ladders switch that runs Run 2's config with
`use_emo=True` and `causal_pool=True` (#83 follow-up).
- A GPU smoke run: loss at Run 2's start, and the step-time overhead of
EMo routing compared with Run 2.
- The full run: 64 GPUs for about 2 days, pending compute from Taira.
Checkpoints go to PT for modularity checks.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
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.

1 participant