|
9 | 9 |
|
10 | 10 | import torch
|
11 | 11 | from compressed_tensors.quantization.utils import is_module_quantized
|
12 |
| -from packaging import version |
13 | 12 | from torch.nn import Linear, Module, Parameter
|
14 | 13 | from torch.nn.modules.conv import _ConvNd
|
15 | 14 | from transformers import PreTrainedModel
|
|
64 | 63 | "get_layer_by_name",
|
65 | 64 | ]
|
66 | 65 |
|
67 |
| - |
68 |
| -_PARSED_TORCH_VERSION = version.parse(torch.__version__) |
69 |
| - |
70 |
| - |
71 | 66 | ALL_TARGET = "__ALL__"
|
72 | 67 | ALL_PRUNABLE_TARGET = "__ALL_PRUNABLE__"
|
73 | 68 | ALL_QUANTIZABLE_TARGET = "__ALL_QUANTIZABLE__"
|
@@ -164,8 +159,47 @@ def match_layers_params(
|
164 | 159 | return resolved
|
165 | 160 |
|
166 | 161 |
|
167 |
| -def get_layers(targets: Union[str, List[str]], module: Module) -> Dict[str, Module]: |
168 |
| - return match_layers_params(targets, module) |
| 162 | +def is_internal_module(name: str) -> bool: |
| 163 | + """ |
| 164 | + llm-compressor adds additional modules to a model, like observers |
| 165 | + and transforms, as part of its operation. |
| 166 | + Return whether module is internally instantiated by llm-compressor, |
| 167 | + based on its name. |
| 168 | +
|
| 169 | + :param name: name of module |
| 170 | + :return: True if name indicates a module instantiated |
| 171 | + """ |
| 172 | + return name.endswith(("_observer", "_transform", "perm")) |
| 173 | + |
| 174 | + |
| 175 | +def get_layers( |
| 176 | + targets: Union[str, List[str]], |
| 177 | + module: Module, |
| 178 | + exclude_internal_modules: bool = False, |
| 179 | +) -> Dict[str, Module]: |
| 180 | + """ |
| 181 | + Get layers (also known as submodules) of module based on targets |
| 182 | +
|
| 183 | + :param targets: names or regexes to search for |
| 184 | + Can be regex, e.g. "re:.*input_layernorm$" to find all layers |
| 185 | + in module whose names end in string "input_layernorm" |
| 186 | + :param module: Parent module in which to search for targets |
| 187 | + :param exclude_internal_modules: If True, don't include internal |
| 188 | + modules added by llm-compressor, e.g. Observers and Transforms. |
| 189 | + Defaults to False to maintain backward compatibility |
| 190 | +
|
| 191 | + :return: dict of layer name -> layer module of all layers in module |
| 192 | + that match targets |
| 193 | + """ |
| 194 | + layer_dict = match_layers_params(targets, module) |
| 195 | + if exclude_internal_modules: |
| 196 | + layer_dict = { |
| 197 | + layer_name: layer |
| 198 | + for layer_name, layer in layer_dict.items() |
| 199 | + if not is_internal_module(layer_name) |
| 200 | + } |
| 201 | + |
| 202 | + return layer_dict |
169 | 203 |
|
170 | 204 |
|
171 | 205 | def get_layer(target: str, module: Module) -> Tuple[str, Module]:
|
|
0 commit comments