📑Paper Review

[paper review] Longformer: The Long-Document Transformer

date
Nov 24, 2023
slug
longformer
author
status
Public
tags
DeepLearning
paper
summary
type
Post
thumbnail
캡처.PNG
category
📑Paper Review
updatedAt
Sep 6, 2024 02:29 PM

Introduction

transformer는 NLP task에서 좋은 성능을 보여줬지만 self-attention의 연산의 복잡도가 sequence length가 증가함에 따라 quadratic하게 증가한다는 한계점이 있음
본 논문은 sequence 길이에 따라 선형적인 복잡도의 연산이 이루어지도록 transformer architecture를 수정함
BERT와 같은 기존 모델들은 토큰 길이를 512로 제한하여 긴 문장을 다루는 task에서는 한계가 있다. 긴 문장을 partitioning하는 과정에서 정보손실이 발생하고 이를 해결하기 위해서 다시 복잡한 구조가 적용됨
longformer는 여러 개의 attention layer를 사용하여 partition없이도 전체 context의 contextual representation을 가능하게 함
longformer의 어텐션 메카니즘은 windowed local-context self-attention와 global attention을 결합시킴

Longformer

기존 transformer의 full self-attention matrix를 attention pattern에 따라 sparsify하여 사용
이를 통해 attention 연산의 복잡도를 선형적을 감소시킴

attention pattern

notion image

sliding window

각 토큰 근처의 고정된 사이즈의 window에 대해서만 attention 연산을 진행하는 방식
고정된 윈도우 사이즈 에 의해 각 토큰 양 사이드의 만큼의 토큰에 윈도우가 적용
이 패턴의 복잡도는 은 input sequence 길이
layer의 개수가 개인 transformer 구조에서 마지막 layer의 receptive field

Dilated sliding window

연산량의 증가 없이 receptive field를 확장하기 위해 sliding window를 ‘dilated’하는 방식
이때 window는 크기의 gap이 들어가게 됨
window 크기는 , dilation크기는 로 고정된다고 가정했을 때 receptive field는
멀티 헤드 어텐션 적용할 때 dilation을 다르게 구성하면 더 성능이 증가하는 것을 확인

global attention

bert와 같은 모델들은 task별로 optimal input representation이 달라짐
MLM의 경우 local context를 통해 masked 단어를 예측
문장 분류 task의 경우 cls와 같은 special token을 통해 문서 전체의 sequence의 representation을 결합하여 사용함
window 또는 dilated window를 사용하는 방식은 task specific representation을 학습하기에 적합하지 않음
따라서 본 논문은 사전에 선택된 token들은 전체 token에서 attention하도록 함.
예를 들어 문장 분류인 경우, cls 토큰에 global attention 사용하고 QA task인 경우 모든 질문 토큰들에 global attention 사용

implementation

notion image
기존 transformer와 다르게 longformer는 고정된 크기의 window에 대해서만 attention 연산이 이루어짐 이로인해 sequence 길이에 따른 memory 사용량이 linear하게 증가함
하지만 이를 구현하기 위해서는 banded matrix multipplication이 필요한데 파이토치와 텐서플로 모두 이를 지원하지 않음 이를 위해 3가지의 다른 방식으로 implemetation
loop:
chunks:
cuda:

Autoregressive Language Modeling

attention pattern

dilated sliding window attention 사용
layer마다 서로 다른 window size 적용.
lower layer에는 작은 사이즈의 윈도우 사용하였고 higher layer로 갈수록 window size 증가시킴
이를 통해 lower layer에서 local information을 학습하고 higher layer는 전체 sequence에 대한 representation을 학습하도록 유도함.
lower layer에서는 dilation 적용하지 않고 higher layer에 대해서도 2 헤드에만 dilation 적용

result

notion image
small 모델에 대해 text8, enwik8에서 SOTA 달성

Pre-training and Fine-tuning

longformer 구조 사용한 pretrained model
MLM으로 pre-training
최대 token 길이가 512인 BERT의 8배인 4096개의 token 한번의 받는 것이 가능

attention pattern

window 크기가 512인 sliding window 사용