|
| 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 torch |
| 16 | +from compressed_tensors.modeling import ( |
| 17 | + IMPL_ATTR, |
| 18 | + KV_CACHE_ATTR, |
| 19 | + QuantizedAttentionImpl, |
| 20 | + QuantizedKVCache, |
| 21 | + initialize_hooked_attention, |
| 22 | + initialize_hooked_kv_cache, |
| 23 | + register_key_hook, |
| 24 | + register_query_hook, |
| 25 | + register_value_hook, |
| 26 | +) |
| 27 | +from tests.testing_utils import requires_gpu |
| 28 | +from transformers import AutoModelForCausalLM |
| 29 | + |
| 30 | + |
| 31 | +@requires_gpu |
| 32 | +def test_attention_cache(): |
| 33 | + model = AutoModelForCausalLM.from_pretrained( |
| 34 | + "nm-testing/llama2.c-stories15M", device_map="cuda" |
| 35 | + ) |
| 36 | + inputs = {key: value.to("cuda") for key, value in model.dummy_inputs.items()} |
| 37 | + true_outputs = model(**inputs) |
| 38 | + layers = model.model.layers |
| 39 | + |
| 40 | + # check if hooks work |
| 41 | + k_called = [False for _ in range(len(layers))] |
| 42 | + v_called = [False for _ in range(len(layers))] |
| 43 | + |
| 44 | + # apply kv cache quantization |
| 45 | + _apply_kv_cache(model, layers, k_called, v_called) |
| 46 | + |
| 47 | + # check kv cache quantization |
| 48 | + outputs = model(**inputs) |
| 49 | + assert torch.equal(outputs.logits, true_outputs.logits) |
| 50 | + assert all(k_called) and all(v_called) |
| 51 | + |
| 52 | + ## apply attention quantization after kv cache quantization ## |
| 53 | + |
| 54 | + # check if hooks work |
| 55 | + q_called = [False for _ in range(len(layers))] |
| 56 | + k_called = [False for _ in range(len(layers))] |
| 57 | + v_called = [False for _ in range(len(layers))] |
| 58 | + |
| 59 | + _apply_attention(model, layers, q_called, k_called, v_called) |
| 60 | + outputs = model(**inputs) |
| 61 | + assert torch.equal(outputs.logits, true_outputs.logits) |
| 62 | + assert all(q_called) and all(k_called) and all(v_called) |
| 63 | + |
| 64 | + |
| 65 | +def _apply_kv_cache(model, layers, k_called, v_called): |
| 66 | + for layer_index, layer in enumerate(layers): |
| 67 | + module = layer.self_attn |
| 68 | + initialize_hooked_kv_cache(model, module) |
| 69 | + assert isinstance(getattr(module, KV_CACHE_ATTR), QuantizedKVCache) |
| 70 | + |
| 71 | + # reapply is no-op |
| 72 | + initialize_hooked_kv_cache(model, module) |
| 73 | + |
| 74 | + def k_hook(_module, _states, layer_index=layer_index): # NOTE: capture by value |
| 75 | + k_called[layer_index] = True |
| 76 | + |
| 77 | + def v_hook(_module, _states, layer_index=layer_index): |
| 78 | + my_index = layer_index |
| 79 | + v_called[my_index] = True |
| 80 | + |
| 81 | + register_key_hook(module, k_hook) |
| 82 | + register_value_hook(module, v_hook) |
| 83 | + |
| 84 | + |
| 85 | +def _apply_attention(model, layers, q_called, k_called, v_called): |
| 86 | + for layer_index, layer in enumerate(layers): |
| 87 | + module = layer.self_attn |
| 88 | + initialize_hooked_attention(model, module) |
| 89 | + assert isinstance(getattr(module, IMPL_ATTR), QuantizedAttentionImpl) |
| 90 | + |
| 91 | + # reapply is no-op |
| 92 | + initialize_hooked_attention(model, module) |
| 93 | + |
| 94 | + def q_hook(_module, _states, layer_index=layer_index): |
| 95 | + q_called[layer_index] = True |
| 96 | + |
| 97 | + def k_hook(_module, _states, layer_index=layer_index): |
| 98 | + k_called[layer_index] = True |
| 99 | + |
| 100 | + def v_hook(_module, _states, layer_index=layer_index): |
| 101 | + v_called[layer_index] = True |
| 102 | + |
| 103 | + register_query_hook(module, q_hook) |
| 104 | + register_key_hook(module, k_hook) |
| 105 | + register_value_hook(module, v_hook) |
0 commit comments