콘텐츠로 이동

MLX 명령어

MLX는 Apple Silicon을 위해 설계된 Apple의 머신러닝 프레임워크입니다. 통합 CPU/GPU 메모리, 지연 평가, 조합 가능한 함수 변환 및 NumPy 유사 API를 특징으로 합니다. mlx-lm 패키지는 LLM 추론, 파인 튜닝 및 모델 변환을 위한 고수준 도구를 제공합니다.

설치

# Install MLX core
pip install mlx

# Install MLX LM tools (inference, fine-tuning, conversion)
pip install mlx-lm

# Install MLX for vision and audio
pip install mlx-vlm
pip install mlx-whisper

# Verify installation
python -c "import mlx.core as mx; print(mx.default_device())"

LLM 텍스트 생성

# Generate text from a HuggingFace model (auto-downloads)
mlx_lm.generate \
  --model mlx-community/Llama-3.1-8B-Instruct-4bit \
  --prompt "Explain transformers in ML:" \
  --max-tokens 256

# With sampling parameters
mlx_lm.generate \
  --model mlx-community/Mistral-7B-Instruct-v0.3-4bit \
  --prompt "Write a haiku about coding:" \
  --max-tokens 100 \
  --temp 0.7 \
  --top-p 0.9

# Chat mode
mlx_lm.chat \
  --model mlx-community/Llama-3.1-8B-Instruct-4bit

텍스트 생성 Python API

from mlx_lm import load, generate

# Load model and tokenizer
model, tokenizer = load("mlx-community/Llama-3.1-8B-Instruct-4bit")

# Generate text
prompt = "Explain gradient descent:"
response = generate(
    model,
    tokenizer,
    prompt=prompt,
    max_tokens=256,
    temp=0.7,
)
print(response)

# Streaming generation
from mlx_lm import stream_generate

for token in stream_generate(model, tokenizer, prompt=prompt, max_tokens=256):
    print(token, end="", flush=True)

모델 변환

# Convert HuggingFace model to MLX format
mlx_lm.convert \
  --hf-path meta-llama/Llama-3.1-8B-Instruct \
  --mlx-path ./mlx-llama-8b \
  --dtype float16

# Convert with 4-bit quantization
mlx_lm.convert \
  --hf-path meta-llama/Llama-3.1-8B-Instruct \
  --mlx-path ./mlx-llama-8b-4bit \
  --quantize \
  --q-bits 4 \
  --q-group-size 64

# Convert with 8-bit quantization
mlx_lm.convert \
  --hf-path mistralai/Mistral-7B-Instruct-v0.3 \
  --mlx-path ./mlx-mistral-8bit \
  --quantize \
  --q-bits 8

LoRA 파인 튜닝

# Fine-tune with LoRA
mlx_lm.lora \
  --model mlx-community/Llama-3.1-8B-Instruct-4bit \
  --data ./training_data \
  --train \
  --iters 1000 \
  --batch-size 4 \
  --lora-layers 16 \
  --learning-rate 1e-5

# Resume training from checkpoint
mlx_lm.lora \
  --model mlx-community/Llama-3.1-8B-Instruct-4bit \
  --data ./training_data \
  --train \
  --resume-adapter-file ./adapters/adapters.safetensors \
  --iters 500

# Evaluate after training
mlx_lm.lora \
  --model mlx-community/Llama-3.1-8B-Instruct-4bit \
  --data ./training_data \
  --adapter-path ./adapters \
  --test

LoRA 어댑터 병합

# Merge LoRA adapters into base model
mlx_lm.fuse \
  --model mlx-community/Llama-3.1-8B-Instruct-4bit \
  --adapter-path ./adapters \
  --save-path ./fused-model

# Fuse and re-quantize
mlx_lm.fuse \
  --model mlx-community/Llama-3.1-8B-Instruct-4bit \
  --adapter-path ./adapters \
  --save-path ./fused-model-4bit \
  --de-quantize

훈련 데이터 형식

{"text": "Below is an instruction.\n\n### Instruction:\nExplain gravity.\n\n### Response:\nGravity is a fundamental force..."}
{"text": "Below is an instruction.\n\n### Instruction:\nWhat is DNA?\n\n### Response:\nDNA is a molecule..."}

데이터 디렉토리에 train.jsonl, valid.jsonl, test.jsonl로 데이터 파일을 배치하세요.

MLX 코어 API

import mlx.core as mx

# Array creation (like NumPy)
a = mx.array([1.0, 2.0, 3.0])
b = mx.zeros((3, 4))
c = mx.ones((2, 3), dtype=mx.float16)
d = mx.random.normal((4, 4))

# Device placement (unified memory - no explicit transfers)
x = mx.array([1.0, 2.0, 3.0])  # Available on both CPU and GPU

# Basic operations
result = mx.matmul(a.reshape(1, -1), mx.ones((3, 4)))
y = mx.exp(x) + mx.sin(x)

# Lazy evaluation - computations only run when needed
z = mx.add(x, x)
z = mx.multiply(z, 2.0)
mx.eval(z)  # Triggers computation

# Automatic differentiation
def loss_fn(x):
    return mx.sum(x ** 2)

grad_fn = mx.grad(loss_fn)
grads = grad_fn(mx.array([1.0, 2.0, 3.0]))

MLX 신경망 모듈

import mlx.core as mx
import mlx.nn as nn
import mlx.optimizers as optim

# Define a model
class MLP(nn.Module):
    def __init__(self, input_dim, hidden_dim, output_dim):
        super().__init__()
        self.layers = [
            nn.Linear(input_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, output_dim),
        ]

    def __call__(self, x):
        for layer in self.layers:
            x = layer(x)
        return x

model = MLP(784, 256, 10)

# Optimizer
optimizer = optim.Adam(learning_rate=1e-3)

# Training step with value_and_grad
def loss_fn(model, x, y):
    logits = model(x)
    return mx.mean(nn.losses.cross_entropy(logits, y))

loss_and_grad_fn = nn.value_and_grad(model, loss_fn)

# Training loop
for batch_x, batch_y in dataloader:
    loss, grads = loss_and_grad_fn(model, batch_x, batch_y)
    optimizer.update(model, grads)
    mx.eval(model.parameters(), optimizer.state)

서버 모드

# Start OpenAI-compatible server
mlx_lm.server \
  --model mlx-community/Llama-3.1-8B-Instruct-4bit \
  --port 8080

# Query the server
curl http://localhost:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "default",
    "messages": [{"role": "user", "content": "Hello!"}],
    "max_tokens": 100
  }'

성능 팁

# Use mx.compile for repeated operations
@mx.compile
def fast_forward(model, x):
    return model(x)

# Use float16 for faster inference
model, tokenizer = load("model-path", dtype=mx.float16)

# Batch processing
inputs = mx.array(batch_of_inputs)
outputs = model(inputs)  # Processes entire batch at once
mx.eval(outputs)

MLX vs PyTorch 비교

FeatureMLXPyTorch
Memory modelUnified (shared CPU/GPU)Explicit transfers
EvaluationLazy (deferred)Eager (immediate)
PlatformApple Silicon onlyCross-platform
Array APINumPy-likeNumPy-like
Auto-diffmx.gradtorch.autograd
Compilationmx.compiletorch.compile

일반적인 명령어

TaskCommand
Generate textmlx_lm.generate --model MODEL --prompt TEXT
Interactive chatmlx_lm.chat --model MODEL
Convert modelmlx_lm.convert --hf-path HF_MODEL --mlx-path OUTPUT
Quantize modelmlx_lm.convert --hf-path MODEL --quantize --q-bits 4
LoRA fine-tunemlx_lm.lora --model MODEL --data DIR --train
Fuse adaptersmlx_lm.fuse --model MODEL --adapter-path DIR
Start servermlx_lm.server --model MODEL --port 8080