Skip to content

Commit aaac495

Browse files
committed
Keep export dtype tests optional-dependency safe
Signed-off-by: realAsma <akuriparambi@nvidia.com>
1 parent 8b9f723 commit aaac495

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

tests/unit/torch/export/test_unified_export_hf.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515

1616
"""Tests for tied-weight helpers in unified_export_hf."""
1717

18+
from types import SimpleNamespace
19+
1820
import pytest
1921
import torch
2022
from _test_utils.torch.quantization.tied_modules import (
2123
make_tied_linear_pair,
2224
wrap_in_parent_with_tied_keys,
2325
)
24-
from diffusers.configuration_utils import FrozenDict
2526

2627
import modelopt.torch.quantization as mtq
2728
from modelopt.torch.export.model_utils import TiedWeightMap
@@ -35,18 +36,20 @@
3536

3637

3738
@pytest.mark.parametrize(
38-
("config", "dtype", "expected_dtype", "warning_count"),
39+
("configured_dtype", "dtype", "expected_dtype", "warning_count"),
3940
[
40-
(object(), None, torch.float32, 0),
41-
(object(), torch.float16, torch.float16, 0),
42-
(FrozenDict(torch_dtype=torch.bfloat16), None, torch.bfloat16, 0),
43-
(FrozenDict(torch_dtype=torch.bfloat16), torch.bfloat16, torch.bfloat16, 0),
44-
(FrozenDict(torch_dtype=torch.bfloat16), torch.float16, torch.float16, 1),
41+
(None, None, torch.float32, 0),
42+
(None, torch.float16, torch.float16, 0),
43+
(torch.bfloat16, None, torch.bfloat16, 0),
44+
(torch.bfloat16, torch.bfloat16, torch.bfloat16, 0),
45+
(torch.bfloat16, torch.float16, torch.float16, 1),
4546
],
4647
)
47-
def test_resolve_export_dtype(config, dtype, expected_dtype, warning_count, recwarn):
48+
def test_resolve_export_dtype(configured_dtype, dtype, expected_dtype, warning_count, recwarn):
4849
model = torch.nn.Linear(1, 1)
49-
model.config = config
50+
model.config = (
51+
SimpleNamespace(torch_dtype=configured_dtype) if configured_dtype is not None else object()
52+
)
5053

5154
assert _resolve_export_dtype(model, dtype) == expected_dtype
5255
assert len(recwarn) == warning_count
@@ -57,6 +60,14 @@ def test_resolve_export_dtype(config, dtype, expected_dtype, warning_count, recw
5760
)
5861

5962

63+
def test_resolve_export_dtype_with_empty_diffusers_config():
64+
frozen_dict = pytest.importorskip("diffusers.configuration_utils").FrozenDict()
65+
model = torch.nn.Linear(1, 1)
66+
model.config = frozen_dict
67+
68+
assert _resolve_export_dtype(model, None) == torch.float32
69+
70+
6071
def test_hf_all_tied_weights_keys_contract():
6172
"""Pin the transformers API we build tied_map from, so a version bump fails loud here.
6273

0 commit comments

Comments
 (0)