본문 바로가기
C.W.K.
Stream
Lesson 01 of 07 · published

mlx-lm 5분 — 불러오고 물으면 끝

~10 min · mlx-lm, quickstart, loading

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

먼저 돌아가는 걸 봐

mlx-lm을 이해하는 가장 빠른 방법은 설명보다 출력 하나를 직접 받는 거야. core.lesson1에서 이미 설치했으니 새로 깔 것도 없어. Hugging Face의 믿을 만한 mlx-community에서 작은 지시 학습 모델을 받고 generate를 부르면 돼. 코드 세 줄과 커피 한 잔이면 Mac에서 진짜 LLM 답이 나와.

뒤의 여섯 레슨에서 방금 일어난 일을 하나씩 뜯어볼 거야. 여기서는 먼저 "잠깐, 이게 전부야?"라는 순간을 얻자.

작고 확실한 모델로 시작해

이번에는 mlx-community/Llama-3.2-1B-Instruct-4bit를 써. 이유는 분명해.

  • 작아. 약 700 MB야. 보통 연결이면 금방 받고, 어떤 M 시리즈 Mac의 통합 메모리에도 들어가.
  • 지시 학습 모델이야. 프롬프트에 답하지, 입력 문장을 멋대로 이어 쓰지 않아. 결과를 알아보기 쉬워.
  • mlx-community 모델이야. 이미 MLX 형식으로 변환하고 양자화했으며, foundations.lesson6에서 본 커뮤니티 검토도 거쳤어.
  • 4비트 양자화야. 시연 품질은 지키면서도 foundations.lesson4의 계산에 맞는 작은 메모리 크기를 가져.

두 줄이면 충분해

아래 코드가 불러오기와 생성의 전부야. load()는 첫 호출에 모델을 받고, 이후에는 캐시를 써서 곧바로 모델과 토크나이저 쌍을 돌려줘. generate()는 프롬프트를 받아 완성된 답을 Python 문자열로 줘.

방금 뒤에서 일어난 일

  1. Hugging Face에서 받거나 캐시를 찾았어. 첫 호출은 huggingface.co/mlx-community/Llama-3.2-1B-Instruct-4bit에서 ~/.cache/huggingface/hub/로 내려받고, 다음부터는 디스크에서 읽어.
  2. 가중치를 통합 메모리에 mmap했어. safetensors 조각을 메모리에 연결하고 MLX 커널이 그 영역을 직접 읽어.
  3. 토큰을 하나씩 생성했어. generate는 내부에서 레슨 2의 stream_generate를 감싼 뒤 토큰 문자열을 이어 붙여. 순전파마다 GPU 커널을 실행하고, 레슨 7에서 볼 KV 캐시가 차곡차곡 쌓여.

파이프라인은 이게 전부야. 이제 스트리밍, 샘플링, 모델 구조, 채팅 템플릿, 내장 HTTP 서버, 메모리 동작을 차례로 뜯어볼 거야. 분석하기 전에 일단 전체가 정말 돌아간다는 걸 손에 넣었어.

Code

두 줄이면 돼 — 불러오고 생성하기·python
from mlx_lm import load, generate

model, tokenizer = load("mlx-community/Llama-3.2-1B-Instruct-4bit")
print(generate(model, tokenizer, prompt="Say hello in one short sentence.", max_tokens=30))

# Verified output (2026-05-03, mlx-lm 0.31.3):
#   '"Hello, how are you?" is a simple yet effective greeting that can start
#    a conversation and break the ice.\n\nHere are some more ideas for greetings'
#
# (Your exact output will vary — sampling defaults are non-deterministic
#  unless you pass a fixed seed. We'll fix the determinism story in lesson 3.)
처음 불러온 뒤 모델이 디스크 어디에 사는지·bash
# After the first `load()` call, the model is cached here:
ls ~/.cache/huggingface/hub/models--mlx-community--Llama-3.2-1B-Instruct-4bit/snapshots/

# Check disk usage:
du -sh ~/.cache/huggingface/hub/models--mlx-community--Llama-3.2-1B-Instruct-4bit/

# Sample (verified 2026-05-03):
#   ~700 MB on disk for the 1B Q4 instruct variant

External links

Exercise

두 줄을 실행해 실제 답이 돌아오는지 확인해. 같은 모델 객체로 generate()를 세 번 더 불러봐. 다시 내려받거나 불러오지 않고 매번 새로운 순전파만 일어난다는 걸 확인해. time.perf_counter()로 첫 번째와 세 번째 생성 시간을 재면 첫 호출이 MLX의 JIT 준비 때문에 조금 느릴 거야. 알아낸 점을 두 문장으로 적어.

Progress

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

댓글 0

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

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