Skip to content

Commit 01e75b2

Browse files
committed
Small fixes
Signed-off-by: Fynn Schmitt-Ulms <[email protected]>
1 parent 72a6c65 commit 01e75b2

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

src/compressed_tensors/quantization/lifecycle/apply.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def apply_quantization_config(
151151
config.ignore or [],
152152
warn_on_fail=True,
153153
warn_on_unmatched_ignores=True,
154-
return_matched_targets=True,
154+
yield_matched_targets=True,
155155
preprocess_name=fix_fsdp_module_name,
156156
):
157157
# mark modules to be quantized by adding

src/compressed_tensors/utils/match.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import logging
1616
import re
1717
from collections.abc import Generator
18-
from typing import Callable, Iterable, Tuple
18+
from typing import Callable, Iterable, List, Tuple
1919

2020
import torch
2121
from compressed_tensors.utils.internal import InternalModule
@@ -38,9 +38,9 @@ def match_named_modules(
3838
ignore: Iterable[str] | None = None,
3939
warn_on_fail: bool = False,
4040
warn_on_unmatched_ignores: bool = False,
41-
return_matched_targets: bool = False,
41+
yield_matched_targets: bool = False,
4242
preprocess_name: Callable[[str], str] = lambda x: x,
43-
) -> Generator[Tuple[str, torch.nn.Module]]:
43+
) -> Generator[Tuple[str, torch.nn.Module] | Tuple[str, torch.nn.Module, List[str]]]:
4444
"""
4545
Yields names and modules which match `targets` but do not match `ignore`.
4646
Values are returned in order of `model.named_modules()`
@@ -49,6 +49,9 @@ def match_named_modules(
4949
:param targets: target strings, potentially containing "re:" prefixes
5050
:param ignore: targets to ignore, potentially containing "re:" prefixes
5151
:param warn_on_fail: if True, warns if any targets do not match any modules in model
52+
:param warn_on_unmatched_ignores: if True, warns if any ignores do not match any modules in model
53+
:param yield_matched_targets: if True, yields the matched targets in addition to the module name and module
54+
:param preprocess_name: a function to preprocess the module name
5255
:return: generator of module names and modules
5356
"""
5457
ignore = ignore or []
@@ -57,6 +60,7 @@ def match_named_modules(
5760
unmatched_targets = set(targets)
5861
unmatched_ignores = set(ignore)
5962

63+
# Note: when yield_matched_targets is True, the ordering of the targets is important
6064
# Order targets by type: exact name match, regex name match, class name match
6165
targets = sorted(targets, key=lambda x: ("re:" in x, x))
6266
for name, module in model.named_modules():
@@ -75,30 +79,24 @@ def match_named_modules(
7579
if ignore_matched:
7680
continue
7781

78-
matched_targets = []
79-
# Check for name matches first (exact then regex)
82+
matched_target_on_name = []
83+
matched_target_on_class = []
84+
# Check for name matches first (exact then regex, enforced by sort above)
8085
for target in targets:
8186
if _match_name(name, target):
8287
unmatched_targets -= {target}
83-
matched_targets.append(target)
84-
if not return_matched_targets:
88+
matched_target_on_name.append(target)
89+
if not yield_matched_targets:
8590
break
86-
87-
if not return_matched_targets and matched_targets:
88-
# Don't need to check other targets, one match is enough
89-
yield name, module
90-
continue
91-
92-
# Check for class matches
93-
for target in targets:
94-
if _match_class(module, target):
91+
elif _match_class(module, target):
9592
unmatched_targets -= {target}
96-
matched_targets.append(target)
97-
if not return_matched_targets:
93+
matched_target_on_class.append(target)
94+
if not yield_matched_targets:
9895
break
9996

97+
matched_targets = matched_target_on_name + matched_target_on_class
10098
if matched_targets:
101-
if return_matched_targets:
99+
if yield_matched_targets:
102100
yield name, module, matched_targets
103101
else:
104102
yield name, module

0 commit comments

Comments
 (0)