|
| 1 | +# Copyright The FMS Model Optimizer Authors |
| 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, software |
| 10 | +# 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 | +# This code is based on QuaRot(https://github.com/spcl/QuaRot/tree/main/quarot). |
| 16 | +# Licensed under Apache License 2.0. |
| 17 | +# Adapted from https://github.com/Cornell-RelaxML/quip-sharp/blob/main/lib/utils/matmul_had.py |
| 18 | +# and https://github.com/facebookresearch/SpinQuant/blob/main/utils/hadamard_utils.py |
| 19 | +""" |
| 20 | +Change original "text tensor implementation" into binaries for better efficiency. Only has 12 |
| 21 | +sizes available in the safetensors file. [12, 20, 28, 36, 40, 44, 52, 60, 108, 140, 156, 172] |
| 22 | +""" |
| 23 | + |
| 24 | +# Third Party |
| 25 | +from fast_hadamard_transform import hadamard_transform |
| 26 | +from safetensors import safe_open |
| 27 | +import torch |
| 28 | + |
| 29 | + |
| 30 | +class HadamardTransform(torch.autograd.Function): |
| 31 | + """The unnormalized Hadamard transform (i.e. without dividing by sqrt(2))""" |
| 32 | + |
| 33 | + # TODO seems redundant, insdie hadamard_transform(), backward is already handled...? |
| 34 | + @staticmethod |
| 35 | + def forward(ctx, u): |
| 36 | + return hadamard_transform(u) |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def backward(ctx, grad): |
| 40 | + return hadamard_transform(grad) |
| 41 | + |
| 42 | + |
| 43 | +def get_hadK(n, transpose=False): |
| 44 | + """Simplify the implementation and use binary tensors instead of text implementation.""" |
| 45 | + for K in [172, 156, 140, 108, 60, 52, 44, 40, 36, 28, 20, 12]: |
| 46 | + if n % K == 0 and is_pow2(n // K): |
| 47 | + with safe_open("hadk.safetensors", framework="pt") as f: |
| 48 | + assert ( |
| 49 | + str(K) in f.keys() |
| 50 | + ), f"Special size Hadamard {K} does not exist in the file." |
| 51 | + hadK = f.get_tensor(str(K)) |
| 52 | + |
| 53 | + if transpose: |
| 54 | + hadK = hadK.T |
| 55 | + |
| 56 | + break |
| 57 | + |
| 58 | + if hadK is None: |
| 59 | + if is_pow2(n): |
| 60 | + K = 1 |
| 61 | + else: |
| 62 | + raise RuntimeError( |
| 63 | + f"{n} is not power of 2 or does not have a special size Hadamard available." |
| 64 | + ) |
| 65 | + |
| 66 | + return hadK, K |
| 67 | + |
| 68 | + |
| 69 | +def matmul_hadU(X, transpose=False): |
| 70 | + n = X.shape[-1] |
| 71 | + hadK, K = get_hadK(n, transpose) |
| 72 | + input = X.clone().view(-1, n, 1) |
| 73 | + output = input.clone() |
| 74 | + while input.shape[1] > K: |
| 75 | + input = input.view(input.shape[0], input.shape[1] // 2, 2, input.shape[2]) |
| 76 | + output = output.view(input.shape) |
| 77 | + output[:, :, 0, :] = input[:, :, 0, :] + input[:, :, 1, :] |
| 78 | + output[:, :, 1, :] = input[:, :, 0, :] - input[:, :, 1, :] |
| 79 | + output = output.view(input.shape[0], input.shape[1], -1) |
| 80 | + (input, output) = (output, input) |
| 81 | + del output |
| 82 | + |
| 83 | + if K > 1: |
| 84 | + # Do not explicitly repeat - OOM |
| 85 | + # input = torch.bmm( |
| 86 | + # hadK.repeat(len(input), 1, 1).to(input.device).to(input.dtype), input) |
| 87 | + # Use bcast instead |
| 88 | + input = hadK.view(1, K, K).to(input) @ input |
| 89 | + |
| 90 | + return input.view(X.shape) / torch.tensor(n).sqrt() |
| 91 | + |
| 92 | + |
| 93 | +def matmul_hadUt(X): |
| 94 | + return matmul_hadU(X, transpose=True) |
| 95 | + |
| 96 | + |
| 97 | +def random_hadamard_matrix(size, device): |
| 98 | + # See https://cornell-relaxml.github.io/quip-sharp/ , Section "Randomized Hadamard Transformation" |
| 99 | + Q = torch.randint(low=0, high=2, size=(size,)).to(torch.float64) |
| 100 | + Q = Q * 2 - 1 |
| 101 | + Q = torch.diag(Q) |
| 102 | + return matmul_hadU(Q).to(device) |
| 103 | + |
| 104 | + |
| 105 | +def hadamard_matrix(size, device): |
| 106 | + # See https://cornell-relaxml.github.io/quip-sharp/ , Section "Randomized Hadamard Transformation" |
| 107 | + Q = torch.eye(size) |
| 108 | + return matmul_hadU(Q).to(device) |
| 109 | + |
| 110 | + |
| 111 | +def matmul_hadU_cuda(X, hadK, K): |
| 112 | + n = X.shape[-1] |
| 113 | + if K == 1: |
| 114 | + return HadamardTransform.apply(X.contiguous()) / torch.tensor(n).sqrt() |
| 115 | + # if transpose: |
| 116 | + # hadK = hadK.T.contiguous() |
| 117 | + input = X.view(-1, K, n // K) |
| 118 | + input = HadamardTransform.apply(input.contiguous()) / torch.tensor(n).sqrt() |
| 119 | + input = hadK.to(input.device).to(input.dtype) @ input |
| 120 | + return input.reshape(X.shape) |
| 121 | + |
| 122 | + |
| 123 | +def matmul_hadUt_cuda(X, hadK, K): |
| 124 | + return matmul_hadU_cuda(X, hadK, K, transpose=True) |
| 125 | + |
| 126 | + |
| 127 | +def apply_exact_had_to_linear(module, had_dim=-1, output=False, R2=None): |
| 128 | + assert isinstance(module, torch.nn.Linear) |
| 129 | + in_features, out_features = module.in_features, module.out_features |
| 130 | + |
| 131 | + if had_dim != -1: |
| 132 | + assert is_pow2(had_dim), "Hadamard dimension must be a power of 2!" |
| 133 | + |
| 134 | + W_ = module.weight.data |
| 135 | + dtype = W_.dtype |
| 136 | + dev = W_.device |
| 137 | + init_shape = W_.shape |
| 138 | + W_ = W_.float().cuda() |
| 139 | + |
| 140 | + if had_dim == -1: |
| 141 | + if output: |
| 142 | + had_K, K = get_hadK(out_features) |
| 143 | + W_ = matmul_hadU_cuda(W_.t(), had_K, K).t() |
| 144 | + if not output: |
| 145 | + had_K, K = get_hadK(in_features) |
| 146 | + W_ = matmul_hadU_cuda(W_, had_K, K) |
| 147 | + else: |
| 148 | + hadK = hadamard_matrix(had_dim, "cuda").to(torch.float64) |
| 149 | + if R2 is not None: |
| 150 | + hadK = R2.to(torch.float64) |
| 151 | + if output: |
| 152 | + W_ = W_.t() |
| 153 | + transposed_shape = W_.shape |
| 154 | + temp = W_.reshape(-1, transposed_shape[-1] // had_dim, had_dim) |
| 155 | + temp = temp.to(torch.float64) @ hadK |
| 156 | + W_ = temp.reshape(transposed_shape).t() |
| 157 | + else: |
| 158 | + init_shape = W_.shape |
| 159 | + temp = W_.reshape(-1, init_shape[-1] // had_dim, had_dim) |
| 160 | + temp = temp.to(torch.float64) @ hadK |
| 161 | + W_ = temp.reshape(init_shape) |
| 162 | + module.weight.data = W_.to(device=dev, dtype=dtype) |
| 163 | + |
| 164 | + |
| 165 | +def is_pow2(n): |
| 166 | + return (n & (n - 1) == 0) and (n > 0) |
| 167 | + |
| 168 | + |
| 169 | +# hadamard matrices for had12, had36.pal2, had52,will, |
| 170 | +# # had60.pal, had108.pal, had140.pal, had156.will, had172.will: |
| 171 | +# http://www.neilsloane.com/hadamard/index.html |
0 commit comments