Skip to content

Commit 55fa53c

Browse files
committed
fix: strengthen LTX2 AOT cache key
1 parent 55ee9a8 commit 55fa53c

1 file changed

Lines changed: 70 additions & 11 deletions

File tree

src/maxdiffusion/generate_ltx2.py

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
from typing import Sequence
16+
import json
1617
import jax
1718
import jax.numpy as jnp
1819
import time
@@ -151,6 +152,73 @@ def maybe_tune_block_sizes(config):
151152
)
152153

153154

155+
def _canonical_aot_value(value):
156+
return json.dumps(value, sort_keys=True, separators=(",", ":"), default=str)
157+
158+
159+
def ltx2_aot_metadata(config, pipeline):
160+
"""Returns every graph- and topology-shaping input to LTX2 AOT caching.
161+
162+
We deliberately exclude values such as model weights and RNG state: they are
163+
executable inputs, not compilation inputs. `use_kv_cache` is also excluded
164+
because it is a static argument of `run_diffusion_loop` and therefore part
165+
of that executable's per-call signature.
166+
"""
167+
transformer_config = dict(getattr(pipeline.transformer, "config", {}))
168+
for key in (
169+
"rngs",
170+
"mesh",
171+
"dtype",
172+
"weights_dtype",
173+
"precision",
174+
"flash_block_sizes",
175+
"flash_min_seq_length",
176+
"scan_layers",
177+
"attention_kernel",
178+
"a2v_attention_kernel",
179+
"v2a_attention_kernel",
180+
"ulysses_shards",
181+
"ulysses_attention_chunks",
182+
"remat_policy",
183+
"names_which_can_be_saved",
184+
"names_which_can_be_offloaded",
185+
"sharding_specs",
186+
"enable_jax_named_scopes",
187+
):
188+
transformer_config.pop(key, None)
189+
190+
device = jax.devices()[0]
191+
return {
192+
"model": config.pretrained_model_name_or_path,
193+
"transformer_architecture": _canonical_aot_value(transformer_config),
194+
"attention": getattr(config, "attention", ""),
195+
"a2v_attention_kernel": getattr(config, "a2v_attention_kernel", "flash"),
196+
"v2a_attention_kernel": getattr(config, "v2a_attention_kernel", "dot_product"),
197+
"flash_block_sizes": _canonical_aot_value(getattr(config, "flash_block_sizes", {})),
198+
"flash_min_seq_length": str(getattr(config, "flash_min_seq_length", 4096)),
199+
"ulysses_shards": str(getattr(config, "ulysses_shards", -1)),
200+
"ulysses_attention_chunks": str(getattr(config, "ulysses_attention_chunks", 1)),
201+
"scan_layers": str(getattr(config, "scan_layers", True)),
202+
"scan_diffusion_loop": str(getattr(config, "scan_diffusion_loop", True)),
203+
"remat_policy": str(getattr(config, "remat_policy", "NONE")),
204+
"spatio_temporal_guidance_blocks": _canonical_aot_value(
205+
getattr(config, "spatio_temporal_guidance_blocks", ())
206+
),
207+
"enable_jax_named_scopes": str(getattr(config, "enable_jax_named_scopes", False)),
208+
"logical_axis_rules": _canonical_aot_value(getattr(config, "logical_axis_rules", ())),
209+
"sharding": _canonical_aot_value(getattr(config, "sharding", {})),
210+
"mesh_shape": str(pipeline.mesh.shape),
211+
"mesh_axes": _canonical_aot_value(pipeline.mesh.axis_names),
212+
"backend": jax.default_backend(),
213+
"device_kind": getattr(device, "device_kind", ""),
214+
"process_count": str(jax.process_count()),
215+
"weights_dtype": str(getattr(config, "weights_dtype", "bfloat16")),
216+
"activations_dtype": str(getattr(config, "activations_dtype", "bfloat16")),
217+
"jax": jax.__version__,
218+
"jaxlib": getattr(jax.lib, "__version__", ""),
219+
}
220+
221+
154222
def run(config, pipeline=None, filename_prefix="", commit_hash=None):
155223
if pipeline is None:
156224
maybe_tune_block_sizes(config)
@@ -233,17 +301,8 @@ def run(config, pipeline=None, filename_prefix="", commit_hash=None):
233301
# Per-shape AOT executable cache
234302
aot_cache.install(
235303
getattr(config, "aot_cache_dir", ""),
236-
meta={
237-
"model": config.pretrained_model_name_or_path,
238-
"attention": getattr(config, "attention", ""),
239-
"flash_block_sizes": str(getattr(config, "flash_block_sizes", "")),
240-
"mesh_shape": str(pipeline.mesh.shape) if pipeline and hasattr(pipeline, "mesh") and pipeline.mesh else "",
241-
"weights_dtype": str(getattr(config, "weights_dtype", "bfloat16")),
242-
"activations_dtype": str(getattr(config, "activations_dtype", "bfloat16")),
243-
"scan_layers": str(getattr(config, "scan_layers", True)),
244-
"jax": jax.__version__,
245-
},
246-
mesh=pipeline.mesh if pipeline else None,
304+
meta=ltx2_aot_metadata(config, pipeline),
305+
mesh=pipeline.mesh,
247306
)
248307
aot_cache.wait_for_loads()
249308

0 commit comments

Comments
 (0)