1515
1616"""Tests for tied-weight helpers in unified_export_hf."""
1717
18+ from types import SimpleNamespace
19+
1820import pytest
1921import torch
2022from _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
2627import modelopt .torch .quantization as mtq
2728from modelopt .torch .export .model_utils import TiedWeightMap
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+
6071def 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