Skip to content

Commit c722758

Browse files
Use resolved mesh size for context parallel sharding
1 parent 85690da commit c722758

5 files changed

Lines changed: 14 additions & 20 deletions

File tree

src/maxtext/configs/pyconfig_deprecated.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,12 @@ def validate_keys(keys):
249249
keys["global_rampup_samples"],
250250
)
251251

252+
context_parallel_size = get_context_parallel_size(keys)
252253
# TODO remove after b/435512699 resolved
253-
if keys["context_parallel_size"] > 1 and keys["context_parallel_load_balance"] and keys["attention_type"] == "chunk":
254+
if context_parallel_size > 1 and keys["context_parallel_load_balance"] and keys["attention_type"] == "chunk":
254255
raise ValueError("Currently load-balanced context parallelism is not supported for chunk attention.")
255256

256-
validate_context_parallel_strategy_ring(
257-
keys["context_parallel_size"], keys["context_parallel_strategy"], keys["hardware"]
258-
)
257+
validate_context_parallel_strategy_ring(context_parallel_size, keys["context_parallel_strategy"], keys["hardware"])
259258

260259
if keys["mtp_eval_target_module"] < 0:
261260
raise ValueError("mtp_eval_target_module cannot be negative. Set to 0 to disable evaluation.")
@@ -817,7 +816,6 @@ def user_init(raw_keys):
817816

818817
raw_keys["num_slices"] = max_utils.get_num_slices(raw_keys)
819818
raw_keys["quantization_local_shard_count"] = get_quantization_local_shard_count(raw_keys)
820-
raw_keys["context_parallel_size"] = get_context_parallel_size(raw_keys)
821819
raw_keys = create_parallelisms_list(raw_keys)
822820
raw_keys = set_and_validate_pipeline_config(raw_keys)
823821

src/maxtext/configs/types.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,11 +2191,6 @@ class DerivedValues(BaseModel):
21912191
description="Boolean flag indicating if pipeline parallelism is active across ICI or DCN.",
21922192
)
21932193

2194-
context_parallel_size: None | int = Field(
2195-
None,
2196-
description="The total size of context parallelism, derived from ICI and DCN values.",
2197-
)
2198-
21992194
num_target_devices: None | int = Field(
22002195
None,
22012196
description="The number of devices computed from topology in train_compile or jax.devices() in train",
@@ -2789,9 +2784,6 @@ def calculate_global_batch_sizes(per_device_batch_size, expansion_factor, num_de
27892784
self.tensors_on_device = [t for t in tensors if getattr(self, t) == "device"]
27902785
self.tensors_to_offload = [t for t in tensors if getattr(self, t) == "offload"]
27912786

2792-
self.context_parallel_size = getattr(self, f"ici_{self.context_sharding}_parallelism", 1) * getattr(
2793-
self, f"dcn_{self.context_sharding}_parallelism", 1
2794-
)
27952787
if self.pipeline_parallel_layers == -1:
27962788
if self.decoder_block == DecoderBlockType.DEEPSEEK:
27972789
moe_layers = self.num_decoder_layers - self.first_num_dense_layers
@@ -3058,7 +3050,10 @@ def calculate_global_batch_sizes(per_device_batch_size, expansion_factor, num_de
30583050
and (self.per_device_batch_size * self.max_target_length) % self.num_vocab_tiling != 0
30593051
):
30603052
raise ValueError("Per device batch size times sequence length should be divisible by the number of vocab tiles.")
3061-
if self.context_parallel_size > 1 and self.context_parallel_strategy.lower() == "ring":
3053+
context_parallel_size = getattr(self, f"ici_{self.context_sharding}_parallelism", 1) * getattr(
3054+
self, f"dcn_{self.context_sharding}_parallelism", 1
3055+
)
3056+
if context_parallel_size > 1 and self.context_parallel_strategy.lower() == "ring":
30623057
if "gpu" not in self.hardware:
30633058
raise ValueError(
30643059
"Ring context parallelism strategy (context_parallel_strategy='ring') is only supported on GPUs."
@@ -3068,7 +3063,7 @@ def calculate_global_batch_sizes(per_device_batch_size, expansion_factor, num_de
30683063
# because test code paths may load the same config but use a different reorder path.
30693064
# Training's runtime path in max_utils.reorder_causal_load_balanced enforces this.
30703065
if (
3071-
self.context_parallel_size > 1
3066+
context_parallel_size > 1
30723067
and "gpu" not in self.hardware
30733068
and self.context_parallel_load_balance
30743069
and self.context_parallel_reorder_strategy == ReorderStrategy.STRIPED

src/maxtext/layers/attention_op.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ def tpu_flash_attention(
11681168
) -> tuple[Array, Array]:
11691169
"""TPU Flash Attention."""
11701170

1171-
cp_size = self.config.context_parallel_size
1171+
cp_size = self.mesh.shape.get(self.config.context_sharding, 1)
11721172
load_balanced_context_parallel = self.config.context_parallel_load_balance
11731173

11741174
# Transpose to ('batch', 'heads', 'length', 'kv')

src/maxtext/utils/train_utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ def setup_train_loop(config, recorder, devices=None):
214214
is_training = True
215215
init_rng = jax.random.PRNGKey(config.init_weights_seed)
216216
mesh = maxtext_utils.get_mesh_from_config(config, devices)
217+
context_parallel_size = mesh.shape.get(config.context_sharding, 1)
217218
if config.pure_nnx:
218219
# Create abstract NNX model.
219220
_create_model_partial, model = model_creation_utils.create_nnx_abstract_model(config, mesh, devices)
@@ -241,7 +242,7 @@ def create_train_state_fn():
241242
data_iterator, eval_data_iterator = create_data_iterator(config, mesh)
242243
rampup_manager = create_rampup_manager(config, checkpoint_manager)
243244
# Validate context parallelism with packing configuration
244-
if config.context_parallel_size > 1 and config.packing:
245+
if context_parallel_size > 1 and config.packing:
245246
if config.dataset_type == "synthetic":
246247
raise ValueError(
247248
"Context parallelism with sequence packing is not supported with synthetic data. "
@@ -255,7 +256,7 @@ def create_train_state_fn():
255256

256257
# Apply reordering wrapper to data iterators if context parallelism is enabled
257258
with jax.set_mesh(mesh):
258-
if config.context_parallel_size > 1 and config.context_parallel_load_balance:
259+
if context_parallel_size > 1 and config.context_parallel_load_balance:
259260

260261
# Determine load balancing reorder strategy based on whether packing is enabled
261262
if config.context_parallel_reorder_strategy == ReorderStrategy.AUTO:
@@ -264,7 +265,7 @@ def create_train_state_fn():
264265
reorder_strategy = config.context_parallel_reorder_strategy
265266

266267
reorder_fn = maxtext_utils.get_reorder_callable(
267-
config.context_parallel_size, config.shard_mode, reorder_strategy, config.hardware
268+
context_parallel_size, config.shard_mode, reorder_strategy, config.hardware
268269
)
269270
data_iterator = map(reorder_fn, data_iterator)
270271
if eval_data_iterator:

tests/utils/attention_test_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def forward_with_context_expert_parallelism(
187187
"""Get logits from attention under context/expert parallelism."""
188188
# If load balanced cp, shuffle along seq dim for input
189189
# This corresponds to the pre-shuffle step in training
190-
context_parallel_size = cfg_cp.context_parallel_size
190+
context_parallel_size = mesh_cp.shape.get(cfg_cp.context_sharding, 1)
191191
# This helper is TPU-oriented and uses the TPU-compatible DUAL_CHUNK_SWAP reorder path.
192192
# It does not model GPU-specific packed/striped reorder behavior.
193193
if context_parallel_size > 1 and cfg_cp.context_parallel_load_balance:

0 commit comments

Comments
 (0)