-
LLM Tokenization 방법 정리 (BytePair, WordPiece, Unigram, SentencePiece)AI/NLP 2024. 4. 22. 10:25728x90
LLM Tokenization 방법 정리 (BytePair, WordPiece, SentencePiece)
- Tokenization이란
- input text sequence를 Token이라는 더 작은 부분으로 변환하는 과정이다
- 가장 단순한 Tokenization 방법은 공백을 기준으로 나누는 거겠지만, 이는 OOV(out of vocabulary) 문제를 일으킨다
- 이 문제를 해결하기 위해 Subword 기반의 Tokenization를 많이 사용하고 있으며, 이때 훈련 데이터에 없는 단어 등 기존에 보지 못했던 단어들에 대해서도 유연하게 대처할 수 있다
- 아래는 Subword 기반의 Tokenization 기법들을 살펴본다
BytePairEncoding
- 원래 데이터 압축 알고리즘의 한 유형으로, 자주 나타나는 패턴을 바이트 수준에서 압축하는 데 사용되었음
- 과정
- 어휘 집합 구축 : 자주 등장하는 문자열를 병합하고 이를 어휘 집합에 추가합니다. 이를 원하는 어휘 집합 크기가 될 때까지 반복합니다.
- 어휘 집합을 만들어주기 위해, Corpus의 모든 문장을 공백 기준으로 나누는 "Pre-Tokenize"를 수행
- 다음으로 어절별로 병합 우선순위(빈도 기준으로 )가 높은 바이그램 쌍을 반복해서 병합
- 토큰화 : 토큰화 대상 문장 내 각 어절(띄어쓰기로 문장을 나눈 것)에서 어휘 집합에 있는 서브워드가 포함되어 있을 때 해당 서브워드를 어절에서 분리합니다.
- 어휘 집합 구축 : 자주 등장하는 문자열를 병합하고 이를 어휘 집합에 추가합니다. 이를 원하는 어휘 집합 크기가 될 때까지 반복합니다.
- GPT, GPT-2, RoBERTa, BART 및 DeBERTa, LLaMA 등에서 사용
WordPieceEncoding
- 말뭉치에서 자주 등장한 문자열을 토큰으로 인식한다는 점에서 BPE와 본질적으로 유사
- 하지만 어휘 집합을 구축할 때 문자열을 병합하는 기준이 다름
- BPE처럼 단순히 빈도를 기준으로 병합하는 것이 아니라, 병합했을 때 말뭉치의 우도(likelihood)를 가장 높이는 쌍을 병합
- BERT, Electra가 이 WordPiece를 사용한다
Unigram
- 모든 Pre-tokenized 토큰과 서브 워드에서 시작해 점차 사전을 줄여나가는 방식으로 진행
- BPE, WordPiece와 같이 기본 캐릭터에서 서브 워드를 점진적으로 병합해나가는 것과는 차이
- 과정
- 매 스텝마다 Unigram은 주어진 코퍼스와 현재 사전에 대한 Loss를 측정
- 이후, 각각의 서브 워드에 대해 해당 서브 워드가 코퍼스에서 제거되었을 때, Loss가 얼마나 증가하는지를 측정
- 이에 따라 Loss를 가장 조금 증가시키는 p 개 토큰을 제거합니다 (p는 보통 전체 사전 크기의 10-20% 값으로 설정)
- Unigram은 해당 과정을 사용자가 원하는 사전 크기를 지니게 될 때 까지 반복
SentencePieceEncoding
- 지금까지 우리가 살펴본 모든 방법들은 Pre-tokenize 과정을 필요로 함
- 하지만 이것의 문제점 : 모든 언어가 공백을 기준으로 단어를 분절할 수 없다 (중국어, 일본어 등 ... )
- 특징
- SentencePiece는 띄어쓰기를 다른 알파벳 혹은 글자처럼 하나의 character로 취급 ( e.g. "_" )
- Unigram 모델과 BPE(Byte Pair Encoding) 두 가지 모드를 지원
- Unigram 모델은 가능한 모든 토큰의 확률을 학습하고, 가장 낮은 확률을 가진 토큰을 사전에서 제거하는 방식으로 최적의 사전을 만든다
- BPE 모드에서는 가장 자주 등장하는 문자 쌍을 하나의 토큰으로 합치는 방식으로 사전을 구성
- 위 두 모드 중에 어떤 것을 고를 것이냐는 상황에 따라 ... (GPT-4 Said)
- 언어의 특성: 처리하고자 하는 언어의 특성에 따라 다른 모드가 더 효과적일 수 있습니다. 예를 들어, 합성어가 많은 언어는 BPE를 사용할 때 더 효과적일 수 있습니다.
- 학습 데이터의 크기: 크고 다양한 학습 데이터는 Unigram 모델의 사용을 정당화할 수 있으며, 반면에 더 일관된 또는 제한된 데이터셋은 BPE로 더 잘 처리될 수 있습니다.
- 모델의 목적: 텍스트 생성이나 번역 같은 특정 NLP 과제에 따라, 하나의 모델이 다른 모델보다 더 적합할 수 있습니다. 예를 들어, 넓은 어휘를 필요로 하는 과제는 Unigram이 유리할 수 있습니다.
import sentencepiece as spm import torch from torch.utils.data import Dataset spm.SentencePieceTrainer.train(input="sample_text.txt", model_prefix="sentencepiece", model_type="unigram", vocab_size=3000) sp = spm.SentencePieceProcessor() sp.load("sentencepiece.model")
- ALBERT, T5, XLNet이 이 SentencePiece를 사용한다
Related Papers
https://arxiv.org/abs/2402.06196
https://arxiv.org/abs/1508.07909
-> BPE
https://arxiv.org/abs/1808.06226
https://arxiv.org/abs/1609.08144
-> wordpiece
https://arxiv.org/abs/1804.10959
-> unigram
Ref.
https://ratsgo.github.io/nlpbook/docs/preprocess/bpe/
https://huffon.github.io/2020/07/05/tokenizers/
https://huggingface.co/docs/transformers/ko/tokenizer_summary
728x90'AI > NLP' 카테고리의 다른 글
- Tokenization이란