Replace RMSNorm modules. Since there is no standard nn.RMSNorm, we look for common class names and attributes.
(self, repl: KernelReplacement)
| 606 | return count |
| 607 | |
| 608 | def _replace_rmsnorm_modules(self, repl: KernelReplacement) -> int: |
| 609 | """ |
| 610 | Replace RMSNorm modules. Since there is no standard nn.RMSNorm, |
| 611 | we look for common class names and attributes. |
| 612 | """ |
| 613 | count = 0 |
| 614 | rmsnorm_names = {"RMSNorm", "LlamaRMSNorm", "T5LayerNorm", "GemmaRMSNorm"} |
| 615 | |
| 616 | for name, module in list(self.model.named_modules()): |
| 617 | cls_name = type(module).__name__ |
| 618 | # Match by class name or by having 'weight' but no 'bias' and a norm-like name |
| 619 | is_rmsnorm = ( |
| 620 | cls_name in rmsnorm_names |
| 621 | or (hasattr(module, "weight") |
| 622 | and hasattr(module, "eps") |
| 623 | and not hasattr(module, "bias") |
| 624 | and cls_name.lower().endswith("norm") |
| 625 | and not isinstance(module, nn.LayerNorm)) |
| 626 | ) |
| 627 | |
| 628 | if is_rmsnorm: |
| 629 | self._original_modules[name] = module |
| 630 | wrapper = _RMSNormWrapper(module, repl.module_fn) |
| 631 | parts = name.split(".") |
| 632 | parent = self.model |
| 633 | for p in parts[:-1]: |
| 634 | parent = getattr(parent, p) |
| 635 | setattr(parent, parts[-1], wrapper) |
| 636 | count += 1 |
| 637 | return count |
| 638 | |
| 639 | @property |
| 640 | def applied_summary(self) -> List[str]: |
no test coverage detected