Skip to content

Commit 7b2380e

Browse files
committed
EMo: emo_causal_pool chooses each token's pool from its document's tokens up to itself
The whole-document pool lets a document's first tokens route inside a pool chosen with its later tokens, which they don't have at inference. With emo_causal_pool the pool totals are the prefix sums instead, so a document's first token keeps its own top experts and the pool tightens as the document goes on.
1 parent 94ecf97 commit 7b2380e

4 files changed

Lines changed: 43 additions & 5 deletions

File tree

‎src/maxtext/configs/base.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ emo_document_expert_pool_choices: []
318318
emo_document_expert_pool_probs: []
319319
# With moe_lean_routing, find the EMo pool threshold by bisection instead of a sort.
320320
emo_threshold_by_bisection: false
321+
# EMo: choose each token's pool from its document's tokens up to itself (causal) instead of the whole document.
322+
emo_causal_pool: false
321323
# OLMoE3: attention/KDA mixers ignore segment ids (whole-instance, as OLMo-core); only EMo uses them.
322324
olmoe3_segments_for_emo_only: false
323325
moe_global_load_balancing: false # OLMo-core global_load_balancing: LB counts averaged over the whole batch, not per device

‎src/maxtext/configs/types.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,11 @@ class MoEGeneral(BaseModel):
14071407
description="With moe_lean_routing, find the EMo pool threshold by bisecting on the score bits"
14081408
" (32 counting passes) instead of sorting every token's expert scores.",
14091409
)
1410+
emo_causal_pool: bool = Field(
1411+
False,
1412+
description="EMo: choose each token's pool from its document's router scores up to and including that token,"
1413+
" instead of the whole document's, so the pool never depends on later tokens.",
1414+
)
14101415
olmoe3_segments_for_emo_only: bool = Field(
14111416
False,
14121417
description=(

‎src/maxtext/models/olmoe3.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,8 @@ def _emo_mask_logits(self, gate_logits, segment_ids, rngs):
14191419
[emo_min, emo_max] or, with ``emo_document_expert_pool_choices`` set,
14201420
from that set of sizes (uniformly, or with ``emo_document_expert_pool_probs``);
14211421
outside training it is fixed at ``emo_eval_document_expert_pool``.
1422+
With ``emo_causal_pool`` a token's totals cover only its document's tokens
1423+
so far, so the pool never depends on later tokens.
14221424
14231425
MaxText packs documents contiguously, so the per-document score totals are
14241426
cumulative-sum differences at document boundaries rather than a scatter
@@ -1441,10 +1443,15 @@ def _emo_mask_logits(self, gate_logits, segment_ids, rngs):
14411443

14421444
scores = jax.nn.softmax(gate_logits.astype(jnp.float32), axis=-1)
14431445
csum = jnp.cumsum(scores, axis=1)
1444-
csum_end = jnp.take_along_axis(csum, end[..., None], axis=1)
14451446
before = jnp.take_along_axis(csum, jnp.maximum(start - 1, 0)[..., None], axis=1)
14461447
csum_before = jnp.where((start == 0)[..., None], 0.0, before)
1447-
document_scores = csum_end - csum_before # [b, s, E]: each token sees its document's totals
1448+
if cfg.emo_causal_pool:
1449+
# Each token's pool comes from its document's tokens up to and including itself, as at inference. The first
1450+
# token's pool is then its own top `pool` experts, which hold its top-k: its routing is unconstrained.
1451+
document_scores = csum - csum_before
1452+
else:
1453+
csum_end = jnp.take_along_axis(csum, end[..., None], axis=1)
1454+
document_scores = csum_end - csum_before # [b, s, E]: each token sees its document's totals
14481455

14491456
if rngs is not None and cfg.model_call_mode != "inference" and not _EMO_EVAL_POOLS.get():
14501457
rng = rngs.params() if hasattr(rngs, "params") and callable(getattr(rngs, "params")) else rngs

‎tests/unit/olmo_ppl_eval_test.py‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@ def p_eval_step(state, batch):
118118
class EmoEvalPoolsTest(unittest.TestCase):
119119
"""Under ``emo_eval_pools`` the router keeps the eval pool instead of sampling a pool size per document."""
120120

121-
def _mask(self, key):
121+
def _mask(self, key, causal=False, logits=None, segments=None):
122122
"""(logits, EMo-masked logits) for a router with pools in [2, 8] of 8 experts, sampled with `key`."""
123123
cfg = types.SimpleNamespace(
124+
emo_causal_pool=causal,
124125
emo_document_expert_pool_choices=[],
125126
emo_document_expert_pool_probs=[],
126127
emo_min_document_expert_pool=2,
@@ -130,8 +131,8 @@ def _mask(self, key):
130131
moe_lean_routing=False,
131132
)
132133
router = types.SimpleNamespace(config=cfg)
133-
logits = jax.random.normal(jax.random.PRNGKey(0), (2, 16, 8))
134-
segments = jnp.ones((2, 16), jnp.int32)
134+
logits = jax.random.normal(jax.random.PRNGKey(0), (2, 16, 8)) if logits is None else logits
135+
segments = jnp.ones((2, 16), jnp.int32) if segments is None else segments
135136
return logits, olmoe3.OLMoE3LatentRoutedMoE._emo_mask_logits( # pylint: disable=protected-access
136137
router, logits, segments, key
137138
)
@@ -156,6 +157,29 @@ def test_eval_context_pool_size_override(self):
156157
logits, masked = self._mask(jax.random.PRNGKey(1))
157158
np.testing.assert_array_equal(np.asarray(masked), np.asarray(logits))
158159

160+
def test_causal_pool_ignores_later_tokens(self):
161+
"""With ``emo_causal_pool`` a token's pool depends only on its document's tokens up to itself."""
162+
segments = jnp.asarray([[1] * 6 + [2] * 10, [1] * 16], jnp.int32)
163+
key = jax.random.PRNGKey(1)
164+
logits, masked = self._mask(key, causal=True, segments=segments)
165+
later = logits.at[:, 9:].add(5.0 * jax.random.normal(jax.random.PRNGKey(2), (2, 7, 8)))
166+
_, masked_later = self._mask(key, causal=True, logits=later, segments=segments)
167+
keep, keep_later = np.isfinite(np.asarray(masked)), np.isfinite(np.asarray(masked_later))
168+
np.testing.assert_array_equal(keep[:, :9], keep_later[:, :9])
169+
self.assertFalse(np.array_equal(keep[:, 9:], keep_later[:, 9:]), "later pools should follow the changed tokens")
170+
171+
def test_causal_pool_first_token_keeps_its_own_top_experts(self):
172+
"""A document's first token keeps its own highest-scoring experts; its last token's pool is the whole-document one."""
173+
segments = jnp.asarray([[1] * 6 + [2] * 10, [1] * 16], jnp.int32)
174+
key = jax.random.PRNGKey(1)
175+
logits, causal = self._mask(key, causal=True, segments=segments)
176+
_, whole = self._mask(key, causal=False, segments=segments)
177+
causal, whole, logits = np.asarray(causal), np.asarray(whole), np.asarray(logits)
178+
for b, first, last in ((0, 0, 5), (0, 6, 15), (1, 0, 15)):
179+
kept = np.isfinite(causal[b, first])
180+
self.assertTrue(np.all(logits[b, first][kept].min() >= logits[b, first][~kept])) # its own top-|pool| logits
181+
np.testing.assert_array_equal(np.isfinite(causal[b, last]), np.isfinite(whole[b, last]))
182+
159183

160184
class PplEvalGraphdefTest(unittest.TestCase):
161185
"""The perplexity program's MoE layers don't drop tokens, whatever the training ragged_buffer_factor."""

0 commit comments

Comments
 (0)