Loading quantized model on CPU only

Im currently trying to run BloomZ 7b1 on a server with ~31GB available ram. Without quantization loading the model starts filling up swap, which is far from desirable. I tried enabling quantization with load_in_8bit:

from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
import torch

modelPath = "/mnt/backup1/BLOOM/"

device = torch.device("cpu")
tokenizer = AutoTokenizer.from_pretrained(modelPath)
model = AutoModelForCausalLM.from_pretrained(modelPath, device_map="auto", local_files_only=True, load_in_8bit=True).to(device)

prompt = 'Write code for finding the prime number in python ?'
input_ids = tokenizer(prompt, return_tensors="pt").to(device)
output = model.generate(**input_ids, max_length=100, do_sample=True, top_k=50, temperature=0.25)
response = tokenizer.decode(output[0])

print(response)

And then fails with an eventual crash:

Log
Overriding torch_dtype=None with `torch_dtype=torch.float16` due to requirements of `bitsandbytes` to enable model loading in mixed int8. Eithe
CUDA_SETUP: WARNING! libcudart.so not found in any environmental path. Searching /usr/local/cuda/lib64...
WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/usr/local/cuda/lib64')}
WARNING: No libcudart.so found! Install CUDA or the cudatoolkit package (anaconda)!
CUDA_SETUP: Loading binary /home/connor/.local/lib/python3.10/site-packages/bitsandbytes/libbitsandbytes_cpu.so...
/home/connor/.local/lib/python3.10/site-packages/bitsandbytes/cextension.py:43: UserWarning: The installed version of bitsandbytes was compiled
  warn(
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Traceback (most recent call last) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ /home/connor/Workspace/chatgpt/bloom/main.py:9 in <module>                                       โ”‚
โ”‚                                                                                                  โ”‚
โ”‚    6 print("Loaded Torch")                                                                       โ”‚
โ”‚    7 tokenizer = AutoTokenizer.from_pretrained("/mnt/backup1/BLOOM/")                            โ”‚
โ”‚    8 print("Loaded Tokenizer")                                                                   โ”‚
โ”‚ โฑ  9 model = AutoModelForCausalLM.from_pretrained("/mnt/backup1/BLOOM", device_map={"lm_head"    โ”‚
โ”‚   10 print("Loaded Model")                                                                       โ”‚
โ”‚   11                                                                                             โ”‚
โ”‚   12 prompt = 'Write code for finding the prime number in python ?'                              โ”‚
โ”‚                                                                                                  โ”‚
โ”‚ /home/connor/.local/lib/python3.10/site-packages/transformers/models/auto/auto_factory.py:471 in โ”‚
โ”‚ from_pretrained                                                                                  โ”‚
โ”‚                                                                                                  โ”‚
โ”‚   468 โ”‚   โ”‚   โ”‚   )                                                                              โ”‚
โ”‚   469 โ”‚   โ”‚   elif type(config) in cls._model_mapping.keys():                                    โ”‚
โ”‚   470 โ”‚   โ”‚   โ”‚   model_class = _get_model_class(config, cls._model_mapping)                     โ”‚
โ”‚ โฑ 471 โ”‚   โ”‚   โ”‚   return model_class.from_pretrained(                                            โ”‚
โ”‚   472 โ”‚   โ”‚   โ”‚   โ”‚   pretrained_model_name_or_path, *model_args, config=config, **hub_kwargs,   โ”‚
โ”‚   473 โ”‚   โ”‚   โ”‚   )                                                                              โ”‚
โ”‚   474 โ”‚   โ”‚   raise ValueError(                                                                  โ”‚
โ”‚                                                                                                  โ”‚
โ”‚ /home/connor/.local/lib/python3.10/site-packages/transformers/modeling_utils.py:2583 in          โ”‚
โ”‚ from_pretrained                                                                                  โ”‚
โ”‚                                                                                                  โ”‚
โ”‚   2580 โ”‚   โ”‚   โ”‚   keep_in_fp32_modules = []                                                     โ”‚
โ”‚   2581 โ”‚   โ”‚                                                                                     โ”‚
โ”‚   2582 โ”‚   โ”‚   if load_in_8bit:                                                                  โ”‚
โ”‚ โฑ 2583 โ”‚   โ”‚   โ”‚   from .utils.bitsandbytes import get_keys_to_not_convert, replace_8bit_linear  โ”‚
โ”‚   2584 โ”‚   โ”‚   โ”‚                                                                                 โ”‚
โ”‚   2585 โ”‚   โ”‚   โ”‚   load_in_8bit_skip_modules = quantization_config.llm_int8_skip_modules         โ”‚
โ”‚   2586 โ”‚   โ”‚   โ”‚   load_in_8bit_threshold = quantization_config.llm_int8_threshold               โ”‚
โ”‚                                                                                                  โ”‚
โ”‚ /home/connor/.local/lib/python3.10/site-packages/transformers/utils/bitsandbytes.py:7 in         โ”‚
โ”‚ <module>                                                                                         โ”‚
โ”‚                                                                                                  โ”‚
โ”‚     4                                                                                            โ”‚
โ”‚     5                                                                                            โ”‚
โ”‚     6 if is_bitsandbytes_available():                                                            โ”‚
โ”‚ โฑ   7 โ”‚   import bitsandbytes as bnb                                                             โ”‚
โ”‚     8 โ”‚   import torch                                                                           โ”‚
โ”‚     9 โ”‚   import torch.nn as nn                                                                  โ”‚
โ”‚    10                                                                                            โ”‚
โ”‚                                                                                                  โ”‚
โ”‚ /home/connor/.local/lib/python3.10/site-packages/bitsandbytes/__init__.py:6 in <module>          โ”‚
โ”‚                                                                                                  โ”‚
โ”‚    3 # This source code is licensed under the MIT license found in the                           โ”‚
โ”‚    4 # LICENSE file in the root directory of this source tree.                                   โ”‚
โ”‚    5                                                                                             โ”‚
โ”‚ โฑ  6 from .autograd._functions import (                                                          โ”‚
โ”‚    7 โ”‚   MatmulLtState,                                                                          โ”‚
โ”‚    8 โ”‚   bmm_cublas,                                                                             โ”‚
โ”‚    9 โ”‚   matmul,                                                                                 โ”‚
โ”‚                                                                                                  โ”‚
โ”‚ /home/connor/.local/lib/python3.10/site-packages/bitsandbytes/autograd/_functions.py:170 in      โ”‚
โ”‚ <module>                                                                                         โ”‚
โ”‚                                                                                                  โ”‚
โ”‚   167                                                                                            โ”‚
โ”‚   168                                                                                            โ”‚
โ”‚   169 @dataclass                                                                                 โ”‚
โ”‚ โฑ 170 class MatmulLtState:                                                                       โ”‚
โ”‚   171 โ”‚   CB = None                                                                              โ”‚
โ”‚   172 โ”‚   CxB = None                                                                             โ”‚
โ”‚   173 โ”‚   SB = None                                                                              โ”‚
โ”‚                                                                                                  โ”‚
โ”‚ /home/connor/.local/lib/python3.10/site-packages/bitsandbytes/autograd/_functions.py:189 in      โ”‚
โ”‚ MatmulLtState                                                                                    โ”‚
โ”‚                                                                                                  โ”‚
โ”‚   186 โ”‚   is_training = True                                                                     โ”‚
โ”‚   187 โ”‚   has_fp16_weights = True                                                                โ”‚
โ”‚   188 โ”‚   use_pool = False                                                                       โ”‚
โ”‚ โฑ 189 โ”‚   formatB = F.get_special_format_str()                                                   โ”‚
โ”‚   190 โ”‚                                                                                          โ”‚
โ”‚   191 โ”‚   def reset_grads(self):                                                                 โ”‚
โ”‚   192 โ”‚   โ”‚   self.CB = None                                                                     โ”‚
โ”‚                                                                                                  โ”‚
โ”‚ /home/connor/.local/lib/python3.10/site-packages/bitsandbytes/functional.py:1684 in              โ”‚
โ”‚ get_special_format_str                                                                           โ”‚
โ”‚                                                                                                  โ”‚
โ”‚   1681                                                                                           โ”‚
โ”‚   1682                                                                                           โ”‚
โ”‚   1683 def get_special_format_str():                                                             โ”‚
โ”‚ โฑ 1684 โ”‚   major, minor = torch.cuda.get_device_capability()                                     โ”‚
โ”‚   1685 โ”‚   if major < 7:                                                                         โ”‚
โ”‚   1686 โ”‚   โ”‚   print(                                                                            โ”‚
โ”‚   1687 โ”‚   โ”‚   โ”‚   f"Device with CUDA capability of {major} not supported for 8-bit matmul. Dev  โ”‚
โ”‚                                                                                                  โ”‚
โ”‚ /home/connor/.local/lib/python3.10/site-packages/torch/cuda/__init__.py:381 in                   โ”‚
โ”‚ get_device_capability                                                                            โ”‚
โ”‚                                                                                                  โ”‚
โ”‚    378 โ”‚   Returns:                                                                              โ”‚
โ”‚    379 โ”‚   โ”‚   tuple(int, int): the major and minor cuda capability of the device                โ”‚
โ”‚    380 โ”‚   """                                                                                   โ”‚
โ”‚ โฑ  381 โ”‚   prop = get_device_properties(device)                                                  โ”‚
โ”‚    382 โ”‚   return prop.major, prop.minor                                                         โ”‚
โ”‚    383                                                                                           โ”‚
โ”‚    384                                                                                           โ”‚
โ”‚                                                                                                  โ”‚
โ”‚ /home/connor/.local/lib/python3.10/site-packages/torch/cuda/__init__.py:395 in                   โ”‚
โ”‚ get_device_properties                                                                            โ”‚
โ”‚                                                                                                  โ”‚
โ”‚    392 โ”‚   Returns:                                                                              โ”‚
โ”‚    393 โ”‚   โ”‚   _CudaDeviceProperties: the properties of the device                               โ”‚
โ”‚    394 โ”‚   """                                                                                   โ”‚
โ”‚ โฑ  395 โ”‚   _lazy_init()  # will define _get_device_properties                                    โ”‚
โ”‚    396 โ”‚   device = _get_device_index(device, optional=True)                                     โ”‚
โ”‚    397 โ”‚   if device < 0 or device >= device_count():                                            โ”‚
โ”‚    398 โ”‚   โ”‚   raise AssertionError("Invalid device id")                                         โ”‚
โ”‚                                                                                                  โ”‚
โ”‚ /home/connor/.local/lib/python3.10/site-packages/torch/cuda/__init__.py:247 in _lazy_init        โ”‚
โ”‚                                                                                                  โ”‚
โ”‚    244 โ”‚   โ”‚   # are found or any other error occurs                                             โ”‚
โ”‚    245 โ”‚   โ”‚   if 'CUDA_MODULE_LOADING' not in os.environ:                                       โ”‚
โ”‚    246 โ”‚   โ”‚   โ”‚   os.environ['CUDA_MODULE_LOADING'] = 'LAZY'                                    โ”‚
โ”‚ โฑ  247 โ”‚   โ”‚   torch._C._cuda_init()                                                             โ”‚
โ”‚    248 โ”‚   โ”‚   # Some of the queued calls may reentrantly call _lazy_init();                     โ”‚
โ”‚    249 โ”‚   โ”‚   # we need to just return without initializing in that case.                       โ”‚
โ”‚    250 โ”‚   โ”‚   # However, we must not let any *other* threads in!                                โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
RuntimeError: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com

Looks like its trying to access a CUDA API that just isnt there, I do not need this! Im assuming there is SOME way to force loading of the model to use the CPU for all tasks, even if slow.

I want a script that forces the use of CPU, that loads BloomZ from a local repo folder and that quantizes the model to 8-bit while loading to prevent out of memory errors.

How do I do this?

I have a similar issue AssertionError: Torch not compiled with CUDA enabled

Did you guys got any solution for loading model only on CPU with quantization?

1 Like

Same here!!! Can anyone help for the workaround ?

The flag load_in_8bit is used to enable 8-bit quantization with LLM.int8(). LLM.int8 is a lightweight wrapper around CUDA custom functions, so the quantization is only possible in GPU.

You have the required details in offical bitsandbytes github page.

Requirements: Python >=3.8. Linux distribution (Ubuntu, MacOS, etc.) + CUDA > 10.0.

1 Like

If you want to run inference of quantized LLMs on CPU, itโ€™s recommended to take a look at the llama cpp project: GitHub - ggerganov/llama.cpp: LLM inference in C/C++. This one leverages a new format called GGUF

Thereโ€™s now also the MLX framework by Apple which allows to run these models on Macbooks: GitHub - ml-explore/mlx: MLX: An array framework for Apple silicon

What you could do is train a model using the Hugging Face tooling (PEFT, TRL, Transformers) and then export your model to the GGUF format: llama.cpp/convert-hf-to-gguf.py at master ยท ggerganov/llama.cpp ยท GitHub. You can then run your quantized model on CPU.

3 Likes