5.8 KiB
MacOS 上 Qwen3.5-9B 模型安装指南
你想要在 macOS 系统上安装并运行 Qwen3.5-9B 模型,我会为你提供两种最常用、最易上手的安装方式:Ollama 一键部署(推荐新手) 和 Hugging Face 手动部署(灵活可控),你可以根据自己的需求选择。
前置条件
首先确认你的 Mac 满足基础要求:
-
macOS 12.0+(推荐 macOS 13/14)
-
内存:至少 16GB(建议 32GB+,M 系列芯片(M1/M2/M3)体验最佳)
-
硬盘:预留至少 10GB(4-bit 量化版)或 20GB(原版)空间
方式一:Ollama 一键部署(推荐)
这是最简单的方式,Ollama 会自动处理模型下载、量化和运行,无需手动配置环境,适合新手快速体验。
步骤 1:安装 Ollama
-
打开终端(Terminal),执行以下命令下载安装 Ollama:
curl -fsSL https://ollama.com/install.sh | sh -
等待安装完成,验证是否安装成功:
ollama --version若输出版本号(如
ollama version 0.1.48),说明安装成功。
步骤 2:拉取并运行 Qwen3.5-9B
-
执行以下命令拉取 Qwen3.5-9B 模型(默认是 4-bit 量化版,占用约 10GB 空间):
ollama pull qwen3.5:9b-
若需要指令微调版(更适合对话),执行:
ollama pull qwen3.5:9b-instruct -
若需要更大的上下文窗口(32k),执行:
ollama pull qwen3.5:9b-32k
-
-
运行模型并进入对话界面:
ollama run qwen3.5:9b -
此时可以直接输入问题与模型对话,例如:
>>> 介绍一下大语言模型 # 模型会返回对应的回答 -
退出对话:输入
/bye并回车。
方式二:Hugging Face 手动部署(灵活可控)
适合需要自定义模型参数、集成到代码中使用的场景,步骤稍复杂,但可灵活调整量化策略。
步骤 1:安装依赖环境
-
安装 Python(推荐 3.10/3.11,macOS 自带的 Python 可能版本过低,建议用 conda 或 brew 安装):
# 若未安装 brew,先执行:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brew install python@3.11 -
安装必要的 Python 库:
pip install torch transformers accelerate sentencepiece protobuf modelscope # 若需要量化运行(减少内存占用),额外安装: pip install bitsandbytes
步骤 2:下载 Qwen3.5-9B 模型
方法 A:通过 ModelScope 下载(国内网络友好)
# 创建并运行以下 Python 脚本(命名为 download_qwen.py)
from modelscope.hub.snapshot_download import snapshot_download
# 下载 Qwen3.5-9B-Instruct 模型到本地指定目录
model_dir = snapshot_download(
'qwen/Qwen3.5-9B-Instruct',
cache_dir='/Users/你的用户名/Documents/models' # 替换为你的本地路径
)
print(f"模型下载完成,路径:{model_dir}")
执行脚本:
python download_qwen.py
- 原版 FP16 模型约占用 18GB 空间,下载完成后会保存在指定的
cache_dir下。
方法 B:通过 Hugging Face 下载(需科学上网)
# 安装 huggingface-hub 工具
pip install huggingface-hub
# 下载模型(替换为你的保存路径)
huggingface-cli download Qwen/Qwen3.5-9B-Instruct --local-dir /Users/你的用户名/Documents/models/Qwen3.5-9B-Instruct
步骤 3:运行模型进行对话
创建运行脚本(命名为 run_qwen.py),支持 4-bit 量化(减少内存占用):
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
# 配置 4-bit 量化(关键:降低内存占用,M 系列芯片友好)
bnb_config = BitsAndBytesConfig(
load_in_4bit=True, # 启用 4-bit 量化
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16
)
# 加载模型和分词器(替换为你的模型本地路径)
model_path = "/Users/你的用户名/Documents/models/Qwen3.5-9B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_path,
trust_remote_code=True,
quantization_config=bnb_config, # 启用量化
device_map="auto", # 自动分配设备(M 芯片会用 Metal)
torch_dtype=torch.bfloat16
)
# 对话示例
prompt = "介绍一下大语言模型的核心原理"
messages = [{"role": "user", "content": prompt}]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to('mps') # M 芯片用 mps,Intel 用 cpu
# 生成回答
generated_ids = model.generate(
model_inputs.input_ids,
max_new_tokens=512, # 最大生成长度
temperature=0.7 # 随机性
)
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print("模型回答:")
print(response)
执行脚本:
python run_qwen.py
总结
-
新手首选 Ollama:仅需 2 条命令即可完成安装和运行,自动处理量化,占用约 10GB 硬盘空间,对话体验流畅。
-
手动部署适合开发:需手动下载模型(18GB 原版/10GB 量化版)、配置环境,但可自定义量化策略、集成到代码中,灵活性更高。
-
关键注意点:M 系列芯片(M1/M2/M3)运行 Qwen3.5-9B 效率远高于 Intel 芯片,建议至少 16GB 内存,4-bit 量化可大幅降低内存/显存占用。
(注:文档部分内容可能由 AI 生成)