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
7 changes: 7 additions & 0 deletions test/quantization/test_quant_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,13 @@ def __init__(self):
assert isinstance(m.nested.linear.weight, AffineQuantizedTensor)
assert isinstance(m.linear1.weight, AffineQuantizedTensor)

def test_fqn_config_module_config_and_fqn_config_both_specified(self):
with self.assertRaises(ValueError):
FqnToConfig(
fqn_to_config={"test": Float8WeightOnlyConfig()},
module_fqn_to_config={"test2": Float8WeightOnlyConfig()},
)


if __name__ == "__main__":
unittest.main()
21 changes: 21 additions & 0 deletions torchao/quantization/quant_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2466,6 +2466,15 @@ class FqnToConfig(AOBaseConfig):
def __post_init__(self):
torch._C._log_api_usage_once("torchao.quantization.FqnToConfig")

if (
len(self.fqn_to_config) > 0
and len(self.module_fqn_to_config) > 0
and self.fqn_to_config != self.module_fqn_to_config
):
raise ValueError(
"`fqn_to_config` and `module_fqn_to_config` are both specified and are not equal!"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also want to check, does torchao config support equality comparison?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, since all the configs are dataclasses, it's the same as creating a tuple of the member attributes.

)

# This code handles BC compatibility with `ModuleFqnToConfig`. It ensures that `self.module_fqn_to_config` and `self.fqn_to_config` share the same object.
if len(self.module_fqn_to_config) > 0 and len(self.fqn_to_config) == 0:
self.fqn_to_config = self.module_fqn_to_config
Expand All @@ -2479,6 +2488,18 @@ def __post_init__(self):
"Config Deprecation: _default is deprecated and will no longer be supported in a future release. Please see https://github.com/pytorch/ao/issues/3229 for more details."
)

def __str__(self):
return "\n".join(
[
"FqnToConfig({",
*(
f" '{key}':\n {value},"
for key, value in self.fqn_to_config.items()
),
"})",
]
)


# maintain BC
ModuleFqnToConfig = FqnToConfig
Expand Down
Loading