Skip to content

Commit 48c763b

Browse files
committed
Fix double-compilation in train_step by propagating input sharding to AOT compilation shaped batch.
Previously, train_step compiled twice in various training loops (pre-train, SFT): first Ahead-Of-Time (AOT) using an unsharded shaped_batch dummy, and then again on the first step execution using a sharded example_batch from the data pipeline. This caused a slow first training step. This change propagates the input data sharding to get_shaped_batch in train.py, train_compile.py, and train_sft_native.py so the dummy AOT batch has the same sharding annotation as the runtime batch, matching their signatures and enabling JAX JIT compilation cache hits. Also added: - Unit tests for get_shaped_batch sharding in maxtext_utils_test.py. - CPU compilation cache regression test in compile_cache_test.py.
1 parent 61c225f commit 48c763b

6 files changed

Lines changed: 186 additions & 97 deletions

File tree

src/maxtext/trainers/post_train/sft/train_sft_native.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ def train_loop(config, recorder, state=None):
8080
)
8181

8282
with jax.set_mesh(mesh), nn_partitioning.axis_rules(config.logical_axis_rules):
83-
shaped_batch = maxtext_utils.get_shaped_batch(config)
83+
data_sharding = sharding.get_input_data_sharding(config, mesh)
84+
shaped_batch = maxtext_utils.get_shaped_batch(config, batch_sharding=data_sharding)
8485
compiled = p_train_step.lower(state, shaped_batch, init_rng).compile()
8586
compiled_stats = compiled.memory_analysis()
8687
max_utils.print_compiled_memory_stats(compiled_stats)

src/maxtext/trainers/pre_train/train.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,8 @@ def train_loop(config, recorder, state=None):
627627
)
628628

629629
with jax.set_mesh(mesh), mesh, nn_partitioning.axis_rules(config.logical_axis_rules):
630-
shaped_batch = maxtext_utils.get_shaped_batch(config)
630+
data_sharding = sharding.get_input_data_sharding(config, mesh)
631+
shaped_batch = maxtext_utils.get_shaped_batch(config, batch_sharding=data_sharding)
631632
if config.shard_optimizer_over_data and isinstance(model, nn.Module):
632633
state = sharding.maybe_shard_with_name(state, state_mesh_shardings, config.shard_mode)
633634
elif config.shard_optimizer_over_data:

src/maxtext/trainers/pre_train/train_compile.py

Lines changed: 29 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@
6161
def validate_config(config):
6262
"""Validates the config is is setup correctly to compile, returning a useful error message if not."""
6363
assert config.compile_topology != "", (
64-
"You must pass your desired target hardware in compile_topology, e.g."
65-
" compile_topology=v5e-256"
64+
"You must pass your desired target hardware in compile_topology, e.g." " compile_topology=v5e-256"
6665
)
67-
assert (
68-
config.compile_topology_num_slices > 0
69-
), "You must set compile_topology_num_slices to a positive integer"
66+
assert config.compile_topology_num_slices > 0, "You must set compile_topology_num_slices to a positive integer"
7067

7168

7269
def get_topology_mesh(config):
@@ -78,18 +75,12 @@ def get_topology_mesh(config):
7875
num_slices=config.compile_topology_num_slices,
7976
).devices
8077
else:
81-
target_hardware = accelerator_to_spec_map.get_system_characteristics(
82-
config.compile_topology
83-
)
78+
target_hardware = accelerator_to_spec_map.get_system_characteristics(config.compile_topology)
8479
if target_hardware.platform == "gpu":
8580
# Disable sharded autotuning. This is an optimization to distribute
8681
# autotuning across the fleet, but can cause hangs with AoT compilation.
87-
os.environ["XLA_FLAGS"] = (
88-
os.environ.get("XLA_FLAGS", "") + " --xla_gpu_shard_autotuning=false"
89-
)
90-
jax.config.update(
91-
"mock_num_gpu_processes", config.compile_topology_num_slices
92-
)
82+
os.environ["XLA_FLAGS"] = os.environ.get("XLA_FLAGS", "") + " --xla_gpu_shard_autotuning=false"
83+
jax.config.update("mock_num_gpu_processes", config.compile_topology_num_slices)
9384
topology_devices = jax.devices()
9485
else:
9586
topology_devices = get_topology_desc(
@@ -104,14 +95,8 @@ def get_topology_mesh(config):
10495
"jax_remove_size_one_mesh_axis_from_type",
10596
config.remove_size_one_mesh_axis_from_type,
10697
)
107-
topology_device_mesh = maxtext_utils.create_device_mesh(
108-
config, topology_devices
109-
)
110-
mesh_axis_type = (
111-
AxisType.Explicit
112-
if config.shard_mode == ShardMode.EXPLICIT
113-
else AxisType.Auto
114-
)
98+
topology_device_mesh = maxtext_utils.create_device_mesh(config, topology_devices)
99+
mesh_axis_type = AxisType.Explicit if config.shard_mode == ShardMode.EXPLICIT else AxisType.Auto
115100
topology_mesh = Mesh(
116101
topology_device_mesh,
117102
config.mesh_axes,
@@ -129,9 +114,7 @@ def _collect_nnx_activation_shardings(create_model_fn, config, mesh):
129114
input_shape = (config.micro_batch_size_to_train_on, config.max_target_length)
130115
abstract_input = jax.ShapeDtypeStruct(input_shape, jnp.int32)
131116

132-
def _nnx_forward(
133-
decoder_input_tokens, decoder_positions, decoder_segment_ids
134-
):
117+
def _nnx_forward(decoder_input_tokens, decoder_positions, decoder_segment_ids):
135118
model_instance = create_model_fn()
136119
return model_instance(
137120
decoder_input_tokens=decoder_input_tokens,
@@ -140,9 +123,7 @@ def _nnx_forward(
140123
enable_dropout=False,
141124
)
142125

143-
with jax.set_mesh(mesh), nn_partitioning.axis_rules(
144-
config.logical_axis_rules
145-
):
126+
with jax.set_mesh(mesh), nn_partitioning.axis_rules(config.logical_axis_rules):
146127
jax.eval_shape(_nnx_forward, abstract_input, abstract_input, abstract_input)
147128

148129

@@ -151,13 +132,9 @@ def get_shaped_inputs(topology_mesh, config):
151132
# Construct the model and optimizer to get shaped versions of the state
152133
quant = quantizations.configure_quantization(config)
153134
if config.pure_nnx:
154-
_create_model_partial, model = (
155-
model_creation_utils.create_nnx_abstract_model(config, topology_mesh)
156-
)
135+
_create_model_partial, model = model_creation_utils.create_nnx_abstract_model(config, topology_mesh)
157136
else:
158-
model = Transformer(
159-
config, topology_mesh, quant=quant, model_mode=MODEL_MODE_TRAIN
160-
)
137+
model = Transformer(config, topology_mesh, quant=quant, model_mode=MODEL_MODE_TRAIN)
161138
# The learning_rate_schedule is baked into the compiled object.
162139
learning_rate_schedule = maxtext_utils.create_learning_rate_schedule(config)
163140
# pass in model for muon
@@ -176,20 +153,14 @@ def create_train_state_fn():
176153

177154
init_state_fn = create_train_state_fn
178155
else:
179-
init_state_fn = functools.partial(
180-
maxtext_utils.init_initial_state, model, tx, config, True, example_rng
181-
)
156+
init_state_fn = functools.partial(maxtext_utils.init_initial_state, model, tx, config, True, example_rng)
182157

183158
# Shaped state
184-
abstract_state, _, state_mesh_shardings = maxtext_utils.get_abstract_state(
185-
config, topology_mesh, init_state_fn, True
186-
)
159+
abstract_state, _, state_mesh_shardings = maxtext_utils.get_abstract_state(config, topology_mesh, init_state_fn, True)
187160

188161
if config.pure_nnx:
189162
# NNX doesn't use Linen logical annotations; derive PartitionSpecs from the physical shardings.
190-
logical_annotations = maxtext_utils_nnx.get_partition_spec_nnx(
191-
state_mesh_shardings
192-
)
163+
logical_annotations = maxtext_utils_nnx.get_partition_spec_nnx(state_mesh_shardings)
193164
# For NNX, get_functional_train_with_signature expects the graphdef (static structure),
194165
# not the raw model — mirroring how the training loop does nnx.split(train_state).
195166
with nn_partitioning.axis_rules(config.logical_axis_rules):
@@ -198,12 +169,11 @@ def create_train_state_fn():
198169
model = graphdef
199170
else:
200171
# unsharded logical annotations
201-
logical_annotations = maxtext_utils.get_logical_annotations(
202-
config, topology_mesh, init_state_fn
203-
)
172+
logical_annotations = maxtext_utils.get_logical_annotations(config, topology_mesh, init_state_fn)
204173

205174
# Shaped batch
206-
shaped_batch = maxtext_utils.get_shaped_batch(config)
175+
data_sharding = sharding.get_input_data_sharding(config, topology_mesh)
176+
shaped_batch = maxtext_utils.get_shaped_batch(config, batch_sharding=data_sharding)
207177

208178
if config.pure_nnx:
209179
shaped_train_args = (
@@ -217,9 +187,7 @@ def create_train_state_fn():
217187
# Collect NNX activation shardings via an abstract forward pass (must run
218188
# after get_abstract_state, which only traces __init__).
219189
if config.debug_sharding and config.pure_nnx:
220-
_collect_nnx_activation_shardings(
221-
_create_model_partial, config, topology_mesh
222-
)
190+
_collect_nnx_activation_shardings(_create_model_partial, config, topology_mesh)
223191

224192
return (
225193
shaped_train_args,
@@ -256,9 +224,7 @@ def jit_and_compile(
256224
maxtext_utils.maybe_dump_jaxpr(config, jitted, func_input_args)
257225
lowered = jitted.lower(*func_input_args, **func_input_kwargs)
258226
# Import libtpu flags as compiler options. Defaults to empty dict if string is empty.
259-
compiler_options = max_utils.parse_libtpu_flags_to_dict(
260-
config.compile_xla_flags
261-
)
227+
compiler_options = max_utils.parse_libtpu_flags_to_dict(config.compile_xla_flags)
262228
compiled = lowered.compile(compiler_options=compiler_options)
263229
return compiled
264230

@@ -293,18 +259,12 @@ def is_oom(argv: Sequence[str]) -> bool:
293259
) = get_shaped_inputs(topology_mesh, config)
294260

295261
# Update params_shardings when shard_optimizer_over_data is enabled (Zero-1)
296-
params_shardings, state_mesh_shardings = (
297-
sharding.maybe_update_params_sharding_with_opt(
298-
config, state_mesh_shardings
299-
)
300-
)
262+
params_shardings, state_mesh_shardings = sharding.maybe_update_params_sharding_with_opt(config, state_mesh_shardings)
301263

302264
# When ZeRO-1 is enabled, we need to use the original params_shardings for input shardings
303265
# but keep the updated state_mesh_shardings for the optimizer state
304266
if config.shard_optimizer_over_data:
305-
input_state_mesh_shardings = state_mesh_shardings.replace(
306-
params=params_shardings
307-
)
267+
input_state_mesh_shardings = state_mesh_shardings.replace(params=params_shardings)
308268
else:
309269
input_state_mesh_shardings = state_mesh_shardings
310270

@@ -355,8 +315,7 @@ def is_oom(argv: Sequence[str]) -> bool:
355315
def main(argv: Sequence[str]) -> None:
356316
jax.config.update("jax_default_prng_impl", "unsafe_rbg")
357317
os.environ["LIBTPU_INIT_ARGS"] = (
358-
os.environ.get("LIBTPU_INIT_ARGS", "")
359-
+ " --xla_tpu_spmd_rng_bit_generator_unsafe=true"
318+
os.environ.get("LIBTPU_INIT_ARGS", "") + " --xla_tpu_spmd_rng_bit_generator_unsafe=true"
360319
)
361320
print("Starting train_compile.py...", flush=True)
362321

@@ -381,18 +340,12 @@ def main(argv: Sequence[str]) -> None:
381340
) = get_shaped_inputs(topology_mesh, config)
382341

383342
# Update params_shardings when shard_optimizer_over_data is enabled (Zero-1)
384-
params_shardings, state_mesh_shardings = (
385-
sharding.maybe_update_params_sharding_with_opt(
386-
config, state_mesh_shardings
387-
)
388-
)
343+
params_shardings, state_mesh_shardings = sharding.maybe_update_params_sharding_with_opt(config, state_mesh_shardings)
389344

390345
# When ZeRO-1 is enabled, we need to use the original params_shardings for input shardings
391346
# but keep the updated state_mesh_shardings for the optimizer state
392347
if config.shard_optimizer_over_data:
393-
input_state_mesh_shardings = state_mesh_shardings.replace(
394-
params=params_shardings
395-
)
348+
input_state_mesh_shardings = state_mesh_shardings.replace(params=params_shardings)
396349
else:
397350
input_state_mesh_shardings = state_mesh_shardings
398351

@@ -401,21 +354,15 @@ def main(argv: Sequence[str]) -> None:
401354
if config.enable_diloco:
402355
# Build abstract DiLoCo state and shardings for AOT compilation
403356
abstract_state = shaped_train_args[0]
404-
diloco_state, state_mesh_shardings, inner_state_shardings = (
405-
diloco.build_abstract_diloco_state(
406-
config, abstract_state, state_mesh_shardings, topology_mesh
407-
)
357+
diloco_state, state_mesh_shardings, inner_state_shardings = diloco.build_abstract_diloco_state(
358+
config, abstract_state, state_mesh_shardings, topology_mesh
408359
)
409360
# For NNX, shaped_train_args has 2 elements (state, batch) — no rng; pass None for prng.
410-
shaped_rng_arg = (
411-
shaped_train_args[2] if len(shaped_train_args) > 2 else None
412-
)
361+
shaped_rng_arg = shaped_train_args[2] if len(shaped_train_args) > 2 else None
413362
shaped_train_args = (diloco_state, shaped_train_args[1], shaped_rng_arg)
414363

415364
# Wrap train_step with diloco
416-
train_step_partial = functools.partial(
417-
train.train_step, model, config, inner_state_shardings, params_shardings
418-
)
365+
train_step_partial = functools.partial(train.train_step, model, config, inner_state_shardings, params_shardings)
419366
train_step_fn = diloco.build_diloco_train_step(config, train_step_partial)
420367

421368
# For DiLoCo, the train_step_fn is already fully wrapped and takes (state, batch, prng)
@@ -480,10 +427,7 @@ def main(argv: Sequence[str]) -> None:
480427
if config.compiled_trainstep_file != "":
481428
print("Saving compiled object...")
482429
save_compiled(compiled, config.compiled_trainstep_file)
483-
print(
484-
"Successfully saved compiled object as"
485-
f" {config.compiled_trainstep_file}"
486-
)
430+
print("Successfully saved compiled object as" f" {config.compiled_trainstep_file}")
487431
print("Finished train_compile.py successfully!", flush=True)
488432
print(f"Cost analysis: {compiled.cost_analysis()}")
489433
print(f"Memory analysis: {compiled.memory_analysis()}")

src/maxtext/utils/maxtext_utils.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def get_reorder_callable(cp_size, shard_mode, reorder_strategy=ReorderStrategy.D
148148
)
149149

150150

151-
def get_shaped_batch(config):
151+
def get_shaped_batch(config, batch_sharding=None):
152152
"""Return the shape of the batch - this is what eval_shape would return for the
153153
output of create_data_iterator, but eval_shape doesn't work, see b/306901078."""
154154
if config.enable_diloco:
@@ -160,21 +160,21 @@ def get_shaped_batch(config):
160160
else:
161161
batch_shape = (config.global_batch_size_to_load, config.max_target_length)
162162
shaped_batch = {}
163-
shaped_batch["inputs"] = jax.ShapeDtypeStruct(batch_shape, jnp.int32)
164-
shaped_batch["inputs_position"] = jax.ShapeDtypeStruct(batch_shape, jnp.int32)
165-
shaped_batch["inputs_segmentation"] = jax.ShapeDtypeStruct(batch_shape, jnp.int32)
166-
shaped_batch["targets"] = jax.ShapeDtypeStruct(batch_shape, jnp.int32)
167-
shaped_batch["targets_position"] = jax.ShapeDtypeStruct(batch_shape, jnp.int32)
168-
shaped_batch["targets_segmentation"] = jax.ShapeDtypeStruct(batch_shape, jnp.int32)
163+
shaped_batch["inputs"] = jax.ShapeDtypeStruct(batch_shape, jnp.int32, sharding=batch_sharding)
164+
shaped_batch["inputs_position"] = jax.ShapeDtypeStruct(batch_shape, jnp.int32, sharding=batch_sharding)
165+
shaped_batch["inputs_segmentation"] = jax.ShapeDtypeStruct(batch_shape, jnp.int32, sharding=batch_sharding)
166+
shaped_batch["targets"] = jax.ShapeDtypeStruct(batch_shape, jnp.int32, sharding=batch_sharding)
167+
shaped_batch["targets_position"] = jax.ShapeDtypeStruct(batch_shape, jnp.int32, sharding=batch_sharding)
168+
shaped_batch["targets_segmentation"] = jax.ShapeDtypeStruct(batch_shape, jnp.int32, sharding=batch_sharding)
169169
if config.use_multimodal:
170170
image_shape = mm_processor.get_dummy_image_shape_for_init(
171171
config.model_name, batch_size=config.micro_batch_size_to_train_on
172172
)
173-
shaped_batch["images"] = jax.ShapeDtypeStruct(image_shape, jnp.int32)
174-
shaped_batch["image_masks"] = jax.ShapeDtypeStruct(image_shape[:2], jnp.int32)
173+
shaped_batch["images"] = jax.ShapeDtypeStruct(image_shape, jnp.int32, sharding=batch_sharding)
174+
shaped_batch["image_masks"] = jax.ShapeDtypeStruct(image_shape[:2], jnp.int32, sharding=batch_sharding)
175175
if config.use_audio:
176176
audio_shape = mm_processor.get_dummy_audio_shape_for_init(config)
177-
shaped_batch["audios"] = jax.ShapeDtypeStruct(audio_shape, jnp.float32)
177+
shaped_batch["audios"] = jax.ShapeDtypeStruct(audio_shape, jnp.float32, sharding=batch_sharding)
178178
return shaped_batch
179179

180180

0 commit comments

Comments
 (0)