已合并
添加aclgraph+<<<>>> 示例代码 #4019
mihudan创建于 1月9日
添加aclgraph+<<<>>> 示例代码 #4019
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 mihudan 的贡献)ascend-robot
1月9日 评论:
1月9日 评论:
AtlasAccount
1月9日 评论:
1月9日 评论:
ascend-robot
1月9日 评论:
1月9日 评论:
以下是根据您提交的修改文件推荐的Reviewer和Committer序列,需各模块评审通过后方可合入
| Module List | Reviewers | Committers |
|---|---|---|
| repo-Ascend/op-plugin | qingfenxiaochong1, dilililiwhy, wangchao430, wang-guangbin, yangkaixin | liwei386, dilililiwhy, wangchao430, yanpengquan, yangkaixin |


1月9日 添加了label:ascend-cla/yes
此处折叠了182条消息 查看更多
examples/kernel_extension_aclgraph/torch_library/test/trig_aclgraph_test.py
@@ -0,0 +54,4 @@
54+ # Test using make_graphed_callables
55+ def test_npugraph_ex_backend(self):
56+ model = Model().npu()
57+ compiled_model = torch.compile(model, backend="npugraph_ex", fullgraph=True, dynamic=True)
fullgrap需要与out_tan风格一致


examples/kernel_extension_aclgraph/torch_library/test/trig_aclgraph_test.py
@@ -0,0 +11,4 @@
11+ return out_tan
12+
13+
14+length = [8, 2048]
需要以dtype相关sizeof计算替换2048,防止功能拓展时散弹式修改


examples/kernel_extension_aclgraph/torch_library/csrc/trig_inplace_custom.asc
@@ -0,0 +109,4 @@
109+{
110+ auto acl_stream = c10_npu::getCurrentNPUStream().stream(true);
111+ at::Tensor out_tan = at::empty_like(x);
112+ uint32_t blockDim = 8;
需要以dtype相关sizeof计算替换8与1,防止功能拓展时散弹式修改


examples/kernel_extension_aclgraph/pybind/test/add_aclgraph_test.py
@@ -0,0 +85,4 @@
85+ x, y = self.get_rand_input()
86+ output = torch.ops.ascendc_ops.ascendc_add(x.npu(), y.npu()).cpu()
87+ cpuout = torch.add(x, y)
88+ self.assertEqual(output, cpuout)
cpuout需要改为cpu_out


examples/kernel_extension_aclgraph/pybind/csrc/add_custom.asc
@@ -0,0 +93,4 @@
93+{
94+ auto acl_stream = c10_npu::getCurrentNPUStream().stream(true);
95+ at::Tensor z = at::empty_like(x);
96+ uint32_t blockDim = 8;
需要以dtype相关sizeof计算替换数字,防止功能拓展时散弹式修改


What type of PR is this?
What does this PR do / why do we need it:
添加aclgraph 与<<<>>>联调的demo测试代码,进行验证并供用户参考。
展示了如何使用PyTorch的torch.librar以及pybind两张方式注册自定义算子,通过<<<>>>内核调用符调用核函数,并适配aclgraph使用该自定义算子,以简单的Add算子和三角函数计算的原地算子为例,实现aclgraph下自定义算子的调用。
展示了3种aclgraph的使能方式,通过对比NPU输出与CPU标准加法结果来验证自定义算子的数值正确性。
算子描述
Add算子
算子功能:
Add算子实现了两个数据相加,返回相加结果的功能。对应的算子原型为:
算子规格:
原地三角函数算子
算子功能:
该算子入参为x, out_sin ,out_cos, 算子调用后,out_sin会被原地修改为sin(x)计算结果,out_cos会被原地修改为cos(x)计算结果,返回值tan(x)计算结果。对应的算子原型为:
算子规格:
代码实现介绍
以Add算子为例,样例在*.asc文件中定义了一个名为ascendc_ops的命名空间,并在其中注册了ascendc_add函数。在ascendc_add函数中通过
c10_npu::getCurrentNPUStream()函数获取当前NPU上的流,并通过内核调用符<<<>>>调用自定义的Kernel函数add_custom,在NPU上执行算子。add_custom<<<blockDim, nullptr, aclStream>>>(xGm, yGm, zGm, totalLength);PyTorch提供
TORCH_LIBRARY_FRAGMENT宏作为自定义算子注册的核心接口,用于创建并初始化自定义算子库,注册后在Python侧可以通过torch.ops.namespace.op_name方式进行调用,例如:TORCH_LIBRARY_FRAGMENT(ascendc_ops, m) { m.def(ascendc_add"(Tensor x, Tensor y) -> Tensor"); }TORCH_LIBRARY_IMPL用于将算子逻辑绑定到特定的DispatchKey(PyTorch设备调度标识)。针对NPU设备,需要将算子实现注册到PrivateUse1这一专属的DispatchKey上,例如:TORCH_LIBRARY_IMPL(ascendc_ops, PrivateUse1, m) { m.impl("ascendc_add", TORCH_FN(ascendc_ops::ascendc_add)); }注册Meta函数:
注册Meta函数使faketensor流程正常工作,在使用fx, compile等功能涉及,注册代码如下:
TORCH_LIBRARY_IMPL(ascendc_ops, Meta, m) { m.impl("ascendc_add", &add_impl_meta); }Special notes for your reviewers: