Skip to content

Commit f13ccd1

Browse files
Merge pull request #4492 from AI-Hypercomputer:single_random_seed
PiperOrigin-RevId: 949273858
2 parents 5d2e6c6 + 7819d62 commit f13ccd1

2 files changed

Lines changed: 89 additions & 12 deletions

File tree

src/maxtext/layers/moe.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ def get_topk(self, gate_logits, pre_bias_logits, rngs=None, input_ids=None):
691691
if rngs is None:
692692
raise ValueError("The random key cannot be None for random routing.")
693693
# Reuse the 'params' RNG stream to ensure random routing
694-
rng = rngs.params()
694+
rng = rngs.params() if hasattr(rngs, "params") and callable(getattr(rngs, "params")) else rngs
695695
top_k_weights, top_k_indices = random_routing(rng, gate_logits, self.num_experts_per_tok)
696696
return top_k_weights, top_k_indices
697697

@@ -1949,16 +1949,9 @@ def moe_emb_chunking(
19491949
expert_shard_id = jax.lax.axis_index(self._expert_parallelism_name) if num_ep > 1 else 0
19501950

19511951
chunk_dim = embed_dim // self.config.num_moe_emb_chunks
1952-
chunk_rngs = (
1953-
jax.random.split(rngs.params(), self.config.num_moe_emb_chunks)
1954-
if self.config.use_random_routing
1955-
and rngs is not None
1956-
and hasattr(rngs, "params")
1957-
and callable(getattr(rngs, "params"))
1958-
else [rngs] * self.config.num_moe_emb_chunks
1952+
chunk_rngs_key = (
1953+
rngs.params() if rngs is not None and hasattr(rngs, "params") and callable(getattr(rngs, "params")) else None
19591954
)
1960-
if rngs is not None:
1961-
chunk_rngs = jax.random.split(rngs.params(), self.config.num_moe_emb_chunks)
19621955

19631956
first_x_unrouted = jax.lax.dynamic_slice_in_dim(x, 0, chunk_dim, axis=2)
19641957
cur_x_chunk, routing, route_metadata = roe_ag_and_route(
@@ -1967,7 +1960,7 @@ def moe_emb_chunking(
19671960
pre_bias_logits,
19681961
num_ep,
19691962
expert_shard_id,
1970-
nnx.Rngs(params=chunk_rngs[0]) if chunk_rngs is not None else None,
1963+
chunk_rngs_key,
19711964
input_ids=sharded_input_ids,
19721965
)
19731966

@@ -2001,7 +1994,7 @@ def scan_fn(carry, _):
20011994
pre_bias_logits,
20021995
num_ep,
20031996
expert_shard_id,
2004-
nnx.Rngs(params=chunk_rngs[chunk_idx + 1]) if chunk_rngs is not None else None,
1997+
chunk_rngs_key,
20051998
input_ids=sharded_input_ids,
20061999
)
20072000
return (next_x, next_ps0, next_ps1, chunk_idx + 1), None

tests/unit/moe_test.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,90 @@ def test_dense(self):
537537
actual_output, _, _ = self.get_moe_output(variables, hidden_states, cfg, mesh)
538538
self.assertTrue(jax.numpy.allclose(expected_output, actual_output, rtol=1e-02, atol=1e-02, equal_nan=False))
539539

540+
@pytest.mark.tpu_only
541+
def test_moe_emb_chunking_random_routing(self):
542+
cfg_chunked = pyconfig.initialize(
543+
[None, get_test_config_path()],
544+
run_name="moe_block_chunking_rr_test",
545+
enable_checkpointing=False,
546+
model_name="mixtral-8x7b",
547+
dtype="bfloat16",
548+
weight_dtype="bfloat16",
549+
megablox=False,
550+
sparse_matmul=True,
551+
use_tokamax_gmm=True,
552+
use_gmm_v2=True,
553+
num_moe_emb_chunks=4,
554+
use_ring_of_experts=True,
555+
use_random_routing=True,
556+
mlp_bias=True,
557+
per_device_batch_size=1,
558+
max_target_length=128,
559+
)
560+
561+
cfg_non_chunked = pyconfig.initialize(
562+
[None, get_test_config_path()],
563+
run_name="moe_block_non_chunking_rr_test",
564+
enable_checkpointing=False,
565+
model_name="mixtral-8x7b",
566+
dtype="bfloat16",
567+
weight_dtype="bfloat16",
568+
megablox=False,
569+
sparse_matmul=True,
570+
use_tokamax_gmm=True,
571+
use_gmm_v2=True,
572+
num_moe_emb_chunks=0,
573+
use_ring_of_experts=True,
574+
use_random_routing=True,
575+
mlp_bias=True,
576+
per_device_batch_size=1,
577+
max_target_length=128,
578+
)
579+
580+
rng = jax.random.PRNGKey(1234)
581+
rng_model, rng_hidden_states = jax.random.split(rng)
582+
device_count = jax.device_count()
583+
hidden_states = jax.random.uniform(
584+
rng_hidden_states,
585+
(int(cfg_chunked.per_device_batch_size) * device_count, cfg_chunked.max_target_length, cfg_chunked.base_emb_dim),
586+
dtype=cfg_chunked.dtype,
587+
)
588+
589+
devices_array = maxtext_utils.create_device_mesh(cfg_chunked)
590+
mesh = Mesh(devices_array, cfg_chunked.mesh_axes)
591+
592+
moe_chunked = moe.RoutedMoE(
593+
config=cfg_chunked,
594+
num_experts=cfg_chunked.num_experts,
595+
num_experts_per_tok=cfg_chunked.num_experts_per_tok,
596+
mesh=mesh,
597+
kernel_init=nd_dense_init(1.0, "fan_in", "truncated_normal"),
598+
kernel_axes=("embed", "mlp"),
599+
dtype=cfg_chunked.dtype,
600+
rngs=nnx.Rngs(params=rng_model),
601+
)
602+
603+
moe_non_chunked = moe.RoutedMoE(
604+
config=cfg_non_chunked,
605+
num_experts=cfg_non_chunked.num_experts,
606+
num_experts_per_tok=cfg_non_chunked.num_experts_per_tok,
607+
mesh=mesh,
608+
kernel_init=nd_dense_init(1.0, "fan_in", "truncated_normal"),
609+
kernel_axes=("embed", "mlp"),
610+
dtype=cfg_non_chunked.dtype,
611+
rngs=nnx.Rngs(params=rng_model),
612+
)
613+
614+
moe_non_chunked.gate.kernel.value = moe_chunked.gate.kernel.value
615+
moe_non_chunked.wi_0.value = moe_chunked.wi_0.value
616+
moe_non_chunked.wi_1.value = moe_chunked.wi_1.value
617+
moe_non_chunked.wo.value = moe_chunked.wo.value
618+
619+
chunked_out, _, _ = moe_chunked(hidden_states)
620+
non_chunked_out, _, _ = moe_non_chunked(hidden_states)
621+
622+
self.assertTrue(jax.numpy.allclose(chunked_out, non_chunked_out, rtol=1e-01, atol=1e-01, equal_nan=False))
623+
540624
@pytest.mark.tpu_only
541625
def test_moe_emb_chunking_gmm_v2(self):
542626
cfg = pyconfig.initialize(

0 commit comments

Comments
 (0)