|
| 1 | +from typing import Iterable |
| 2 | + |
| 3 | +import torch |
| 4 | +from compressed_tensors import ( |
| 5 | + align_module_device, |
| 6 | + get_execution_device, |
| 7 | + update_offload_parameter, |
| 8 | +) |
| 9 | + |
| 10 | +__all__ = ["center_embeddings", "fuse_norm_linears"] |
| 11 | + |
| 12 | + |
| 13 | +PRECISION = torch.float64 |
| 14 | + |
| 15 | + |
| 16 | +def center_embeddings(embedding: torch.nn.Module): |
| 17 | + """ |
| 18 | + Shift each embedding to have a mean of zero |
| 19 | +
|
| 20 | + :param embedding: embedding module containing embeddings to center |
| 21 | + """ |
| 22 | + if not hasattr(embedding, "weight"): |
| 23 | + raise ValueError(f"Cannot fuse norm of type {type(embedding)}") |
| 24 | + |
| 25 | + with align_module_device(embedding): |
| 26 | + weight_dtype = embedding.weight.dtype |
| 27 | + weight = embedding.weight.to(PRECISION) |
| 28 | + new_weight = weight - weight.mean(dim=-1, keepdim=True) |
| 29 | + new_weight = new_weight.to(weight_dtype) |
| 30 | + |
| 31 | + update_offload_parameter(embedding, "weight", new_weight) |
| 32 | + |
| 33 | + |
| 34 | +def fuse_norm_linears(norm: torch.nn.Module, linears: Iterable[torch.nn.Linear]): |
| 35 | + """ |
| 36 | + Fuse the scaling operation of norm layer into subsequent linear layers. |
| 37 | + This useful for ensuring transform invariance between norm and linear layers. |
| 38 | +
|
| 39 | + Note that unitary transforms (rotation) commute with normalization, but not scaling |
| 40 | +
|
| 41 | + :param norm: norm layer whose weight will be fused into subsequent linears |
| 42 | + :param linears: linear layers which directly follow the norm layer |
| 43 | + """ |
| 44 | + if not hasattr(norm, "weight"): |
| 45 | + raise ValueError(f"Cannot fuse norm of type {type(norm)}") |
| 46 | + |
| 47 | + for linear in linears: |
| 48 | + # NOTE: spinquant does this op in float64 |
| 49 | + exec_device = get_execution_device(norm) |
| 50 | + with align_module_device(norm, exec_device), align_module_device( |
| 51 | + linear, exec_device |
| 52 | + ): |
| 53 | + weight_dtype = linear.weight.dtype |
| 54 | + new_weight = linear.weight.to(PRECISION) * norm.weight.to(PRECISION) |
| 55 | + new_weight = new_weight.to(weight_dtype) |
| 56 | + |
| 57 | + update_offload_parameter(linear, "weight", new_weight) |
| 58 | + |
| 59 | + new_norm_weight = torch.ones_like(norm.weight, device="cpu") |
| 60 | + update_offload_parameter(norm, "weight", new_norm_weight) |
0 commit comments