대규모 training data set으로 훈련된 foundation model은 prompt engineering으로 잘 활용할 수 있지만, 활용하고자 하는 분야가 매우 domain-specific 하는 등의 경우에서는 모델을 customizing해야 할 필요가 있다. 이처럼 domain/task-specific 할 때, foundation model의 performance를 개선하기 위한 방법으로 (작은) data set으로 모델을 추가로 훈련하는 방법이 있다. 
이게 바로 fine-tuning인데, fine-tuning도 그냥 하면 문제가 뭐냐. 

 

LLM을 SFT하는데  그 많은 파라미터들을 다 파인튜닝하면서 모든 모델 가중치를 수정한다고 생각해보자. (예를 들어 Llama-2는 최소 7B임) computation cost가 어마어마할 것이다. 

 

그래서 LoRA : 로라를 공부해야겠다, 라는 생각이 들었다. 

 

https://arxiv.org/abs/2106.09685

 

LoRA: Low-Rank Adaptation of Large Language Models

An important paradigm of natural language processing consists of large-scale pre-training on general domain data and adaptation to particular tasks or domains. As we pre-train larger models, full fine-tuning, which retrains all model parameters, becomes le

arxiv.org

 

Huggingface에서는 효율적인 파라미터 파인 튜닝 (PEFT ; Parameter-Efficient Fine-Tuning) 라이브러리를 도입했다. 
이 라이브러리를 이용하면 훨씬 적은 컴퓨팅 리소스, 메모리, 비용으로 파인튜닝이 가능하다. 

 

얼마나?

예를 들어 허깅페이스의 BLOOMZ_7B 모델은 pre-training 과정에서 A100 80GB GPU 8개, 512GB CPU 8개 리소스가 사용됐다는데, 위의 LoRA fine-tuning 방식을 이용하면 NVIDIA A10G GPU 1개 : ml.g5.2xlarge 인스턴스에서 파인튜닝 가능. (추가로 8-bit로 양자화까지 함)
이 어마무시한 효율.

 

이 효율충에게 너무너무 필요한 공부.

 


 

LoRA ?

 

We propose Low-Rank Adaptation, or LoRA,
(1) which freezes the pretrained model weights and
(2) injects trainable rank decomposition matrices into each layer of the Transformer architecture,
(효과) greatly reducing the number of trainable parameters for downstream tasks

 

: 예측 성능의 손실 없이 새로운 task에 맞게 fine-tuning하는데 필요한 parameter수와 computing을 크게 줄여주는 기법

: 전체 weight를 다 update하는 대신,

  pre-trained weight는 고정된 상태로 유지하면서,

  더 적은 rank-decomposition matrices를 새로운 task에 맞게 최적화하고, 조정된 weight를 원래 모델에 합산함. 

 

Low-rank adaptation 에서

- 'rank' : how many independent row or column vectors exist in the matrix 

- 'low-rank' : rank is smaller than dimensions

- 'adaptation' : fine-tuning of models

 

W_0 + (delta)W = W_0 + BA

 

( W_0 : 기존 model weight, B & A : low rank matrices, 
  BA = (delta)W : the change in model weights )

 

(delta)W가 훈련을 시작할 때는 0이 되어야 하므로 B 행렬을 0으로, A 행렬의 weight는 정규분포로 초기화시킴

 

https://www.youtube.com/watch?v=t509sv5MT0w

 

- 훈련이 시작되면 학습 중 W_0는 gradient update를 수행하지 않음, 

- 행렬 B, A만 Trainable parameters

 

 

이론은 간단하게 공부해놓고 PEFT 라이브러리를 불러와서 어떻게 코드로 구현하고 어떻게 학습시키는지 이어서 

적어야겠다.  

현재 1.3B LLM을 학습시키고 있다. 

 

시간은 마찬가지로 엄청 걸리지만 원래는 cuda out으로 학습 시작 조차도 못했었는데 LoRA로 학습이 되서 다행이다.

 

 

 

참고 영상 : 

https://www.youtube.com/watch?v=BJqwmDpa0wM

'Study > 경량화' 카테고리의 다른 글

AI 모델의 경량화  (0) 2024.04.01