三个命令装好五分钟验证这篇把 ops-nnn 在昇腾NPU上的完整使用流程走一遍。环境确认# CANN 版本npu-smi info|grepVersion# Python torch_npupython3-cimport torch_npu; print(torch_npu.__version__)# NPU 设备python3-cimport torch; print(torch.npu.device_count())三个输出都正常才能继续。CANN 版本建议 8.0torch_npu 版本跟 CANN 对齐。克隆编译gitclone https://atomgit.com/cann/ops-nn.gitcdops-nnbashbuild.sh编译时间约 3-5 分钟。产物在output/目录下是.so文件。torch_npu 启动时会自动搜索这些.so并注册算子。编译问题排查ASCEND_HOME_PATH not setexportASCEND_HOME_PATH/usr/local/Ascend/ascend-toolkit/latestgcc too old需要 GCC 7.5CentOS 用户装 devtoolset。编译通过但算子没注册检查output/目录下的.so是否在LD_LIBRARY_PATH里。验证单个算子importtorchimporttorch_npu# 测试 LayerNormxtorch.randn(2,4096,4096,devicenpu,dtypetorch.float16)wtorch.ones(4096,devicenpu,dtypetorch.float16)btorch.zeros(4096,devicenpu,dtypetorch.float16)outtorch_npu.npu.layer_norm(x,[4096],w,b)print(fLayerNorm 输出 shape:{out.shape})# [2, 4096, 4096]# 测试融合 Linear SiLUxtorch.randn(1,4096,4096,devicenpu,dtypetorch.float16)wtorch.randn(14336,4096,devicenpu,dtypetorch.float16)btorch.randn(14336,devicenpu,dtypetorch.float16)outtorch_npu.npu.linear_activation(x,w,b,activationsilu)print(f融合 LinearSiLU 输出 shape:{out.shape})# [1, 4096, 14336]性能基准importtimedefbench(func,*args,warmup10,repeat100):for_inrange(warmup):func(*args)torch.npu.synchronize()t0time.time()for_inrange(repeat):func(*args)torch.npu.synchronize()return(time.time()-t0)/repeat*1000# 标准写法defstandard_linear_silu(x,w,b):outtorch.nn.functional.linear(x,w,b)returntorch.nn.functional.silu(out)# 融合写法deffused_linear_silu(x,w,b):returntorch_npu.npu.linear_activation(x,w,b,activationsilu)xtorch.randn(1,4096,4096,devicenpu,dtypetorch.float16)wtorch.randn(14336,4096,devicenpu,dtypetorch.float16)btorch.randn(14336,devicenpu,dtypetorch.float16)t_stdbench(standard_linear_silu,x,w,b)t_fusedbench(fused_linear_silu,x,w,b)print(f标准:{t_std:.3f}ms, 融合:{t_fused:.3f}ms, 加速比:{t_std/t_fused:.2f}x)正常输出标准约 0.5ms融合约 0.35ms加速比 1.3-1.5x。如果加速比低于 1.2x说明融合没生效——检查 weight 是否.contiguous()。和 ATB 配合ops-nn 的算子在 ATB 推理服务里自动启用。不需要手动调用torch_npu.npu.*接口fromatbimportLLM modelLLM(meta-llama/Llama-2-7b-hf,devicenpu:0)# ATB 内部自动调用 ops-nn 的 LayerNorm、SiLU、RMSNorm# 以及 ops-transformer 的 FlashAttention、MergedMatMuloutputmodel.generate(Hello)自定义模型使用 ops-nn如果你的模型不在 ATB 支持列表里需要手动确保算子走 ops-nn 的优化路径# 关键确保所有输入 tensor 是 contiguous 的xx.contiguous()# 关键使用 float16 而不是 float32xx.half()# 关键用 torch.compile 走 GE 图编译modeltorch.compile(model,backendnpu)这三步做对了ops-nn 的基础算子和 graph-autofusion 的自动融合都能正常工作。ops-nn 是 CANN 算子生态的底座大部分情况下你不需要直接跟它打交道。但自定义模型场景下确保基础算子走优化路径是性能的底线。仓库在这里https://atomgit.com/cann/ops-nn
CANN-ops-nn-昇腾NPU基础算子库从装到跑全流程
三个命令装好五分钟验证这篇把 ops-nnn 在昇腾NPU上的完整使用流程走一遍。环境确认# CANN 版本npu-smi info|grepVersion# Python torch_npupython3-cimport torch_npu; print(torch_npu.__version__)# NPU 设备python3-cimport torch; print(torch.npu.device_count())三个输出都正常才能继续。CANN 版本建议 8.0torch_npu 版本跟 CANN 对齐。克隆编译gitclone https://atomgit.com/cann/ops-nn.gitcdops-nnbashbuild.sh编译时间约 3-5 分钟。产物在output/目录下是.so文件。torch_npu 启动时会自动搜索这些.so并注册算子。编译问题排查ASCEND_HOME_PATH not setexportASCEND_HOME_PATH/usr/local/Ascend/ascend-toolkit/latestgcc too old需要 GCC 7.5CentOS 用户装 devtoolset。编译通过但算子没注册检查output/目录下的.so是否在LD_LIBRARY_PATH里。验证单个算子importtorchimporttorch_npu# 测试 LayerNormxtorch.randn(2,4096,4096,devicenpu,dtypetorch.float16)wtorch.ones(4096,devicenpu,dtypetorch.float16)btorch.zeros(4096,devicenpu,dtypetorch.float16)outtorch_npu.npu.layer_norm(x,[4096],w,b)print(fLayerNorm 输出 shape:{out.shape})# [2, 4096, 4096]# 测试融合 Linear SiLUxtorch.randn(1,4096,4096,devicenpu,dtypetorch.float16)wtorch.randn(14336,4096,devicenpu,dtypetorch.float16)btorch.randn(14336,devicenpu,dtypetorch.float16)outtorch_npu.npu.linear_activation(x,w,b,activationsilu)print(f融合 LinearSiLU 输出 shape:{out.shape})# [1, 4096, 14336]性能基准importtimedefbench(func,*args,warmup10,repeat100):for_inrange(warmup):func(*args)torch.npu.synchronize()t0time.time()for_inrange(repeat):func(*args)torch.npu.synchronize()return(time.time()-t0)/repeat*1000# 标准写法defstandard_linear_silu(x,w,b):outtorch.nn.functional.linear(x,w,b)returntorch.nn.functional.silu(out)# 融合写法deffused_linear_silu(x,w,b):returntorch_npu.npu.linear_activation(x,w,b,activationsilu)xtorch.randn(1,4096,4096,devicenpu,dtypetorch.float16)wtorch.randn(14336,4096,devicenpu,dtypetorch.float16)btorch.randn(14336,devicenpu,dtypetorch.float16)t_stdbench(standard_linear_silu,x,w,b)t_fusedbench(fused_linear_silu,x,w,b)print(f标准:{t_std:.3f}ms, 融合:{t_fused:.3f}ms, 加速比:{t_std/t_fused:.2f}x)正常输出标准约 0.5ms融合约 0.35ms加速比 1.3-1.5x。如果加速比低于 1.2x说明融合没生效——检查 weight 是否.contiguous()。和 ATB 配合ops-nn 的算子在 ATB 推理服务里自动启用。不需要手动调用torch_npu.npu.*接口fromatbimportLLM modelLLM(meta-llama/Llama-2-7b-hf,devicenpu:0)# ATB 内部自动调用 ops-nn 的 LayerNorm、SiLU、RMSNorm# 以及 ops-transformer 的 FlashAttention、MergedMatMuloutputmodel.generate(Hello)自定义模型使用 ops-nn如果你的模型不在 ATB 支持列表里需要手动确保算子走 ops-nn 的优化路径# 关键确保所有输入 tensor 是 contiguous 的xx.contiguous()# 关键使用 float16 而不是 float32xx.half()# 关键用 torch.compile 走 GE 图编译modeltorch.compile(model,backendnpu)这三步做对了ops-nn 的基础算子和 graph-autofusion 的自动融合都能正常工作。ops-nn 是 CANN 算子生态的底座大部分情况下你不需要直接跟它打交道。但自定义模型场景下确保基础算子走优化路径是性能的底线。仓库在这里https://atomgit.com/cann/ops-nn