Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/diffusers/pipelines/cosmos/mixed_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions tests/pipelines/cosmos/test_cosmos3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down