Skip to content

Fix: Attach _linear_extra_repr to quantized linear layers #1546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions torchchat/utils/quantize.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from __future__ import annotations

import json
import types

# from functools import reduce
# from math import gcd
Expand All @@ -32,6 +33,8 @@
import torch.nn as nn
import torch.nn.functional as F

from torchao.quantization.quant_api import _linear_extra_repr

# AttributeError: '_OpNamespace' 'quantized_decomposed' object has no attribute 'quantize_per_channel_group'
from torch.ao.quantization.fx._decomposed import quantized_decomposed_lib # noqa
from torchao.dtypes import PackedLinearInt8DynamicActivationIntxWeightLayout, QDQLayout
Expand Down Expand Up @@ -111,6 +114,16 @@ def quantize_model(
if isinstance(quantize_options, str):
quantize_options = json.loads(quantize_options)

def _attach_extra_repr(module):
for name, child in module.named_children():
if isinstance(child, nn.Linear):
if not hasattr(child, 'extra_repr'):
child.extra_repr = types.MethodType(_linear_extra_repr, child)
else:
_attach_extra_repr(child)

_attach_extra_repr(model)

for quantizer, q_kwargs in quantize_options.items():
if quantizer not in quantizer_class_dict:
raise RuntimeError(f"unknown quantizer {quantizer} specified")
Expand Down