|
| 1 | +# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, |
| 10 | +# software distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import logging |
| 16 | +import re |
| 17 | +from collections.abc import Generator |
| 18 | +from typing import Iterable, Tuple |
| 19 | + |
| 20 | +import torch |
| 21 | + |
| 22 | + |
| 23 | +_LOGGER: logging.Logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +__all__ = [ |
| 27 | + "match_named_modules", |
| 28 | + "match_named_parameters", |
| 29 | + "match_modules_set", |
| 30 | + "is_match", |
| 31 | + "match_name", |
| 32 | + "match_class", |
| 33 | +] |
| 34 | + |
| 35 | + |
| 36 | +def match_named_modules( |
| 37 | + model: torch.nn.Module, |
| 38 | + targets: Iterable[str], |
| 39 | + ignore: Iterable[str] = tuple(), |
| 40 | + warn_on_fail: bool = False, |
| 41 | +) -> Generator[Tuple[str, torch.nn.Module]]: |
| 42 | + """ |
| 43 | + Yields names and modules which match `targets` but do not match `ignore`. |
| 44 | + Values are returned in order of `model.named_modules()` |
| 45 | +
|
| 46 | + :param model: model containing submodules to match against |
| 47 | + :param targets: target strings, potentially containing "re:" prefixes |
| 48 | + :param ignore: targets to ignore, potentially containing "re:" prefixes |
| 49 | + :param warn_on_fail: if True, warns if any targets do not match any modules in model |
| 50 | + :return: generator of module names and modules |
| 51 | + """ |
| 52 | + unmatched_targets = set(targets) |
| 53 | + for name, module in model.named_modules(): |
| 54 | + for target in targets: |
| 55 | + if is_match(name, module, target): |
| 56 | + unmatched_targets -= {target} |
| 57 | + |
| 58 | + if not any(is_match(name, module, ign) for ign in ignore): |
| 59 | + yield name, module |
| 60 | + |
| 61 | + if warn_on_fail: |
| 62 | + for target in unmatched_targets: |
| 63 | + _LOGGER.warning( |
| 64 | + f"Could not match `{target}` in instance of {model.__class__.__name__}" |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +def match_named_parameters( |
| 69 | + model: torch.nn.Module, |
| 70 | + targets: Iterable[str], |
| 71 | + ignore: Iterable[str] = tuple(), |
| 72 | + warn_on_fail: bool = False, |
| 73 | +) -> Generator[Tuple[str, torch.nn.Module, torch.nn.Parameter]]: |
| 74 | + """ |
| 75 | + Yields parameters which match `targets` but do not match `ignore`. |
| 76 | + Values are returned in order of `model.named_modules()` |
| 77 | +
|
| 78 | + :param model: model containing params to match against |
| 79 | + :param targets: target strings, potentially containing "re:" prefixes |
| 80 | + :param ignore: targets to ignore, potentially containing "re:" prefixes |
| 81 | + :param warn_on_fail: if True, warns if any targets do not match any params in model |
| 82 | + :return: generator of fully-qualified param names, parent modules, and params |
| 83 | + """ |
| 84 | + unmatched_targets = set(targets) |
| 85 | + for module_name, module in model.named_modules(): |
| 86 | + for param_name, param in module.named_parameters(recurse=False): |
| 87 | + param_fqn = f"{module_name}.{param_name}" |
| 88 | + for target in targets: |
| 89 | + if match_name(param_fqn, target): |
| 90 | + unmatched_targets -= {target} |
| 91 | + |
| 92 | + if not any(match_name(param_fqn, ign) for ign in ignore): |
| 93 | + yield param_fqn, module, param |
| 94 | + |
| 95 | + if warn_on_fail: |
| 96 | + for target in unmatched_targets: |
| 97 | + _LOGGER.warning( |
| 98 | + f"Could not match `{target}` in instance of {model.__class__.__name__}" |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +def match_modules_set( |
| 103 | + model: torch.nn.Module, |
| 104 | + targets: Iterable[str], |
| 105 | + ignore: Iterable[str] = tuple(), |
| 106 | +) -> Generator[Iterable[torch.nn.Module]]: |
| 107 | + """ |
| 108 | + Yields modules grouped with the same order and size as `targets`. |
| 109 | + Values are returned in order of `model.named_modules()` |
| 110 | +
|
| 111 | + For example, the following targets would yield module belonging to the following layers: |
| 112 | + ```python3 |
| 113 | + match_modules_set(model, ["q_proj", "k_proj", "v_proj"]) == ( |
| 114 | + ( |
| 115 | + `model.layers.0.self_attn.q_proj`, |
| 116 | + `model.layers.0.self_attn.k_proj`, |
| 117 | + `model.layers.0.self_attn.v_proj`, |
| 118 | + ), |
| 119 | + ( |
| 120 | + `model.layers.1.self_attn.q_proj`, |
| 121 | + `model.layers.1.self_attn.k_proj`, |
| 122 | + `model.layers.1.self_attn.v_proj`, |
| 123 | + ), |
| 124 | + ... |
| 125 | + ( |
| 126 | + `model.layers.32.self_attn.q_proj`, |
| 127 | + `model.layers.32.self_attn.k_proj`, |
| 128 | + `model.layers.32.self_attn.v_proj`, |
| 129 | + ), |
| 130 | + ) |
| 131 | + ``` |
| 132 | +
|
| 133 | + This can be used to match layers to their corresponding downstream counterparts. |
| 134 | + For example, matching layer norms to their subsequent linear layers |
| 135 | + ```python3 |
| 136 | + for norm, q, k, v in match_modules_set(model, (norm_tgt, q_tgt, k_tgt, v_tgt)): |
| 137 | + fuse_norm_linears(norm, [q, k, v]) |
| 138 | +
|
| 139 | + :param model: model containing modules to match against |
| 140 | + :param targets: target strings, potentially containing "re:" prefixes |
| 141 | + :param ignore: targets to ignore, potentially containing "re:" prefixes |
| 142 | + """ |
| 143 | + matches = dict.fromkeys(targets, None) |
| 144 | + for name, module in model.named_modules(): |
| 145 | + # match until we get a full set |
| 146 | + for target in targets: |
| 147 | + if is_match(name, module, target) and not any( |
| 148 | + is_match(name, module, ign) for ign in ignore |
| 149 | + ): |
| 150 | + if matches[target] is not None: |
| 151 | + raise ValueError(f"Matched a {target} twice before completing set") |
| 152 | + matches[target] = module |
| 153 | + |
| 154 | + # once we have a full set, yield and reset |
| 155 | + if targets and all((matches[target] is not None for target in targets)): |
| 156 | + yield [matches[target] for target in targets] # ensure correct ordering |
| 157 | + matches = dict.fromkeys(targets, None) |
| 158 | + |
| 159 | + # check that none are left over |
| 160 | + unmatched_keys = [match for match, value in matches.items() if value is not None] |
| 161 | + if len(unmatched_keys): |
| 162 | + raise ValueError(f"Unable to match targets into set: {unmatched_keys}") |
| 163 | + |
| 164 | + |
| 165 | +def is_match(name: str, module: torch.nn.Module, target: str) -> bool: |
| 166 | + """ |
| 167 | + Returns true if either module name or module parent classes match against target |
| 168 | + """ |
| 169 | + return match_name(name, target) or match_class(module, target) |
| 170 | + |
| 171 | + |
| 172 | +def match_name(name: str, target: str) -> bool: |
| 173 | + """ |
| 174 | + Returns true if target string begins with "re:" and |
| 175 | + regex matches or if target string exactly matches name |
| 176 | + """ |
| 177 | + if target.startswith("re:"): |
| 178 | + return re.match(target.removeprefix("re:"), name) is not None |
| 179 | + else: |
| 180 | + return target == name |
| 181 | + |
| 182 | + |
| 183 | +def match_class(module: torch.nn.Module, target: str) -> bool: |
| 184 | + """ |
| 185 | + Returns true if any torch parent class names match the target string exactly |
| 186 | + """ |
| 187 | + # will never match against a regex pattern since `:` is not allowed in class names |
| 188 | + return any( |
| 189 | + issubclass(cls, torch.nn.Module) and cls.__name__ == target |
| 190 | + for cls in module.__class__.__mro__ |
| 191 | + ) |
0 commit comments