From 68e4e73fb25c82e502af2e11d20849e0a6a45c11 Mon Sep 17 00:00:00 2001 From: Jesse Cai Date: Tue, 11 Nov 2025 08:16:02 -0800 Subject: [PATCH 1/2] Adds __str__ to FqnToConfig to make printing more readable Summary: att, adds `__str__` method to `FqnToConfig` so that printing is more legible. For some config: ```python config = FqnToConfig({ "model.layers.fig.1.1": Float8DynamicActivationFloat8WeightConfig( granularity=PerRow(), ), "model.layers.fig.1.3": Float8DynamicActivationFloat8WeightConfig( granularity=PerRow(), ), "model.layers.fig.8.3": Float8DynamicActivationFloat8WeightConfig( granularity=PerRow(), ), }) ``` the output will be: ``` FqnToConfig({ 'model.layers.fig.1.1': Float8DynamicActivationFloat8WeightConfig(activation_dtype=torch.float8_e4m3fn, weight_dtype=torch.float8_e4m3fn, granularity=[PerRow(dim=-1), PerRow(dim=-1)], mm_config=Float8MMConfig(emulate=False, use_fast_accum=True, pad_inner_dim=False), activation_value_lb=None, activation_value_ub=None, kernel_preference=, set_inductor_config=True, version=2), 'model.layers.fig.1.3': Float8DynamicActivationFloat8WeightConfig(activation_dtype=torch.float8_e4m3fn, weight_dtype=torch.float8_e4m3fn, granularity=[PerRow(dim=-1), PerRow(dim=-1)], mm_config=Float8MMConfig(emulate=False, use_fast_accum=True, pad_inner_dim=False), activation_value_lb=None, activation_value_ub=None, kernel_preference=, set_inductor_config=True, version=2), 'model.layers.fig.8.3': Float8DynamicActivationFloat8WeightConfig(activation_dtype=torch.float8_e4m3fn, weight_dtype=torch.float8_e4m3fn, granularity=[PerRow(dim=-1), PerRow(dim=-1)], mm_config=Float8MMConfig(emulate=False, use_fast_accum=True, pad_inner_dim=False), activation_value_lb=None, activation_value_ub=None, kernel_preference=, set_inductor_config=True, version=2), }) ``` also adds in a test so that you cannot specify both fqn_to_config and module_fqn_to_config unless they are both equal. Test Plan: ``` pytest test/quantization/test_quant_api.py -k test_fqn_config_module_config_and_fqn_config_both_specified ``` Reviewers: Subscribers: Tasks: Tags: --- test/quantization/test_quant_api.py | 7 +++++++ torchao/quantization/quant_api.py | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/test/quantization/test_quant_api.py b/test/quantization/test_quant_api.py index 506cec9dea..39f0008be2 100644 --- a/test/quantization/test_quant_api.py +++ b/test/quantization/test_quant_api.py @@ -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): + config = FqnToConfig( + fqn_to_config={"test": Float8WeightOnlyConfig()}, + module_fqn_to_config={"test2": Float8WeightOnlyConfig()}, + ) + if __name__ == "__main__": unittest.main() diff --git a/torchao/quantization/quant_api.py b/torchao/quantization/quant_api.py index 09c2edcd9f..425af1feb9 100644 --- a/torchao/quantization/quant_api.py +++ b/torchao/quantization/quant_api.py @@ -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!" + ) + # 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 @@ -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 From c6de0f159a12492a1c0865805aaa6f7da0f4a8a9 Mon Sep 17 00:00:00 2001 From: Jesse Cai Date: Tue, 11 Nov 2025 08:22:23 -0800 Subject: [PATCH 2/2] fix ruff check --- test/quantization/test_quant_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/quantization/test_quant_api.py b/test/quantization/test_quant_api.py index 39f0008be2..e530babdb9 100644 --- a/test/quantization/test_quant_api.py +++ b/test/quantization/test_quant_api.py @@ -1180,7 +1180,7 @@ def __init__(self): def test_fqn_config_module_config_and_fqn_config_both_specified(self): with self.assertRaises(ValueError): - config = FqnToConfig( + FqnToConfig( fqn_to_config={"test": Float8WeightOnlyConfig()}, module_fqn_to_config={"test2": Float8WeightOnlyConfig()}, )