-
[AutoGPTQ] KoAlpaca 양자화하기ML/딥러닝 2023. 8. 18. 15:54반응형
개요
KoAlpaca, KULLM과 같은 한국어 LLM도 많이 등장하고 있다. 하지만 모델 크기가 너무 커서 가지고 있는 GPU로 모델을 돌리기엔 메모리가 부족하다... 따라서 양자화를 시도해 띄워본다.
KoAlpaca
https://github.com/qwopqwop200/GPTQ-for-KoAlpaca
GitHub - qwopqwop200/GPTQ-for-KoAlpaca
Contribute to qwopqwop200/GPTQ-for-KoAlpaca development by creating an account on GitHub.
github.com
이미 똑똑하신 분들이 다 코드를 공개해두었다.
Dockerfile
환경구축을 위해 도커를 사용하였다. CPU 버전도 있는거 같지만 GPU 버전으로 사용했다.
FROM huggingface/transformers-pytorch-gpu:latest RUN pip install auto-gptq[llama]==0.2.2 WORKDIR /opt/project COPY ./ /opt/project
- huggingface/transformers-pytorch-gpu:latest 이미지 안에 필요한 것들 대부분 다 있다. CUDA, torch, transformers 등등..
- COPY ./ /opr/project는 필자 서버에 외부망 접근이 안돼서 GPTQ-for-Koalpaca 소스를 복사하는 과정이다.
- RUN git clone https://github.com/qwopqwop200/GPTQ-for-KoAlpaca.git 으로 대체가능.
Docker image build & run
docker build -t gptq-koalpaca . docker run -it --rm --gpus=all -v ~/polyglot-5.8B:/opr/project/polyglot-5.8B gptq-koalpaca bash
- huggingface 이미지가 워낙 뚱뚱해서 처음 다운 받을 때 오래걸린다.
- docker run 옵션 설명
- -it : 터미널을 사용할 수 있게 해준다. interactive terminal?
- --rm : 컨테이너 종료시 컨테이너를 삭제시켜준다. (필수아님.)
- --gpus=all : 컨테이너 내부에서 GPU 사용하는 옵션
- -v : 서버의 ~/polyglot-5.8B와 컨테이너의 /opt/project/polyglot-5.8B를 마운트해준다.
- 필자 서버에 외부망이 안되서 넣은 것으로 모델 자체를 네트워크로 받아도 된다.
GPTQ 코드 실행
# github 명령어 python quant_with_alpaca.py --pretrained_model_dir beomi/KoAlpaca-Polyglot-5.8B --quantized_model_dir ./model # 필자 사용 명령어 python3 quant_with_alpaca.py --pretrained_model_dir ./polyglot-5.8B --quantized_model_dir ./model
- python 대신 python3를 사용
- beomi/KoAlpaca-Polyglot-5.8B를 다른 서버에서 받아서 옮겼기 때문에 ./polyglot-5.8B와 같은 local path를 입력해주었다.
output
메모리는 최대 6GB정도 사용하는 것 같다.
2023-08-18 06:58:28 INFO [auto_gptq.modeling._utils] gpt_neox.layers.24.mlp.dense_4h_to_h 2023-08-18 06:58:33 INFO [auto_gptq.modeling._utils] gpt_neox.layers.24.mlp.dense_h_to_4h 2023-08-18 06:58:38 INFO [auto_gptq.modeling._utils] gpt_neox.layers.25.attention.dense 2023-08-18 06:58:39 INFO [auto_gptq.modeling._utils] gpt_neox.layers.25.attention.query_key_value 2023-08-18 06:58:42 INFO [auto_gptq.modeling._utils] gpt_neox.layers.25.mlp.dense_4h_to_h 2023-08-18 06:58:46 INFO [auto_gptq.modeling._utils] gpt_neox.layers.25.mlp.dense_h_to_4h 2023-08-18 06:58:51 INFO [auto_gptq.modeling._utils] gpt_neox.layers.26.attention.dense 2023-08-18 06:58:52 INFO [auto_gptq.modeling._utils] gpt_neox.layers.26.attention.query_key_value 2023-08-18 06:58:55 INFO [auto_gptq.modeling._utils] gpt_neox.layers.26.mlp.dense_4h_to_h 2023-08-18 06:59:01 INFO [auto_gptq.modeling._utils] gpt_neox.layers.26.mlp.dense_h_to_4h 2023-08-18 06:59:06 INFO [auto_gptq.modeling._utils] gpt_neox.layers.27.attention.dense 2023-08-18 06:59:07 INFO [auto_gptq.modeling._utils] gpt_neox.layers.27.attention.query_key_value 2023-08-18 06:59:10 INFO [auto_gptq.modeling._utils] gpt_neox.layers.27.mlp.dense_4h_to_h 2023-08-18 06:59:16 INFO [auto_gptq.modeling._utils] gpt_neox.layers.27.mlp.dense_h_to_4h 2023-08-18 06:59:20 INFO [auto_gptq.modeling._utils] Model packed. quantization took: 1367.5650s
22분 정도 소요됐다.
테스트
import torch from transformers import pipeline from auto_gptq import AutoGPTQForCausalLM MODEL = './polyglot-5.8B' QUANT_MODEL = './model' model = AutoGPTQForCausalLM.from_quantized(QUANT_MODEL, device="cuda:0", use_triton=False) pipe = pipeline('text-generation', model=model,tokenizer=MODEL) def ask(x, context='', is_input_full=False): ans = pipe( f"### 질문: {x}\n\n### 맥락: {context}\n\n### 답변:" if context else f"### 질문: {x}\n\n### 답변:", do_sample=True, max_new_tokens=512, temperature=0.7, top_p=0.9, return_full_text=False, eos_token_id=2, ) print(ans[0]['generated_text']) ask("딥러닝이 뭐야?")
모델을 띄운 상태로만 3.8GB의 메모리를 사용했다.
ask 함수를 사용하니 4.7GB까지 증가.
output
딥러닝은 인공 신경망을 통해 입력과 출력 사이의 복잡한 관계를 학습하는 머신러닝 기술입니다. 예를 들어, 인간이 학습을 통해 데이터를 분석하는 것처럼, 딥러닝은 인공 신경망을 통해 데이터 속에 담긴 패턴을 찾고, 이를 통해 새로운 데이터를 예측합니다. 이러한 방식으로, 딥러닝은 데이터 속의 패턴을 찾고, 예측하는 데 큰 도움이 됩니다.
Troubleshooting
AutoGPTQ 버전 이슈
NameError: name 'autogptq_cuda_256' is not defined
와 같은 에러가 발생했었다.
pip install auto-gptq[llama]==0.2.2
AutoGPTQ를 0.2.2 버전을 사용하지 않으면 발생한다.
https://github.com/PromtEngineer/localGPT/issues/251
NameError: name 'autogptq_cuda_256' is not defined · Issue #251 · PromtEngineer/localGPT
It seems like AutoGPTQ quantization module not being able to access the CUDA extension. The previous ingest problem is solve by pip install git+https://github.com/Keith-Hon/bitsandbytes-windows.git...
github.com
pipeline
pipe = pipeline('text-generation', model=model,tokenizer=MODEL,device=0) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/transformers/src/transformers/pipelines/__init__.py", line 993, in pipeline return pipeline_class(model=model, framework=framework, task=task, **kwargs) File "/transformers/src/transformers/pipelines/text_generation.py", line 67, in __init__ super().__init__(*args, **kwargs) File "/transformers/src/transformers/pipelines/base.py", line 782, in __init__ raise ValueError( ValueError: The model has been loaded with `accelerate` and therefore cannot be moved to a specific device. Please discard the `device` argument when creating your pipeline object.
device=0를 지우니 정상 동작.
반응형'ML > 딥러닝' 카테고리의 다른 글
TRAINING NEURAL AUDIO CLASSIFIERS WITH FEW DATA 논문 리뷰 (0) 2020.11.02 Chapter 16 Natural Language Processing with RNNs and Attention (0) 2020.06.18 Chapter 9 Unsupervised Learning Techniques (0) 2020.05.07 Chapter 7 Ensemble Learning and Random Forests (0) 2020.04.13