installation.mdx 9.18 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
15
16

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.
-->

17
18
# Installation

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

Steven Liu's avatar
Steven Liu committed
21
🤗 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:
22

Steven Liu's avatar
Steven Liu committed
23
24
25
* [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.
26

Steven Liu's avatar
Steven Liu committed
27
## Install with pip
28

Steven Liu's avatar
Steven Liu committed
29
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.
30

Steven Liu's avatar
Steven Liu committed
31
32
33
34
35
36
37
38
39
40
41
42
43
Start by creating a virtual environment in your project directory:

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

Activate the virtual environment:

```bash
source .env/bin/activate
```

Now you're ready to install 🤗 Transformers with the following command:
44
45

```bash
46
47
48
pip install transformers
```

Steven Liu's avatar
Steven Liu committed
49
For CPU-support only, you can conveniently install 🤗 Transformers and a deep learning library in one line. For example, install 🤗 Transformers and PyTorch with:
50
51
52
53
54

```bash
pip install transformers[torch]
```

Steven Liu's avatar
Steven Liu committed
55
🤗 Transformers and TensorFlow 2.0:
56
57
58
59
60

```bash
pip install transformers[tf-cpu]
```

Steven Liu's avatar
Steven Liu committed
61
🤗 Transformers and Flax:
62
63
64
65
66

```bash
pip install transformers[flax]
```

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

```bash
70
python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))"
71
72
```

Steven Liu's avatar
Steven Liu committed
73
Then print out the label and score:
74
75

```bash
76
[{'label': 'POSITIVE', 'score': 0.9998704791069031}]
77
78
```

Steven Liu's avatar
Steven Liu committed
79
## Install from source
80

Steven Liu's avatar
Steven Liu committed
81
Install 🤗 Transformers from source with the following command:
82
83
84
85
86

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

Steven Liu's avatar
Steven Liu committed
87
This command installs the bleeding edge `master` version rather than the latest `stable` version. The `master` 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 `master` version may not always be stable. We strive to keep the `master` 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!
88

Steven Liu's avatar
Steven Liu committed
89
Check if 🤗 Transformers has been properly installed by running the following command:
90
91

```bash
Steven Liu's avatar
Steven Liu committed
92
python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I love you'))"
93
94
95
96
```

## Editable install

Steven Liu's avatar
Steven Liu committed
97
98
99
100
You will need an editable install if you'd like to:

* Use the `master` version of the source code.
* Contribute to 🤗 Transformers and need to test changes in the code.
101

Steven Liu's avatar
Steven Liu committed
102
103
104
Clone the repository and install 🤗 Transformers with the following commands:

```bash
105
106
git clone https://github.com/huggingface/transformers.git
cd transformers
107
108
109
pip install -e .
```

Steven Liu's avatar
Steven Liu committed
110
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/`.
111

Steven Liu's avatar
Steven Liu committed
112
<Tip warning={true}>
113

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

Steven Liu's avatar
Steven Liu committed
116
117
118
119
120
</Tip>

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

```bash
121
122
cd ~/transformers/
git pull
123
124
```

Steven Liu's avatar
Steven Liu committed
125
Your Python environment will find the `master` version of 🤗 Transformers on the next run.
126

Steven Liu's avatar
Steven Liu committed
127
## Install with conda
128

Steven Liu's avatar
Steven Liu committed
129
Install from the conda channel `huggingface`:
130

Steven Liu's avatar
Steven Liu committed
131
```bash
132
133
134
conda install -c huggingface transformers
```

Steven Liu's avatar
Steven Liu committed
135
## Cache setup
136

Steven Liu's avatar
Steven Liu committed
137
Pretrained models are downloaded and locally cached at: `~/.cache/huggingface/transformers/`. 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\transformers`. You can change the shell environment variables shown below - in order of priority - to specify a different cache directory:
138

Steven Liu's avatar
Steven Liu committed
139
140
141
1. Shell environment variable (default): `TRANSFORMERS_CACHE`.
2. Shell environment variable: `HF_HOME` + `transformers/`.
3. Shell environment variable: `XDG_CACHE_HOME` + `/huggingface/transformers`.
142

Steven Liu's avatar
Steven Liu committed
143
<Tip>
144

Steven Liu's avatar
Steven Liu committed
145
🤗 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`.
146

Steven Liu's avatar
Steven Liu committed
147
</Tip>
148

Steven Liu's avatar
Steven Liu committed
149
## Offline mode
150

Steven Liu's avatar
Steven Liu committed
151
🤗 Transformers is able to run in a firewalled or offline environment by only using local files. Set the environment variable `TRANSFORMERS_OFFLINE=1` to enable this behavior.
152

Steven Liu's avatar
Steven Liu committed
153
<Tip>
154

Steven Liu's avatar
Steven Liu committed
155
Add [🤗 Datasets](https://huggingface.co/docs/datasets/) to your offline training workflow by setting the environment variable `HF_DATASETS_OFFLINE=1`.
156

Steven Liu's avatar
Steven Liu committed
157
</Tip>
158

Steven Liu's avatar
Steven Liu committed
159
For example, you would typically run a program on a normal network firewalled to external instances with the following command:
160

Steven Liu's avatar
Steven Liu committed
161
```bash
Sylvain Gugger's avatar
Sylvain Gugger committed
162
python examples/pytorch/translation/run_translation.py --model_name_or_path t5-small --dataset_name wmt16 --dataset_config ro-en ...
163
164
```

Steven Liu's avatar
Steven Liu committed
165
166
167
Run this same program in an offline instance with:

```bash
168
HF_DATASETS_OFFLINE=1 TRANSFORMERS_OFFLINE=1 \
Sylvain Gugger's avatar
Sylvain Gugger committed
169
python examples/pytorch/translation/run_translation.py --model_name_or_path t5-small --dataset_name wmt16 --dataset_config ro-en ...
170
171
```

Steven Liu's avatar
Steven Liu committed
172
173
174
175
176
177
178
179
180
181
182
183
184
The script should now run without hanging or waiting to timeout because it knows it should only look for local files.

### 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`]:
185

Steven Liu's avatar
Steven Liu committed
186
187
    ```py
    >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
188

Steven Liu's avatar
Steven Liu committed
189
190
191
    >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/T0_3B")
    >>> model = AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0_3B")
    ```
192

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

Steven Liu's avatar
Steven Liu committed
195
196
197
198
    ```py
    >>> tokenizer.save_pretrained("./your/path/bigscience_t0")
    >>> model.save_pretrained("./your/path/bigscience_t0")
    ```
199

Steven Liu's avatar
Steven Liu committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
    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")
```
230

Steven Liu's avatar
Steven Liu committed
231
<Tip>
232

Steven Liu's avatar
Steven Liu committed
233
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.
234

Steven Liu's avatar
Steven Liu committed
235
</Tip>