Skip to content

Commit 547169c

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

4 files changed

Lines changed: 364 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: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ InfiniCore 是一个跨平台统一编程工具集,为不同芯片平台的功
2626

2727
API 定义以及使用方式详见 [`InfiniCore文档`](https://github.com/InfiniTensor/InfiniCore-Documentation)
2828

29+
## 项目依赖
30+
31+
- [Xmake](https://xmake.io/):跨平台自动构建工具,用于编译 InfiniCore 项目。
32+
- [gcc-11](https://gcc.gnu.org/) 以上或者 [clang-16](https://clang.llvm.org/):基础编译器,需要支持 C++ 17 标准。
33+
- [Python>=3.10](https://www.python.org/)
34+
- [PyTorch](https://pytorch.org/):可选,用于对比测试。
35+
- 各个硬件平台的工具包:请参考各厂商官方文档(如英伟达平台需要安装 CUDA Toolkit)。
36+
2937
## 配置和使用
3038

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

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

@@ -42,7 +50,27 @@ git clone --recursive https://github.com/InfiniTensor/InfiniCore.git
4250
git submodule update --init --recursive
4351
```
4452

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

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

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

72-
### 手动安装
100+
##### 手动安装底层库
73101

74102
0. 生成九齿算子(可选)
75103

76-
参见[使用九齿](#使用九齿)章节。
104+
- 克隆并安装[九齿算子库](https://github.com/InfiniTensor/ntops)
105+
106+
-`InfiniCore` 文件夹下运行以下命令 AOT 编译库中的九齿算子:
107+
108+
```shell
109+
PYTHONPATH=${PYTHONPATH}:src python scripts/build_ntops.py
110+
```
77111

78112
1. 项目配置
79113

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

119153
按输出提示设置 `INFINI_ROOT``LD_LIBRARY_PATH` 环境变量。
120154

121-
### 运行测试
122-
123-
#### 运行Python算子测试
155+
#### 2. 安装 C++ 库
124156

125157
```shell
126-
python test/infiniop/[operator].py [--cpu | --nvidia | --cambricon | --ascend]
127-
```
128-
129-
#### 一键运行所有Python算子测试
130-
131-
```shell
132-
python scripts/python_test.py [--cpu | --nvidia | --cambricon | --ascend]
158+
xmake build _infinicore
159+
xmake install _infinicore
133160
```
134161

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

143164
```shell
144-
xmake build infiniccl-test
165+
pip install .
145166
```
146167

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

149170
```shell
150-
infiniccl-test --nvidia
171+
pip install . -e
151172
```
152173

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

155-
#### 构建
176+
### 三、运行测试
156177

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

160-
```shell
161-
xmake build _infinicore
180+
```bash
181+
python test/infinicore/run.py --nvidia --verbose --bench
162182
```
163183

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

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

168188
```shell
169-
xmake install _infinicore
170-
```
171-
172-
2. 安装 `infinicore`
173-
174-
```shell
175-
pip install .
189+
# 测试单算子
190+
python test/infiniop/[operator].py [--cpu | --nvidia | --cambricon | --ascend]
191+
# 测试全部算子
192+
python scripts/python_test.py [--cpu | --nvidia | --cambricon | --ascend]
176193
```
177194

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`,并在运行一键安装脚本前完成以下准备工作:
195+
#### 通信库(InfiniCCL)测试
185196

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

188199
```shell
189-
git clone https://github.com/InfiniTensor/ntops.git
190-
cd ntops
191-
pip install -e .
200+
xmake build infiniccl-test
192201
```
193202

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

198205
```shell
199-
PYTHONPATH=${PYTHONPATH}:src python scripts/build_ntops.py
206+
infiniccl-test --nvidia
200207
```
201208

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

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

0 commit comments

Comments
 (0)