diff --git a/src/diffusers/pipelines/cosmos/mixed_precision.py b/src/diffusers/pipelines/cosmos/mixed_precision.py index c1ec2d44dc92..728d1c4465ae 100644 --- a/src/diffusers/pipelines/cosmos/mixed_precision.py +++ b/src/diffusers/pipelines/cosmos/mixed_precision.py @@ -447,7 +447,9 @@ def _on_disk_quantization_config(module: nn.Module) -> dict[str, Any] | None: name_or_path = getattr(config, "_name_or_path", None) if name_or_path is None and hasattr(config, "get"): name_or_path = config.get("_name_or_path") - loader = getattr(type(module), "load_config", None) + # Look up load_config on the instance, not the class: torch.compile's OptimizedModule and other + # attribute-forwarding wrappers expose the original module's load_config. + loader = getattr(module, "load_config", None) if not name_or_path or not callable(loader): return None try: diff --git a/tests/pipelines/cosmos/test_cosmos3.py b/tests/pipelines/cosmos/test_cosmos3.py index 5660698405c3..29bcf6dd7760 100644 --- a/tests/pipelines/cosmos/test_cosmos3.py +++ b/tests/pipelines/cosmos/test_cosmos3.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json from contextlib import contextmanager from unittest import mock @@ -27,6 +28,7 @@ Cosmos3OmniTransformer, UniPCMultistepScheduler, ) +from diffusers.pipelines.cosmos.mixed_precision import FIRST_LAST_N_FP8_POLICY, Cosmos3MixedPrecisionConfig from diffusers.pipelines.cosmos.pipeline_cosmos3_omni import _preprocess_conditioning_image from ...testing_utils import enable_full_determinism, torch_device @@ -116,6 +118,26 @@ def test_inference(self): assert video[0].shape == self.output_shape + def test_mixed_precision_policy_resolves_through_torch_compile(self, tmp_path): + # `pipe.transformer = torch.compile(pipe.transformer)` wraps the model, and the checkpoint's step policy + # must still resolve, or every step silently runs W8A8 instead of the checkpoint's W8A16 first/last steps. + transformer = self.get_dummy_components()["transformer"] + transformer.save_config(tmp_path) + config_path = tmp_path / "config.json" + config = json.loads(config_path.read_text()) + config["quantization_config"] = { + "quant_method": "modelopt", + "quant_algo": "FP8", + "runtime": {"diffusion_step_policy": FIRST_LAST_N_FP8_POLICY}, + } + config_path.write_text(json.dumps(config)) + # `from_pretrained` records the checkpoint path, and the on-disk config is the only source of the policy. + transformer.register_to_config(_name_or_path=str(tmp_path)) + + policy = Cosmos3MixedPrecisionConfig.resolve(transformer) + assert policy.format == "fp8" + assert Cosmos3MixedPrecisionConfig.resolve(torch.compile(transformer, backend="eager")) == policy + def test_fp32_sampling_state_keeps_transformer_inputs_in_model_dtype(self): pipeline = self.pipeline_class(**self.get_dummy_components()).to(torch_device) pipeline.transformer.to(dtype=torch.float16)