|
| 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 inspect |
| 16 | +from typing import Callable, Optional, Tuple |
| 17 | +from weakref import ref |
| 18 | + |
| 19 | +from compressed_tensors.quantization import QuantizationStrategy, forward_quantize |
| 20 | +from compressed_tensors.quantization.lifecycle.initialize import ( |
| 21 | + _initialize_scale_zero_point, |
| 22 | +) |
| 23 | +from compressed_tensors.utils import getattr_chain |
| 24 | +from compressed_tensors.utils.internal import InternalModule |
| 25 | +from torch import Tensor |
| 26 | +from torch.nn import Module |
| 27 | +from torch.utils.hooks import RemovableHandle |
| 28 | +from transformers import Cache, PretrainedConfig, PreTrainedModel |
| 29 | + |
| 30 | + |
| 31 | +__all__ = [ |
| 32 | + "QuantizedKVCache", |
| 33 | + "initialize_hooked_kv_cache", |
| 34 | + "register_key_hook", |
| 35 | + "register_value_hook", |
| 36 | +] |
| 37 | + |
| 38 | + |
| 39 | +KV_CACHE_ATTR = "kv_cache" |
| 40 | + |
| 41 | + |
| 42 | +class QuantizedKVCache(InternalModule): |
| 43 | + """ |
| 44 | + QuantizedKVCache module which wraps the functionality of any existing kvcache args. |
| 45 | + Unlike transform Cache instances, this cache is a `torch.nn.Module` which can be |
| 46 | + hooked to trigger transforms and calibration hooks. |
| 47 | +
|
| 48 | + This module works by being registered as a submodule to attention modules via |
| 49 | + `initialize_hooked_kv_cache`, then adding a hook which replaces `past_key_values` |
| 50 | + kwargs with this module. This module adopts the functionality of the replaced cache, |
| 51 | + preserving caching functionality such as sliding window attention, ect. |
| 52 | +
|
| 53 | + :param attn_module: parent attention module |
| 54 | + """ |
| 55 | + |
| 56 | + def __init__(self, config: PretrainedConfig, attn_module: Module): |
| 57 | + super().__init__() |
| 58 | + self.config = config |
| 59 | + self.attn_module = ref(attn_module) # avoid circular reference |
| 60 | + self.past_key_values: Optional[Cache] = None |
| 61 | + self._qparams_initialized = False |
| 62 | + |
| 63 | + def update(self, *args, **kwargs) -> Tuple[Tensor, Tensor]: |
| 64 | + return self(*args, **kwargs) |
| 65 | + |
| 66 | + def forward( |
| 67 | + self, |
| 68 | + key_states: Tensor, |
| 69 | + value_states: Tensor, |
| 70 | + *args, |
| 71 | + **kwargs, |
| 72 | + ) -> Tuple[Tensor, Tensor]: |
| 73 | + # quantization |
| 74 | + module = self.attn_module() |
| 75 | + quant_args_attr = "quantization_scheme.input_activations" |
| 76 | + quant_args = getattr_chain(module, quant_args_attr, None) |
| 77 | + quant_enabled = getattr(module, "quantization_enabled", True) |
| 78 | + if quant_args is not None and quant_enabled and self._qparams_initialized: |
| 79 | + key_states = forward_quantize(module, key_states, "k", quant_args) |
| 80 | + value_states = forward_quantize(module, value_states, "v", quant_args) |
| 81 | + |
| 82 | + # original cache |
| 83 | + if self.past_key_values is not None: |
| 84 | + ret = self.past_key_values.update(key_states, value_states, *args, **kwargs) |
| 85 | + else: |
| 86 | + ret = (key_states, value_states) |
| 87 | + |
| 88 | + self.past_key_values = None |
| 89 | + return ret |
| 90 | + |
| 91 | + def initialize_qparams_once(self, model: PreTrainedModel, module: Module): |
| 92 | + """ |
| 93 | + Initialize kv cache quantization parameters if they have not already been |
| 94 | + initialized |
| 95 | +
|
| 96 | + :param model: parent model of attention module |
| 97 | + :param module: attention module to initialize with |
| 98 | + """ |
| 99 | + # TODO: move to initialize.py |
| 100 | + assert module is self.attn_module() |
| 101 | + scheme = getattr(module, "quantization_scheme", None) |
| 102 | + quant_args = getattr(scheme, "input_activations", None) |
| 103 | + |
| 104 | + if not self._qparams_initialized and quant_args is not None: |
| 105 | + assert quant_args.strategy == QuantizationStrategy.TENSOR |
| 106 | + _initialize_scale_zero_point(module, "k", quant_args) |
| 107 | + _initialize_scale_zero_point(module, "v", quant_args) |
| 108 | + self._qparams_initialized = True |
| 109 | + |
| 110 | + |
| 111 | +# ----- initialize ----- # |
| 112 | + |
| 113 | + |
| 114 | +def _kv_cache_attention_hook(module: Module, args, kwargs): |
| 115 | + kv_cache: QuantizedKVCache = getattr(module, KV_CACHE_ATTR) |
| 116 | + _past_kv_name = ( |
| 117 | + "past_key_values" # transformers#39956 |
| 118 | + if "past_key_values" in inspect.signature(module.forward).parameters |
| 119 | + else "past_key_value" |
| 120 | + ) |
| 121 | + kv_cache.past_key_values = kwargs.get(_past_kv_name, None) |
| 122 | + kwargs[_past_kv_name] = kv_cache |
| 123 | + |
| 124 | + return args, kwargs |
| 125 | + |
| 126 | + |
| 127 | +def initialize_hooked_kv_cache( |
| 128 | + model: PreTrainedModel, module: Module, quantize: bool = False |
| 129 | +): |
| 130 | + """ |
| 131 | + Initialize a `QuantizedKVCache` instance attached to attention |
| 132 | +
|
| 133 | + :param model: parent model of attention module |
| 134 | + :param module: attention module to initialize with |
| 135 | + :param quantize: initialize kv cache quantization parameters |
| 136 | + """ |
| 137 | + if not hasattr(module, KV_CACHE_ATTR): |
| 138 | + module.register_module(KV_CACHE_ATTR, QuantizedKVCache(model.config, module)) |
| 139 | + module.register_forward_pre_hook(_kv_cache_attention_hook, with_kwargs=True) |
| 140 | + |
| 141 | + kv_cache: QuantizedKVCache = getattr(module, KV_CACHE_ATTR) |
| 142 | + if quantize: |
| 143 | + kv_cache.initialize_qparams_once(model, module) |
| 144 | + |
| 145 | + |
| 146 | +# ----- hooks ----- # |
| 147 | + |
| 148 | + |
| 149 | +def register_key_hook( |
| 150 | + module: Module, hook: Callable[[Module, Tensor], Optional[Tensor]] |
| 151 | +) -> RemovableHandle: |
| 152 | + """ |
| 153 | + Register a hook which takes post-rope key states as an argument and |
| 154 | + returns the modified key states or `None` |
| 155 | +
|
| 156 | + :param module: attention module to add hook to |
| 157 | + :param hook: key hook function |
| 158 | + """ |
| 159 | + kv_cache: QuantizedKVCache = getattr(module, KV_CACHE_ATTR) |
| 160 | + |
| 161 | + def _hook(cache: QuantizedKVCache, args, kwargs): |
| 162 | + bound = inspect.signature(cache.forward).bind(*args, **kwargs) |
| 163 | + value = hook(module, bound.arguments["key_states"]) |
| 164 | + if value is not None: |
| 165 | + bound.arguments["key_states"] = value |
| 166 | + |
| 167 | + return bound.args, bound.kwargs |
| 168 | + |
| 169 | + return kv_cache.register_forward_pre_hook(_hook, with_kwargs=True) |
| 170 | + |
| 171 | + |
| 172 | +def register_value_hook( |
| 173 | + module: Module, hook: Callable[[Module, Tensor], Optional[Tensor]] |
| 174 | +) -> RemovableHandle: |
| 175 | + """ |
| 176 | + Register a hook which takes value states as an argument and |
| 177 | + returns the modified value states or `None` |
| 178 | +
|
| 179 | + :param module: attention module to add hook to |
| 180 | + :param hook: value hook function |
| 181 | + """ |
| 182 | + kv_cache: QuantizedKVCache = getattr(module, KV_CACHE_ATTR) |
| 183 | + |
| 184 | + def _hook(cache: QuantizedKVCache, args, kwargs): |
| 185 | + bound = inspect.signature(cache.forward).bind(*args, **kwargs) |
| 186 | + value = hook(module, bound.arguments["value_states"]) |
| 187 | + if value is not None: |
| 188 | + bound.arguments["value_states"] = value |
| 189 | + |
| 190 | + return bound.args, bound.kwargs |
| 191 | + |
| 192 | + return kv_cache.register_forward_pre_hook(_hook, with_kwargs=True) |
0 commit comments