[perf] Add configurable diffusion compile granularity (#4603)

Signed-off-by: TaffyOfficial <2324465096@qq.com>
Co-authored-by: TaffyOfficial <2324465096@qq.com>
This commit is contained in:
TaffyOfficialandTaffyOfficial authored and GitHub committed 2026-07-24 15:22:06 +00:00
1 parent 84bbd57e4d
commit 93786c9cd3
15 files changed
+359 -15

No files matched your search

+14 -8
View File
@@ -53,23 +53,29 @@ def regionally_compile(
repeated_block_attrs = getattr(model, "_layerwise_offload_blocks_attrs", [])
# Check if we have modules with the specified class names
has_compiled_region = False
compiled_region_count = 0
# Build all compiled callables before mutating the model. This keeps setup
# failures atomic: callers can safely continue with the uncompiled model if
# torch.compile raises synchronously for any repeated block.
compiled_forwards: list[tuple[nn.Module, Any]] = []
for name, submod in model.named_modules():
if _matches_repeated_block(name, submod, repeated_blocks, repeated_block_attrs):
# Compile the block compute while keeping nn.Module.__call__ hooks
# outside the compiled graph.
submod.forward = torch.compile(submod.forward, *compile_args, **compile_kwargs)
has_compiled_region = True
compiled_region_count += 1
compiled_forwards.append(
(
submod,
torch.compile(submod.forward, *compile_args, **compile_kwargs),
)
)
if not has_compiled_region:
if not compiled_forwards:
logger.warning(f"Regional compilation skipped because {repeated_blocks} classes are not found in the model.")
else:
for submod, compiled_forward in compiled_forwards:
submod.forward = compiled_forward
logger.info(
"Regional compilation applied to %d module(s) for repeated blocks %s.",
compiled_region_count,
len(compiled_forwards),
repeated_blocks,
)