9️⃣Convert GGUF gemma-2b with llama.cpp

Quantizing LLM GGUF With llama.cpp

대부분의 언어 모델은 너무 커서 소비자 하드웨어에서 미세 조정할 수 없습니다. 예를 들어 650억 개의 파라미터 모델을 미세 조정하려면 780GB 이상의 GPU 메모리가 필요합니다. 이는 A100 80GB GPU 10대에 해당하는 용량입니다.

이제 LoRA 및 QLoRA와 같은 효율적인 파파미터 기술을 통해 소비자 하드웨어에서 모델을 보다 쉽게 미세 조정할 수 있게 되었습니다.

LoRA는 소량의 훈련 가능한 파라미터, 즉 LLM의 각 레이어에 대한 어댑터를 추가하고 모든 원래 파라미터를 동결합니다.

미세 조정을 위해 어댑터 무게만 업데이트하면 되므로 메모리 사용량을 크게 줄일 수 있습니다.

QLoRA는 4비트 양자화, 이중 양자화, 페이징을 위한 NVIDIA 통합 메모리 활용을 도입하여 세 단계 더 나아갔습니다.

  • 4-bit NormalFloat Quantization: 각 양자화 빈에서 동일한 수의 값을 보장하여 이상값에 대한 계산 문제와 오류를 방지합니다.

  • Double quantization: 추가 메모리 절약을 위해 양자화 상수를 양자화하는 프로세스입니다.

  • Paging with unified memory: NVIDIA 통합 메모리 기능을 사용하며 CPU와 GPU 간의 페이지 간 전송을 자동으로 처리합니다.

Basic steps Involved in fine-tuning:

  1. 기본 모델을 로드

  2. 기본 모델을 학습

  3. LoRA 어댑터를 저장

  4. 기본 모델을 절반/최대 정밀도(half/full precision)로 다시 로드

  5. LoRA 가중치를 기본 모델과 병합

  6. 병합된 모델을 저장하고 허깅 페이스 허브로 푸시

1. gemma-2B Fine-tuning

Setup Environments

Import dependencies

google/gemma 모델을 사용하려며 huggingface google 페이지에서 Acknowledge License를 클릭하여 사용을 신청하고 승인 후 활용 가능합니다. 신청후 승인은 5분이내에 이뤄집니다.

Load Dataset

의료 진료데이터인 medical-reasoning 데이터셋으로 fine-tuning을 해보겠습니다. https://huggingface.co/datasets/mamachang/medical-reasoning

trainset에 input, instruction, output 컬럼이 있는 것을 확인할 수 있습니다. 이를 데이터프레임으로 변환해서 확인해 보겠습니다.

input
instruction
output

0

Q:An 8-year-old boy is brought to the pediatri...

Please answer with one of the option in the br...

<analysis>\n\nThis is a clinical vignette desc...

1

Q:A 23-year-old man comes to the physician bec...

Please answer with one of the option in the br...

<analysis>\n\nThis is a clinical vignette desc...

2

Q:A 27-year-old man presents to the emergency ...

Please answer with one of the option in the br...

<analysis>\n\nThis is a question about a 27-ye...

3

Q:A 13-year-old girl presents with a 4-week hi...

Please answer with one of the option in the br...

<analysis>\n\nThis is a patient with signs and...

4

Q:A 53-year-old Asian woman comes to the physi...

Please answer with one of the option in the br...

<analysis>\n\nThis is a patient with symptoms ...

5

Q:A 7-year-old boy is brought to the physician...

Please answer with one of the option in the br...

<analysis>\n\nThis is a clinical vignette desc...

6

Q:A 21-year-old man comes to the military base...

Please answer with one of the option in the br...

<analysis>\n\nThis is a clinical case question...

7

Q:A 48-year-old woman presents to her primary ...

Please answer with one of the option in the br...

<analysis>\n\nThis is a question about determi...

8

Q:A 62-year-old man presents to the emergency ...

Please answer with one of the option in the br...

<analysis>\n\nThis is a patient with a history...

9

Q:A 34-year-old female presents to her primary...

Please answer with one of the option in the br...

<analysis>\n\nThis is a clinical vignette desc...

Generate prompt for training

Train/Test Split

LoraConifg

PeftModel을 로드하고 PEFT의 get_peft_model 유틸리티 함수와 prepare_model_for_kbit_training 메서드를 사용하여 LoRA를 사용하도록 지정합니다.

Training

이제 학습을 시켜 보겠습니다.

Step Training Loss 1 2.132100 2 2.036100 3 1.966600 4 1.794400 5 1.723900 6 1.644200 7 1.579900 8 1.402000 9 1.336500 10 1.288700 11 1.134200 12 1.228800 13 1.130100 14 1.171200 15 1.154600 16 1.165900 17 1.166300 18 1.093700 19 1.128000 20 1.083000 21 1.115100 22 1.134300 23 1.133400 24 1.085800 25 1.086600 26 1.090700 27 1.101500 28 1.024200 29 1.115900 30 1.055200 31 1.031000 32 1.038400 33 1.071800 34 1.060800 35 1.073500 36 1.013900 37 1.053400 38 1.062800 39 1.060000 40 1.067900 41 1.004100 42 1.036200 43 1.118600 44 1.054600 45 1.040600 46 0.987600 47 1.075600 48 1.050100 49 1.108100 50 1.057900 51 1.043800 52 1.109800 53 1.109200 54 1.032400 55 1.013100 56 1.010800 57 1.056000 58 1.075000 59 1.019000 60 1.042600 61 1.012100 62 1.053700 63 1.022000 64 1.063300 65 1.044900 66 1.021100 67 0.994300 68 1.004900 69 1.041000 70 1.087700 71 1.071200 72 1.010600 73 0.990200 74 1.061600 75 1.001700 76 1.030700 77 0.983900 78 1.056900 79 1.015400 80 1.035800 81 0.983800 82 0.996300 83 1.069300 84 1.058400 85 1.031700 86 1.039900 87 1.086900 88 1.067800 89 1.021400 90 1.022100 91 0.983400 92 1.072000 93 1.030100 94 1.041800 95 0.944500 96 1.009800 97 1.016500 98 1.043500 99 1.043800 100 1.011100

Model Save & Huggingface Push

학습된 모델을 저장하고 이를 Huggingface의 계정에 push 해보겠습니다.

Test Fine-tuned model

Inference


이제 llama.cpp를 사용하여 4-bit GGUF 모델로 변환한 후 Hugginface Hub에 push를 하겠습니다.

2. Convert to GGUF format with llama.cpp

Setup Environments

Model Download

Convert gguf

Quantize 4-bit format.

Push Model to HF

Download the quantized model for inference

Install llama.cpp on GPU

GGUF model inference with Llama.cpp.

Extracting the answer


Step 1: brew install llama.cpp

Step 2: llama-server --hf-repo microsoft/Phi-3-mini-4k-instruct-gguf --hf-file Phi-3-mini-4k-instruct-q4.gguf

Step 3: curl 8080/v1/chat/completions

Last updated