1- # 算子库
1+ # InfiniOperators 算子库
22
3- 跨平台高性能通用算子库 。形式为 C 接口动态库。
3+ 跨平台高性能统一算子库 。形式为 C 接口动态库。
44
5- 采用二段式算子设计,每个算子都实现并对外暴露以下的 C 接口:
5+ ## 简介
66
7- - 第一阶段:构造算子 Descriptor。用户提供的算子名称、硬件、以及算子配置(如计算的数据类型、计算排布等),相应模组会被 load 到硬件上。
7+ ### 算子接口设计
8+
9+ 采用3+1段式算子设计,每个算子都实现并对外暴露以下的 C 接口:
10+
11+ - 第一阶段:构造硬件控柄(Handle)。用户提供控柄地址、硬件类型以及硬件序号。控柄所在的内存空间由用户管理。
812
913 ``` C
10- void * createOpDescriptor (Device, void * config );
14+ infiniopStatus_t infiniopCreateHandle (infiniopHandle_t * handle_ptr, int device, int device_id );
1115 ```
1216
13- - 第二阶段:计算。根据一阶段的 Descriptor,执行相应计算,用户需要提供输入输出张量,以及硬件计算流(CPU 为 NULL) 。
17+ - 第二阶段:构造算子描述( Descriptor)。用户提供描述符地址、硬件控柄、以及算子涉及的张量描述(含张量数据类型、形状和步长)。这一步会完成算子所需的与张量数据无关的预计算 。
1418
1519 ```C
16- void op(void *descriptor, Tensor output, Tensor input, void *stream );
20+ infiniopStatus_t infiniopCreateOpDescriptor(infiniopHandle_t handle, infiniopOpDescriptor_t *desc_ptr, infiniopTensorDescriptor_t t, ... );
1721 ```
1822
19- - 销毁 Descriptor 。
23+ - 第三阶段(可选):计算额外工作空间。根据算子描述,计算算子所需的额外工作空间大小,并存储于用户提供的位置。具体空间分配由用户负责 。
2024
2125 ``` C
22- void destroyOpDescriptor (void * descriptor );
26+ infiniopStatus_t infiniopGetOpWorkspaceSize (infiniopOpDescriptor_t desc, uint64_t * size );
2327 ```
2428
29+ - 第四阶段:计算。根据算子描述符,在指定的硬件上执行相应计算,用户需要提供输入输出的数据,以及硬件计算流(CPU 为 NULL)。
30+
31+ ```C
32+ infiniopStatus_t infiniopGetOp(infiniopOpDescriptor_t desc, [void *workspace, uint64_t workspace_size,] void *output_data, void *input_data, ..., void *stream);
33+ ```
34+
35+ - 销毁描述和硬件控柄。
36+
37+ ``` C
38+ infiniopStatus_t infiniopDestroyOpDescriptor (infiniopOpDescriptor_t desc);
39+ infiniopStatus_t infiniopDestroyHandle(infiniopHandle_t handle);
40+ ```
41+
42+ ### 张量(Tensor)描述设计
43+
44+ 张量描述由以下几个部分组成:
45+
46+ 1.数据类型,由打包大小(即一个元素代表几个数据)、符号位、元素大小、尾数位数、指数位数共4字节表示。定义如下:
47+
48+ ```C
49+ typedef struct DataLayout {
50+ unsigned short
51+ packed : 8,
52+ sign : 1,
53+ size : 7,
54+ mantissa : 8,
55+ exponent : 8;
56+ } DataLayout;
57+ ```
58+
59+ 2.维度信息。张量有多少个维度。类型为uint64_t。
60+
61+ 3.张量形状。张量每个维度的大小。类型为uint64_t* 。
62+
63+ 4.张量步长。张量每个维度的步长。类型为uint64_t* 。
64+
65+ 创建和销毁张量描述符的接口:
66+
67+ ``` C
68+ infiniopStatus_t infiniopCreateTensorDescriptor (infiniopTensorDescriptor_t * desc_ptr, DataLayout layout, uint64_t ndim, uint64_t * shape, uint64_t * strides);
69+ infiniopStatus_t infiniopDestroyTensorDescriptor(infiniopTensorDescriptor_t desc);
70+ ```
71+
2572## 一、使用说明
2673
27- ### 配置
74+ ### 1. 配置
2875
2976#### 查看当前配置
3077
@@ -52,23 +99,27 @@ xmake f --nv-gpu=true --cuda=$CUDA_HOME -cv
5299xmake f --cambricon-mlu=true -cv
53100```
54101
55- ### 编译
102+ #### 配置 NPU
103+
104+ ```` xmake
105+ xmake f --ascend-npu=true -cv
106+ ````
107+
108+ ### 2. 编译安装
56109
57110``` xmake
58- xmake
111+ xmake build && xmake install
59112```
60113
61- ### 将编译好的算子库添加至环境变量 ` INFINI_ROOT `
114+ ### 3. 设置环境变量
62115
63- ``` bash
64- export INFINI_ROOT=[PATH_TO_LIBRARY]
65- ```
116+ 按输出提示设置 ` INFINI_ROOT ` 和 ` LD_LIBRARY_PATH ` 环境变量。
66117
67- ### 运行算子测试
118+ ### 4. 运行算子测试
68119
69120``` bash
70121cd operatorspy/tests
71- python operator_name.py
122+ python operator_name.py [--cpu | --cuda | --cambricon | --ascend]
72123```
73124
74125## 二、开发说明
@@ -82,6 +133,8 @@ python operator_name.py
82133│ │ ├── [operator_name].h # 对外暴露的算子 C 接口定义,descriptor 定义
83134│ ├── tensor
84135│ │ ├── tensor_descriptor.h # 对外暴露的张量 descriptor 定义
136+ │ ├── handle
137+ │ │ ├── handle_export.h # 对外暴露的硬件 handle 定义
85138│ ├── * .h # 对外暴露的核心结构体定义
86139├── src
87140│ ├── devices
@@ -105,7 +158,7 @@ python operator_name.py
105158
106159- 在 ` src/device.h ` 和 ` operatorspy/devices.py ` 中增加新的硬件类型,注意两者需要一一对应;
107160- 在 ` xmake.lua ` 中增加新硬件的编译选项以及编译方式;
108- - 在 ` src/ops/devices/[device_name] ` 下编写特定硬件的通用代码 ;
161+ - 在 ` src/ops/devices/[device_name] ` 下编写特定硬件的handle实现和通用代码 ;
109162- 实现该硬件的算子;
110163
111164### 增加新的算子
0 commit comments