SK Networks Family AI bootcamp 강의노트

51일차 [ Fine Tuning ]

HyunJung_Jo 2025. 3. 31. 17:19

deepspeed

 

Google Colab Notebook

Run, share, and edit Python notebooks

colab.research.google.com

하나의 프로젝트를 열번 만들기 > 열개의 프로젝트 만들기 (몸만 바쁘고 남는게 없음)
이게 더 빠르다. 

accelerate

https://colab.research.google.com/drive/17Wh0hyEfARuN42ZdheYpPl-4m2dv7met

 

Google Colab Notebook

Run, share, and edit Python notebooks

colab.research.google.com

huggingface에서 제공하는 accelerate를 쓰면 deepspeed와는 달리 코드를 거의 바꾸지 않은 상태로 모델 학습을 할 수 있다.

  • runpod: 20gb gpu, 40gb ram으로 생성해서 실습해봄.
  • clean_memory() : garbage collection 
def clean_memory():
  import gc
  import torch

  for key in list(globals().keys()): # 정의된 변수 리스트
    if key not in ['clean_memory', '__name__', '__doc__', '__package__', '__loader__', '__spec__', '__builtin__', '__builtins__', '_ih', '_oh', '_dh', 'In', 'Out', 'get_ipython', 'exit', 'quit', '_', '__', '___', '_i', '_ii', '_iii', '_i1', 'gc', '_1', '_i2', '_i3']:
      del globals()[key] # 정의된 변수 삭제

  # GC(Garbage Collection) 사용
  gc.collect() # 변수에 연결되지 않은, 메모리에 올라와 있는 객체들을 삭제하는 함수
  # CUDA 메모리 해제
  torch.cuda.empty_cache()

clean_memory()

LLM Project

모듈화

  • 모듈화를 하려면  꼭 그림을 그려야 한다. 실무에선 복잡한 프로세스를 그리기 때문에 지금부터 그림을 그려야 한다.

  • 비지니스 로직은 'service' 폴더에 담기.
  • enum 클래스로 sql logic, data 관리
  • feature branch > dev branch merge
  • gguf : 단일 추론 모델로 다운 받기 편리함
  • unsloth: finetuning을 엄청 빨리할 수 있음.

ollama에 모델 추가하기 (gemma3-q8.gguf)

  • gguf 모델 하나 다운 받아서 (지정된 경로에다가)
  • Modelfile에 모델 이름 추가
FROM model.Q8_0.gguf # << 여기에 모델 이름 추가

TEMPLATE """{{- if .System }}
<s>{{ .System }}</s>
{{- end }}
<s>Human:
{{ .Prompt }}</s>
<s>Assistant:
"""

SYSTEM """A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions."""

PARAMETER temperature 0
PARAMETER num_predict 3000
PARAMETER num_ctx 4096
PARAMETER stop <s>
PARAMETER stop </s>
  • 지정된 경로에 `ollama create gemma3-q8 -f Modelfile` 파워쉘에 명령어 입력
  • `ollama list`로 모델 있는 거 확인
  • ollama.py에서 이미 모듈화 되어 있는 곳에 모델 하나 추가

ollama 모델 추가하기 (gemma3-1b.gguf)

colab

 

Google Colab Notebook

Run, share, and edit Python notebooks

colab.research.google.com

 이 스크립트는 Gemma 3-1b-it 모델을 Google Colab에서 사용하기 위한 준비를 수행합니다. 환경 설정 후 Hugging Face Hub에서 모델을 다운로드하고, llama.cpp에서 사용할 수 있도록 gguf 형식으로 변환합니다.

-> 여기에서 모델 만들어서 새로 챗봇 프젝에 추가함