Skip to content

Commit 39fb10a

Browse files
committed
fixes to smoke test batch sizing
1 parent 2f012e6 commit 39fb10a

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/maxdiffusion/generate_flux2klein.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,20 @@ def main(argv):
637637
devices_array = create_device_mesh(config)
638638
mesh = Mesh(devices_array, config.mesh_axes)
639639

640+
# Auto-adjust batch sharding axes if batch_size is not divisible by (data * fsdp)
641+
data_size = mesh.shape.get('data', 1)
642+
fsdp_size = mesh.shape.get('fsdp', 1)
643+
if config.batch_size % (data_size * fsdp_size) != 0:
644+
print(f"⚠️ Warning: batch_size ({config.batch_size}) is not divisible by FSDP*Data mesh size ({fsdp_size * data_size}).")
645+
print(" Automatically falling back to sharding batch dimension across 'data' axis only to prevent JAX SPMD errors.")
646+
new_rules = []
647+
for rule in config.logical_axis_rules:
648+
if rule[0] in ('activation_batch', 'conv_batch'):
649+
new_rules.append([rule[0], 'data'])
650+
else:
651+
new_rules.append(rule)
652+
pyconfig._config.keys['logical_axis_rules'] = tuple(new_rules)
653+
640654
# 3. Load or generate initial latents
641655
# Unpacked shape: (batch_size, 32, height//8, width//8)
642656
latents_unpacked = load_or_generate_latents(config)

src/maxdiffusion/generate_flux2klein_9B.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,20 @@ def main(argv):
654654
devices_array = create_device_mesh(config)
655655
mesh = Mesh(devices_array, config.mesh_axes)
656656

657+
# Auto-adjust batch sharding axes if batch_size is not divisible by (data * fsdp)
658+
data_size = mesh.shape.get('data', 1)
659+
fsdp_size = mesh.shape.get('fsdp', 1)
660+
if config.batch_size % (data_size * fsdp_size) != 0:
661+
print(f"⚠️ Warning: batch_size ({config.batch_size}) is not divisible by FSDP*Data mesh size ({fsdp_size * data_size}).")
662+
print(" Automatically falling back to sharding batch dimension across 'data' axis only to prevent JAX SPMD errors.")
663+
new_rules = []
664+
for rule in config.logical_axis_rules:
665+
if rule[0] in ('activation_batch', 'conv_batch'):
666+
new_rules.append([rule[0], 'data'])
667+
else:
668+
new_rules.append(rule)
669+
pyconfig._config.keys['logical_axis_rules'] = tuple(new_rules)
670+
657671
# 3. Load or generate initial latents
658672
# Unpacked shape: (batch_size, 32, height//8, width//8)
659673
latents_unpacked = load_or_generate_latents(config)

0 commit comments

Comments
 (0)