-
LLM Positional Encoding 방법 정리 (Absolute, Relative, Rotary, ... )AI/NLP 2024. 4. 22. 10:26728x90
LLM Positional Encoding 방법 정리 (Absolute, Relative, Rotary, ... )
Absolute Positional Encoding (APE)
- Vanilla Transformer 모델에서 input sequence의 순서 정보를 유지하기 위해서 사용
- RNN 계열과 달리 Transformer 구조는 모든 input token이 병렬적으로 들어가기 때문에 positional information이 없으면 input token들은 그냥 BoW처럼 취급된다
- 위와 같이, encoder와 decoder stack 이전에 들어갈 input embedding들에 element-wise addition을 해준다
- Vanilla Transformer 모델에서는 Sine과 Cosine 함수를 사용해서 수행함 ( sinusoidal 기반)
- 그냥 0,1,2, .. 이런 식으로 사용하면 숫자가 너무 빨리 커져서 Weight가 갈수록 커지게 되고, gradient vanishing이나 gradient explosion등 학습이 불안정하게 진행될 수 있기 때문에
- 부드러운 함수에서 추출되고, 좋은 성질이 있어서 포지션 끼리 가까우면 내적이 크고, 멀면 내적이 작아져서 내적으로부터 거리관계를 대략 유추해낼 수 있습니다. 또한 max_len이 다양한 dataset에도 동일하게 적용가능
- 위치 인코딩은 (a)sinusoidal 함수를 사용한 결정론적인 벡터나 (b)학습한 벡터를 주로 사용
- (a) sinusoidal 함수를 사용한 인코딩
- 이 방법은 학습 중에 보지 않은 임의의 인풋 길이로 일반화가 가능
- 인코더나 디코더의 첫 번째 레이어 이전의 인풋에 sinusoid 함수를 사용한 frequency를 더해주는 방법
- (b) 학습된 absolute representation
- 위치 정보 인풋에 대해 projection layer을 학습하여 사용하는 방법
- 학습 중에 보지 않은 더 긴 길이의 인풋으로 확장이 어렵다는 한계가 있다
- (a) sinusoidal 함수를 사용한 인코딩
- 단점
- 고정된 길이를 사용하기 때문에 Token의 수가 제한된다
- Token간의 상대적 거리를 고려하지 못한다
- 각 토큰의 절대적 위치만을 고려
- 토큰 간의 상대적 거리—즉, 토큰들이 얼마나 가깝게 또는 멀리 떨어져 있는지에 대한 정보—를 직접적으로 모델에 반영하지 않음
Relative Positional Encoding (RPE)
- APE의 한계를 극복할 수 있는 방법으로
- 두 input 사이의 임의의 관계 (graph 등)에 대해 self-attention을 확장하여 적용할 수 있다.
- self-attention의 key와 query 사이의 거리에 따라 학습된 embedding을 생성한다
- T5에서 이 RPE를 사용한다 그런데 조금 다름
- T5에서는 position embedding으로 단순하게 scalar을 사용한다. 그리고 이 스칼라는 attention weight를 계산할 때 logit에 더해주는 값으로 사용한다.
- 효율성을 위해 position embedding 값은 모든 레이어에서 공유하되, 각 레이어의 attention head에서는 각기 다른 학습된 position embedding을 사용한다.
Rotary Positional Encoding (RoPE)
- 위의 Relative Positional Encoding 방식에서 위치들간의 attention matrix 만으로는 순서 정보를 줄 수가 없더라
- 상대거리를 clip하여 일정 거리 이상은 정확한 상대거리는 사용하지 않는다
- relative position 정보를 additive 한 방식으로 사용함
- RoPE 는 rotation 행렬을 이용하여 절대 위치를 인코딩하고, self-attention 식에서 relative position dependency (상대 위치 의존성) 정보를 더해준다.
- 워드임베딩 벡터를 complex(복소수) 꼴로 변환시킨 후 이를 rotation -> word embedding 의 절대적인 값이 바뀐다
- Self attention에서 사용하는 ‘내적’ 은 내적 하는 벡터 사이 각도에 대한 함수이므로 두 벡터에 대한 상대 거리가 보존될 수 있다
- GPT-NeoX-20B, PaLM, CODEGEN, LLaMA에서 사용하는 Positional Encoding
def rotate_half(x): """Rotates half the hidden dims of the input.""" x1 = x[..., : x.shape[-1] // 2] x2 = x[..., x.shape[-1] // 2 :] return torch.cat((-x2, x1), dim=-1) def apply_rotary_pos_emb(q, k, cos, sin, position_ids): # The first two dimensions of cos and sin are always 1, so we can `squeeze` them. cos = cos.squeeze(1).squeeze(0) # [seq_len, dim] sin = sin.squeeze(1).squeeze(0) # [seq_len, dim] cos = cos[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim] sin = sin[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim] q_embed = (q * cos) + (rotate_half(q) * sin) k_embed = (k * cos) + (rotate_half(k) * sin) return q_embed, k_embed
Related Papers
https://arxiv.org/abs/2402.06196
Large Language Models: A Survey
Large Language Models (LLMs) have drawn a lot of attention due to their strong performance on a wide range of natural language tasks, since the release of ChatGPT in November 2022. LLMs' ability of general-purpose language understanding and generation is a
arxiv.org
https://arxiv.org/abs/1803.02155
Self-Attention with Relative Position Representations
Relying entirely on an attention mechanism, the Transformer introduced by Vaswani et al. (2017) achieves state-of-the-art results for machine translation. In contrast to recurrent and convolutional neural networks, it does not explicitly model relative or
arxiv.org
https://arxiv.org/abs/2104.09864
RoFormer: Enhanced Transformer with Rotary Position Embedding
Position encoding recently has shown effective in the transformer architecture. It enables valuable supervision for dependency modeling between elements at different positions of the sequence. In this paper, we first investigate various methods to integrat
arxiv.org
Ref.
https://arxiv.org/pdf/2402.06196.pdf
https://aiml.com/explain-the-need-for-positional-encoding-in-transformer-models/
Explain the need for Positional Encoding in Transformer models
Positional encoding is a technique used in the Transformer architecture and other sequence-to-sequence models to provide information about the order and position of elements in an input sequence.
aiml.com
https://velog.io/@gibonki77/DLmathPE
[Transformer]-1 Positional Encoding은 왜 그렇게 생겼을까? 이유
트랜스포머의 입력은 RNN과 달리 한꺼번에 모든 요소가 들어가기 때문에, 위치관계의 패턴을 Encode해주는데요. 주로 사용되는 Sinusoidal Positional Encoding의 장점에 대해 알아보겠습니다.
velog.io
https://littlefoxdiary.tistory.com/94
[논문리뷰] Relative Position Representations in Transformer
MOTIVATION Transformer 아키텍쳐는 인풋 시퀀스 사이의 attention을 통해 인풋 사이의 관계를 모델링한다. 이때 이 매커니즘만으로는 시퀀스의 순서를 모델링할 수 없다. 예를 들어 "철수 / 가 / 영희 / 를
littlefoxdiary.tistory.com
https://mari970.tistory.com/49
Rotary Position Embedding (RoPE)
Roformer: Enhanced Transformer with Rotary Positon Embedding https://arxiv.org/pdf/2104.09864.pdf 에서 처음 제안된 방법이다. Abstract 기존의 PE 는 트랜스포머에서 제안된 방법으로, 시퀀스 내의 토큰을 attention 만으로
mari970.tistory.com
https://github.com/huggingface/transformers/issues/25199
[LLaMA] Rotary positional embedding differs with official implementation · Issue #25199 · huggingface/transformers
transformers implement LLaMA model's Rotary Positional Embedding (RoPE) as follows: transformers/src/transformers/models/llama/modeling_llama.py Lines 173 to 188 in e42587f def rotate_half(x): """R...
github.com
728x90'AI > NLP' 카테고리의 다른 글
- Vanilla Transformer 모델에서 input sequence의 순서 정보를 유지하기 위해서 사용