Skip to content

Commit 1aaf271

Browse files
issue/554 更新InfiniCore开发指南
1 parent a5e20fc commit 1aaf271

4 files changed

Lines changed: 356 additions & 74 deletions

File tree

DEV.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ Dear 开发者,感谢你参与 InfiniCore 开源项目的开发!本文档将
66

77
### 项目模块体系
88

9+
- infinicore:统一计算框架。提供 Python 和 C++ 接口,支持多种硬件平台。
10+
- infinirt:统一底层运行时库,提供 C 语言接口,依赖 infini-utils。
11+
- infiniop:统一底层算子库,提供 C 语言接口,依赖 infinirt。除了 C++ 算子实现之外,也包括使用九齿(triton)的算子实现,这部分算子需要在编译之前使用脚本生成源文件。安装后可以运行位于 `test/infiniop` 中的单测脚本进行测试。
12+
- infiniccl:统一通信库,提供 C 语言接口,依赖 infinirt。
913
- infini-utils:全模块通用工具代码。
10-
- infinirt:运行时库,依赖 infini-utils。
11-
- infiniop:算子库,依赖 infinirt。除了 C++ 算子实现之外,也包括使用九齿(triton)的算子实现,这部分算子需要在编译之前使用脚本生成源文件。安装后可以运行位于 `test/infiniop` 中的单测脚本进行测试。
12-
- infiniccl:通信库,依赖 infinirt。
1314
- utils-test:工具库测试代码,依赖 infini-utils。
14-
- infiniop-test:算子库测试框架代码。与单测不同,读取gguf测例文件进行测试(详见[`测例文档`](test/infiniop-test/README.md))。使用前需要安装好 infiniop。
1515
- infiniccl-test:通信库测试代码,使用前需要安装好 infiniccl。
1616

1717
### 文件目录结构
@@ -21,10 +21,18 @@ Dear 开发者,感谢你参与 InfiniCore 开源项目的开发!本文档将
2121
├── xmake/*.lua # 各平台 xmake 编译配置, 包含各平台特有的编译方式
2222
2323
├── include/ # 对外暴露的头文件目录,安装时会被复制到安装目录
24-
│ ├── infiniop/*.h # InfiniOP算子库子头文件
25-
│ ├── *.h # 模块核心头文件
24+
│ ├── infinicore/*.hpp # InfiniCore计算库头文件(C++)
25+
│ ├── infiniop/*.h # InfiniOP算子库子头文件(C)
26+
│ ├── *.h/.hpp # 模块核心头文件
2627
2728
├── src/ # 各模块源代码目录,包含源代码文件以及不对外暴露的头文件
29+
│ ├── infinicore/ # InfiniCore源代码目录
30+
│ │ ├── context/ # 张量运行时/硬件上下文管理源代码目录
31+
│ │ ├── nn/ # 机器学习模块源代码目录
32+
│ │ ├── ops/ # 张量算子源代码目录
33+
│ │ ├── pybind/ # pybind 接口源代码目录
34+
│ │ ├── tensor/ # 张量库源代码目录
35+
│ │
2836
│ ├── infiniop/ # InfiniOP算子库源代码目录
2937
│ │ ├── devices/ # 每个设备平台各自的通用代码目录
3038
│ │ ├── ops/ # 算子实现代码目录
@@ -35,15 +43,14 @@ Dear 开发者,感谢你参与 InfiniCore 开源项目的开发!本文档将
3543
│ │ ├── elementwise/ # 逐元素类算子通用代码目录
3644
│ │ ├── *.h # 核心结构体定义
3745
│ │
38-
│ ├── infiniop-test/ # InfiniOP算子库测试框架
3946
│ ├── infinirt/ # InfiniRT运行时库源代码目录
4047
│ ├── infiniccl/ # InfiniCCL集合通信库源代码目录
4148
4249
├── test/ # 测试源代码目录
50+
│ ├── infinicore/ # InfiniCore测试目录
51+
│ │ ├── ops/*.py # 算子单测脚本(依赖各平台PyTorch)
4352
│ ├── infiniop/ # InfiniOP算子库单元测试目录
4453
│ │ ├── *.py # 单测脚本(依赖各平台PyTorch)
45-
│ ├── infiniop-test/
46-
│ │ ├── test_generate/ # 算子库测试框架测例生成脚本
4754
4855
├── scripts/ # 脚本目录
4956
│ ├── install.py # 安装编译脚本
@@ -64,13 +71,7 @@ Dear 开发者,感谢你参与 InfiniCore 开源项目的开发!本文档将
6471

6572
### 如何开发一个新算子
6673

67-
1. 根据算子定义设计算子接口,在 [`InfiniCore文档`](https://github.com/InfiniTensor/InfiniCore-Documentation) 中添加算子文档。提交文档 PR 。
68-
2.`include/infiniop/` 中添加算子头文件,并 include 到 `include/infiniop.h` 中。
69-
3.`src/infiniop/ops/` 中添加算子实现目录,并在目录中创建 `operator.cc` 文件实现头文件中的接口。
70-
4.`src/infiniop/ops/[op]/[device]/` 中添加平台算子实现。注意复用平台公共代码(比如逐元素计算和规约计算),开发过程中把未来可复用的代码写在相应公用代码目录里。比如 cuda kernel 可以多个平台公用,可以考虑在头文件中实现,并在多个源文件中使用。
71-
5. 算子实现可以成功编译安装后,在 `test/infiniop/` 中添加单测脚本,与 PyTorch 实现进行正确性和性能比较。测例应覆盖算子常用类型和形状。测试成功之后可以将测例添加至 `scripts/python_test.py` 一键测试脚本中(这样 Github 自动测试也会包含该算子)。
72-
6.`test/infiniop-test/` 算子测试框架中添加该算子的测例脚本。脚本应该包含构建该算子 gguf 测例的类,并在 main 函数中添加几个随机测例。验证随机 gguf 测例可以通过测试框架的测试程序。
73-
7. 按照流程提交代码 PR 。
74+
- 如果你想通过 C++ 以及硬件原生语言开发一个新的算子,请阅读 [infinicore::ops 开发指南](/src/infinicore/ops/README.md)
7475

7576
### C++ 代码命名书写规范
7677

@@ -138,6 +139,8 @@ Dear 开发者,感谢你参与 InfiniCore 开源项目的开发!本文档将
138139
int getMaxValue() const;
139140
```
140141

142+
InfiniCore 中和 torch 对齐的接口,使用 `snake_case`。
143+
141144
4. const/volatile修饰符写在类型前面
142145

143146
```c++

README.md

Lines changed: 53 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ API 定义以及使用方式详见 [`InfiniCore文档`](https://github.com/Infin
2828

2929
## 配置和使用
3030

31-
### 子模块
31+
### 一、克隆项目
3232

3333
由于仓库中含有子模块,所以在克隆时请添加 `--recursive``--recurse-submodules`,如:
3434

@@ -42,7 +42,27 @@ git clone --recursive https://github.com/InfiniTensor/InfiniCore.git
4242
git submodule update --init --recursive
4343
```
4444

45-
### 一键安装
45+
如果你需要在本地开发九齿算子(即需要对九齿算子库进行修改),推荐单独克隆[九齿算子库](https://github.com/InfiniTensor/ntops),并从本地安装:
46+
47+
```shell
48+
git clone https://github.com/InfiniTensor/ntops.git
49+
cd ntops
50+
pip install -e .
51+
```
52+
53+
### 二、编译安装
54+
55+
InfiniCore 项目主要包括:
56+
57+
1. 底层 C 库(InfiniOP/InfiniRT/InfiniCCL):[`一键安装`](#一键安装底层库)|[`手动安装`](#手动安装底层库)
58+
2. InfiniCore C++ 库:[`安装指令`](#2-安装-c-库)
59+
3. InfiniCore Python 包(依赖[九齿算子库](https://github.com/InfiniTensor/ntops)):[`安装指令`](#3-安装-python-包)
60+
61+
三者需要按照顺序进行编译安装。
62+
63+
#### 1. 安装底层库
64+
65+
##### 一键安装底层库
4666

4767
`script/` 目录中提供了 `install.py` 安装脚本。使用方式如下:
4868

@@ -69,11 +89,17 @@ python scripts/install.py [XMAKE_CONFIG_FLAGS]
6989
| `--ninetoothed=[y\|n]` | 是否编译九齿实现 | n
7090
| `--ccl=[y\|n]` | 是否编译 InfiniCCL 通信库接口实现 | n
7191

72-
### 手动安装
92+
##### 手动安装底层库
7393

7494
0. 生成九齿算子(可选)
7595

76-
参见[使用九齿](#使用九齿)章节。
96+
- 克隆并安装[九齿算子库](https://github.com/InfiniTensor/ntops)
97+
98+
-`InfiniCore` 文件夹下运行以下命令 AOT 编译库中的九齿算子:
99+
100+
```shell
101+
PYTHONPATH=${PYTHONPATH}:src python scripts/build_ntops.py
102+
```
77103

78104
1. 项目配置
79105

@@ -118,91 +144,60 @@ python scripts/install.py [XMAKE_CONFIG_FLAGS]
118144

119145
按输出提示设置 `INFINI_ROOT``LD_LIBRARY_PATH` 环境变量。
120146

121-
### 运行测试
122-
123-
#### 运行Python算子测试
124-
125-
```shell
126-
python test/infiniop/[operator].py [--cpu | --nvidia | --cambricon | --ascend]
127-
```
128-
129-
#### 一键运行所有Python算子测试
147+
#### 2. 安装 C++ 库
130148

131149
```shell
132-
python scripts/python_test.py [--cpu | --nvidia | --cambricon | --ascend]
150+
xmake build _infinicore
151+
xmake install _infinicore
133152
```
134153

135-
#### 算子测试框架
136-
137-
详见 `test/infiniop-test` 目录
138-
139-
#### 通信库(InfiniCCL)测试
140-
141-
编译(需要先安装InfiniCCL):
154+
#### 3. 安装 Python 包
142155

143156
```shell
144-
xmake build infiniccl-test
157+
pip install .
145158
```
146159

147-
在英伟达平台运行测试(会自动使用所有可见的卡):
160+
148161

149162
```shell
150-
infiniccl-test --nvidia
163+
pip install . -e
151164
```
152165

153-
### `infinicore` Python 包
166+
注:开发时建议加入 `-e` 选项(即 `pip install -e .`),这样对 `python/infinicore` 做的更改将会实时得到反映,同时对 C++ 层所做的修改也只需要运行 `xmake build _infinicore && xmake install _infinicore` 便可以生效。
154167

155-
#### 构建
168+
### 三、运行测试
156169

157-
1. 进行[手动安装](#手动安装)。
158-
2. 构建与安装内部依赖库 `_infinicore`
170+
#### 运行 InfiniCore Python算子接口测试
159171

160-
```shell
161-
xmake build _infinicore
172+
```bash
173+
python test/infinicore/run.py --nvidia --verbose --bench
162174
```
163175

164-
#### 安装
176+
使用 -h 查看更多参数。
165177

166-
1. 安装 `_infinicore`
178+
#### 运行 InfiniOP 算子测试
167179

168180
```shell
169-
xmake install _infinicore
170-
```
171-
172-
2. 安装 `infinicore`
173-
174-
```shell
175-
pip install .
181+
# 测试单算子
182+
python test/infiniop/[operator].py [--cpu | --nvidia | --cambricon | --ascend]
183+
# 测试全部算子
184+
python scripts/python_test.py [--cpu | --nvidia | --cambricon | --ascend]
176185
```
177186

178-
注:开发时建议加入 `-e` 选项(即 `pip install -e .`),这样对 `python/infinicore` 做的更改将会实时得到反映,同时对 C++ 层所做的修改也只需要运行 `xmake build _infinicore && xmake install _infinicore` 便可以生效。
179-
180-
### 使用九齿
181-
182-
[九齿](https://github.com/InfiniTensor/ninetoothed)是一门基于 Triton 但提供更高层抽象的领域特定语言(DSL)。使用九齿可以降低算子的开发门槛,并且提高开发效率。
183-
184-
InfiniCore 目前已经可以接入使用九齿实现的算子,但是这部分实现的编译是默认关闭的。如果选择编译库中的九齿实现,需要使用 `--ninetoothed=y`,并在运行一键安装脚本前完成以下准备工作:
187+
#### 通信库(InfiniCCL)测试
185188

186-
1. 安装九齿与[九齿算子库](https://github.com/InfiniTensor/ntops)
189+
编译(需要先安装底层库中的 InfiniCCL 库)
187190

188191
```shell
189-
git clone https://github.com/InfiniTensor/ntops.git
190-
cd ntops
191-
pip install -e .
192+
xmake build infiniccl-test
192193
```
193194

194-
注:安装 `ntops` 时,`ninetoothed` 会被当成依赖也一并安装进来。
195-
196-
2. 在 `InfiniCore` 文件夹下运行以下命令 AOT 编译库中的九齿算子:
195+
在英伟达平台运行测试(会自动使用所有可见的卡):
197196

198197
```shell
199-
PYTHONPATH=${PYTHONPATH}:src python scripts/build_ntops.py
198+
infiniccl-test --nvidia
200199
```
201200

202-
注:如果对九齿相关文件有修改,需要重新构建 InfiniCore 时,也需要同时运行以上命令进行重新生成。
203-
204-
3. 按照上面的指引进行[一键安装](#一键安装)或者[手动安装](#手动安装)。
205-
206201
## 如何开源贡献
207202

208203
见 [`InfiniCore开发者手册`](DEV.md)。

0 commit comments

Comments
 (0)