|
9 | 9 | # Helper functions for tranforming the model to be able to run SpinQuant.
|
10 | 10 | # See https://github.com/facebookresearch/SpinQuant for more details about SpinQuant.
|
11 | 11 |
|
| 12 | +from typing import Any |
| 13 | + |
12 | 14 | import torch
|
13 | 15 |
|
14 | 16 | import torch.nn.functional as F
|
15 | 17 |
|
16 | 18 | from executorch.examples.models.llama2.llama_transformer import FeedForward
|
17 | 19 | from torch import nn
|
| 20 | +from torchao.quantization.GPTQ import _check_linear_int4_k, Int8DynActInt4WeightLinear |
| 21 | +from torchao.quantization.quant_api import _replace_with_custom_fn_if_matches_filter |
18 | 22 |
|
19 | 23 |
|
20 | 24 | def _inject_fast_hadamard_transform_cuda_for_spin_quant(module: torch.nn.Module):
|
@@ -53,3 +57,92 @@ def inject_fast_hadamard_transform_cuda_for_spin_quant(
|
53 | 57 | ) -> torch.nn.Module:
|
54 | 58 | _inject_fast_hadamard_transform_cuda_for_spin_quant(module)
|
55 | 59 | return module
|
| 60 | + |
| 61 | + |
| 62 | +def _replace_linear_with_linear_8da4w_for_spin_quant( |
| 63 | + module: torch.nn.Module, |
| 64 | + checkpoint: Any, |
| 65 | + group_size: int, |
| 66 | + precision: torch.dtype, |
| 67 | + scales_precision: torch.dtype, |
| 68 | +): |
| 69 | + def filter_fn(child: torch.nn.Module, cur_fqn: str) -> bool: |
| 70 | + # Only replace linear layers where the checkpoint contains explicit scales |
| 71 | + scales_key = f"{cur_fqn}.scale" |
| 72 | + if isinstance(child, nn.Linear) and scales_key in checkpoint: |
| 73 | + assert _check_linear_int4_k(child.in_features, group_size) |
| 74 | + assert checkpoint[f"{cur_fqn}.weight"].dtype == torch.int8 |
| 75 | + assert checkpoint[scales_key].dtype == scales_precision |
| 76 | + return True |
| 77 | + return False |
| 78 | + |
| 79 | + def replacement_fn(child: torch.nn.Module) -> torch.nn.Module: |
| 80 | + new_linear = Int8DynActInt4WeightLinear( |
| 81 | + child.in_features, |
| 82 | + child.out_features, |
| 83 | + bias=False, |
| 84 | + device=child.weight.device, |
| 85 | + groupsize=group_size, |
| 86 | + precision=precision, |
| 87 | + scales_precision=scales_precision, |
| 88 | + ) |
| 89 | + return new_linear |
| 90 | + |
| 91 | + _replace_with_custom_fn_if_matches_filter(module, replacement_fn, filter_fn) |
| 92 | + |
| 93 | + |
| 94 | +def transform_for_spinquant( |
| 95 | + module: torch.nn.Module, |
| 96 | + checkpoint: Any, |
| 97 | + group_size: int, |
| 98 | + quantization_mode: str, |
| 99 | + dtype: torch.dtype, |
| 100 | +) -> torch.nn.Module: |
| 101 | + """ |
| 102 | + Transform the model to be able to load SpinQuant checkpoints that |
| 103 | + are quantized with the given group size and quantization mode. |
| 104 | + """ |
| 105 | + |
| 106 | + if group_size not in [32, 64, 128, 256]: |
| 107 | + raise ValueError(f"Group size {group_size} is not supported for SpinQuant.") |
| 108 | + if quantization_mode not in ["8da4w"]: |
| 109 | + raise ValueError( |
| 110 | + f"Quantization mode {quantization_mode} is not compatible with SpinQuant." |
| 111 | + ) |
| 112 | + _replace_linear_with_linear_8da4w_for_spin_quant( |
| 113 | + module, |
| 114 | + checkpoint, |
| 115 | + group_size, |
| 116 | + dtype, |
| 117 | + dtype, |
| 118 | + ) |
| 119 | + return module |
| 120 | + |
| 121 | + |
| 122 | +def sanitize_checkpoint_from_spinquant( |
| 123 | + checkpoint: Any, |
| 124 | + group_size: int, |
| 125 | +): |
| 126 | + """ |
| 127 | + Sanitize the SpinQuant checkpoint. |
| 128 | + - Renames 'scale' to 'scales' |
| 129 | + - Groups scales |
| 130 | + - Removes 'o_weight' |
| 131 | + - Converts all tensors to contiguous format |
| 132 | + """ |
| 133 | + keys_to_rename = [] |
| 134 | + keys_to_remove = [] |
| 135 | + for k, _ in checkpoint.items(): |
| 136 | + if k.endswith(".scale"): |
| 137 | + new_key = k + "s" |
| 138 | + keys_to_rename.append((k, new_key)) |
| 139 | + if k.endswith(".o_weight"): |
| 140 | + keys_to_remove.append(k) |
| 141 | + |
| 142 | + for old_key, new_key in keys_to_rename: |
| 143 | + old_val = checkpoint.pop(old_key) |
| 144 | + checkpoint[new_key] = old_val if group_size == -1 else old_val[:, ::group_size] |
| 145 | + for k in keys_to_remove: |
| 146 | + checkpoint.pop(k) |
| 147 | + for k, v in checkpoint.items(): |
| 148 | + checkpoint[k] = v.contiguous() |
0 commit comments