Hugging Face, End to EndLesson 21 of 75

Module 3 · Running it on the hardware you actually have

Spreading a model across whatever you have

In this lesson

  • 8 min read
  • 7 reading sections
  • Knowledge check
  • Lesson 3 of 9 in this chapter

This chapter’s goalCompute from a model’s parameter count and dtype whether it will fit the machine in front of you, pick between a full-precision, quantized, GGUF or ONNX route for a stated constraint, and measure peak memory, first-token latency and quality loss well enough to defend the choice.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch2:05Not available
Full audiobookNot availableNot available
Where device_map auto puts a layerA GPU indexThe layer stays on the card. Full speed.cpuCopied to the GPU when that layer runs, thenevicted. Correct, several times slower.diskRead from an offload folder for every token of everyrequest. Correct, hundreds of times slower.Print hf_device_map and read it. Every entry a zero means the model fits and the speed is thehardware’s. A cpu or a disk entry is the whole explanation for four minutes a token, and the fix isquantization rather than patience.
Print hf_device_map and read it. Every entry a zero means the model fits and the speed is the hardware's. A cpu or a disk entry is the whole explanation for four minutes a token, and the fix is quantization rather than patience.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 9Free · no account needed
Lesson steps

One line that does a surprising amount

Python
model = AutoModelForCausalLM.from_pretrained(
    name, dtype=torch.bfloat16, device_map="auto"
)

device_map="auto" comes from the accelerate library and does four things: inspect available devices and their free memory, load the model skeleton without weights, decide which layers go where, then stream the weights in one shard at a time so the full model never has to exist in system RAM at once.

That last part is why it also helps on a single GPU. Without it, the naive path is: read 14 GB into CPU memory, then copy to the GPU. On a machine with 16 GB of RAM that fails before the GPU is involved at all. With it, shards move through and are released.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.

Stuck on something?Ask Arjun, your AI tutor

How to cite: Addaly (2026) ‘Spreading a model across whatever you have’, in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/device-map-and-offloading (Accessed: 29 November 2026).