installation.md 4.63 KB
Newer Older
1
2
# Installation

3
馃 Transformers is tested on Python 3.6+, and PyTorch 1.1.0+ or TensorFlow 2.0+.
4

5
6
7
You should install 馃 Transformers in a [virtual environment](https://docs.python.org/3/library/venv.html). If you're
unfamiliar with Python virtual environments, check out the [user guide](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/). Create a virtual environment with the version of Python you're going 
to use and activate it.
8

9
10
Now, if you want to use 馃 Transformers, you can install it with pip. If you'd like to play with the examples, you
must install it from source.
11

12
13
14
## Installation with pip

First you need to install one of, or both, TensorFlow 2.0 and PyTorch.
15
16
17
18
Please refer to [TensorFlow installation page](https://www.tensorflow.org/install/pip#tensorflow-2.0-rc-is-available), 
[PyTorch installation page](https://pytorch.org/get-started/locally/#start-locally) and/or 
[Flax installation page](https://github.com/google/flax#quick-install)
regarding the specific install command for your platform.
19
20
21
22

When TensorFlow 2.0 and/or PyTorch has been installed, 馃 Transformers can be installed using pip as follows:

```bash
23
24
25
pip install transformers
```

26
Alternatively, for CPU-support only, you can install 馃 Transformers and PyTorch in one line with:
27
28
29
30
31

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

32
or 馃 Transformers and TensorFlow 2.0 in one line with:
33
34
35
36
37

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

38
39
40
41
42
43
or 馃 Transformers and Flax in one line with:

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

44
45
46
To check 馃 Transformers is properly installed, run the following command:

```bash
47
python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))"
48
49
50
51
52
```

It should download a pretrained model then print something like

```bash
53
[{'label': 'POSITIVE', 'score': 0.9998704791069031}]
54
55
56
57
58
```

(Note that TensorFlow will print additional stuff before that last statement.)

## Installing from source
59

60
To install from source, clone the repository and install with the following commands:
61
62
63
64

``` bash
git clone https://github.com/huggingface/transformers.git
cd transformers
65
66
67
68
69
70
71
pip install -e .
```

Again, you can run 

```bash
python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I hate you'))"
72
73
```

74
75
to check 馃 Transformers is properly installed.

76
77
78
79
80
81
82
83
84
85
86
87
88

## With conda

Since Transformers version v4.0.0, we now have a conda channel: `huggingface`.

馃 Transformers can be installed using conda as follows:

```
conda install -c huggingface transformers
```

Follow the installation pages of TensorFlow, PyTorch or Flax to see how to install them with conda. 

89
90
91
## Caching models

This library provides pretrained models that will be downloaded and cached locally. Unless you specify a location with
92
`cache_dir=...` when you use methods like `from_pretrained`, these models will automatically be downloaded in the
93
94
folder given by the shell environment variable ``TRANSFORMERS_CACHE``. The default value for it will be the Hugging
Face cache home followed by ``/transformers/``. This is (by order of priority):
95

96
97
98
  * shell environment variable ``HF_HOME`` 
  * shell environment variable ``XDG_CACHE_HOME`` + ``/huggingface/``
  * default: ``~/.cache/huggingface/``
99

100
So if you don't have any specific environment variable set, the cache directory will be at
101
``~/.cache/huggingface/transformers/``.
102

103
**Note:** If you have set a shell environment variable for one of the predecessors of this library
104
(``PYTORCH_TRANSFORMERS_CACHE`` or ``PYTORCH_PRETRAINED_BERT_CACHE``), those will be used if there is no shell
105
environment variable for ``TRANSFORMERS_CACHE``.
106

107
### Note on model downloads (Continuous Integration or large-scale deployments)
108

109
110
111
If you expect to be downloading large volumes of models (more than 1,000) from our hosted bucket (for instance through
your CI setup, or a large-scale production deployment), please cache the model files on your end. It will be way
faster, and cheaper. Feel free to contact us privately if you need any help.
112
113
114
115
116

## Do you want to run a Transformer model on a mobile device?

You should check out our [swift-coreml-transformers](https://github.com/huggingface/swift-coreml-transformers) repo.

117
118
It contains a set of tools to convert PyTorch or TensorFlow 2.0 trained Transformer models (currently contains `GPT-2`, 
`DistilGPT-2`, `BERT`, and `DistilBERT`) to CoreML models that run on iOS devices.
119

120
At some point in the future, you'll be able to seamlessly move from pretraining or fine-tuning models in PyTorch or
121
122
TensorFlow 2.0 to productizing them in CoreML, or prototype a model or an app in CoreML then research its
hyperparameters or architecture from PyTorch or TensorFlow 2.0. Super exciting!