Skip to content

Commit

Permalink
FIX: Broken repr of TorchAoConfig (#34560)
Browse files Browse the repository at this point in the history
FIX Broken repr of TorchAoConfig

The __repr__ method references a non-existent self.kwargs. This is now
fixed.

There does not appear to be a uniform way of defining __repr__ for
quantization configs. I copied the method as implemented for HQQ:

https://github.com/huggingface/transformers/blob/e2ac16b28a0b8b900e136750309ca40c49d975c5/src/transformers/utils/quantization_config.py#L285-L287
  • Loading branch information
BenjaminBossan authored Nov 5, 2024
1 parent d0b1d8d commit 5e1fd4e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/transformers/utils/quantization_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,8 @@ def get_apply_tensor_subclass(self):
return _STR_TO_METHOD[self.quant_type](**self.quant_type_kwargs)

def __repr__(self):
return f"{self.quant_type}({', '.join(str(k) + '=' + str(v) for k, v in self.kwargs.items())})"
config_dict = self.to_dict()
return f"{self.__class__.__name__} {json.dumps(config_dict, indent=2, sort_keys=True)}\n"


@dataclass
Expand Down
7 changes: 7 additions & 0 deletions tests/quantization/torchao_integration/test_torchao.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ def test_post_init_check(self):
with self.assertRaisesRegex(ValueError, "Unexpected keyword arg"):
_ = TorchAoConfig("int4_weight_only", group_size1=32)

def test_repr(self):
"""
Check that there is no error in the repr
"""
quantization_config = TorchAoConfig("int4_weight_only", modules_to_not_convert=["conv"], group_size=8)
repr(quantization_config)


@require_torch_gpu
@require_torchao
Expand Down

0 comments on commit 5e1fd4e

Please sign in to comment.