# Move trainer.save\_pretrained("xyz") to CPU

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmove-trainer-save-pretrained-xyz-to-cpu%2F42011%3E%5C
**Category:** Beginners\
**Created:** [June 4, 2023, 3:05am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmove-trainer-save-pretrained-xyz-to-cpu%2F42011 "2023-06-04T03:05:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdroth](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mdroth/32/29308_2.png) [@mdroth](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmdroth)%5C
**Post date:** [June 4, 2023, 3:05am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmove-trainer-save-pretrained-xyz-to-cpu%2F42011%2F1 "2023-06-04T03:05:32Z")

</div>

Hi,

I am using the `Trainer` class to train a language model on my RTX 3060. It fits just so on the 3060’s 12 GB of GPU RAM. Once training has finished, I would like to save the model via `trainer.save_pretrained("my_local_folder")`. However, that command leads to `RuntimeError: CUDA out or memory`.

So I have tried the following to move the trainer to the CPU and save it from there:

1. `trainer.to("cpu").save_pretrained("my_local_folder")`  
or
2. `trainer.to("cpu")`  
`trainer.save_pretrained("my_local_folder")`  
or
3. `trainer.push_to_hub("my_hub_model)`

yet in any case, I still get the same `RuntimeError`. **How can I save my model?**

Your help is much appreciated!  
Matthias

---

<div class="post-metadata">

**Author:** ![mdroth](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mdroth/32/29308_2.png) [@mdroth](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmdroth)%5C
**Post date:** [June 26, 2023, 2:00am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmove-trainer-save-pretrained-xyz-to-cpu%2F42011%2F2 "2023-06-26T02:00:43Z")

</div>

ChatGPT had the answer (adapted):

```auto
save_folder = "model_and_tokenizer"
model = trainer.model # trick: get the trained model from trainer
model.cpu().save_pretrained(save_folder) # now, it's easy 
tokenizer.save_pretrained(save_folder) # also save the tokenizer

```
