1. 在MacOS M2上运行Llama2的准备工作
在M2芯片的Mac上运行Llama2需要先准备好基础环境。M系列芯片采用ARM架构,与传统的x86架构有所不同,这会影响一些工具的安装方式。
首先确保你的系统版本足够新,建议升级到最新稳定版的macOS。打开终端,输入以下命令检查系统信息:
bash复制system_profiler SPSoftwareDataType
你会看到类似这样的输出:
code复制Software:
System Software Overview:
System Version: macOS 14.5 (23F79)
Kernel Version: Darwin 23.5.0
Boot Volume: Macintosh HD
Boot Mode: Normal
Computer Name: MacBook Pro
User Name: Your Name (yourusername)
Secure Virtual Memory: Enabled
System Integrity Protection: Enabled
Time since boot: 2 days 23:14
1.1 安装Homebrew
Homebrew是macOS上最常用的包管理工具。如果你的系统还没有安装,可以通过以下命令安装:
bash复制/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
安装完成后,将Homebrew添加到你的PATH环境变量中:
bash复制echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc
source ~/.zshrc
1.2 Python环境配置
Llama2主要使用Python运行,建议使用pyenv管理多个Python版本:
bash复制brew install pyenv
pyenv install 3.10.12 # 推荐使用3.10.x版本
pyenv global 3.10.12
验证Python版本:
bash复制python --version
# 应该显示 Python 3.10.12
1.3 安装必要的依赖库
Llama2需要一些特定的库支持:
bash复制brew install cmake protobuf rust
pip install torch numpy transformers
注意:如果你使用M1/M2芯片,需要安装专门优化的PyTorch版本:
bash复制pip install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
