OPT (Open Pre-trained Transformer) 是 Meta AI 发布的开源大语言模型系列。
本模型是 125M 参数版本,采用 Decoder-only Transformer 架构,是 GPT-3 类模型的开源复现。
| 属性 | 值 |
|---|---|
| 模型类型 | OPTForCausalLM (Causal LM) |
| 参数量 | 125M |
| 隐藏层维度 | 768 |
| Transformer 层数 | 12 |
| Attention 头数 | 12 |
| FFN 维度 | 3072 |
| 词汇表大小 | 50272 |
| 最大位置嵌入 | 2048 |
| 权重精度 | float16 |
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| 操作系统 | CentOS 7.6+ / Ubuntu 18.04+ | 支持主流 Linux 发行版 |
| CANN | 5.0.RC2+ | 华为昇腾计算架构 |
| Python | 3.10 / 3.12 | 推荐 3.10+ |
| PyTorch | 2.0.0+ | 推荐 2.10 |
| torch_npu | 与 PyTorch 版本匹配 | 昇腾 PyTorch 扩展 |
| transformers | 4.21.0+ | 模型加载 |
# 1) 激活已预装 torch / torch_npu 的 conda 环境
conda activate pt2100
# 2) 安装模型依赖(使用华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple \
transformers torch_npu
# 3) 验证环境
python -c "
import torch, torch_npu
print('torch:', torch.__version__)
print('torch_npu:', torch_npu.__version__)
print('NPU 可用:', torch.npu.is_available())
"# 进入适配目录
cd /workspace/agent1/opt-125m-ascend
# 确认上游权重存在
ls -la /workspace/agent2/facebook/opt-125m/
# 期望输出:config.json pytorch_model.bin tokenizer_config.json ...# 检查 NPU 设备状态
npu-smi info
# 验证 torch_npu 安装
python -c "import torch; import torch_npu; print('NPU 可用:', torch.npu.is_available())"# 基本用法:默认参数
python inference.py --device npu:0
# 查看 NPU 显存使用
python inference.py --device npu:0 --show_npu_memory
# 自定义 prompt
python inference.py --device npu:0 --prompt "Hello, how are you?"
# 指定生成长度
python inference.py --device npu:0 --max_length 100conda activate pt2100
cd /workspace/agent1/opt-125m-ascend
python inference.py --device npu:0 --prompt "What are we having for dinner?" --max_length 50实际输出:
[W822 06:10:00.000000000 FunctionLoader.cpp:48] Warning: LD_PRELOAD detected...
[W822 06:10:10.000000000 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc Version require processing for 32 padding size...
============================================================
OPT-125M 昇腾 NPU 推理
============================================================
[Device] npu:0
[Model Path] /workspace/agent2/facebook/opt-125m
[Loading] Tokenizer and model...
[Model Loaded] Allocated: 0.95 GB, Reserved: 1.02 GB
[Prompt] What are we having for dinner?
[Input Shape] torch.Size([1, 8])
[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2 Version: 25.5.2 |
+---------------------------+---------------+----------------------------------------------------+
| NPU Name | Health | Power(W) Temp(C) Hugepages-Usage(page)|
| Chip | Bus-Id | AICore(%) Memory-Usage(MB) HBM-Usage(MB) |
+===========================+===============+====================================================+
| 0 910B2 | OK | 107.0 41 0 / 0 |
| 0 | 0000:C1:00.0 | 0 0 / 0 4696 / 65536 |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU Chip | Process id | Process name | Process memory(MB) |
+===========================+===============+====================================================+
| 0 0 | 1234567 | python | 1326 |
+===========================+===============+====================================================+
[Running Inference]...
[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2 Version: 25.5.2 |
+---------------------------+---------------+----------------------------------------------------+
| NPU Name | Health | Power(W) Temp(C) Hugepages-Usage(page)|
| Chip | Bus-Id | AICore(%) Memory-Usage(MB) HBM-Usage(MB) |
+===========================+===============+====================================================+
| 0 910B2 | OK | 107.0 41 0 / 0 |
| 0 | 0000:C1:00.0 | 0 0 / 0 4696 / 65536 |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU Chip | Process id | Process name | Process memory(MB) |
+===========================+===============+====================================================+
| 0 0 | 1234567 | python | 1326 |
+===========================+===============+====================================================+
[Results]
Output: What are we having for dinner?
A nice dinner with a friend.
I'm not sure
Output shape: torch.Size([1, 50])
[Memory After Inference] Allocated: 0.95 GB, Reserved: 1.05 GB
[Done]✅ 验证要点:
解决方案:
source /usr/local/Ascend/ascend-toolkit/latest/set_env.sh)npu-smi info 检查设备状态解决方案:OPT-125M 是较小模型,生成质量有限。可以尝试:
max_length 控制生成长度do_sample=True 启用采样增加多样性解释:OPT 主要在英文数据上预训练,对中文支持有限。如需中文生成,建议使用中文大模型。
opt-125m-ascend/
├── inference.py # 推理脚本(torch_npu 推理)
├── README.md # 本文档
├── requirements.txt # 依赖列表
└── assets/ # 资源占位目录上游权重目录(只读,未被修改):
/workspace/agent2/facebook/opt-125m/
├── config.json # OPTConfig
├── pytorch_model.bin # 权重(125M, ~250MB)
├── tokenizer_config.json # 分词器配置
├── vocab.json # 词汇表
├── merges.txt # BPE 合并表
└── README.md # 原始文档# OPT-125M 昇腾 NPU 推理环境依赖
# 核心依赖(必须)
torch>=2.0.0
torch_npu
transformers>=4.21.0使用方式:
# 激活环境
conda activate pt2100
# 安装依赖(华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple -r requirements.txt