ao icon indicating copy to clipboard operation
ao copied to clipboard

[QAT] Linear layer's weight quantization granularity can only be per_group

Open Cypher-Bruce opened this issue 1 year ago • 1 comments

Although fake_quantizer implements forwarding method for all three kinds of granularities, only per_group can be used for linear layer weight in QAT. This is because when FakeQuantizedLinear's __init__ is called, it first trys to get the group size in weight config:

https://github.com/pytorch/ao/blob/2c901b393846ff39d97598abab586d08765f7ea2/torchao/quantization/qat/linear.py#L84-L94

And if the weight config use any granularity other than per_group, an exception will be raised

https://github.com/pytorch/ao/blob/2c901b393846ff39d97598abab586d08765f7ea2/torchao/quantization/qat/api.py#L216-L226

An easy fix can be checking granularity type before getting group size

        # initialize weight fake quantizer
        if weight_config is not None:
            if isinstance(weight_config.granularity, PerGroup):
                group_size = weight_config.group_size
                if group_size is not None and in_features % group_size != 0:
                    raise ValueError(
                        "in_features (%s) %% group_size (%s) must be == 0"
                        % (in_features, group_size)
                    )
            self.weight_fake_quantizer = FakeQuantizer(weight_config)
        else:
            self.weight_fake_quantizer = None

Cypher-Bruce avatar May 09 '25 05:05 Cypher-Bruce

Hi @Cypher-Bruce, thanks for reporting this. I think this is fixed in the latest main, can you try again? https://github.com/pytorch/ao/blob/266f7497b4fde6476c59cc1e1ac58e505a0dccf3/torchao/quantization/qat/linear.py#L95-L97

andrewor14 avatar Sep 02 '25 15:09 andrewor14