Skip to content

Commit 5547345

Browse files
Merge pull request AI-Hypercomputer#4211 from huytransformer:htn/context-parallel-size-from-mesh
PiperOrigin-RevId: 938634915
2 parents c5bc2e4 + 84f4110 commit 5547345

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
@@ -2293,11 +2293,6 @@ class DerivedValues(BaseModel):
22932293
description="Boolean flag indicating if pipeline parallelism is active across ICI or DCN.",
22942294
)
22952295

2296-
context_parallel_size: None | int = Field(
2297-
None,
2298-
description="The total size of context parallelism, derived from ICI and DCN values.",
2299-
)
2300-
23012296
num_target_devices: None | int = Field(
23022297
None,
23032298
description="The number of devices computed from topology in train_compile or jax.devices() in train",
@@ -2891,9 +2886,6 @@ def calculate_global_batch_sizes(per_device_batch_size, expansion_factor, num_de
28912886
self.tensors_on_device = [t for t in tensors if getattr(self, t) == "device"]
28922887
self.tensors_to_offload = [t for t in tensors if getattr(self, t) == "offload"]
28932888

2894-
self.context_parallel_size = getattr(self, f"ici_{self.context_sharding}_parallelism", 1) * getattr(
2895-
self, f"dcn_{self.context_sharding}_parallelism", 1
2896-
)
28972889
if self.pipeline_parallel_layers == -1:
28982890
if self.decoder_block == DecoderBlockType.DEEPSEEK:
28992891
moe_layers = self.num_decoder_layers - self.first_num_dense_layers
@@ -3199,7 +3191,10 @@ def calculate_global_batch_sizes(per_device_batch_size, expansion_factor, num_de
31993191
and (self.per_device_batch_size * self.max_target_length) % self.num_vocab_tiling != 0
32003192
):
32013193
raise ValueError("Per device batch size times sequence length should be divisible by the number of vocab tiles.")
3202-
if self.context_parallel_size > 1 and self.context_parallel_strategy.lower() == "ring":
3194+
context_parallel_size = getattr(self, f"ici_{self.context_sharding}_parallelism", 1) * getattr(
3195+
self, f"dcn_{self.context_sharding}_parallelism", 1
3196+
)
3197+
if context_parallel_size > 1 and self.context_parallel_strategy.lower() == "ring":
32033198
if "gpu" not in self.hardware:
32043199
raise ValueError(
32053200
"Ring context parallelism strategy (context_parallel_strategy='ring') is only supported on GPUs."
@@ -3209,7 +3204,7 @@ def calculate_global_batch_sizes(per_device_batch_size, expansion_factor, num_de
32093204
# because test code paths may load the same config but use a different reorder path.
32103205
# Training's runtime path in max_utils.reorder_causal_load_balanced enforces this.
32113206
if (
3212-
self.context_parallel_size > 1
3207+
context_parallel_size > 1
32133208
and "gpu" not in self.hardware
32143209
and self.context_parallel_load_balance
32153210
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
@@ -1189,7 +1189,7 @@ def tpu_flash_attention(
11891189
) -> tuple[Array, Array]:
11901190
"""TPU Flash Attention."""
11911191

1192-
cp_size = self.config.context_parallel_size
1192+
cp_size = self.mesh.shape.get(self.config.context_sharding, 1)
11931193
load_balanced_context_parallel = self.config.context_parallel_load_balance
11941194

11951195
# 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
@@ -216,6 +216,7 @@ def setup_train_loop(config, recorder, devices=None):
216216
is_training = True
217217
init_rng = jax.random.PRNGKey(config.init_weights_seed)
218218
mesh = maxtext_utils.get_mesh_from_config(config, devices)
219+
context_parallel_size = mesh.shape.get(config.context_sharding, 1)
219220
if config.pure_nnx:
220221
# Create abstract NNX model.
221222
_create_model_partial, model = model_creation_utils.create_nnx_abstract_model(config, mesh, devices)
@@ -244,7 +245,7 @@ def create_train_state_fn():
244245
rampup_manager = create_rampup_manager(config, checkpoint_manager)
245246
# Validate context parallelism with packing configuration
246247
context_parallel_strategy = config.context_parallel_strategy.lower()
247-
if config.context_parallel_size > 1 and config.packing:
248+
if context_parallel_size > 1 and config.packing:
248249
if config.dataset_type == "synthetic":
249250
raise ValueError(
250251
"Context parallelism with sequence packing is not supported with synthetic data. "
@@ -263,7 +264,7 @@ def create_train_state_fn():
263264

264265
# Apply reordering wrapper to data iterators if context parallelism is enabled
265266
with jax.set_mesh(mesh):
266-
if config.context_parallel_size > 1 and config.context_parallel_load_balance:
267+
if context_parallel_size > 1 and config.context_parallel_load_balance:
267268

268269
# Determine load balancing reorder strategy based on whether packing is enabled
269270
if config.context_parallel_reorder_strategy == ReorderStrategy.AUTO:
@@ -276,7 +277,7 @@ def create_train_state_fn():
276277
reorder_strategy = config.context_parallel_reorder_strategy
277278

278279
reorder_fn = maxtext_utils.get_reorder_callable(
279-
config.context_parallel_size, config.shard_mode, reorder_strategy, config.hardware
280+
context_parallel_size, config.shard_mode, reorder_strategy, config.hardware
280281
)
281282
data_iterator = map(reorder_fn, data_iterator)
282283
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)