Skip to content

Commit 44627cc

Browse files
Merge pull request #4568 from AI-Hypercomputer:chengnuojin-train-eval-mesh
PiperOrigin-RevId: 953055860
2 parents 1a7305f + 86cd649 commit 44627cc

9 files changed

Lines changed: 58 additions & 63 deletions

File tree

src/maxtext/layers/attention_op.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
from maxtext.layers.initializers import variable_to_logically_partitioned
7676
from maxtext.layers.quantizations import AqtQuantization as Quant
7777
from maxtext.utils import max_utils
78-
from maxtext.utils.sharding import logical_to_mesh_axes, maybe_shard_with_pspec, get_logical_axis_rules_from_config
78+
from maxtext.utils.sharding import logical_to_mesh_axes, maybe_shard_with_pspec, get_logical_axis_rules
7979
import numpy as np
8080
# pylint: disable=line-too-long, g-doc-args, g-doc-return-or-yield, bad-continuation, g-inconsistent-quotes
8181
# pytype: disable=attribute-error
@@ -633,7 +633,7 @@ def maybe_create_nnx(einsum, *args):
633633
self.AqtEinsum_3 = jnp.einsum
634634

635635
def _logical_to_mesh_axes(self, logical_name):
636-
logical_rules = get_logical_axis_rules_from_config(self.config)
636+
logical_rules = get_logical_axis_rules()
637637
return logical_to_mesh_axes(logical_name, mesh=self.mesh, rules=logical_rules)
638638

639639
def check_attention_inputs(self, query: Array, key: Array | KVTensor, value: Array | KVTensor) -> None:

src/maxtext/layers/moe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from maxtext.utils import max_utils
4444
from maxtext.utils import maxtext_utils
4545
from maxtext.utils.sharding import create_sharding, maybe_shard_with_logical, maybe_shard_with_pspec
46-
from maxtext.utils.sharding import logical_to_mesh_axes, remove_expert_from_partition_spec, get_logical_axis_rules_from_config
46+
from maxtext.utils.sharding import logical_to_mesh_axes, remove_expert_from_partition_spec, get_logical_axis_rules
4747
import numpy as np
4848
import qwix
4949
from qwix.contrib.sparsity import sparsity_module
@@ -644,7 +644,7 @@ def _maybe_shard_with_logical(self, inputs, logical_name):
644644
)
645645

646646
def _logical_to_mesh_axes(self, logical_name):
647-
logical_rules = get_logical_axis_rules_from_config(self.config)
647+
logical_rules = get_logical_axis_rules()
648648
return logical_to_mesh_axes(logical_name, mesh=self.mesh, rules=logical_rules)
649649

650650
def _maybe_shard_with_pspec(self, inputs, pspec: jax.sharding.PartitionSpec | None):

src/maxtext/layers/multi_token_prediction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def __call__(
177177
("activation_batch", "activation_length", "activation_embed"),
178178
self.mesh,
179179
self.config.shard_mode,
180-
self.config.logical_axis_rules,
180+
sharding.get_logical_axis_rules(),
181181
)
182182

183183
embedding_norm = self.embedding_norm(target_token_embedding)
@@ -200,7 +200,7 @@ def extract_fn(x):
200200
x.sharding_names,
201201
self.mesh,
202202
shard_mode=self.config.shard_mode,
203-
rules=self.config.logical_axis_rules,
203+
rules=sharding.get_logical_axis_rules(),
204204
)
205205
return x
206206

src/maxtext/layers/pipeline.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
create_sharding,
3939
logical_to_mesh_axes,
4040
logical_to_mesh,
41+
get_logical_axis_rules,
4142
)
4243
from maxtext.utils import pipeline_utils
4344

@@ -61,21 +62,21 @@ def _setup_pipeline_attributes(self):
6162
self.spmd_axis_name = "stage" if self.config.shard_mode == ShardMode.AUTO else None
6263

6364
self.stages_in_logical = ("activation_stage", self.batch_axis_name, self.seq_len_axis_name, "activation_embed")
64-
self.stages_in_spec = logical_to_mesh_axes(self.stages_in_logical, self.mesh, rules=self.config.logical_axis_rules)
65+
self.stages_in_spec = logical_to_mesh_axes(self.stages_in_logical, self.mesh, rules=get_logical_axis_rules())
6566
self.stages_in_sharding = (
6667
NamedSharding(self.mesh, self.stages_in_spec) if self.config.shard_mode == ShardMode.EXPLICIT else None
6768
)
6869

6970
self.state_io_logical = ("activation_stage", None, self.batch_axis_name, self.seq_len_axis_name, "activation_embed")
70-
self.state_io_spec = logical_to_mesh_axes(self.state_io_logical, self.mesh, rules=self.config.logical_axis_rules)
71+
self.state_io_spec = logical_to_mesh_axes(self.state_io_logical, self.mesh, rules=get_logical_axis_rules())
7172
self.state_io_sharding = (
7273
NamedSharding(self.mesh, self.state_io_spec) if self.config.shard_mode == ShardMode.EXPLICIT else None
7374
)
7475
self.input_sharding = (
7576
create_sharding(
7677
self.mesh,
7778
(None, self.batch_axis_name, self.seq_len_axis_name, "activation_embed"),
78-
rules=self.config.logical_axis_rules,
79+
rules=get_logical_axis_rules(),
7980
)
8081
if self.config.shard_mode == ShardMode.EXPLICIT
8182
else None
@@ -84,7 +85,7 @@ def _setup_pipeline_attributes(self):
8485
create_sharding(
8586
self.mesh,
8687
(self.batch_axis_name, self.seq_len_axis_name, "activation_embed"),
87-
rules=self.config.logical_axis_rules,
88+
rules=get_logical_axis_rules(),
8889
)
8990
if self.config.shard_mode == ShardMode.EXPLICIT
9091
else None
@@ -114,7 +115,7 @@ def _maybe_shard_with_logical(self, inputs, logical_axes):
114115
logical_axes,
115116
shard_mode=self.config.shard_mode,
116117
mesh=self.mesh,
117-
rules=self.config.logical_axis_rules,
118+
rules=get_logical_axis_rules(),
118119
debug_sharding=self.config.debug_sharding,
119120
extra_stack_level=1,
120121
)

src/maxtext/models/deepseek.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from maxtext.utils import max_utils
4343
from maxtext.utils.sharding import create_sharding
4444
from maxtext.utils.sharding import maybe_shard_with_logical
45-
from maxtext.utils.sharding import get_logical_axis_rules_from_config
45+
from maxtext.utils.sharding import get_logical_axis_rules
4646

4747
import transformers
4848

@@ -79,9 +79,9 @@ def __init__(
7979
batch_size, sequence_length = max_utils.get_batch_seq_len_for_mode(self.config, self.model_mode)
8080
self.dummy_inputs_shape = (batch_size, sequence_length, self.config.emb_dim)
8181

82-
self.out_sharding = create_sharding(self.mesh, self.logical_axis_names, rules=self.config.logical_axis_rules)
82+
self.out_sharding = create_sharding(self.mesh, self.logical_axis_names, rules=get_logical_axis_rules())
8383
self.mlp_intermediate_sharding = create_sharding(
84-
self.mesh, self.mlp_logical_axis_names, rules=self.config.logical_axis_rules
84+
self.mesh, self.mlp_logical_axis_names, rules=get_logical_axis_rules()
8585
)
8686

8787
self.pre_self_attention_layer_norm = RMSNorm(
@@ -190,7 +190,7 @@ def with_logical_constraint(self, x):
190190
shard_mode=self.config.shard_mode,
191191
debug_sharding=self.config.debug_sharding,
192192
extra_stack_level=1,
193-
rules=get_logical_axis_rules_from_config(self.config),
193+
rules=get_logical_axis_rules(),
194194
)
195195

196196
def dropout_op(self, x, deterministic):
@@ -527,7 +527,7 @@ def extract_fn(x):
527527
x.sharding_names,
528528
self.mesh,
529529
shard_mode=self.config.shard_mode,
530-
rules=self.config.logical_axis_rules,
530+
rules=get_logical_axis_rules(),
531531
)
532532
return x
533533

src/maxtext/utils/sharding.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,6 @@ def get_logical_axis_rules():
5656
return flax_get_logical_axis_rules()
5757

5858

59-
def get_logical_axis_rules_from_config(config):
60-
"""Get the logical axis rules from the config.
61-
When we use pipeline parallelism or in eval step, we may use
62-
a different logical axis rule than the one in config.
63-
Plan to deprecate (b/536927795) and use get_logical_axis_rules instead.
64-
"""
65-
if config.using_pipeline_parallelism or config.eval_interval != -1:
66-
return None
67-
return config.logical_axis_rules
68-
69-
7059
def _get_sharding_desc(inputs, extra_stack_level):
7160
"""Get the inputs sharding description using inspect module"""
7261
frame = inspect.currentframe()

src/maxtext/utils/vocabulary_tiling.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
maybe_shard_with_name,
2626
all_gather_over_fsdp,
2727
create_sharding,
28+
get_logical_axis_rules,
2829
)
2930
from maxtext.common.common_types import ShardMode
3031
from maxtext.utils import max_utils
@@ -128,7 +129,7 @@ def _reshape(inputs, out_shape, out_sharding):
128129
labels = _maybe_shard_with_name(labels, label_spec)
129130
segmentation = _maybe_shard_with_name(segmentation, label_spec)
130131
# TODO (chengnuojin) all gather only embedding table instead of all params after NNX module is enabled
131-
gathered_params = all_gather_over_fsdp(params, param_spec, model.mesh, config.logical_axis_rules, config.shard_mode)
132+
gathered_params = all_gather_over_fsdp(params, param_spec, model.mesh, get_logical_axis_rules(), config.shard_mode)
132133

133134
# Customized forward and backward maps for the embedding tiling
134135
@jax.custom_vjp
@@ -354,7 +355,7 @@ def _reshape(inputs, out_shape, out_sharding):
354355
head_params,
355356
nnx.get_partition_spec(head_params),
356357
model.mesh,
357-
config.logical_axis_rules,
358+
get_logical_axis_rules(),
358359
config.shard_mode,
359360
)
360361

tests/unit/attention_test.py

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,22 +1190,23 @@ def test_tpu_flash_attention_ring_context_parallel(self, context_parallel_load_b
11901190
)
11911191
generic_state = nnx.state(attention_as_mha_generic)
11921192

1193-
attention_as_mha_flash_cp = Attention(
1194-
config=cfg_cp,
1195-
num_query_heads=cfg_cp.num_query_heads,
1196-
num_kv_heads=cfg_cp.num_kv_heads,
1197-
head_dim=cfg_cp.head_dim,
1198-
max_target_length=cfg_cp.max_target_length,
1199-
max_prefill_predict_length=cfg_cp.max_prefill_predict_length,
1200-
inputs_q_shape=lnx.shape,
1201-
inputs_kv_shape=lnx.shape,
1202-
mesh=mesh_cp,
1203-
attention_kernel="flash",
1204-
dtype=cfg_cp.dtype,
1205-
dropout_rate=cfg_cp.dropout_rate,
1206-
model_mode=MODEL_MODE_PREFILL,
1207-
rngs=self.nnx_rng,
1208-
)
1193+
with nn_partitioning.axis_rules(cfg_cp.logical_axis_rules):
1194+
attention_as_mha_flash_cp = Attention(
1195+
config=cfg_cp,
1196+
num_query_heads=cfg_cp.num_query_heads,
1197+
num_kv_heads=cfg_cp.num_kv_heads,
1198+
head_dim=cfg_cp.head_dim,
1199+
max_target_length=cfg_cp.max_target_length,
1200+
max_prefill_predict_length=cfg_cp.max_prefill_predict_length,
1201+
inputs_q_shape=lnx.shape,
1202+
inputs_kv_shape=lnx.shape,
1203+
mesh=mesh_cp,
1204+
attention_kernel="flash",
1205+
dtype=cfg_cp.dtype,
1206+
dropout_rate=cfg_cp.dropout_rate,
1207+
model_mode=MODEL_MODE_PREFILL,
1208+
rngs=self.nnx_rng,
1209+
)
12091210
nnx.update(attention_as_mha_flash_cp, generic_state)
12101211

12111212
mha_generic_flash_cp_output = attention_test_util.forward_with_context_expert_parallelism(
@@ -1266,22 +1267,23 @@ def test_tpu_flash_attention_ring_context_parallel_grad(self, context_parallel_l
12661267
)
12671268
generic_state = nnx.state(attention_as_mha_generic)
12681269

1269-
attention_as_mha_flash_cp = Attention(
1270-
config=cfg_cp,
1271-
num_query_heads=cfg_cp.num_query_heads,
1272-
num_kv_heads=cfg_cp.num_kv_heads,
1273-
head_dim=cfg_cp.head_dim,
1274-
max_target_length=cfg_cp.max_target_length,
1275-
max_prefill_predict_length=cfg_cp.max_prefill_predict_length,
1276-
inputs_q_shape=lnx.shape,
1277-
inputs_kv_shape=lnx.shape,
1278-
mesh=mesh_cp,
1279-
attention_kernel="flash",
1280-
dtype=cfg_cp.dtype,
1281-
dropout_rate=cfg_cp.dropout_rate,
1282-
model_mode=MODEL_MODE_PREFILL,
1283-
rngs=self.nnx_rng,
1284-
)
1270+
with nn_partitioning.axis_rules(cfg_cp.logical_axis_rules):
1271+
attention_as_mha_flash_cp = Attention(
1272+
config=cfg_cp,
1273+
num_query_heads=cfg_cp.num_query_heads,
1274+
num_kv_heads=cfg_cp.num_kv_heads,
1275+
head_dim=cfg_cp.head_dim,
1276+
max_target_length=cfg_cp.max_target_length,
1277+
max_prefill_predict_length=cfg_cp.max_prefill_predict_length,
1278+
inputs_q_shape=lnx.shape,
1279+
inputs_kv_shape=lnx.shape,
1280+
mesh=mesh_cp,
1281+
attention_kernel="flash",
1282+
dtype=cfg_cp.dtype,
1283+
dropout_rate=cfg_cp.dropout_rate,
1284+
model_mode=MODEL_MODE_PREFILL,
1285+
rngs=self.nnx_rng,
1286+
)
12851287
nnx.update(attention_as_mha_flash_cp, generic_state)
12861288

12871289
def generic_loss(lnx):

tests/unit/moe_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,8 @@ def test_fused_vs_sparse_softmax(self):
15061506
copy_weights(self.dense_model, sparse_model)
15071507

15081508
inputs = self._inputs()
1509-
sparse_out, _, _ = sparse_model(inputs)
1509+
with nn_partitioning.axis_rules(sparse_cfg.logical_axis_rules):
1510+
sparse_out, _, _ = sparse_model(inputs)
15101511
fused_out, lb_loss, bias_updates = self.fused_model(inputs)
15111512

15121513
np.testing.assert_allclose(
@@ -1647,7 +1648,8 @@ def test_prefused_vs_sparse_softmax(self):
16471648
copy_weights_prefused(self.dense_model, prefused_model)
16481649

16491650
inputs = self._inputs()
1650-
sparse_out, _, _ = sparse_model(inputs)
1651+
with nn_partitioning.axis_rules(sparse_cfg.logical_axis_rules):
1652+
sparse_out, _, _ = sparse_model(inputs)
16511653
prefused_out, lb_loss, bias_updates = prefused_model(inputs)
16521654

16531655
np.testing.assert_allclose(

0 commit comments

Comments
 (0)