Fix the No File Named pytorch_model.bin Error for GGUF Models
The "no file named pytorch_model.bin" error means from_pretrained found no standard weights file in the repo, usually because it holds GGUF, GPTQ or AWQ files instead. Identify the format in the Files tab, then use the loader that matches it.
- from_pretrained only looks for model.safetensors, pytorch_model.bin, tf_model.h5, model.ckpt or flax_model.msgpack, so GGUF-only repos always fail.
- Check the repo's Files tab: .gguf means llama-cpp-python, quantization_config in config.json means GPTQ or AWQ, and model-0000x-of-0000y.safetensors means original weights.
- A 7B fp16 model needs about 13-14 GB, so an 11 GB RTX 2080 Ti needs a quantized model such as Q5_K_M (about 4.8 GB) with all layers offloaded.
- Loading GGUF through transformers' gguf_file argument dequantizes to fp16/fp32, so it gives no speed or memory benefit.
- Twenty-minute answers mean the model is on CPU or in fp32; confirm with torch.cuda.is_available() and the 'offloaded 33/33 layers' log line.
Why does from_pretrained say there is no file named pytorch_model.bin?
AutoModelForCausalLM.from_pretrained(repo_id) downloads config.json and then searches the repo for a weights file in a fixed order: model.safetensors (or sharded model.safetensors.index.json), pytorch_model.bin, tf_model.h5, model.ckpt and flax_model.msgpack. If none exists, it raises an OSError saying the repo has no file named pytorch_model.bin and listing the other names.
TheBloke/Llama-2-7B-GGUF contains only .gguf files, which is a different container format made for llama.cpp. Transformers does not look for them unless you pass gguf_file explicitly. The bare name TheBloke/Llama-2-7b is not a valid repo at all, which produces a similar error or a 404.
The wording of the message is misleading: it is not a corrupted install, it is a format mismatch between what the repo stores and what the loader expects.
How do I tell which format a repo uses?
Open the repo on the Hub, read the model card, then look at the Files tab. The file names identify the format in seconds.
| What you see in Files | Format | Loader to use |
|---|---|---|
| llama-2-7b.Q5_K_M.gguf, other .gguf files | GGUF | llama-cpp-python (or gguf_file in transformers, slow path) |
| model.safetensors, quantize_config.json, repo name ends in GPTQ | GPTQ | transformers + optimum + gptqmodel or auto-gptq |
| model.safetensors, repo name ends in AWQ | AWQ | transformers + autoawq |
| model-00001-of-00002.safetensors, config.json, tokenizer files | Original fp16 | transformers, optionally with bitsandbytes 4-bit |
How do I run a GGUF Llama 2 model with llama-cpp-python on a GPU?
This is the best path for a TheBloke GGUF repo, and it removes the no file named pytorch_model.bin error because no transformers weights lookup happens. The default pip install builds a CPU-only wheel, which is the most common reason for slowness, so build with CUDA. Older versions used -DLLAMA_CUBLAS=on; current ones use -DGGML_CUDA=on. You need the CUDA toolkit (nvcc) and a C++ compiler installed.
Compiling takes several minutes. Run it inside your venv, and add --force-reinstall --no-cache-dir if a CPU build is already installed.
- Install with CUDA: CMAKE_ARGS="-DGGML_CUDA=on" pip install llama-cpp-python --force-reinstall --no-cache-dir
- Download one file with hf_hub_download, or point to a file you already have.
- Create Llama with n_gpu_layers=-1 and n_ctx=4096.
- Watch the startup log for the offload line and run nvidia-smi.
from llama_cpp import Llama
from huggingface_hub import hf_hub_download
path = hf_hub_download(
repo_id="TheBloke/Llama-2-7B-GGUF",
filename="llama-2-7b.Q5_K_M.gguf",
)
# or: path = "/home/me/models/llama-2-7b.Q5_K_M.gguf"
llm = Llama(model_path=path, n_gpu_layers=-1, n_ctx=4096, verbose=True)
out = llm("Q: What is GGUF? A:", max_tokens=128)
print(out["choices"][0]["text"])
# one-liner alternative
# llm = Llama.from_pretrained(repo_id="TheBloke/Llama-2-7B-GGUF",
# filename="*Q5_K_M.gguf", n_gpu_layers=-1, n_ctx=4096)Can transformers load GGUF directly?
Yes, since transformers 4.41, with from_pretrained(repo_id, gguf_file=...). Only some architectures are supported (Llama, Mistral, Qwen2 and a few others, expanding by release), and the tensors are dequantized to fp16 or fp32 on load.
That means no memory or speed saving: a 7B model becomes 13-14 GB again and will not fit in 11 GB of VRAM. Use it only for converting or inspecting, not for fast inference.
Do not pass the tokenizer separately from a different repo without checking it matches; AutoTokenizer.from_pretrained accepts the same gguf_file argument.
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
repo = "TheBloke/Llama-2-7B-GGUF"
f = "llama-2-7b.Q5_K_M.gguf"
tok = AutoTokenizer.from_pretrained(repo, gguf_file=f)
model = AutoModelForCausalLM.from_pretrained(repo, gguf_file=f, torch_dtype=torch.float16) How do I load GPTQ, AWQ or bitsandbytes 4-bit models in transformers?
If you want to stay in the transformers API, these formats keep weights quantized on the GPU.
GPTQ: install optimum and a backend (gptqmodel, or the older auto-gptq), then load TheBloke/Llama-2-7B-GPTQ with device_map="auto". AWQ: pip install autoawq and load the AWQ repo the same way.
bitsandbytes: use the original meta-llama/Llama-2-7b-hf (or the chat variant). It is gated, so request access on the model page and run huggingface-cli login first. On-the-fly 4-bit brings it to roughly 4 GB.
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
# GPTQ / AWQ
m = AutoModelForCausalLM.from_pretrained("TheBloke/Llama-2-7B-GPTQ", device_map="auto")
# bitsandbytes 4-bit
cfg = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_quant_type="nf4")
m = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf",
quantization_config=cfg, device_map="auto")
tok = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf") Why does an answer take 20 minutes, and how fast should it be?
Twenty minutes per answer means the model is running on CPU, or in fp32 on a GPU too small for it so layers spill to system RAM. A 7B model is about 13-14 GB in fp16 and 28 GB in fp32, while the RTX 2080 Ti has 11 GB. Quantization is therefore mandatory, not optional.
With a Q5_K_M file (about 4.8 GB) and every layer offloaded, expect tens of tokens per second on a 2080 Ti. Work through the checklist below.
Quantization choice for 7B (approximate file sizes): Q4_K_M is about 4.1 GB with a small quality loss, Q5_K_M about 4.8 GB with very little loss, and Q8_0 about 7.2 GB, close to fp16 quality. All fit in 11 GB, with room left for a 4096-token context.
| Check | Good sign | Fix if bad |
|---|---|---|
| torch.cuda.is_available() | True | Install a CUDA build of PyTorch, update the NVIDIA driver |
| llama.cpp log line | offloaded 33/33 layers to GPU | Reinstall llama-cpp-python with -DGGML_CUDA=on, set n_gpu_layers=-1 |
| nvidia-smi | Several GB used by the python process | You are on CPU; see above |
| transformers dtype | torch_dtype=torch.float16, device_map="cuda" | Never leave the default fp32 path |
| Model size | Fits in 11 GB with context | Use Q4_K_M or a smaller context |
What other causes give the same OSError, and which prompt format applies?
Other causes: a typo in the repo id; a private or gated repo without login (huggingface-cli login); an interrupted download or broken cache; or a local folder path that lacks weight files. For a broken cache, delete only that model's models--org--name folder under ~/.cache/huggingface/hub and retry, not the whole cache.
Prompt format matters once it runs. Chat variants (Llama-2-7B-Chat) expect: <s>[INST] <<SYS>>\nsystem text\n<</SYS>>\n\nuser message [/INST]. Base models just continue text and ignore this format, so a plain question may ramble. With llama-cpp-python, llm.create_chat_completion(messages=[...]) applies a template when the GGUF metadata includes one.
Llama 2 is old. Newer 7B-8B models in GGUF form (Llama 3.x, Mistral, Qwen) are noticeably better at the same file size and load with the same code.
Common questions
Which is better with transformers: GGUF, GPTQ or AWQ?
For transformers, GPTQ and AWQ are the native choices because they stay quantized on the GPU. GGUF is best with llama.cpp or llama-cpp-python, and also supports CPU and partial offload for larger models.
Do I need to download the whole TheBloke repo?
No. A GGUF repo contains one file per quantization level, and you need only one, such as llama-2-7b.Q5_K_M.gguf. Downloading the whole repo wastes tens of gigabytes.
Why does n_gpu_layers=-1 not use my GPU?
Almost always the installed wheel was built without CUDA. Reinstall with CMAKE_ARGS="-DGGML_CUDA=on" and --force-reinstall --no-cache-dir, then check the log for the offload line.
Do I need a Hugging Face login for TheBloke repos?
No, TheBloke repos are public. Only gated originals such as meta-llama/Llama-2-7b-hf need approved access and huggingface-cli login.