def quantize_and_save(past_key_values, save_path="cache_nf4.pth"):
quantized_past_key_values = []
for k, v in past_key_values:
# Convert tensors to float16 for quantization
k, v = k.to(torch.float16), v.to(torch.float16)
code
# Perform 4-bit quantization using NF4
k_quant, k_absmax = bnb.functional.quantize_4bit(k, quant_type="nf4")
v_quant, v_absmax = bnb.functional.quantize_4bit(v, quant_type="nf4")
# Extract float values from the scaling factors
k_absmax_value = k_absmax.item() # Directly extract the float value
v_absmax_value = v_absmax.item() # Directly extract the float value
# Append quantized tensors and their corresponding absmax values
quantized_past_key_values.append(((k_quant, k_absmax_value), (v_quant, v_absmax_value)))
# Save the quantized data to disk
torch.save(quantized_past_key_values, save_path)
print("Cache saved successfully with NF4 quantization and absmax.")
past_key_values = DynamicCache()
if loaded_past_key_values:
for layer_idx, ((k_quant, k_absmax), (v_quant, v_absmax)) in enumerate(loaded_past_key_values):
# Convert absmax values back to tensors
k_absmax_tensor = torch.tensor(k_absmax, dtype=torch.float32, device=device)
v_absmax_tensor = torch.tensor(v_absmax, dtype=torch.float32, device=device)
# Define output shapes based on quantized tensor dimensions
key_out_shape = (k_quant.shape[0] * 8, k_quant.shape[1])
value_out_shape = (v_quant.shape[0] * 8, v_quant.shape[1])
# Dequantize the key and value tensors
key_states = bnb.functional.dequantize_4bit(
k_quant,
absmax=k_absmax_tensor,
out=torch.empty(key_out_shape, dtype=torch.float16, device=device),
quant_type="nf4"
)
value_states = bnb.functional.dequantize_4bit(
v_quant,
absmax=v_absmax_tensor,
out=torch.empty(value_out_shape, dtype=torch.float16, device=device),
quant_type="nf4"
)
# Update the DynamicCache with the dequantized tensors
past_key_values.update(key_states, value_states, layer_idx)
print("DynamicCache populated with dequantized float16 past_key_values.")
return past_key_values
Assuming past_key_values is already defined and populated somewhere in your code
quantize_and_save(past_key_values)
Load the quantized cache
past_key_values = load_and_dequantize()
NotImplementedError Traceback (most recent call last)
Cell In[13], line 71
68 return past_key_values
70 # Assuming past_key_values is already defined and populated somewhere in your code
---> 71 quantize_and_save(past_key_values)
73 # Load the quantized cache
74 past_key_values = load_and_dequantize()
Cell In[13], line 15, in quantize_and_save(past_key_values, save_path)
12 k, v = k.to(torch.float16), v.to(torch.float16)
14 # Perform 4-bit quantization using NF4
---> 15 k_quant, k_absmax = bnb.functional.quantize_4bit(k, quant_type="nf4")
16 v_quant, v_absmax = bnb.functional.quantize_4bit(v, quant_type="nf4")
18 # Extract float values from the scaling factors
File /opt/conda/lib/python3.10/site-packages/bitsandbytes/functional.py:1182, in quantize_4bit(A, absmax, out, blocksize, compress_statistics, quant_type, quant_storage)
1156 """
1157 Quantize tensor A in blocks of 4-bit values.
1158
(...)
1179 The quantization state to undo the quantization.
1180 """
1181 if A.device.type != "cuda":
-> 1182 raise NotImplementedError(f"Device type not supported for FP4 quantization: {A.device.type}")
1183 if quant_type not in ["fp4", "nf4"]:
1184 raise NotImplementedError(f"4-bit quantization data type {quant_type} is not implemented.")
NotImplementedError: Device type not supported for FP4 quantization: cpu
why it's using cpu i need to direct passs in gpu
The error you’re encountering:
code
NotImplementedError: Device type not supported for FP4 quantization: cpu
indicates that the bitsandbytes (bnb) library's 4-bit quantization (quantize_4bit) function does not support CPU tensors. It requires tensors to be on a GPU (CUDA) device. To resolve this, you need to ensure that all tensors involved in the quantization process are explicitly moved to a CUDA device before performing any quantization operations.
Here’s how you can modify your code to ensure that tensors are on the GPU:
Check for CUDA Availability:
First, verify that a CUDA-enabled GPU is available. If it's not, you should handle the fallback or raise an appropriate error.
Move Tensors to GPU Before Quantization:
Make sure that k and v tensors are moved to the GPU before calling quantize_4bit.
Consistency in Device Usage:
Ensure that both quantization and dequantization processes use the same device to avoid device mismatch errors.
Below is the revised version of your script with these adjustments:
python
import torch
import bitsandbytes as bnb
from transformers.cache_utils import DynamicCache
# Ensure that DynamicCache and set are safe during serialization
torch.serialization.add_safe_globals([DynamicCache, set])
def quantize_and_save(past_key_values, save_path="cache_nf4.pth"):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if device.type != "cuda":
raise RuntimeError("CUDA device not available. 4-bit quantization requires a GPU.")
quantized_past_key_values = []
for layer_idx, (k, v) in enumerate(past_key_values):
# Move tensors to GPU and convert to float16 for quantization
k = k.to(torch.float16).to(device)
v = v.to(torch.float16).to(device)
# Perform 4-bit quantization using NF4
k_quant, k_absmax = bnb.functional.quantize_4bit(k, quant_type="nf4")
v_quant, v_absmax = bnb.functional.quantize_4bit(v, quant_type="nf4")
# Extract float values from the scaling factors
k_absmax_value = k_absmax.item() # Extract the float value
v_absmax_value = v_absmax.item() # Extract the float value
# Append quantized tensors and their corresponding absmax values
quantized_past_key_values.append(((k_quant.cpu(), k_absmax_value), (v_quant.cpu(), v_absmax_value)))
# Save the quantized data to disk
torch.save(quantized_past_key_values, save_path)
print("Cache saved successfully with NF4 quantization and absmax.")
def load_and_dequantize(save_path="cache_nf4.pth"):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if device.type != "cuda":
raise RuntimeError("CUDA device not available. 4-bit dequantization requires a GPU.")
try:
loaded_past_key_values = torch.load(save_path, map_location=device)
print("NF4-quantized cache loaded successfully.")
except FileNotFoundError:
print("No NF4 quantized cache file found.")
loaded_past_key_values = []
past_key_values = DynamicCache()
if loaded_past_key_values:
for layer_idx, ((k_quant, k_absmax), (v_quant, v_absmax)) in enumerate(loaded_past_key_values):
# Move quantized tensors back to GPU
k_quant = k_quant.to(device)
v_quant = v_quant.to(device)
# Convert absmax values back to tensors
k_absmax_tensor = torch.tensor(k_absmax, dtype=torch.float32, device=device)
v_absmax_tensor = torch.tensor(v_absmax, dtype=torch.float32, device=device)
# Define output shapes based on quantized tensor dimensions
key_out_shape = (k_quant.shape[0] * 8, k_quant.shape[1])
value_out_shape = (v_quant.shape[0] * 8, v_quant.shape[1])
# Dequantize the key and value tensors
key_states = bnb.functional.dequantize_4bit(
k_quant,
absmax=k_absmax_tensor,
out=torch.empty(key_out_shape, dtype=torch.float16, device=device),
quant_type="nf4"
)
value_states = bnb.functional.dequantize_4bit(
v_quant,
absmax=v_absmax_tensor,
out=torch.empty(value_out_shape, dtype=torch.float16, device=device),
quant_type="nf4"
)
# Update the DynamicCache with the dequantized tensors
past_key_values.update(key_states, value_states, layer_idx)
print("DynamicCache populated with dequantized float16 past_key_values.")
return past_key_values
# Example usage:
# Ensure that `past_key_values` is defined and resides on the GPU before calling `quantize_and_save`.
# For instance:
# past_key_values = model.get_past_key_values()
quantize_and_save(past_key_values)
# Load the quantized cache
past_key_values = load_and_dequantize()
Key Changes and Explanations:
Device Initialization:
python
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if device.type != "cuda":
raise RuntimeError("CUDA device not available. 4-bit quantization requires a GPU.")
This ensures that your code explicitly uses a CUDA device. If CUDA isn't available, it raises an error, preventing the code from proceeding with unsupported operations.
Moving Tensors to GPU Before Quantization:
python
k = k.to(torch.float16).to(device)
v = v.to(torch.float16).to(device)
Here, tensors k and v are first cast to float16 and then moved to the CUDA device. This is crucial because bitsandbytes functions expect tensors to be on the GPU.
After quantization, tensors are moved back to CPU before saving to disk using torch.save. This ensures that the saved file isn't tied to a specific GPU state and can be loaded later, possibly on a different GPU or after restarting the session.
Handling Dequantization:
In load_and_dequantize, after loading the quantized tensors, they are moved back to the CUDA device before dequantization.
The DynamicCache is updated with dequantized tensors that are ensured to reside on the GPU.
Error Handling for CUDA Availability:
Both quantize_and_save and load_and_dequantize functions check for CUDA availability and raise errors if CUDA is not available. This prevents unexpected behavior and provides clear feedback on what went wrong.
Consistency in Device Usage:
Ensure that wherever you handle tensors related to quantization and dequantization, they consistently reside on the CUDA device.
Additional Recommendations:
Verify bitsandbytes Installation:
Ensure that you have the correct version of bitsandbytes installed that supports NF4 quantization and is compatible with your CUDA version.
bash
pip install bitsandbytes
CUDA Toolkit and Drivers:
Ensure that your system has the appropriate CUDA toolkit and drivers installed that match the version required by bitsandbytes.
Memory Management:
Quantization is memory-efficient, but working with large models can still consume significant GPU memory. Monitor your GPU memory usage to prevent out-of-memory errors.
Testing with a Subset:
Before processing the entire past_key_values, consider testing the quantization and dequantization process with a small subset to ensure everything works as expected.
By ensuring that all tensors involved in the quantization process are on the GPU, you should be able to resolve the NotImplementedError and successfully perform 4-bit quantization with bitsandbytes.