# How is memory managed when loading a model?

**URL:** <https://discuss.huggingface.co/t/how-is-memory-managed-when-loading-a-model/45438>\
**Category:** Beginners\
**Created:** [July 3, 2023, 10:17am UTC](https://discuss.huggingface.co/t/how-is-memory-managed-when-loading-a-model/45438 "2023-07-03T10:17:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amon-epa](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@amon-epa](https://discuss.huggingface.co/u/amon-epa)\
**Post date:** [July 3, 2023, 10:17am UTC](https://discuss.huggingface.co/t/how-is-memory-managed-when-loading-a-model/45438/1 "2023-07-03T10:17:25Z")

</div>

Hi,

I used the transformers library in the past, however I didn’t really understand how a model is loaded in memory. Recently, I tried using a larger model than usual on my computer using only the CPU as I do not have a proper GPU yet.

I used the following code to load the model and test inference time:

```python
from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6B")
model = AutoModelForCausalLM.from_pretrained("EleutherAI/gpt-j-6B")

# ask input in terminal
prompt = input("Model prompt >>> ")

# encode input
input_ids = tokenizer(prompt, return_tensors="pt").input_ids

# generate output
output = model.generate(input_ids, max_length=200, do_sample=True, top_p=0.95, top_k=60)

# decode output
print("Output >>> " + tokenizer.decode(output[0], skip_special_tokens=True))

```

I expected the model to use approximately 24Gb of memory based on the number of parameters, but as the model started loading, I noticed that it used way more, reaching about 40Gb. When the model finished loading however, memory usage came back down to around 24Gb as expected.

I don’t really understand how memory is handled while loading a model. I know it has something to do with the way transformers use PyTorch or TensorFlow, but I can’t find a detailed loading process that can explain my findings.

Does anyone know the process in which model data is loaded in memory and maybe have some resources on this topic?  
Thanks!

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [July 3, 2023, 12:54pm UTC](https://discuss.huggingface.co/t/how-is-memory-managed-when-loading-a-model/45438/2 "2023-07-03T12:54:28Z")

</div>

Hi,

See this guide: [Handling big models for inference](https://huggingface.co/docs/accelerate/usage_guides/big_modeling). Basically we first create the model with randomly initialized weights, then equip the model with the pre-trained ones.

---

<div class="post-metadata">

**Author:** ![amon-epa](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@amon-epa](https://discuss.huggingface.co/u/amon-epa)\
**Post date:** [July 4, 2023, 12:10pm UTC](https://discuss.huggingface.co/t/how-is-memory-managed-when-loading-a-model/45438/3 "2023-07-04T12:10:25Z")

</div>

I see, thanks for the quick reply!
