Skip to content

Commit 96d5f59

Browse files
codreview updates
Signed-off-by: Brian Dellabetta <[email protected]>
1 parent 75a1602 commit 96d5f59

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/llmcompressor/modifiers/awq/base.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
from typing import Dict, List, Optional, Tuple, Union
33

44
import torch
5-
from compressed_tensors.quantization import disable_quantization
5+
from compressed_tensors.quantization import (
6+
disable_quantization,
7+
find_name_or_class_matches,
8+
)
69
from compressed_tensors.utils import (
710
align_module_device,
811
get_execution_device,
@@ -308,9 +311,8 @@ def _set_resolved_mappings(self, model: Module) -> None:
308311
smooth_names = [
309312
smooth_name
310313
for smooth_name in smooth_layers
311-
if (
312-
smooth_name not in self.ignore
313-
and not smooth_name.endswith("_observer")
314+
if not find_name_or_class_matches(
315+
smooth_name, model, self.ignore + ["re:.*_observer$"]
314316
)
315317
]
316318

@@ -340,15 +342,15 @@ def _set_resolved_mappings(self, model: Module) -> None:
340342
if (
341343
isinstance(smooth_layer, torch.nn.Linear)
342344
and isinstance(balance_layer, torch.nn.Linear)
343-
and ".o_proj" in balance_name
345+
and balance_name.endswith(".o_proj")
344346
and (
345347
(
346-
".v_proj" in smooth_name
348+
smooth_name.endswith(".v_proj")
347349
and smooth_layer.out_features
348350
!= balance_layer.in_features
349351
)
350352
or (
351-
".qkv_proj" in smooth_name
353+
smooth_name.endswith(".qkv_proj")
352354
and smooth_layer.out_features
353355
!= 3 * balance_layer.in_features
354356
)
@@ -475,7 +477,7 @@ def _apply_smoothing(self, model: Module) -> None:
475477
# [STEP 3]: Compute output of module
476478
# could cache from hook, rather than recomputing here
477479
fp16_output = self._run_samples(parent_module)
478-
if fp16_output.shape[0] == 0:
480+
if fp16_output.numel() == 0:
479481
logger.info(
480482
f"Skipping smooth_layer {mapping.smooth_name}, no activations "
481483
"found to scale. This can occasionally occur in MoE models "
@@ -549,6 +551,7 @@ def _run_samples(self, module: Module) -> torch.Tensor:
549551
]
550552
return torch.cat(
551553
[
554+
# If Tuple, assume that first argument is the input
552555
output[0] if isinstance(output, Tuple) else output
553556
for output in outputs
554557
],
@@ -751,9 +754,14 @@ def _accumulate_mean(
751754

752755
def get_lowest_common_parent(names: List[str], module: Module) -> Tuple[str, Module]:
753756
"""
754-
Given a list of names, returns the lowest-scope common parent,
755-
excluding parents of type ModuleList, which don't seem to play
756-
nicely with hooks.
757+
Given a list of names, returns the lowest-scope common parent.
758+
759+
NOTE: function excludes parents of type ModuleList, which don't play
760+
nicely with hooks because their forward method is never directly
761+
called for MoE models. See Qwen3MoeSparseMoeBlock for example, experts
762+
are selected based on router output and their forward method is called.
763+
https://github.com/huggingface/transformers/blob/v4.52.4/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py#L233
764+
757765
Returns name of parent and pointer to parent module
758766
759767
Implementation is a small alteration of os.path.commonprefix

src/llmcompressor/utils/pytorch/module.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import difflib
66
import re
7-
from functools import reduce
7+
from operator import attrgetter
88
from typing import Dict, List, Optional, Tuple, Union
99

1010
import torch
@@ -343,5 +343,4 @@ def get_layer_by_name(layer_name: str, module: Module) -> Module:
343343
:param module: Module in which to search for layer_name
344344
:return: Module, the layer with name layer_name
345345
"""
346-
names = layer_name.split(sep=".")
347-
return reduce(getattr, names, module)
346+
return attrgetter(layer_name)(module)

0 commit comments

Comments
 (0)