본문 바로가기
C.W.K.
Stream
Lesson 05 of 06 · published

어댑터 결합 — LoRA 계산 없이 추론하기

~12 min · merging, fuse, inference

Level 0호기심
0 XP0/51 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

왜 결합하나

학습 뒤에는 기반 모델과 어댑터가 따로 남아. 추론할 때마다 (W + A · B) · x를 계산하므로 어댑터가 닿은 모든 층에서 토큰마다 행렬 곱이 하나 더 붙어.

어댑터를 기반 가중치에 한 번 접어 넣으면 W가 학습된 A · B를 이미 포함한 새 모델이 돼. 품질은 분리 상태와 같고 이후 순전파에는 어댑터 비용이 없어 토큰당 계산이 싸져.

mlx_lm.fuse 한 명령이면 돼

기반 모델과 어댑터 폴더를 지정하면 mlx_lm.load로 바로 쓸 새 모델 폴더를 만들어.

대신 바꿔 끼우는 자유를 잃어

결합한 모델에는 어댑터가 구워져 있어 다른 것으로 바꿀 수 없어. SQL, 하이쿠, 요약처럼 작업마다 어댑터가 여럿이라면 따로 두고 필요할 때 불러오는 편이 유연해. 특정 기반과 특정 어댑터가 배포할 한 결과물이라고 결정했을 때만 결합해.

새 매개변수를 더하는 게 아니라 기존 가중치에 보정을 접으므로 디스크 크기는 기반 모델과 거의 같아.

GGUF로도 내보낼 수 있어

mlx_lm.fuse --export-gguf는 결합 모델을 GGUF로 내보내. MLX에서 파인튜닝한 결과를 Apple이 아닌 기계의 llama.cpp나 Ollama로 보내는 가장 깨끗한 길이야. 먼저 결합하고 그다음 내보내.

Hugging Face에 올리기

--upload-repo your-username/repo-name를 쓰면 결합 모델을 Hugging Face 저장소에 올려. 다른 사람이 저장소 ID만으로 mlx_lm.load하게 만들고 싶을 때 써.

Code

어댑터를 기반 모델에 결합해 배포 가능한 모델 만들기·bash
# Folds ./my-adapter into the base model, writes the result to ./my-fused.
python -m mlx_lm fuse \
  --model mlx-community/Mistral-7B-Instruct-v0.3-4bit \
  --adapter-path ./my-adapter \
  --save-path ./my-fused

# After this, ./my-fused looks like a normal MLX model directory:
#   config.json  model.safetensors  tokenizer.json  ...
# And you can load it without referencing the adapter at all:
python -c "from mlx_lm import load, generate; m, t = load('./my-fused'); print(generate(m, t, prompt='hi', max_tokens=20))"
결합한 모델을 자신의 Hugging Face 저장소에 올리기(명령 하나)·bash
# Requires `huggingface-cli login` first.
python -m mlx_lm fuse \
  --model mlx-community/Mistral-7B-Instruct-v0.3-4bit \
  --adapter-path ./my-adapter \
  --save-path ./my-fused \
  --upload-repo your-username/Mistral-7B-Instruct-v0.3-MyDomain-4bit

# Others can then pull your fine-tune by repo id:
#   model, tok = load("your-username/Mistral-7B-Instruct-v0.3-MyDomain-4bit")
결합한 모델을 GGUF로 내보내기(Apple 밖에 배포할 때)·bash
# Less common path: cross-format export so the result can run in llama.cpp / Ollama
# on Linux/Windows machines.
python -m mlx_lm fuse \
  --model mlx-community/Mistral-7B-Instruct-v0.3-4bit \
  --adapter-path ./my-adapter \
  --save-path ./my-fused \
  --export-gguf \
  --gguf-path ./my-fused.gguf

# ./my-fused.gguf is now a single file you can copy to any GGUF-capable runtime.

External links

Exercise

앞에서 학습한 어댑터를 mlx_lm.fuse로 결합해. 따로 불러온 기반+어댑터와 결합 모델의 생성 시간을 비교해. 결합 모델은 층마다 어댑터 계산을 하지 않아 토큰당 조금 더 빨라야 해. 지연시간 차이와 어댑터를 바꿔 끼우는 자유를 포기할 가치가 있는지 두 문장으로 적어.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.