---
title: 新建算子工程开发算子
description: "1. 创建工程与IR json文件。"
url: https://www.hiascend.com/document/detail/zh/Atlas200IDKA2DeveloperKit/latest/operatordev/Ascendcopdevg/opdev_003.html
sourcePath: /source/zh/Atlas200IDKA2DeveloperKit/23.0.RC2/operatordev/Ascendcopdevg/opdev_003.html
indexId: 882151b69e48d87ca9d88de7dce3967c656585df85a62e9d9588dfd893677a1586
---
# 新建算子工程开发算子

1. 创建工程与IR json文件。

  a. 执行如下命令返回根目录。

```
cd
~
```


  b. 创建AddCustom.json文件。

```
vim AddCustom.json
```


  c. 键盘按下“i”键进入INSERT模式，复制以下内容至文件，以    AddCustom(https://gitee.com/ascend/samples/blob/8.0.RC1/operator/AddCustomSample/FrameworkLaunch/AddCustom.json)
为例：

```
[
{
"op": "AddCustom",
"language": "cpp",
"input_desc": [
{
"name": "x",
"param_type": "required",
"format": [
"ND"
],
"type": [
"float16"
]
},
{
"name": "y",
"param_type": "required",
"format": [
"ND"
],
"type": [
"float16"
]
}
],
"output_desc": [
{
"name": "z",
"param_type": "required",
"format": [
"ND"
],
"type": [
"float16"
]
}
]
}
]
```


  d. 编辑完成后，按“ESC”键退出INSERT模式，执行:wq保存AddCustom.json文件。
2. 使用msopgen工具创建Ascend C算子工程。

```
/usr/local/Ascend/ascend-toolkit/latest/python/site-packages/bin/msopgen gen -i AddCustom.json -f tf -c ai_core-ascend310b -lan cpp -out
~
/AddCustom
```


3. 开发算子的host侧代码及kernel侧代码。
  可参考Samples仓AddCustom算子样例，host侧：

```
cp
~
/samples/operator/AddCustomSample/FrameworkLaunch/AddCustom/op_host/*
~
/AddCustom/op_host/
```


  kernel侧：

```
cp
~
/samples/operator/AddCustomSample/FrameworkLaunch/AddCustom/op_kernel/*
~
/AddCustom/op_kernel/
```


4. 修改算子工程中配置文件CMakePresets.json，将CMakePresets.json中ASCEND_CANN_PACKAGE_PATH的value值修改为实际CANN包安装目录/usr/local/Ascend/ascend-toolkit/latest。

  a. 执行如下命令打开配置文件。

```
cd
~
/AddCustom
vim CMakePresets.json
```


  b. 使用键盘的方向键令光标移动到ASCEND_CANN_PACKAGE_PATH的值位置，按下键盘上“i”键进入到INSERT模式，修改为CANN包安装目录
    修改完成后按“ESC”键退出INSERT模式，执行:wq保存文件。

5. 编译生成算子安装包。
  在AddCustom目录下执行如下命令编译自定义AddCustom算子，生成算子安装包。

```
./build.sh
```
