@@ -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