|
<!--- |
|
Copyright 2022 The HuggingFace Team. All rights reserved. |
|
|
|
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. |
|
|
|
โ ๏ธ 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. |
|
|
|
--> |
|
|
|
# ์ค์น๋ฐฉ๋ฒ[[installation]] |
|
|
|
๐ค Transformers๋ฅผ ์ฌ์ฉ ์ค์ธ ๋ฅ๋ฌ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋ง์ถฐ ์ค์นํ๊ณ , ์บ์๋ฅผ ๊ตฌ์ฑํ๊ฑฐ๋ ์ ํ์ ์ผ๋ก ์คํ๋ผ์ธ์์๋ ์คํํ ์ ์๋๋ก ๐ค Transformers๋ฅผ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ฐ๊ฒ ์ต๋๋ค. |
|
|
|
๐ค Transformers๋ Python 3.6+, PyTorch 1.1.0+, TensorFlow 2.0+ ๋ฐ Flax์์ ํ
์คํธ๋์์ต๋๋ค. ๋ฅ๋ฌ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํ๋ ค๋ฉด ์๋ ๋งํฌ๋ ์ ๋ง๋ค์ ๊ณต์ ์ฌ์ดํธ๋ฅผ ์ฐธ๊ณ ํด์ฃผ์ธ์. |
|
|
|
* [PyTorch](https://pytorch.org/get-started/locally/) ์ค์นํ๊ธฐ |
|
* [TensorFlow 2.0](https://www.tensorflow.org/install/pip) ์ค์นํ๊ธฐ |
|
* [Flax](https://flax.readthedocs.io/en/latest/) ์ค์นํ๊ธฐ |
|
|
|
## pip์ผ๋ก ์ค์นํ๊ธฐ[[install-with-pip]] |
|
|
|
๐ค Transformers๋ฅผ [๊ฐ์ ํ๊ฒฝ](https://docs.python.org/3/library/venv.html)์ ์ค์นํ๋ ๊ฒ์ ์ถ์ฒ๋๋ฆฝ๋๋ค. Python ๊ฐ์ ํ๊ฒฝ์ ์ต์ํ์ง ์๋ค๋ฉด, ์ด [๊ฐ์ด๋](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/)๋ฅผ ์ฐธ๊ณ ํ์ธ์. ๊ฐ์ ํ๊ฒฝ์ ์ฌ์ฉํ๋ฉด ์๋ก ๋ค๋ฅธ ํ๋ก์ ํธ๋ค์ ๋ณด๋ค ์ฝ๊ฒ ๊ด๋ฆฌํ ์ ์๊ณ , ์์กด์ฑ ๊ฐ์ ํธํ์ฑ ๋ฌธ์ ๋ฅผ ๋ฐฉ์งํ ์ ์์ต๋๋ค. |
|
|
|
๋จผ์ ํ๋ก์ ํธ ๋๋ ํ ๋ฆฌ์์ ๊ฐ์ ํ๊ฒฝ์ ๋ง๋ค์ด ์ค๋๋ค. |
|
|
|
```bash |
|
python -m venv .env |
|
``` |
|
|
|
๊ฐ์ ํ๊ฒฝ์ ํ์ฑํํด์ฃผ์ธ์. Linux๋ MacOS์ ๊ฒฝ์ฐ: |
|
|
|
```bash |
|
source .env/bin/activate |
|
``` |
|
Windows์ ๊ฒฝ์ฐ: |
|
|
|
```bash |
|
.env/Scripts/activate |
|
``` |
|
|
|
์ด์ ๐ค Transformers๋ฅผ ์ค์นํ ์ค๋น๊ฐ ๋์์ต๋๋ค. ๋ค์ ๋ช
๋ น์ ์
๋ ฅํด์ฃผ์ธ์. |
|
|
|
```bash |
|
pip install transformers |
|
``` |
|
|
|
CPU๋ง ์จ๋ ๋๋ค๋ฉด, ๐ค Transformers์ ๋ฅ๋ฌ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋จ 1์ค๋ก ์ค์นํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ๐ค Transformers์ PyTorch์ ๊ฒฝ์ฐ: |
|
|
|
```bash |
|
pip install transformers[torch] |
|
``` |
|
|
|
๐ค Transformers์ TensorFlow 2.0์ ๊ฒฝ์ฐ: |
|
|
|
```bash |
|
pip install transformers[tf-cpu] |
|
``` |
|
|
|
๐ค Transformers์ Flax์ ๊ฒฝ์ฐ: |
|
|
|
```bash |
|
pip install transformers[flax] |
|
``` |
|
|
|
๋ง์ง๋ง์ผ๋ก ๐ค Transformers๊ฐ ์ ๋๋ก ์ค์น๋์๋์ง ํ์ธํ ์ฐจ๋ก์
๋๋ค. ์ฌ์ ํ๋ จ๋ ๋ชจ๋ธ์ ๋ค์ด๋ก๋ํ๋ ์ฝ๋์
๋๋ค. |
|
|
|
```bash |
|
python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))" |
|
``` |
|
|
|
๋ผ๋ฒจ๊ณผ ์ ์๊ฐ ์ถ๋ ฅ๋๋ฉด ์ ์ค์น๋ ๊ฒ์
๋๋ค. |
|
|
|
```bash |
|
[{'label': 'POSITIVE', 'score': 0.9998704791069031}] |
|
``` |
|
|
|
## ์์ค์์ ์ค์นํ๊ธฐ[[install-from-source]] |
|
|
|
๐ค Transformers๋ฅผ ์์ค์์ ์ค์นํ๋ ค๋ฉด ์๋ ๋ช
๋ น์ ์คํํ์ธ์. |
|
|
|
```bash |
|
pip install git+https://github.com/huggingface/transformers |
|
``` |
|
|
|
์ ๋ช
๋ น์ ์ต์ ์ด์ง๋ง (์์ ์ ์ธ) `stable` ๋ฒ์ ์ด ์๋ ์คํ์ฑ์ด ์ง์ `main` ๋ฒ์ ์ ์ค์นํฉ๋๋ค. `main` ๋ฒ์ ์ ๊ฐ๋ฐ ํํฉ๊ณผ ๋ฐ๋ง์ถ๋๋ฐ ์ ์ฉํฉ๋๋ค. ์์๋ก ๋ง์ง๋ง ๊ณต์ ๋ฆด๋ฆฌ์ค ์ดํ ๋ฐ๊ฒฌ๋ ๋ฒ๊ทธ๊ฐ ํจ์น๋์์ง๋ง, ์ ๋ฆด๋ฆฌ์ค๋ก ์์ง ๋กค์์๋์ง๋ ์์ ๊ฒฝ์ฐ๋ฅผ ๋ค ์ ์์ต๋๋ค. ๋ฐ๊ฟ ๋งํ๋ฉด `main` ๋ฒ์ ์ด ์์ ์ฑ๊ณผ๋ ๊ฑฐ๋ฆฌ๊ฐ ์๋ค๋ ๋ป์ด๊ธฐ๋ ํฉ๋๋ค. ์ ํฌ๋ `main` ๋ฒ์ ์ ์ฌ์ฉํ๋๋ฐ ๋ฌธ์ ๊ฐ ์๋๋ก ๋
ธ๋ ฅํ๊ณ ์์ผ๋ฉฐ, ๋๋ถ๋ถ์ ๋ฌธ์ ๋ ๋๊ฐ ๋ช ์๊ฐ์ด๋ ํ๋ฃจ ์์ ํด๊ฒฐ๋ฉ๋๋ค. ๋ง์ฝ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ฉด [์ด์](https://github.com/huggingface/transformers/issues)๋ฅผ ์ด์ด์ฃผ์๋ฉด ๋ ๋นจ๋ฆฌ ํด๊ฒฐํ ์ ์์ต๋๋ค! |
|
|
|
์ ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๐ค Transformers๊ฐ ์ ๋๋ก ์ค์น๋์๋์ง ํ์ธํ ์ฐจ๋ก์
๋๋ค. |
|
|
|
```bash |
|
python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I love you'))" |
|
``` |
|
|
|
## ์์ ๊ฐ๋ฅํ ์ค์น[[editable-install]] |
|
|
|
์์ ๊ฐ๋ฅํ ์ค์น๊ฐ ํ์ํ ๊ฒฝ์ฐ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. |
|
|
|
* `main` ๋ฒ์ ์ ์์ค ์ฝ๋๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด |
|
* ๐ค Transformers์ ๊ธฐ์ฌํ๊ณ ์ถ์ด์ ์ฝ๋์ ๋ณ๊ฒฝ ์ฌํญ์ ํ
์คํธํ๊ธฐ ์ํด |
|
|
|
๋ฆฌํฌ์งํฐ๋ฆฌ๋ฅผ ๋ณต์ ํ๊ณ ๐ค Transformers๋ฅผ ์ค์นํ๋ ค๋ฉด ๋ค์ ๋ช
๋ น์ ์
๋ ฅํด์ฃผ์ธ์. |
|
|
|
```bash |
|
git clone https://github.com/huggingface/transformers.git |
|
cd transformers |
|
pip install -e . |
|
``` |
|
|
|
์ ๋ช
๋ น์ ๋ฆฌํฌ์งํฐ๋ฆฌ๋ฅผ ๋ณต์ ํ ์์น์ ํด๋์ Python ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๊ฒฝ๋ก๋ฅผ ์ฐ๊ฒฐ์ํต๋๋ค. Python์ด ์ผ๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฒฝ๋ก ์ธ์ ๋ณต์ ํ ํด๋ ๋ด๋ถ๋ฅผ ํ์ธํ ๊ฒ์
๋๋ค. ์๋ฅผ ๋ค์ด Python ํจํค์ง๊ฐ ์ผ๋ฐ์ ์ผ๋ก `~/anaconda3/envs/main/lib/python3.7/site-packages/`์ ์ค์น๋์ด ์๋๋ฐ, ๋ช
๋ น์ ๋ฐ์ Python์ด ์ด์ ๋ณต์ ํ ํด๋์ธ `~/transformers/`๋ ๊ฒ์ํ๊ฒ ๋ฉ๋๋ค. |
|
|
|
<Tip warning={true}> |
|
|
|
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ณ์ ์ฌ์ฉํ๋ ค๋ฉด `transformers` ํด๋๋ฅผ ๊ผญ ์ ์งํด์ผ ํฉ๋๋ค. |
|
|
|
</Tip> |
|
|
|
๋ณต์ ๋ณธ์ ์ต์ ๋ฒ์ ์ ๐ค Transformers๋ก ์ฝ๊ฒ ์
๋ฐ์ดํธํ ์ ์์ต๋๋ค. |
|
|
|
```bash |
|
cd ~/transformers/ |
|
git pull |
|
``` |
|
|
|
Python ํ๊ฒฝ์ ๋ค์ ์คํํ๋ฉด ์
๋ฐ์ดํธ๋ ๐ค Transformers์ `main` ๋ฒ์ ์ ์ฐพ์๋ผ ๊ฒ์
๋๋ค. |
|
|
|
## conda๋ก ์ค์นํ๊ธฐ[[install-with-conda]] |
|
|
|
`huggingface` conda ์ฑ๋์์ ์ค์นํ ์ ์์ต๋๋ค. |
|
|
|
```bash |
|
conda install -c huggingface transformers |
|
``` |
|
|
|
## ์บ์ ๊ตฌ์ฑํ๊ธฐ[[cache-setup]] |
|
|
|
์ฌ์ ํ๋ จ๋ ๋ชจ๋ธ์ ๋ค์ด๋ก๋๋ ํ ๋ก์ปฌ ๊ฒฝ๋ก `~/.cache/huggingface/hub`์ ์บ์๋ฉ๋๋ค. ์
ธ ํ๊ฒฝ ๋ณ์ `TRANSFORMERS_CACHE`์ ๊ธฐ๋ณธ ๋๋ ํฐ๋ฆฌ์
๋๋ค. Windows์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ๋๋ ํฐ๋ฆฌ๋ `C:\Users\username\.cache\huggingface\hub`์
๋๋ค. ์๋์ ์
ธ ํ๊ฒฝ ๋ณ์๋ฅผ (์ฐ์ ์์) ์์๋๋ก ๋ณ๊ฒฝํ์ฌ ๋ค๋ฅธ ์บ์ ๋๋ ํ ๋ฆฌ๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค. |
|
|
|
1. ์
ธ ํ๊ฒฝ ๋ณ์ (๊ธฐ๋ณธ): `HUGGINGFACE_HUB_CACHE` ๋๋ `TRANSFORMERS_CACHE` |
|
2. ์
ธ ํ๊ฒฝ ๋ณ์: `HF_HOME` |
|
3. ์
ธ ํ๊ฒฝ ๋ณ์: `XDG_CACHE_HOME` + `/huggingface` |
|
|
|
<Tip> |
|
|
|
๊ณผ๊ฑฐ ๐ค Transformers์์ ์ฐ์๋ ์
ธ ํ๊ฒฝ ๋ณ์ `PYTORCH_TRANSFORMERS_CACHE` ๋๋ `PYTORCH_PRETRAINED_BERT_CACHE`์ด ์ค์ ๋์๋ค๋ฉด, ์
ธ ํ๊ฒฝ ๋ณ์ `TRANSFORMERS_CACHE`์ ์ง์ ํ์ง ์๋ ํ ์ฐ์ ์ฌ์ฉ๋ฉ๋๋ค. |
|
|
|
</Tip> |
|
|
|
## ์คํ๋ผ์ธ ๋ชจ๋[[offline-mode]] |
|
|
|
๐ค Transformers๋ฅผ ๋ก์ปฌ ํ์ผ๋ง ์ฌ์ฉํ๋๋ก ํด์ ๋ฐฉํ๋ฒฝ ๋๋ ์คํ๋ผ์ธ ํ๊ฒฝ์์ ์คํํ ์ ์์ต๋๋ค. ํ์ฑํํ๋ ค๋ฉด `TRANSFORMERS_OFFLINE=1` ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ธ์. |
|
|
|
<Tip> |
|
|
|
`HF_DATASETS_OFFLINE=1` ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ฌ ์คํ๋ผ์ธ ํ๋ จ ๊ณผ์ ์ [๐ค Datasets](https://huggingface.co/docs/datasets/)์ ์ถ๊ฐํ ์ ์์ต๋๋ค. |
|
|
|
</Tip> |
|
|
|
์๋ฅผ ๋ค์ด ์ธ๋ถ ๊ธฐ๊ธฐ ์ฌ์ด์ ๋ฐฉํ๋ฒฝ์ ๋ ์ผ๋ฐ ๋คํธ์ํฌ์์ ํ์์ฒ๋ผ ํ๋ก๊ทธ๋จ์ ๋ค์๊ณผ ๊ฐ์ด ์คํํ ์ ์์ต๋๋ค. |
|
|
|
```bash |
|
python examples/pytorch/translation/run_translation.py --model_name_or_path t5-small --dataset_name wmt16 --dataset_config ro-en ... |
|
``` |
|
|
|
์คํ๋ผ์ธ ๊ธฐ๊ธฐ์์ ๋์ผํ ํ๋ก๊ทธ๋จ์ ๋ค์๊ณผ ๊ฐ์ด ์คํํ ์ ์์ต๋๋ค. |
|
|
|
```bash |
|
HF_DATASETS_OFFLINE=1 TRANSFORMERS_OFFLINE=1 \ |
|
python examples/pytorch/translation/run_translation.py --model_name_or_path t5-small --dataset_name wmt16 --dataset_config ro-en ... |
|
``` |
|
|
|
์ด์ ์คํฌ๋ฆฝํธ๋ ๋ก์ปฌ ํ์ผ์ ํํด์๋ง ๊ฒ์ํ ๊ฒ์ด๋ฏ๋ก, ์คํฌ๋ฆฝํธ๊ฐ ์ค๋จ๋๊ฑฐ๋ ์๊ฐ์ด ์ด๊ณผ๋ ๋๊น์ง ๋ฉ์ถฐ์์ง ์๊ณ ์ ์คํ๋ ๊ฒ์
๋๋ค. |
|
|
|
### ์คํ๋ผ์ธ์ฉ ๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์ ๋ง๋ค์ด๋๊ธฐ[[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: |
|
๐ค Transformers๋ฅผ ์คํ๋ผ์ธ์ผ๋ก ์ฌ์ฉํ๋ ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ํ์ผ์ ๋ฏธ๋ฆฌ ๋ค์ด๋ก๋ํ ๋ค์, ์คํ๋ผ์ธ์ผ ๋ ์ฌ์ฉํ ๋ก์ปฌ ๊ฒฝ๋ก๋ฅผ ์ง์ ํด๋๋ ๊ฒ์
๋๋ค. 3๊ฐ์ง ์ค ํธํ ๋ฐฉ๋ฒ์ ๊ณ ๋ฅด์ธ์. |
|
|
|
* [Model Hub](https://huggingface.co/models)์ UI๋ฅผ ํตํด ํ์ผ์ ๋ค์ด๋ก๋ํ๋ ค๋ฉด โ ์์ด์ฝ์ ํด๋ฆญํ์ธ์. |
|
|
|
 |
|
|
|
* [`PreTrainedModel.from_pretrained`]์ [`PreTrainedModel.save_pretrained`] ์ํฌํ๋ก๋ฅผ ํ์ฉํ์ธ์. |
|
|
|
1. ๋ฏธ๋ฆฌ [`PreTrainedModel.from_pretrained`]๋ก ํ์ผ์ ๋ค์ด๋ก๋ํด๋์ธ์. |
|
|
|
```py |
|
>>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM |
|
|
|
>>> tokenizer = AutoTokenizer.from_pretrained("bigscience/T0_3B") |
|
>>> model = AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0_3B") |
|
``` |
|
|
|
2. [`PreTrainedModel.save_pretrained`]๋ก ์ง์ ๋ ๊ฒฝ๋ก์ ํ์ผ์ ์ ์ฅํด๋์ธ์. |
|
|
|
```py |
|
>>> tokenizer.save_pretrained("./your/path/bigscience_t0") |
|
>>> model.save_pretrained("./your/path/bigscience_t0") |
|
``` |
|
|
|
3. ์ด์ ์คํ๋ผ์ธ์ผ ๋ [`PreTrainedModel.from_pretrained`]๋ก ์ ์ฅํด๋๋ ํ์ผ์ ์ง์ ๋ ๊ฒฝ๋ก์์ ๋ค์ ๋ถ๋ฌ์ค์ธ์. |
|
|
|
```py |
|
>>> tokenizer = AutoTokenizer.from_pretrained("./your/path/bigscience_t0") |
|
>>> model = AutoModel.from_pretrained("./your/path/bigscience_t0") |
|
``` |
|
|
|
* [huggingface_hub](https://github.com/huggingface/huggingface_hub/tree/main/src/huggingface_hub) ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํ์ฉํด์ ํ์ผ์ ๋ค์ด๋ก๋ํ์ธ์. |
|
|
|
1. ๊ฐ์ํ๊ฒฝ์ `huggingface_hub` ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํ์ธ์. |
|
|
|
```bash |
|
python -m pip install huggingface_hub |
|
``` |
|
|
|
2. [`hf_hub_download`](https://huggingface.co/docs/hub/adding-a-library#download-files-from-the-hub) ํจ์๋ก ํ์ผ์ ํน์ ์์น์ ๋ค์ด๋ก๋ํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ์๋ ๋ช
๋ น์ [T0](https://huggingface.co/bigscience/T0_3B) ๋ชจ๋ธ์ `config.json` ํ์ผ์ ์ง์ ๋ ๊ฒฝ๋ก์ ๋ค์ด๋ก๋ํฉ๋๋ค. |
|
|
|
```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") |
|
``` |
|
|
|
ํ์ผ์ ๋ค์ด๋ก๋ํ๊ณ ๋ก์ปฌ์ ์บ์ ํด๋๊ณ ๋๋ฉด, ๋์ค์ ๋ถ๋ฌ์ ์ฌ์ฉํ ์ ์๋๋ก ๋ก์ปฌ ๊ฒฝ๋ก๋ฅผ ์ง์ ํด๋์ธ์. |
|
|
|
```py |
|
>>> from transformers import AutoConfig |
|
|
|
>>> config = AutoConfig.from_pretrained("./your/path/bigscience_t0/config.json") |
|
``` |
|
|
|
<Tip> |
|
|
|
Hub์ ์ ์ฅ๋ ํ์ผ์ ๋ค์ด๋ก๋ํ๋ ๋ฐฉ๋ฒ์ ๋ ์์ธํ ์์๋ณด๋ ค๋ฉด [Hub์์ ํ์ผ ๋ค์ด๋ก๋ํ๊ธฐ](https://huggingface.co/docs/hub/how-to-downstream) ์น์
์ ์ฐธ๊ณ ํด์ฃผ์ธ์. |
|
|
|
</Tip> |