ascend-ai-coding/awesome-ascend-skills/ascend-tf-community
Claude Code and agent skill by ascend-ai-coding/awesome-ascend-skills
- community
Checking that the SKILL.md file is still in the repository.
昇腾 TensorFlow Community 迁移适配 Skill,适用于将基于 TensorFlow 2.x 的模型原生部署到华为 Ascend NPU,而不经过 TF 到 PyTorch 转换,覆盖 aarch64 源码编译 TF 2.6.5、tfplugin 安装、自动迁移工具使用、手动适配与精度验证。
Install
npx vskill@latest install ascend-ai-coding/awesome-ascend-skills/ascend-tf-communitySKILL.md
昇腾 TensorFlow Community 迁移适配 Skill
项目概述
本 Skill 记录在昇腾 NPU 上使用 TensorFlow Community 方式(非 TF→PyTorch 转换) 部署 TF 2.x 模型的完整流程。核心思路是:保持原始 TF 代码基本不变,通过 npu_device 插件将算子调度到 Ascend NPU 上执行。
适用场景:
- 已有 TF 2.x 训练/推理代码,希望直接运行在昇腾 NPU 上
- 不希望或不适合做 TF→PyTorch 框架转换
- 参考文档:https://www.hiascend.com/document/detail/zh/TensorFlowCommunity/850/migration/tfmigr2/
前置条件
| 项目 | 要求 | |
|---|---|---|
| 硬件 | Ascend910 系列(至少 1 卡) | |
| CANN | ≥ 8.0(推荐 8.5) | |
| Python | 3.7.x / 3.8.x / 3.9.x | |
| TensorFlow | 2.6.5(需源码编译,ABI=0) | |
| npu_device | 0.1(来自 Ascend-cann-tfplugin) | |
| Bazel | 3.7.2(编译 TF 所需) |
迁移流程总览
Step 1:环境检查
Step 2:源码编译 TF 2.6.5(aarch64)
关键:aarch64 平台 pip 不提供 TF 2.6.x wheel,必须从源码编译。x86_64 平台可直接pip install tensorflow-cpu==2.6.5。
2.1 创建 Conda 环境
2.2 安装 Bazel 3.7.2
2.3 克隆 TF 源码 + nsync 补丁
nsync aarch64 补丁(昇腾文档要求):
编辑 /tmp/nsync-1.22.0/platform/c++11/atomic.h,在 NSYNC_CPP_START_ 后添加:
并在每个 atm_cas_*_u32_ 函数中,将 return (std::atomic_compare_exchange_strong_explicit(...)); 改为:
重新打包并更新 sha256:
修改 tensorflow/workspace2.bzl 中 nsync 的 sha256 和 urls(添加 file:///tmp/nsync-1.22.0.tar.gz)。
2.4 配置编译
关键:设置 ABI=0(昇腾文档要求 TF 和 FwkPlugin ABI 一致):
在 .tf_configure.bazelrc 顶部添加:
2.5 编译
2.6 安装配套依赖
Step 3:安装 tfplugin(npu_device)
3.1 下载 Ascend-cann-tfplugin
3.2 提取并安装 npu_device wheel
3.3 安装 CANN Python 依赖
3.4 验证 NPU 设备
Step 4:代码适配
4.1 最小侵入式适配(推荐)
对于大多数 TF 2.x 项目,只需在入口脚本顶部添加两行:
TF 的标准 Keras API(Dense、Conv、LSTM、BatchNorm 等)会自动在 NPU 上执行。
4.2 自动迁移工具 convert_tf2npu(可选)
CANN 提供了 convert_tf2npu 工具进行批量代码转换:
注意:该工具主要处理tf.Session、tf.ConfigProto等 TF 1.x 模式的适配。对于纯 TF 2.x Eager 模式项目,通常手动添加npu_device.open()即可。
4.3 常见适配点
| 原始代码 | 适配方式 | |
|---|---|---|
import tensorflow as tf | 前面加 import npu_device; npu_device.open().as_default() | |
tf.device('/gpu:0') | 删除或改为 tf.device('/cpu:0')(npu_device 自动接管默认设备) | |
tensorflow-gpu 依赖 | 替换为源码编译的 tensorflow | |
tf.config.experimental.set_memory_growth(gpu, True) | 删除(NPU 不需要) | |
| CUDA 相关环境变量 | 不再需要,改用 ASCEND_RT_VISIBLE_DEVICES |
Step 5:精度验证
CPU vs NPU 对比模板
预期精度范围
| 精度模式 | 典型 max_diff | 说明 | |
|---|---|---|---|
| allow_fp32 | < 1e-5 | 纯 FP32,精度最高 | |
| allow_hf32(默认) | < 1e-2 | 使用 HF32 混合精度,性能更好 |
如需更高精度:
踩坑记录
编译相关
- aarch64 无 pip wheel:TF 2.6.5 在 aarch64 上无 pip 预编译包,必须源码编译
- ABI 必须为 0:TF 和 tfplugin 的
_GLIBCXX_USE_CXX11_ABI必须一致(均为 0) - nsync 补丁:aarch64 上 nsync 原子操作需要添加
__sync_synchronize()屏障 - GitHub 超时:bazel 下载依赖时 GitHub 可能超时,用
sed替换为镜像(ghfast.top) - Bazel 版本:TF 2.6.5 严格要求 Bazel 3.7.2,过高或过低都会失败
运行时相关
- npu_device 缺少 Python 依赖:
decorator、sympy等模块缺失会导致 GE 引擎初始化失败 - numpy 版本:TF 2.6 不兼容 numpy 1.24+(
np.object已移除),需使用 1.21.x - h5py 版本:TF 2.6 要求 h5py~=3.1.0,但 3.7.0 也可运行,用
--no-deps安装 TF 绕过 - CANN 版本兼容性:tfplugin 8.0.RC3 的 npu_device 可在 CANN 8.5 环境下正常工作
精度相关
- HF32 模式:Ascend910 默认使用 HF32(half-float32),部分算子精度低于纯 FP32
- Unrecognized graph engine option 警告:CANN 版本差异导致,不影响正确性
已知限制
- 仅支持 TF 2.6.5(与 tfplugin ABI 绑定)
- aarch64 必须源码编译 TF(约 4-5 分钟,取决于核心数)
- 不支持 tf.distribute 多卡策略(需使用 npu_device 专用分布式 API)
- 动态 shape 算子部分可能 fallback 到 CPU 执行
配套脚本
- TensorFlow Community 环境检查:
python scripts/check_tf_npu_env.py --open-device
参考资料
- TF Community 编译与安装检查单:`references/build-checklist.md`
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/ai-for-science/tf-framework/ascend-tf-community/SKILL.md
Popularity
- Repository stars
- 126
- Repository forks
- 51
- 7-day trend score
- 24
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/ascend-ai-coding/awesome-ascend-skills/ascend-tf-community)