AI/기타

HuggingFace Hub에 모델 업로드

sangwonYoon 2023. 5. 18. 03:03

문장 내 개체간 관계 추출 모델을 만들면서 TAPT(Task Adaptive Pretraining)를 적용시킨 모델을 팀원들에게 공유하고자 Hugging Face Hub에 모델을 업로드했다.

이번에 얻은 경험을 통해 Git을 활용하여 Hugging Face Hub에 모델을 업로드하는 방법을 포스팅해보려고 한다.

 

사전 준비

 

1. 패키지 설치

pip install transformers

huggingface에서 제공하는 API를 활용할 수 있는 라이브러리인 transformers 패키지를 설치한다.

sudo apt-get install git-lfs # Linux OS인 경우

brew install git-lfs # Mac OS인 경우

모델의 크기가 상당히 크기 때문에 대용량 파일을 git repo에 올릴 때 사용하는 Git LFS(Large File Storage)를 설치한다.

 

2. 업로드 할 파일 준비

내가 이번에 업로드 한 파일은 위와 같다. 파일 하나씩 그 역할에 대해 살펴보자.

2-1. pytorch_model.bin

pytorch 모델의 checkpoint

2-2. config.json

모델이 저장된 상태를 나타내는 파일

// config.json
{
  "_name_or_path": "klue/roberta-large",
  "architectures": [
    "RobertaForMaskedLM"
  ],
  "attention_probs_dropout_prob": 0.1,
  "bos_token_id": 0,
  "classifier_dropout": null,
  "eos_token_id": 2,
  "gradient_checkpointing": false,
  "hidden_act": "gelu",
  "hidden_dropout_prob": 0.1,
  "hidden_size": 1024,
  "initializer_range": 0.02,
  "intermediate_size": 4096,
  "layer_norm_eps": 1e-05,
  "max_position_embeddings": 514,
  "model_type": "roberta",
  "num_attention_heads": 16,
  "num_hidden_layers": 24,
  "pad_token_id": 1,
  "position_embedding_type": "absolute",
  "tokenizer_class": "BertTokenizer",
  "torch_dtype": "float32",
  "transformers_version": "4.28.1",
  "type_vocab_size": 1,
  "use_cache": true,
  "vocab_size": 32000
}

2-3. tokenizer 관련 파일들

  • vocab.txt : tokenizer의 vocab이 저장되어 있는 파일
  • tokenizer.json : tokenizer의 전체적인 정보가 담겨있는 파일
  • tokenizer_config.json : tokenizer의 상태를 나타내는 파일
// tokenizer_config.json
{
  "do_lower_case": false,
  "do_basic_tokenize": true,
  "never_split": null,
  "unk_token": "[UNK]",
  "sep_token": "[SEP]",
  "pad_token": "[PAD]",
  "cls_token": "[CLS]",
  "mask_token": "[MASK]",
  "bos_token": "[CLS]",
  "eos_token": "[SEP]",
  "tokenize_chinese_chars": true,
  "strip_accents": null,
  "model_max_length": 512,
  "tokenizer_class": "BertTokenizer"
}
  • special_tokens_map.json : special 토큰에 대한 정보를 담고 있는 파일
// special_tokens_map.json
{
  "unk_token": "[UNK]",
  "sep_token": "[SEP]",
  "pad_token": "[PAD]",
  "cls_token": "[CLS]",
  "mask_token": "[MASK]",
  "bos_token": "[CLS]",
  "eos_token": "[SEP]"
}

내가 이번에 업로드 한 모델에서 tokenizer의 경우, klue/roberta-large 모델의 tokenizer를 그대로 가져와서 사용했기 때문에 파일들도 마찬가지로 klue/roberta-large의 저장소에서 가져와서 업로드했다.

pytorch_model.bin과 config.json은 huggingface trainer를 통해 학습시킨 뒤 trainer.save_model()을 통해 저장된 모델에서 가져와 업로드했다.

 

huggingface 로그인

 

huggingface에 모델을 업로드 하기 전, 먼저 로그인을 해야 한다. 아직 회원가입이 안되어 있다면 홈페이지에서 회원가입을 진행하자.

 

Hugging Face – The AI community building the future.

The AI community building the future. Build, train and deploy state of the art models powered by the reference open source in machine learning.

huggingface.co

 

huggingface-cli login

CLI에 위 명령어를 입력하면 아래와 같은 화면이 등장할 것이다.

CLI에 나와있는 주소인 https://huggingface.co/settings/tokens로 이동하면 token을 발급받을 수 있다. 해당 토큰을 복사하여 CLI에 붙여 넣으면 로그인이 완료된다.

 

huggingface hub에 저장소 생성

 

huggingface-cli repo create (저장소 이름)

CLI에 위와 같이 입력하면 huggingface hub에 저장소가 생성된다.

 

huggingface hub 저장소 clone

 

git clone https://사용자이름:토큰@huggingface.co/사용자이름/저장소이름

CLI에 위와 같이 입력하여 저장소를 clone한다. 토큰은 로그인 단계에서 생성했던 토큰을 사용한다.

git config --global user.email (huggingface 이메일)
git config --global user.name (사용자 이름)

마찬가지로 CLI에 위와 같이 입력하여 git config에 huggingface 이메일과 사용자 이름을 등록한다.

 

파일 이동

 

mv (준비한 파일 경로) (clone 받은 디렉토리 경로)

사전 준비 단계에서 준비했던 파일들을 clone 받은 디렉토리로 이동시킨다.

 

모델 push

# working directory : clone 받은 디렉토리

git lfs install

git add --all

git commit -m "커밋 메세지"

git push

clone받은 디렉토리로 이동해서 위 명령어를 실행시킨다.

 

대용량 파일을 업로드 해야하므로 git lfs를 설치한 뒤, git으로 파일들을 push한다.

 

모델 업로드 성공!