Skip to content

Commit 8e14b2c

Browse files
author
Mirza-Samad-Ahmed-Baig
committed
Fix: Attach _linear_extra_repr to quantized linear layers
1 parent 71552fd commit 8e14b2c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

torchchat/utils/quantize.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from __future__ import annotations
2424

2525
import json
26+
import types
2627

2728
# from functools import reduce
2829
# from math import gcd
@@ -32,6 +33,8 @@
3233
import torch.nn as nn
3334
import torch.nn.functional as F
3435

36+
from torchao.quantization.quant_api import _linear_extra_repr
37+
3538
# AttributeError: '_OpNamespace' 'quantized_decomposed' object has no attribute 'quantize_per_channel_group'
3639
from torch.ao.quantization.fx._decomposed import quantized_decomposed_lib # noqa
3740
from torchao.dtypes import PackedLinearInt8DynamicActivationIntxWeightLayout, QDQLayout
@@ -111,6 +114,16 @@ def quantize_model(
111114
if isinstance(quantize_options, str):
112115
quantize_options = json.loads(quantize_options)
113116

117+
def _attach_extra_repr(module):
118+
for name, child in module.named_children():
119+
if isinstance(child, nn.Linear):
120+
if not hasattr(child, 'extra_repr'):
121+
child.extra_repr = types.MethodType(_linear_extra_repr, child)
122+
else:
123+
_attach_extra_repr(child)
124+
125+
_attach_extra_repr(model)
126+
114127
for quantizer, q_kwargs in quantize_options.items():
115128
if quantizer not in quantizer_class_dict:
116129
raise RuntimeError(f"unknown quantizer {quantizer} specified")

0 commit comments

Comments
 (0)