installation.md 9.54 KB
Newer Older
Sylvain Gugger's avatar
Sylvain Gugger committed
1
<!---
Steven Liu's avatar
Steven Liu committed
2
Copyright 2022 The HuggingFace Team. All rights reserved.
Sylvain Gugger's avatar
Sylvain Gugger committed
3
4
5
6
7
8
9
10
11
12
13
14

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
15
16
17
18

⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.

Sylvain Gugger's avatar
Sylvain Gugger committed
19
20
-->

21
22
# Installation

Steven Liu's avatar
Steven Liu committed
23
Install 🤗 Transformers for whichever deep learning library you're working with, setup your cache, and optionally configure 🤗 Transformers to run offline.
24

Steven Liu's avatar
Steven Liu committed
25
🤗 Transformers is tested on Python 3.6+, PyTorch 1.1.0+, TensorFlow 2.0+, and Flax. Follow the installation instructions below for the deep learning library you are using:
26

Steven Liu's avatar
Steven Liu committed
27
28
29
* [PyTorch](https://pytorch.org/get-started/locally/) installation instructions.
* [TensorFlow 2.0](https://www.tensorflow.org/install/pip) installation instructions.
* [Flax](https://flax.readthedocs.io/en/latest/) installation instructions.
30

Steven Liu's avatar
Steven Liu committed
31
## Install with pip
32

Steven Liu's avatar
Steven Liu committed
33
You should install 🤗 Transformers in a [virtual environment](https://docs.python.org/3/library/venv.html). If you're unfamiliar with Python virtual environments, take a look at this [guide](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/). A virtual environment makes it easier to manage different projects, and avoid compatibility issues between dependencies.
34

Steven Liu's avatar
Steven Liu committed
35
36
37
38
39
40
Start by creating a virtual environment in your project directory:

```bash
python -m venv .env
```

41
Activate the virtual environment. On Linux and MacOs:
Steven Liu's avatar
Steven Liu committed
42
43
44
45

```bash
source .env/bin/activate
```
46
47
48
49
50
Activate Virtual environment on Windows

```bash
.env/Scripts/activate
```
Steven Liu's avatar
Steven Liu committed
51
52

Now you're ready to install 🤗 Transformers with the following command:
53
54

```bash
55
56
57
pip install transformers
```

Steven Liu's avatar
Steven Liu committed
58
For CPU-support only, you can conveniently install 🤗 Transformers and a deep learning library in one line. For example, install 🤗 Transformers and PyTorch with:
59
60

```bash
61
pip install 'transformers[torch]'
62
63
```

Steven Liu's avatar
Steven Liu committed
64
🤗 Transformers and TensorFlow 2.0:
65
66

```bash
67
pip install 'transformers[tf-cpu]'
68
69
```

70
71
72
73
74
75
76
77
78
79
80
81
<Tip warning={true}>

M1 / ARM Users
    
You will need to install the following before installing TensorFLow 2.0
```
brew install cmake
brew install pkg-config
```

</Tip>

Steven Liu's avatar
Steven Liu committed
82
🤗 Transformers and Flax:
83
84

```bash
85
pip install 'transformers[flax]'
86
87
```

Steven Liu's avatar
Steven Liu committed
88
Finally, check if 🤗 Transformers has been properly installed by running the following command. It will download a pretrained model:
89
90

```bash
91
python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))"
92
93
```

Steven Liu's avatar
Steven Liu committed
94
Then print out the label and score:
95
96

```bash
97
[{'label': 'POSITIVE', 'score': 0.9998704791069031}]
98
99
```

Steven Liu's avatar
Steven Liu committed
100
## Install from source
101

Steven Liu's avatar
Steven Liu committed
102
Install 🤗 Transformers from source with the following command:
103
104
105
106
107

```bash
pip install git+https://github.com/huggingface/transformers
```

108
This command installs the bleeding edge `main` version rather than the latest `stable` version. The `main` version is useful for staying up-to-date with the latest developments. For instance, if a bug has been fixed since the last official release but a new release hasn't been rolled out yet. However, this means the `main` version may not always be stable. We strive to keep the `main` version operational, and most issues are usually resolved within a few hours or a day. If you run into a problem, please open an [Issue](https://github.com/huggingface/transformers/issues) so we can fix it even sooner!
109

Steven Liu's avatar
Steven Liu committed
110
Check if 🤗 Transformers has been properly installed by running the following command:
111
112

```bash
Steven Liu's avatar
Steven Liu committed
113
python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I love you'))"
114
115
116
117
```

## Editable install

Steven Liu's avatar
Steven Liu committed
118
119
You will need an editable install if you'd like to:

120
* Use the `main` version of the source code.
Steven Liu's avatar
Steven Liu committed
121
* Contribute to 🤗 Transformers and need to test changes in the code.
122

Steven Liu's avatar
Steven Liu committed
123
124
125
Clone the repository and install 🤗 Transformers with the following commands:

```bash
126
127
git clone https://github.com/huggingface/transformers.git
cd transformers
128
129
130
pip install -e .
```

Steven Liu's avatar
Steven Liu committed
131
These commands will link the folder you cloned the repository to and your Python library paths. Python will now look inside the folder you cloned to in addition to the normal library paths. For example, if your Python packages are typically installed in `~/anaconda3/envs/main/lib/python3.7/site-packages/`, Python will also search the folder you cloned to: `~/transformers/`.
132

Steven Liu's avatar
Steven Liu committed
133
<Tip warning={true}>
134

Steven Liu's avatar
Steven Liu committed
135
You must keep the `transformers` folder if you want to keep using the library.
136

Steven Liu's avatar
Steven Liu committed
137
138
139
140
141
</Tip>

Now you can easily update your clone to the latest version of 🤗 Transformers with the following command:

```bash
142
143
cd ~/transformers/
git pull
144
145
```

146
Your Python environment will find the `main` version of 🤗 Transformers on the next run.
147

Steven Liu's avatar
Steven Liu committed
148
## Install with conda
149

Steven Liu's avatar
Steven Liu committed
150
Install from the conda channel `huggingface`:
151

Steven Liu's avatar
Steven Liu committed
152
```bash
153
154
155
conda install -c huggingface transformers
```

Steven Liu's avatar
Steven Liu committed
156
## Cache setup
157

158
Pretrained models are downloaded and locally cached at: `~/.cache/huggingface/hub`. This is the default directory given by the shell environment variable `TRANSFORMERS_CACHE`. On Windows, the default directory is given by `C:\Users\username\.cache\huggingface\hub`. You can change the shell environment variables shown below - in order of priority - to specify a different cache directory:
159

160
161
162
1. Shell environment variable (default): `HUGGINGFACE_HUB_CACHE` or `TRANSFORMERS_CACHE`.
2. Shell environment variable: `HF_HOME`.
3. Shell environment variable: `XDG_CACHE_HOME` + `/huggingface`.
163

Steven Liu's avatar
Steven Liu committed
164
<Tip>
165

Steven Liu's avatar
Steven Liu committed
166
🤗 Transformers will use the shell environment variables `PYTORCH_TRANSFORMERS_CACHE` or `PYTORCH_PRETRAINED_BERT_CACHE` if you are coming from an earlier iteration of this library and have set those environment variables, unless you specify the shell environment variable `TRANSFORMERS_CACHE`.
167

Steven Liu's avatar
Steven Liu committed
168
</Tip>
169

Steven Liu's avatar
Steven Liu committed
170
## Offline mode
171

172
Run 🤗 Transformers in a firewalled or offline environment with locally cached files by setting the environment variable `TRANSFORMERS_OFFLINE=1`.
173

Steven Liu's avatar
Steven Liu committed
174
<Tip>
175

176
Add [🤗 Datasets](https://huggingface.co/docs/datasets/) to your offline training workflow with the environment variable `HF_DATASETS_OFFLINE=1`.
177

Steven Liu's avatar
Steven Liu committed
178
</Tip>
179

Steven Liu's avatar
Steven Liu committed
180
```bash
181
HF_DATASETS_OFFLINE=1 TRANSFORMERS_OFFLINE=1 \
Sylvain Gugger's avatar
Sylvain Gugger committed
182
python examples/pytorch/translation/run_translation.py --model_name_or_path t5-small --dataset_name wmt16 --dataset_config ro-en ...
183
184
```

185
This script should run without hanging or waiting to timeout because it won't attempt to download the model from the Hub.
Steven Liu's avatar
Steven Liu committed
186

187
You can also bypass loading a model from the Hub from each [`~PreTrainedModel.from_pretrained`] call with the [`local_files_only`] parameter. When set to `True`, only local files are loaded:
188

189
190
191
192
193
```py
from transformers import T5Model

model = T5Model.from_pretrained("./path/to/local/directory", local_files_only=True)
```
Steven Liu's avatar
Steven Liu committed
194
195
196
197
198
199
200
201
202
203
204
205

### Fetch models and tokenizers to use offline

Another option for using 🤗 Transformers offline is to download the files ahead of time, and then point to their local path when you need to use them offline. There are three ways to do this:

* Download a file through the user interface on the [Model Hub](https://huggingface.co/models) by clicking on the ↓ icon.

    ![download-icon](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/download-icon.png)

* Use the [`PreTrainedModel.from_pretrained`] and [`PreTrainedModel.save_pretrained`] workflow:

    1. Download your files ahead of time with [`PreTrainedModel.from_pretrained`]:
206

Steven Liu's avatar
Steven Liu committed
207
208
    ```py
    >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
209

Steven Liu's avatar
Steven Liu committed
210
211
212
    >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/T0_3B")
    >>> model = AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0_3B")
    ```
213

Steven Liu's avatar
Steven Liu committed
214
    2. Save your files to a specified directory with [`PreTrainedModel.save_pretrained`]:
215

Steven Liu's avatar
Steven Liu committed
216
217
218
219
    ```py
    >>> tokenizer.save_pretrained("./your/path/bigscience_t0")
    >>> model.save_pretrained("./your/path/bigscience_t0")
    ```
220

Steven Liu's avatar
Steven Liu committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
    3. Now when you're offline, reload your files with [`PreTrainedModel.from_pretrained`] from the specified directory:

    ```py
    >>> tokenizer = AutoTokenizer.from_pretrained("./your/path/bigscience_t0")
    >>> model = AutoModel.from_pretrained("./your/path/bigscience_t0")
    ```

* Programmatically download files with the [huggingface_hub](https://github.com/huggingface/huggingface_hub/tree/main/src/huggingface_hub) library:

    1. Install the `huggingface_hub` library in your virtual environment:

    ```bash
    python -m pip install huggingface_hub
    ```

    2. Use the [`hf_hub_download`](https://huggingface.co/docs/hub/adding-a-library#download-files-from-the-hub) function to download a file to a specific path. For example, the following command downloads the `config.json` file from the [T0](https://huggingface.co/bigscience/T0_3B) model to your desired path:

    ```py
    >>> from huggingface_hub import hf_hub_download

    >>> hf_hub_download(repo_id="bigscience/T0_3B", filename="config.json", cache_dir="./your/path/bigscience_t0")
    ```

Once your file is downloaded and locally cached, specify it's local path to load and use it:

```py
>>> from transformers import AutoConfig

>>> config = AutoConfig.from_pretrained("./your/path/bigscience_t0/config.json")
```
251

Steven Liu's avatar
Steven Liu committed
252
<Tip>
253

Steven Liu's avatar
Steven Liu committed
254
See the [How to download files from the Hub](https://huggingface.co/docs/hub/how-to-downstream) section for more details on downloading files stored on the Hub.
255

256
</Tip>