| 1 |
<!--Copyright 2022 The HuggingFace Team. All rights reserved. |
| 2 |
|
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 |
the License. You may obtain a copy of the License at |
| 5 |
|
| 6 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
|
| 8 |
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 |
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 |
specific language governing permissions and limitations under the License. |
| 11 |
|
| 12 |
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 |
rendered properly in your Markdown viewer. |
| 14 |
--> |
| 15 |
|
| 16 |
# Model memory estimator |
| 17 |
|
| 18 |
One very difficult aspect when exploring potential models to use on your machine is knowing just how big of a model will *fit* into memory with your current device (such as loading the model onto CUDA or XPU). |
| 19 |
|
| 20 |
To help alleviate this, Accelerate has a CLI interface through `accelerate estimate-memory`. This tutorial will |
| 21 |
help walk you through using it, what to expect, and at the end link to the interactive demo hosted on the Hub which will |
| 22 |
even let you post those results directly on the model repo! |
| 23 |
|
| 24 |
Currently we support searching for models that can be used in `timm` and `transformers`. |
| 25 |
|
| 26 |
<Tip> |
| 27 |
|
| 28 |
This API will load the model into memory on the `meta` device, so we are not actually downloading |
| 29 |
and loading the full weights of the model into memory, nor do we need to. As a result it's |
| 30 |
perfectly fine to measure 8 billion parameter models (or more), without having to worry about |
| 31 |
if your CPU can handle it! |
| 32 |
|
| 33 |
</Tip> |
| 34 |
|
| 35 |
## Gradio Demos |
| 36 |
|
| 37 |
Below are a few gradio demos related to what was described above. The first is the official Hugging Face memory estimation space, utilizing Accelerate directly: |
| 38 |
|
| 39 |
<div class="block dark:hidden"> |
| 40 |
<iframe |
| 41 |
src="https://hf-accelerate-model-memory-usage.hf.space?__theme=light" |
| 42 |
width="850" |
| 43 |
height="1600" |
| 44 |
></iframe> |
| 45 |
</div> |
| 46 |
<div class="hidden dark:block"> |
| 47 |
<iframe |
| 48 |
src="https://hf-accelerate-model-memory-usage.hf.space?__theme=dark" |
| 49 |
width="850" |
| 50 |
height="1600" |
| 51 |
></iframe> |
| 52 |
</div> |
| 53 |
|
| 54 |
A community member has taken the idea and expanded it further, allowing you to filter models directly and see if you can run a particular LLM given GPU constraints and LoRA configurations. To play with it, see [here](https://huggingface.co/spaces/Vokturz/can-it-run-llm) for more details. |
| 55 |
|
| 56 |
## The Command |
| 57 |
|
| 58 |
When using `accelerate estimate-memory`, you need to pass in the name of the model you want to use, potentially the framework |
| 59 |
that model utilizing (if it can't be found automatically), and the data types you want the model to be loaded in with. |
| 60 |
|
| 61 |
For example, here is how we can calculate the memory footprint for `bert-base-cased`: |
| 62 |
|
| 63 |
```bash |
| 64 |
accelerate estimate-memory bert-base-cased |
| 65 |
``` |
| 66 |
|
| 67 |
This will download the `config.json` for `bert-based-cased`, load the model on the `meta` device, and report back how much space |
| 68 |
it will use: |
| 69 |
|
| 70 |
Memory Usage for loading `bert-base-cased`: |
| 71 |
|
| 72 |
| dtype | Largest Layer | Total Size | Training using Adam | |
| 73 |
|---------|---------------|------------|---------------------| |
| 74 |
| float32 | 84.95 MB | 418.18 MB | 1.61 GB | |
| 75 |
| float16 | 42.47 MB | 206.59 MB | 826.36 MB | |
| 76 |
| int8 | 21.24 MB | 103.29 MB | 413.18 MB | |
| 77 |
| int4 | 10.62 MB | 51.65 MB | 206.59 MB | |
| 78 |
|
| 79 |
By default it will return all the supported dtypes (`int4` through `float32`), but if you are interested in specific ones these can be filtered. |
| 80 |
|
| 81 |
### Specific libraries |
| 82 |
|
| 83 |
If the source library cannot be determined automatically (like it could in the case of `bert-base-cased`), a library name can |
| 84 |
be passed in. |
| 85 |
|
| 86 |
```bash |
| 87 |
accelerate estimate-memory HuggingFaceM4/idefics-80b-instruct --library_name transformers |
| 88 |
``` |
| 89 |
|
| 90 |
Memory Usage for loading `HuggingFaceM4/idefics-80b-instruct`: |
| 91 |
|
| 92 |
| dtype | Largest Layer | Total Size | Training using Adam | |
| 93 |
|---------|---------------|------------|---------------------| |
| 94 |
| float32 | 3.02 GB | 297.12 GB | 1.16 TB | |
| 95 |
| float16 | 1.51 GB | 148.56 GB | 594.24 GB | |
| 96 |
| int8 | 772.52 MB | 74.28 GB | 297.12 GB | |
| 97 |
| int4 | 386.26 MB | 37.14 GB | 148.56 GB | |
| 98 |
|
| 99 |
|
| 100 |
```bash |
| 101 |
accelerate estimate-memory timm/resnet50.a1_in1k --library_name timm |
| 102 |
``` |
| 103 |
|
| 104 |
Memory Usage for loading `timm/resnet50.a1_in1k`: |
| 105 |
|
| 106 |
| dtype | Largest Layer | Total Size | Training using Adam | |
| 107 |
|---------|---------------|------------|---------------------| |
| 108 |
| float32 | 9.0 MB | 97.7 MB | 390.78 MB | |
| 109 |
| float16 | 4.5 MB | 48.85 MB | 195.39 MB | |
| 110 |
| int8 | 2.25 MB | 24.42 MB | 97.7 MB | |
| 111 |
| int4 | 1.12 MB | 12.21 MB | 48.85 MB | |
| 112 |
|
| 113 |
### Specific dtypes |
| 114 |
|
| 115 |
As mentioned earlier, while we return `int4` through `float32` by default, any dtype can be used from `float32`, `float16`, `int8`, and `int4`. |
| 116 |
|
| 117 |
To do so, pass them in after specifying `--dtypes`: |
| 118 |
|
| 119 |
```bash |
| 120 |
accelerate estimate-memory bert-base-cased --dtypes float32 float16 |
| 121 |
``` |
| 122 |
|
| 123 |
Memory Usage for loading `bert-base-cased`: |
| 124 |
|
| 125 |
| dtype | Largest Layer | Total Size | Training using Adam | |
| 126 |
|---------|---------------|------------|---------------------| |
| 127 |
| float32 | 84.95 MB | 413.18 MB | 1.61 GB | |
| 128 |
| float16 | 42.47 MB | 206.59 MB | 826.36 MB | |
| 129 |
|
| 130 |
## Caveats with this calculator |
| 131 |
|
| 132 |
This calculator will tell you how much memory is needed to purely load the model in, *not* to perform inference. |
| 133 |
|
| 134 |
This calculation is accurate within a few % of the actual value, so it is a very good view of just how much memory it will take. For instance loading `bert-base-cased` actually takes `413.68 MB` when loaded on CUDA in full precision, and the calculator estimates `413.18 MB`. |
| 135 |
|
| 136 |
When performing inference you can expect to add up to an additional 20% as found by [EleutherAI](https://blog.eleuther.ai/transformer-math/). We'll be conducting research into finding a more accurate estimate to these values, and will update |
| 137 |
this calculator once done. |