-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
compressed-tensors support for KV cache Quantization #103
base: upstream-a564d10af
Are you sure you want to change the base?
Conversation
@@ -16,6 +19,13 @@ | |||
if is_hqq_available(): | |||
from hqq.core.quantize import Quantizer as HQQQuantizer | |||
|
|||
if is_compressed_tensors_available() or True: # hack for now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will take care of this hack once the PR is in the "landable" state
self.key_cache.append(torch.zeros(0, dtype=key_states.dtype, device=key_states.device)) | ||
self.value_cache.append(torch.zeros(0, dtype=key_states.dtype, device=key_states.device)) | ||
keys_to_return, values_to_return = key_states, value_states | ||
else: | ||
dequant_key = self._dequantize(self._quantized_key_cache[layer_idx]) | ||
dequant_value = self._dequantize(self._quantized_value_cache[layer_idx]) | ||
dequant_key = self._dequantize(self._quantized_key_cache[layer_idx], cache_type="key") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of rewriting update()
method for CompressedTensorsCache
I decided to expand the interface.
|
||
@staticmethod | ||
def _establish_quant_dtype(num_bits: int, type_: str) -> torch.dtype: | ||
if num_bits == 8 and type_ == "int": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should potentially add more supported quantization types, to be discussed @bfineran
from transformers import AutoTokenizer, AutoModelForCausalLM
from transformers.cache_utils import CompressedTensorsQuantizedCacheConfig, CompressedTensorsCache
model_id = "/root/compressed-tensors/llama1.1b_new_quant_out" # quantized model with kv cache quantization enabled
tokenizer = AutoTokenizer.from_pretrained("Xenova/llama2.c-stories15M") # somehow the tokenizer is missing from the model in `model_id`
tokenizer.pad_token_id = tokenizer.eos_token_id
model = AutoModelForCausalLM.from_pretrained(model_id, attn_implementation="eager").to("cuda:0")
inputs = tokenizer("I like rock music because", return_tensors="pt").to(model.device)
cache_config = CompressedTensorsQuantizedCacheConfig.from_pretrained(model_id)
cache_object = CompressedTensorsCache(cache_config)
out = model(**inputs)
out_quant = model(**inputs, past_key_values=cache_object)
assert (out.logits == out_quant.logits).all() working fine! |
Feature description
Implements quantized kv cache support for the transformer models that have been quantized using
compressed-tensors
.Introduces:
CompressedTensorsQuantizedCacheConfig
- a config object that stores the static qparams for quantizing/dequantizing KV CacheQuantizedCache
interface, to make it more general in the future for other implementationsCompressedTensorsCache
- very lightweight wrapper aroundQuantizedCache
, that allows us to enable kv cache quantizationManual testing:
Note
Compatible with this branch of compressed-tensors: neuralmagic/compressed-tensors#86
Pending missing items: Add tests, add
compressed-tensors
as transformers dependency, fix ugly import issues (circular imports between transformers and compressed-tensors)