Loading...
Loading...
Compare original and translation side by side
| Aspect | PyTorch on Mac | MLX |
|---|---|---|
| Memory | Separate CPU/GPU copies | Unified memory, no copies |
| Optimization | Generic Metal backend | Apple Silicon native |
| Model loading | Slower, more memory | Lazy loading, efficient |
| Quantization | Limited support | Built-in 4/8-bit |
| 对比维度 | Mac上的PyTorch | MLX |
|---|---|---|
| 内存管理 | CPU与GPU内存分离,数据需复制 | 统一内存,无需数据复制 |
| 优化程度 | 通用Metal后端 | Apple Silicon原生优化 |
| 模型加载 | 速度慢,占用内存多 | 延迟加载,内存使用高效 |
| 量化支持 | 支持有限 | 内置4/8位量化 |
pip install mlx-lmpip install mlx-lmfrom mlx_lm import load, generatefrom mlx_lm import load, generateundefinedundefinedfrom mlx_lm import load, stream_generate
model, tokenizer = load("mlx-community/Mistral-7B-Instruct-v0.3-4bit")
prompt = "Write a haiku about programming:"
for response in stream_generate(model, tokenizer, prompt, max_tokens=100):
print(response.text, end="", flush=True)
print()from mlx_lm import load, stream_generate
model, tokenizer = load("mlx-community/Mistral-7B-Instruct-v0.3-4bit")
prompt="写一首关于编程的俳句:"
for response in stream_generate(model, tokenizer, prompt, max_tokens=100):
print(response.text, end="", flush=True)
print()from mlx_lm import load, batch_generate
model, tokenizer = load("mlx-community/Qwen2.5-7B-Instruct-4bit")
prompts = [
"What is machine learning?",
"Explain neural networks:",
"Define deep learning:",
]
responses = batch_generate(
model,
tokenizer,
prompts,
max_tokens=100,
)
for prompt, response in zip(prompts, responses):
print(f"Q: {prompt}\nA: {response}\n")from mlx_lm import load, batch_generate
model, tokenizer = load("mlx-community/Qwen2.5-7B-Instruct-4bit")
prompts = [
"什么是机器学习?",
"解释神经网络:",
"定义深度学习:",
]
responses = batch_generate(
model,
tokenizer,
prompts,
max_tokens=100,
)
for prompt, response in zip(prompts, responses):
print(f"问:{prompt}\n答:{response}\n")undefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedfrom mlx_lm import load, generate
model, tokenizer = load("mlx-community/Llama-3.2-3B-Instruct-4bit")
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What's the capital of France?"},
]
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
response = generate(model, tokenizer, prompt=prompt, max_tokens=256)
print(response)from mlx_lm import load, generate
model, tokenizer = load("mlx-community/Llama-3.2-3B-Instruct-4bit")
messages = [
{"role": "system", "content": "你是一个乐于助人的助手。"},
{"role": "user", "content": "法国的首都是哪里?"},
]
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
response = generate(model, tokenizer, prompt=prompt, max_tokens=256)
print(response)undefinedundefinedundefinedundefinedfrom mlx_lm import convert
convert(
hf_path="meta-llama/Llama-3.2-3B-Instruct",
mlx_path="./llama-3.2-3b-mlx",
quantize=True,
q_bits=4,
q_group_size=64,
)from mlx_lm import convert
convert(
hf_path="meta-llama/Llama-3.2-3B-Instruct",
mlx_path="./llama-3.2-3b-mlx",
quantize=True,
q_bits=4,
q_group_size=64,
)| Option | Default | Description |
|---|---|---|
| 4 | Quantization bits (4 or 8) |
| 64 | Group size for quantization |
| float16 | Data type for non-quantized weights |
| 选项 | 默认值 | 描述 |
|---|---|---|
| 4 | 量化位数(4或8) |
| 64 | 量化分组大小 |
| float16 | 非量化权重的数据类型 |
| Method | Best For | Command |
|---|---|---|
| Basic | Quick conversion | |
| DWQ | Quality-preserving | |
| AWQ | Activation-aware | |
| Dynamic | Per-layer precision | |
| GPTQ | Established method | |
| 方法 | 适用场景 | 命令 |
|---|---|---|
| Basic | 快速转换 | |
| DWQ | 保留模型质量 | |
| AWQ | 感知激活值的量化 | |
| Dynamic | 逐层动态精度 | |
| GPTQ | 成熟的量化方法 | |
undefinedundefined
For detailed coverage of each method, see `reference/quantization.md`.
如需了解每种方法的详细说明,请查看`reference/quantization.md`。undefinedundefinedundefinedundefinedundefinedundefined
For detailed LoRA configuration and training patterns, see `reference/fine-tuning.md`.
如需了解详细的LoRA配置和训练模式,请查看`reference/fine-tuning.md`。undefinedundefinedundefinedundefinedfrom openai import OpenAI
client = OpenAI(base_url="http://localhost:8080/v1", api_key="not-needed")
response = client.chat.completions.create(
model="default",
messages=[{"role": "user", "content": "Explain MLX in one sentence."}],
max_tokens=100,
)
print(response.choices[0].message.content)from openai import OpenAI
client = OpenAI(base_url="http://localhost:8080/v1", api_key="not-needed")
response = client.chat.completions.create(
model="default",
messages=[{"role": "user", "content": "用一句话解释MLX。"}],
max_tokens=100,
)
print(response.choices[0].message.content)mlx-communitystream_generatemlx_lm.cache_promptbatch_generatetokenizer.apply_chat_template()mlx-communitystream_generatemlx_lm.cache_promptbatch_generatetokenizer.apply_chat_template()reference/quantization.mdfine-tuning.mdreference/quantization.mdfine-tuning.md