Skip to content

Commit 4e6ff90

Browse files
committed
Port PR 447 (Tile-size auto-tuning) to LTX2 and LTX2.3
1 parent 510b87c commit 4e6ff90

4 files changed

Lines changed: 354 additions & 2 deletions

File tree

src/maxdiffusion/configs/ltx2_3_video.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
hardware: 'tpu'
33
skip_jax_distributed_system: False
44
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_ring
5-
5+
use_base2_exp: True
6+
use_experimental_scheduler: True
67
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
78
ulysses_shards: -1
89
# Splits Ulysses all-to-all into head-group chunks. The last chunk carries any remainder.
@@ -111,6 +112,10 @@ enable_ondemand_xprof: True
111112
skip_first_n_steps_for_profiler: 0
112113
profiler_steps: 5
113114

115+
# Enable JAX named scopes for detailed profiling and debugging
116+
# When enabled, adds named scopes around key operations in transformer and attention layers
117+
enable_jax_named_scopes: False
118+
114119
replicate_vae: False
115120

116121
run_text_encoder_on_tpu: False
@@ -183,3 +188,11 @@ upsampler_output_type: "pil"
183188
aot_cache_dir: ''
184189
converted_weights_dir: ''
185190

191+
192+
# Tile-size auto-tuning. When enable_tile_search: True, generate_ltx2 runs a fast one-DiT-block
193+
# grid search (maxdiffusion/utils/tile_size_grid_search.py) before inference and overwrites
194+
# flash_block_sizes' block_q/block_kv/block_kv_compute with the winner. Default off (no-op).
195+
enable_tile_search: False
196+
tile_search_mode: 'smart' # 'smart' (VMEM-capped candidate ladders) | 'full' (2D sweep)
197+
tile_search_iters: 10
198+
tile_search_out: '' # dir for the results CSV; '' -> print only

src/maxdiffusion/configs/ltx2_video.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
hardware: 'tpu'
33
skip_jax_distributed_system: False
44
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_ring
5-
5+
use_base2_exp: True
6+
use_experimental_scheduler: True
67
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
78
ulysses_shards: -1
89
# Splits Ulysses all-to-all into head-group chunks. The last chunk carries any remainder.
@@ -116,6 +117,10 @@ enable_ondemand_xprof: True
116117
skip_first_n_steps_for_profiler: 0
117118
profiler_steps: 5
118119

120+
# Enable JAX named scopes for detailed profiling and debugging
121+
# When enabled, adds named scopes around key operations in transformer and attention layers
122+
enable_jax_named_scopes: False
123+
119124
replicate_vae: False
120125
use_bwe: False
121126

@@ -180,3 +185,11 @@ upsampler_output_type: "pil"
180185
aot_cache_dir: ''
181186
converted_weights_dir: ''
182187

188+
189+
# Tile-size auto-tuning. When enable_tile_search: True, generate_ltx2 runs a fast one-DiT-block
190+
# grid search (maxdiffusion/utils/tile_size_grid_search.py) before inference and overwrites
191+
# flash_block_sizes' block_q/block_kv/block_kv_compute with the winner. Default off (no-op).
192+
enable_tile_search: False
193+
tile_search_mode: 'smart' # 'smart' (VMEM-capped candidate ladders) | 'full' (2D sweep)
194+
tile_search_iters: 10
195+
tile_search_out: '' # dir for the results CSV; '' -> print only

src/maxdiffusion/generate_ltx2.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,55 @@ def call_pipeline(config, pipeline, prompt, negative_prompt):
113113
return out
114114

115115

116+
def maybe_tune_block_sizes(config):
117+
"""If enable_tile_search, run a fast one-DiT-block tile-size grid search and overwrite
118+
flash_block_sizes' block_q/block_kv/block_kv_compute with the winner IN PLACE.
119+
"""
120+
keys = config.get_keys()
121+
val = keys.get("enable_tile_search", False)
122+
if str(val).lower() not in ("true", "1", "yes"):
123+
return
124+
from maxdiffusion.utils.tile_size_grid_search import grid_search
125+
from maxdiffusion.utils.ltx2_block_benchmark import LTX2BlockBenchmark
126+
127+
vmem = config.flash_block_sizes.get("vmem_limit_bytes", None) if config.flash_block_sizes else None
128+
if vmem is None:
129+
import os, re
130+
m = re.search(r'--xla_tpu_scoped_vmem_limit_kib=(\d+)', os.environ.get("LIBTPU_INIT_ARGS", ""))
131+
vmem = int(m.group(1)) * 1024 if m else 32 * 1024 * 1024
132+
133+
mesh = jax.sharding.Mesh(max_utils.create_device_mesh(config), config.mesh_axes)
134+
bench = LTX2BlockBenchmark.from_config(config, mesh, vmem_limit_bytes=vmem)
135+
max_logging.log(f"[tile-search] tuning block sizes for {bench.label} (vmem={vmem/1024/1024:.1f}MB) before inference...")
136+
result = grid_search(
137+
bench,
138+
mode=keys.get("tile_search_mode", "smart"),
139+
iters=keys.get("tile_search_iters", 10),
140+
out_dir=(keys.get("tile_search_out", "") or None),
141+
log=max_logging.log,
142+
)
143+
if result.best is None:
144+
max_logging.log("[tile-search] no config succeeded; keeping configured flash_block_sizes")
145+
return
146+
fbs = dict(config.flash_block_sizes) if config.flash_block_sizes else {}
147+
fbs.update({
148+
"block_q": result.best.bq,
149+
"block_kv": result.best.bkv,
150+
"block_kv_compute": result.best.bkv_compute,
151+
"block_kv_compute_in": result.best.bkv_compute,
152+
"vmem_limit_bytes": vmem,
153+
})
154+
config.get_keys()["flash_block_sizes"] = fbs
155+
max_logging.log(
156+
f"[tile-search] using block_q={result.best.bq} block_kv={result.best.bkv} "
157+
f"(block-bench {result.best.mean_ms:.2f} ms)"
158+
)
159+
160+
116161
def run(config, pipeline=None, filename_prefix="", commit_hash=None):
162+
if pipeline is None:
163+
maybe_tune_block_sizes(config)
164+
117165
writer = max_utils.initialize_summary_writer(config)
118166
if jax.process_index() == 0 and writer:
119167
max_logging.log(f"TensorBoard logs will be written to: {config.tensorboard_dir}")

0 commit comments

Comments
 (0)