[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:
1 parent
84bbd57e4d
commit
93786c9cd3
15 files changed
+359
-15
No files matched your search
@@ -628,6 +628,47 @@ def test_from_pipeline_config_preserves_diffusion_parallel_mask_sp_padding(tmp_p
|
||||
assert stage.parallel_config.mask_sp_padding is True
|
||||
|
||||
|
||||
def test_from_pipeline_config_routes_regional_compile_dynamic(tmp_path):
|
||||
deploy_path = tmp_path / "dreamzero_compile.yaml"
|
||||
deploy_path.write_text(
|
||||
"\n".join(
|
||||
[
|
||||
"pipeline: dreamzero",
|
||||
"async_chunk: false",
|
||||
"stages:",
|
||||
" - stage_id: 0",
|
||||
" diffusion_compile_granularity: regional",
|
||||
" diffusion_compile_dynamic: false",
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
configured_stage = _from_pipeline_key("dreamzero", deploy_config_path=str(deploy_path)).stage_by_id(0)
|
||||
overridden_stage = _from_pipeline_key(
|
||||
"dreamzero",
|
||||
deploy_config_path=str(deploy_path),
|
||||
cli_overrides={
|
||||
"diffusion_compile_granularity": "full",
|
||||
"diffusion_compile_dynamic": True,
|
||||
},
|
||||
).stage_by_id(0)
|
||||
|
||||
assert configured_stage.diffusion_config.diffusion_compile_granularity == "regional"
|
||||
assert configured_stage.diffusion_config.diffusion_compile_dynamic is False
|
||||
assert overridden_stage.diffusion_config.diffusion_compile_granularity == "full"
|
||||
assert overridden_stage.diffusion_config.diffusion_compile_dynamic is True
|
||||
|
||||
|
||||
def test_structured_diffusion_config_rejects_non_boolean_compile_dynamic():
|
||||
with pytest.raises(ValidationError, match="diffusion_compile_dynamic"):
|
||||
omni_config_module._DiffusionConfigProjection(diffusion_compile_dynamic="false")
|
||||
|
||||
|
||||
def test_structured_diffusion_config_rejects_invalid_compile_granularity():
|
||||
with pytest.raises(ValidationError, match="diffusion_compile_granularity"):
|
||||
omni_config_module._DiffusionConfigProjection(diffusion_compile_granularity="block")
|
||||
|
||||
|
||||
def test_from_pipeline_config_matches_stage_config_to_omegaconf_behavior_for_representative_stage():
|
||||
pipeline = _resolve_pipeline_or_skip("qwen3_tts")
|
||||
legacy_stage = merge_pipeline_deploy(pipeline, _load_default_deploy(pipeline))[0]
|
||||
|
||||
@@ -54,3 +54,23 @@ def test_regionally_compile_matches_wrapped_blocks_by_declared_container_attr(mo
|
||||
assert all(not block.compile_called for block in model.transformer_blocks)
|
||||
assert not model.other_blocks[0].compile_called
|
||||
assert model.transformer_blocks[0].forward("ok") == "compiled:ok"
|
||||
|
||||
|
||||
def test_regionally_compile_does_not_partially_mutate_on_setup_failure(monkeypatch):
|
||||
model = _ModelWithWrappedRepeatedBlocks()
|
||||
original_forwards = [block.forward.__func__ for block in model.transformer_blocks]
|
||||
compile_calls = 0
|
||||
|
||||
def _compile(fn, *args, **kwargs):
|
||||
nonlocal compile_calls
|
||||
compile_calls += 1
|
||||
if compile_calls == 2:
|
||||
raise RuntimeError("compile setup failed")
|
||||
return lambda *fn_args, **fn_kwargs: fn(*fn_args, **fn_kwargs)
|
||||
|
||||
monkeypatch.setattr(compile_module.torch, "compile", _compile)
|
||||
|
||||
with pytest.raises(RuntimeError, match="compile setup failed"):
|
||||
regionally_compile(model, dynamic=True)
|
||||
|
||||
assert [block.forward.__func__ for block in model.transformer_blocks] == original_forwards
|
||||
@@ -392,6 +392,57 @@ class TestRequestBatchCapability:
|
||||
warmup.assert_called_once_with()
|
||||
|
||||
|
||||
class TestDiffusionCompileConfig:
|
||||
pytestmark = [pytest.mark.core_model, pytest.mark.diffusion, pytest.mark.cpu]
|
||||
|
||||
def test_config_defaults_to_dynamic_regional_compile(self) -> None:
|
||||
config = OmniDiffusionConfig(model="test")
|
||||
|
||||
assert config.diffusion_compile_granularity == "regional"
|
||||
assert config.diffusion_compile_dynamic is True
|
||||
|
||||
def test_from_kwargs_preserves_compile_controls(self) -> None:
|
||||
config = OmniDiffusionConfig.from_kwargs(
|
||||
model="test",
|
||||
diffusion_compile_granularity="full",
|
||||
diffusion_compile_dynamic=False,
|
||||
)
|
||||
|
||||
assert config.diffusion_compile_granularity == "full"
|
||||
assert config.diffusion_compile_dynamic is False
|
||||
|
||||
def test_config_rejects_invalid_compile_granularity(self) -> None:
|
||||
with pytest.raises(ValueError, match="diffusion_compile_granularity"):
|
||||
OmniDiffusionConfig(model="test", diffusion_compile_granularity="block")
|
||||
|
||||
def test_config_rejects_non_boolean_compile_dynamic(self) -> None:
|
||||
with pytest.raises(TypeError, match="diffusion_compile_dynamic"):
|
||||
OmniDiffusionConfig(model="test", diffusion_compile_dynamic="false")
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"kwargs, feature",
|
||||
[
|
||||
({"parallel_config": {"ulysses_degree": 2}, "num_gpus": 2}, "sequence parallelism"),
|
||||
(
|
||||
{
|
||||
"parallel_config": {"use_hsdp": True, "hsdp_shard_size": 2},
|
||||
"num_gpus": 2,
|
||||
},
|
||||
"HSDP",
|
||||
),
|
||||
({"enable_cpu_offload": True}, "CPU offload"),
|
||||
({"enable_layerwise_offload": True}, "layerwise offload"),
|
||||
],
|
||||
)
|
||||
def test_full_compile_rejects_incompatible_features(self, kwargs, feature) -> None:
|
||||
with pytest.raises(ValueError, match=feature):
|
||||
OmniDiffusionConfig(
|
||||
model="test",
|
||||
diffusion_compile_granularity="full",
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
|
||||
class TestRequestBatchAdmission:
|
||||
pytestmark = [pytest.mark.core_model, pytest.mark.diffusion, pytest.mark.cpu]
|
||||
|
||||
|
||||
@@ -121,6 +121,15 @@ class _FinalOnlyStepPipeline:
|
||||
return DiffusionOutput(output=state.latents.clone())
|
||||
|
||||
|
||||
class _CompileTrackingModel:
|
||||
def __init__(self):
|
||||
self.compile_calls = []
|
||||
|
||||
def compile(self, *args, **kwargs):
|
||||
self.compile_calls.append((args, kwargs))
|
||||
return self
|
||||
|
||||
|
||||
def _make_request():
|
||||
sampling_params = SimpleNamespace(
|
||||
generator=None,
|
||||
@@ -177,10 +186,20 @@ def _make_runner(cache_backend, cache_backend_name: str, enable_cache_dit_summar
|
||||
return runner
|
||||
|
||||
|
||||
def _make_compile_runner(*, use_hsdp: bool):
|
||||
def _make_compile_runner(
|
||||
model=None,
|
||||
*,
|
||||
compile_granularity: str = "regional",
|
||||
compile_dynamic: bool = True,
|
||||
use_hsdp: bool = False,
|
||||
):
|
||||
runner = object.__new__(DiffusionModelRunner)
|
||||
runner.pipeline = SimpleNamespace(transformer=SimpleNamespace())
|
||||
runner.od_config = SimpleNamespace(parallel_config=SimpleNamespace(use_hsdp=use_hsdp))
|
||||
runner.pipeline = SimpleNamespace(transformer=model or SimpleNamespace())
|
||||
runner.od_config = SimpleNamespace(
|
||||
diffusion_compile_granularity=compile_granularity,
|
||||
diffusion_compile_dynamic=compile_dynamic,
|
||||
parallel_config=SimpleNamespace(use_hsdp=use_hsdp),
|
||||
)
|
||||
return runner
|
||||
|
||||
|
||||
@@ -208,6 +227,65 @@ def test_compile_transformer_regionally_compiles_blocks(monkeypatch, use_hsdp):
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.core_model
|
||||
@pytest.mark.cpu
|
||||
def test_compile_transformer_uses_regional_dynamic_false_config(monkeypatch):
|
||||
model = _CompileTrackingModel()
|
||||
runner = _make_compile_runner(model, compile_dynamic=False)
|
||||
compiled_model = object()
|
||||
regional_calls = []
|
||||
|
||||
def _regionally_compile(target, **kwargs):
|
||||
regional_calls.append((target, kwargs))
|
||||
return compiled_model
|
||||
|
||||
monkeypatch.setattr(model_runner_module, "regionally_compile", _regionally_compile)
|
||||
|
||||
DiffusionModelRunner._compile_transformer(runner, "transformer")
|
||||
|
||||
assert regional_calls == [(model, {"dynamic": False})]
|
||||
assert model.compile_calls == []
|
||||
assert runner.pipeline.transformer is compiled_model
|
||||
|
||||
|
||||
@pytest.mark.core_model
|
||||
@pytest.mark.cpu
|
||||
def test_compile_transformer_uses_full_granularity(monkeypatch):
|
||||
model = _CompileTrackingModel()
|
||||
runner = _make_compile_runner(model, compile_granularity="full", compile_dynamic=False)
|
||||
regional_calls = []
|
||||
|
||||
monkeypatch.setattr(
|
||||
model_runner_module,
|
||||
"regionally_compile",
|
||||
lambda *args, **kwargs: regional_calls.append((args, kwargs)),
|
||||
)
|
||||
|
||||
DiffusionModelRunner._compile_transformer(runner, "transformer")
|
||||
|
||||
assert model.compile_calls == [((), {"dynamic": False})]
|
||||
assert regional_calls == []
|
||||
assert runner.pipeline.transformer is model
|
||||
|
||||
|
||||
@pytest.mark.core_model
|
||||
@pytest.mark.cpu
|
||||
def test_compile_transformer_falls_back_after_synchronous_setup_failure(monkeypatch, caplog):
|
||||
model = _CompileTrackingModel()
|
||||
runner = _make_compile_runner(model)
|
||||
|
||||
def _regionally_compile(*args, **kwargs):
|
||||
raise RuntimeError("compile setup failed")
|
||||
|
||||
monkeypatch.setattr(model_runner_module, "regionally_compile", _regionally_compile)
|
||||
|
||||
DiffusionModelRunner._compile_transformer(runner, "transformer")
|
||||
|
||||
assert runner.pipeline.transformer is model
|
||||
assert "failed before activation" in caplog.text
|
||||
assert "lazy compilation errors" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.core_model
|
||||
@pytest.mark.cpu
|
||||
def test_execute_stepwise_streaming_returns_chunks_at_boundaries(monkeypatch):
|
||||
|
||||
@@ -85,6 +85,8 @@ def test_default_stage_config_defaults_nullified_parallel_size_kwargs():
|
||||
"tensor_parallel_size": None,
|
||||
"enable_expert_parallel": None,
|
||||
"enforce_eager": None,
|
||||
"diffusion_compile_granularity": None,
|
||||
"diffusion_compile_dynamic": None,
|
||||
}
|
||||
)[0]
|
||||
|
||||
@@ -94,6 +96,8 @@ def test_default_stage_config_defaults_nullified_parallel_size_kwargs():
|
||||
assert parallel_config.tensor_parallel_size == 1
|
||||
assert parallel_config.enable_expert_parallel is False
|
||||
assert stage_cfg["engine_args"]["enforce_eager"] is False
|
||||
assert stage_cfg["engine_args"]["diffusion_compile_granularity"] == "regional"
|
||||
assert stage_cfg["engine_args"]["diffusion_compile_dynamic"] is True
|
||||
|
||||
|
||||
def test_default_stage_config_propagates_ulysses_mode():
|
||||
@@ -258,6 +262,32 @@ def test_serve_cli_accepts_diffusion_pipeline_profiler_flag():
|
||||
assert stage_cfg["engine_args"]["enable_diffusion_pipeline_profiler"] is True
|
||||
|
||||
|
||||
def test_serve_cli_accepts_diffusion_compile_controls():
|
||||
"""Ensure both compile controls reach the diffusion stage."""
|
||||
parser = TrackingArgumentParser()
|
||||
subparsers = parser.add_subparsers(dest="command")
|
||||
OmniServeCommand().subparser_init(subparsers)
|
||||
|
||||
args = parser.parse_args(
|
||||
[
|
||||
"serve",
|
||||
"Lightricks/LTX-Video-0.9.8-13B-distilled",
|
||||
"--omni",
|
||||
"--diffusion-compile-granularity",
|
||||
"full",
|
||||
"--no-diffusion-compile-dynamic",
|
||||
]
|
||||
)
|
||||
|
||||
explicit_kwargs = args.get_explicit_kwargs_dict()
|
||||
stage_cfg = AsyncOmniEngine._create_default_diffusion_stage_cfg(explicit_kwargs)[0]
|
||||
|
||||
assert args.diffusion_compile_granularity == "full"
|
||||
assert args.diffusion_compile_dynamic is False
|
||||
assert stage_cfg["engine_args"]["diffusion_compile_granularity"] == "full"
|
||||
assert stage_cfg["engine_args"]["diffusion_compile_dynamic"] is False
|
||||
|
||||
|
||||
def test_serve_cli_accepts_diffusion_attention_backend():
|
||||
"""Ensure diffusion serve CLI exposes the shorthand backend flag."""
|
||||
parser = TrackingArgumentParser()
|
||||
|
||||
Reference in new issue
Block a user