Skip to content

simplified key二进制配置JSON

本文介绍在自定义算子工程中如何提供核函数(Kernel)二进制配置JSON,以及该JSON如何驱动按自定义simplified key编译多份binary。该机制适用于一个OpType需要按dtype、format、shape分档或属性组合预编译多份binary,并由运行时根据GenSimplifiedKey回调选择的场景。

回调的编写和注册方式见GenSimplifiedKey回调实现,运行时选择过程见基于simplified key的运行时选择流程。

完整可运行样例请参考自定义simplified key编译样例。

配置目录和文件准备​

使用--kernel-json-file时,Binary JSON是用户提供的输入文件。op_host/config/<soc>只是推荐的存放位置,CMake不会自动创建该目录或文件。

  • 仓库样例已提供对应目录和JSON,直接编译无需创建。
  • 自定义工程需要自行准备Binary JSON,并让--kernel-json-file指向实际路径;目录名和文件名可以按工程调整。
  • build/.../binary/config/<soc>是编译生成的输出目录,无需预先创建。

JSON如何提供​

将JSON放在算子工程现有的Host侧配置目录中,并按AI处理器型号划分。例如AddCustomTemplate样例使用:

Text
op_host/config/ascendxxx/add_custom_template_binary.json

在op_kernel/CMakeLists.txt中通过npu_op_kernel_options()传递--kernel-json-file。该参数属于编译选项,不写入npu_op_kernel_sources():

CMake
npu_op_kernel_sources(ascendc_kernels
    OP_TYPE AddCustomTemplate
    KERNEL_DIR add_custom_template
    KERNEL_FILE add_custom_template.cpp
)

npu_op_kernel_options(ascendc_kernels AddCustomTemplate
    OPTIONS
        --kernel-json-file=${CMAKE_CURRENT_SOURCE_DIR}/../op_host/config/ascendxxx/add_custom_template_binary.json
)

构建配置阶段会将该选项记录到自动生成的custom_opc_options.ini,后续二进制编译从该文件读取。例如:

Text
AddCustomTemplate@@--kernel-json-file=<工程路径>/op_host/config/ascendxxx/add_custom_template_binary.json

JSON格式​

JSON文件最外层包含op_type和op_list两个字段:op_type指定算子类型,op_list中的每个元素描述一份待编译的binary。以下是AddCustomTemplate的完整示例,按FP16和FP32生成两份binary:

JSON
{
  "op_type": "AddCustomTemplate",
  "op_list": [
    {
      "bin_filename": "AddCustomTemplate_FP16",
      "simplified_key": "custom_fp16",
      "inputs": [
        {
          "name": "x",
          "index": 0,
          "dtype": "float16",
          "format": "ND",
          "shape": [-2]
        },
        {
          "name": "y",
          "index": 1,
          "dtype": "float16",
          "format": "ND",
          "shape": [-2]
        }
      ],
      "outputs": [
        {
          "name": "z",
          "index": 0,
          "dtype": "float16",
          "format": "ND",
          "shape": [-2]
        }
      ]
    },
    {
      "bin_filename": "AddCustomTemplate_FP32",
      "simplified_key": "custom_fp32",
      "inputs": [
        {
          "name": "x",
          "index": 0,
          "dtype": "float32",
          "format": "ND",
          "shape": [-2]
        },
        {
          "name": "y",
          "index": 1,
          "dtype": "float32",
          "format": "ND",
          "shape": [-2]
        }
      ],
      "outputs": [
        {
          "name": "z",
          "index": 0,
          "dtype": "float32",
          "format": "ND",
          "shape": [-2]
        }
      ]
    }
  ]
}

字段说明如下:

表1 simplified key二进制配置JSON字段说明

字段说明
op_type必填,必须与npu_op_kernel_sources()中的OP_TYPE一致。
op_list必填非空数组;数组中的每一项对应一份独立编译的binary。
bin_filename必填,不含目录;用于生成.o和配套.json文件名。去掉扩展名后必须等于${op_type}或以${op_type}_开头,其中${op_type}为大驼峰风格的算子名,且文件名不能重复。
simplified_key必填非空字符串;是GenSimplifiedKey回调返回的自定义片段,必须与回调逐字节一致;字符串经UTF-8编码后的长度必须小于256字节。
inputs、outputs每个输入输出包含name、index、dtype、format和shape,描述本项binary支持的组合。

name和index必须与算子原型定义的输入输出顺序一致;dtype和format必须属于原型声明的支持范围;shape应表达该binary覆盖的shape范围。JSON解析会检查最外层结构、op_type、非空op_list、bin_filename和simplified_key等基本格式,后续asc_opc编译还会依据输入输出描述处理具体组合。因此,JSON中的组合必须与原型定义、核函数(Kernel)实现和回调分支保持一致。

对编译过程的影响​

配置JSON后,编译链路按以下顺序工作:

  1. CMake将--kernel-json-file写入custom_opc_options.ini。
  2. 二进制构建读取JSON,按op_list拆分为单组合JSON;每一项使用自身的bin_filename作为输出名称。
  3. 对每个单组合JSON调用asc_opc编译核函数(Kernel)源码,得到对应的.o和描述该.o的.json。
  4. JSON中存在非空simplified_key时,asc_opc使用自定义simplified key模式2,并将算子类型、运行时公共维度和该片段组合为完整simplified key。
  5. 汇总阶段生成算子配置JSON和binary_info_config.json,记录每个完整Key关联的.o与.json路径。

以FP16项为例,主要产物如下:

Text
binary/ascendxxx/add_custom_template/AddCustomTemplate_FP16.o
binary/ascendxxx/add_custom_template/AddCustomTemplate_FP16.json
binary/config/ascendxxx/add_custom_template.json
binary/config/ascendxxx/binary_info_config.json

其中AddCustomTemplate_FP16.json记录单份.o的核函数(Kernel)元信息;add_custom_template.json汇总每个组合的支持信息;binary_info_config.json为运行时提供完整simplified key到binary路径的查找表。示例如下:

JSON
{
  "AddCustomTemplate": {
    "simplifiedKeyMode": 2,
    "binaryList": [
      {
        "simplifiedKey": ["AddCustomTemplate/d=0,p=1/custom_fp16"],
        "binPath": "ascendxxx/add_custom_template/AddCustomTemplate_FP16.o",
        "jsonPath": "ascendxxx/add_custom_template/AddCustomTemplate_FP16.json"
      },
      {
        "simplifiedKey": ["AddCustomTemplate/d=0,p=1/custom_fp32"],
        "binPath": "ascendxxx/add_custom_template/AddCustomTemplate_FP32.o",
        "jsonPath": "ascendxxx/add_custom_template/AddCustomTemplate_FP32.json"
      }
    ]
  }
}

实际产物可能针对确定性和实现模式写入多个完整Key;它们共享同一个自定义片段和binary路径。算子开发者不应在回调中手动拼接AddCustomTemplate/d=0,p=1/等前缀。

与普通编译的关系​

不配置--kernel-json-file且不注册GenSimplifiedKey时,算子继续使用原有编译和binary匹配流程。只有同时满足“JSON为每个组合提供非空simplified_key”和“Host侧注册相应回调”时,运行时才具备自定义simplified key选择所需的两端信息。

相关文档​

免责声明:本站内容由 asc-devkit 仓 master 分支自动编译生成,属于持续开发版本,可能存在缺陷,仅供预览与参考。如需稳定及商用资料,请查阅官方 昇腾社区。