# Load\_checkpoint\_and\_dispatch without heavy system memory usage

**URL:** <https://discuss.huggingface.co/t/load-checkpoint-and-dispatch-without-heavy-system-memory-usage/35944>\
**Category:** 🤗Accelerate\
**Created:** [April 8, 2023, 4:37am UTC](https://discuss.huggingface.co/t/load-checkpoint-and-dispatch-without-heavy-system-memory-usage/35944 "2023-04-08T04:37:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryulord](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@Ryulord](https://discuss.huggingface.co/u/Ryulord)\
**Post date:** [April 8, 2023, 4:37am UTC](https://discuss.huggingface.co/t/load-checkpoint-and-dispatch-without-heavy-system-memory-usage/35944/1 "2023-04-08T04:37:06Z")

</div>

I’m trying to load llama-13b for inference on a system with 24GB VRAM and 32GB system memory using load\_checkpoint\_and\_dispatch. The model should fit in the amount of combined memory I have but it looks like load\_checkpoint\_and\_dispatch starts by trying to load the whole model into system memory at full precision before moving anything to GPU, causing me to run out of system memory. Is there any way around this or is this just a limitation of the current implementation? The model is sharded so it seems like it should be able to load shards and move them to GPU one at a time until the GPU is full and only then start loading the shards meant to stay in system memory.

Here’s my code:

```auto
checkpoint = "decapoda-research/llama-13b-hf"
model_index_path = hf_hub_download(checkpoint, "pytorch_model.bin.index.json")

tokenizer = LlamaTokenizer.from_pretrained(checkpoint)
with init_empty_weights():
    model = LlamaForCausalLM.from_pretrained(checkpoint, low_cpu_mem_usage=True).half()

device_map = infer_auto_device_map(
    model,
    max_memory={
        0: "20GiB",
        "cpu": "16GiB"
    },
)

model = load_checkpoint_and_dispatch(
    model,
    model_index_path,
    device_map=device_map,
    no_split_module_classes=["LlamaDecoderLayer"],
    dtype=torch.float16,
)

```

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](https://discuss.huggingface.co/u/sgugger)\
**Post date:** [April 10, 2023, 1:26pm UTC](https://discuss.huggingface.co/t/load-checkpoint-and-dispatch-without-heavy-system-memory-usage/35944/2 "2023-04-10T13:26:02Z")

</div>

You should just use`

```auto
LlamaForCausalLM.from_pretrained(checkpoint, low_cpu_mem_usage=True, torch_dtype=torch.float16)

```

which will do the same thing.

`load_checkpoint_and_dispatch` will load the scheckpoint shard by shard, but it can’t work with the code sample you provided as it requires the shards to live in the same folder as the model index, and it doesn’t look like you are downloading them?

Also that checkpoint does not work at all (see the ~70 PRs opened being ignored), you should really use the conversion script on the official weights or use other checkpoints.
