|
1 | | -// ==================== 1. 类型系统 ==================== |
2 | | -// 基础数据类型 |
3 | | -type f16, f32, f64, bf16,bf8 // 浮点类型 |
4 | | -type i8, i16, i32, i64, u8 // 整数类型 |
5 | | -type bool // 布尔类型 |
| 1 | +# DeepX IR(deepxir)规范 |
6 | 2 |
|
7 | | -//类型约束 |
8 | | -f32|f64 //支持2种类型之一 |
| 3 | +## 1. 类型系统 |
9 | 4 |
|
| 5 | +### 基础数据类型 |
| 6 | +``` |
| 7 | +type f16, f32, f64, bf16, bf8 // 浮点类型 |
| 8 | +type i8, i16, i32, i64, u8 // 整数类型 |
| 9 | +type bool // 布尔类型 |
| 10 | +``` |
10 | 11 |
|
11 | | -// Tensor类型模板 |
12 | | -type tensor<shape; elem_type> |
13 | | -// shape格式: dim1xdim2x...xdimN 或 ? 表示动态维度 |
14 | | -// 示例: tensor<10x20xf32>, tensor<?x?xi32> |
| 12 | +### 动态长度类型 |
| 13 | +``` |
| 14 | +list<type> // list 可以和以上基础类型组合 |
| 15 | +``` |
15 | 16 |
|
16 | | -//动态维度的维度变量 |
17 | | -? //任意数字 |
18 | | -?1 //动态维度变量1 |
19 | | -?2 //动态维度变量2,用来告诉出现?2的tensor对应维度需要保持一致 |
| 17 | +### 类型约束 |
| 18 | +``` |
| 19 | +f32|f64 // 支持两种/多种 类型之一 |
| 20 | +``` |
20 | 21 |
|
21 | | -// ==================== 2. ir定义格式 ==================== |
22 | | -deepxir ir_name(ro_p1:type1,ro_param2:type2,...) -> (w_p1:type3,w_p2:type4,...) |
| 22 | +### Tensor 类型模板 |
| 23 | +``` |
| 24 | +type tensor<shape, elem_type> |
| 25 | +``` |
| 26 | +- shape 格式:dim1xdim2x...xdimN,或使用 `?` 表示动态维度。 最后一个x后的是精度。 |
| 27 | +- 示例:`tensor<10x20xf32>`, `tensor<?x?xi32>` |
| 28 | + |
| 29 | +tensor 也可以没有 shape 和 dtype 的约束,例如: |
| 30 | +``` |
| 31 | +deepxir addscalar(A:tensor, b:i8|i16|i32|i64) -> (c:tensor) { ... } |
| 32 | +``` |
| 33 | +表示任意 shape、任意 dtype 的 tensor 都可作为参数。 |
| 34 | + |
| 35 | +### 动态维度变量 |
| 36 | +- `?` 任意数字 |
| 37 | +- `?1` 动态维度变量 1 |
| 38 | +- `?2` 动态维度变量 2(用于表示同名变量处维度需一致) |
| 39 | +- 示例:`tensor<?1x?2xf32>` |
| 40 | + |
| 41 | +## 2. IR 定义格式 |
| 42 | + |
| 43 | +语法示例: |
| 44 | +``` |
| 45 | +deepxir ir_name(ro_p1:type1, ro_param2:type2, ...) -> (w_p1:type3, w_p2:type4, ...) |
23 | 46 | { |
24 | | - // 函数体: IR操作序列 |
25 | | - operation_name( ro_p1, ro_p1)-> w_p1 |
26 | | - operation_name( ro_p2, ro_p2)-> w_p2 |
| 47 | + // 函数体:IR 操作序列 |
| 48 | + operation_name(ro_p1, ro_p2) -> w_p1 |
| 49 | + operation_name(ro_p2, ro_p2) -> w_p2 |
27 | 50 | } |
28 | | -deepxir是关键词,或者我们也可以使用function,func这些传统关键字 |
29 | | -用来定义新的ir名 |
30 | | - |
31 | | -deepxir的参数,遵循左读右写的规则,没有返回值 |
32 | | -deepxir的参数类型,既包括tensor,还有list<tensor>,也包括基础类型,以及list<基础类型> |
| 51 | +``` |
| 52 | +- `deepxir` 为关键字,也可使用 `function`、`func` 等。 |
| 53 | +- 参数遵循“左读右写”规则(无返回值;通过写入参数实现输出)。 |
| 54 | +- 参数类型支持:`tensor`、`list<tensor>`、基础类型,以及基础类型的 list。 |
33 | 55 |
|
34 | | -// ==================== 3.设计思考 ==================== |
| 56 | +## 3. 设计思考 |
| 57 | +DeepX IR 采用简洁的文本格式表示张量类型约束、运算定义与运算体,便于阅读与解析。 |
| 58 | +deepx不是ssa,调用时,依然遵循左读右写的参数列表原则,右写的参数列表支持多个。 |
35 | 59 |
|
36 | | -// ==================== 4. 具体示例 ==================== |
| 60 | +## 4. 具体示例 |
37 | 61 |
|
38 | | -// 示例1: 包含多个操作 |
39 | | -deepxir conv_relu(input: tensor<1x32x32x3xf32>,filter: tensor<3x3x3x16xf32>) -> (out: tensor<1x30x30x16xf32>) { |
40 | | - tensor.new([1 30 30 16],f32)->conv |
41 | | - tensor.conv2d( input, filter)->conv |
42 | | - tensor.relu(conv)-> out |
| 62 | +### 示例 1:融合 Linear + 归一化 |
| 63 | +``` |
| 64 | +deepxir fused_linear_norm( |
| 65 | + A: tensor<?1x?2xf32>, |
| 66 | + W: tensor<?2x?3xf32>, |
| 67 | + b: tensor<?3xf32>, |
| 68 | + axis: i32, |
| 69 | + keepdims: bool |
| 70 | +) -> (out: tensor<?1x?3xf32>) { |
| 71 | + newtensor(?1x?3, f32)->(mm) |
| 72 | + matmul(A, W)-> (mm) |
| 73 | + newtensor(?1x?3, f32)-> bias |
| 74 | + add(mm, b)-> bias |
| 75 | + deltensor(mm)-> mm |
| 76 | + newtensor(?1, f32)-> mean |
| 77 | + sum(bias, axis, keepdims)-> mean |
| 78 | + newtensor(?1x?3, f32)-> centered |
| 79 | + sub(bias, mean)-> centered |
| 80 | + deltensor(bias)-> bias |
| 81 | + deltensor(mean)-> mean |
| 82 | + newtensor(?1x?3, f32)-> sq |
| 83 | + mul(centered, centered)-> sq |
| 84 | + deltensor(centered)-> centered |
| 85 | + newtensor(?1, f32)-> var |
| 86 | + sum(sq, axis, keepdims)-> var |
| 87 | + deltensor(sq)-> sq |
| 88 | + constant(1e-5)-> eps |
| 89 | + newtensor(?1, f32)-> var_eps |
| 90 | + add(var, eps)-> var_eps |
| 91 | + deltensor(var)-> var |
| 92 | + deltensor(eps)-> eps |
| 93 | + newtensor(?1, f32)-> std |
| 94 | + sqrt(var_eps)-> std |
| 95 | + deltensor(var_eps)-> var_eps |
| 96 | + div(std, std)-> std |
| 97 | + deltensor(std)-> std |
| 98 | + div(centered, std)-> out |
43 | 99 | } |
| 100 | +``` |
44 | 101 |
|
45 | | -// 示例3: 支持动态形状和类型推断 |
46 | | -deepxir dynamic_operations( A: tensor<?x?xf32>,B: tensor<?x?xf32> |
47 | | -) -> (out: tensor<?x?xf32>) { |
48 | | - tensor.add( A, B)-> %add |
49 | | - tensor.matmul( %add, A)-> out |
| 102 | +下面给出一个完整的 `deepxir` 调用示例:在一个 IR 中先构造输入张量和辅助参数,然后调用 `fused_linear_norm`,输出 `out`。 |
| 103 | + |
| 104 | +``` |
| 105 | +deepxir example_use_fused_linear_norm() -> (out: tensor<2x3xf32>) { |
| 106 | + newtensor([2,4], f32)-> A |
| 107 | + newtensor([4,3], f32)-> W |
| 108 | + newtensor([3], f32)-> b |
| 109 | + fused_linear_norm(A, W, b, 1, false) -> out |
50 | 110 | } |
| 111 | +``` |
51 | 112 |
|
52 | | -// 示例4: 带有属性约束的函数 |
53 | | -deepxir batch_norm( |
54 | | - input: tensor<?x?x?x?xf32>, |
55 | | - scale: tensor<?xf32>, |
56 | | - bias: tensor<?xf32> |
57 | | -) -> (output: tensor<?x?x?x?xf32>) { |
58 | | - tensor.batch_norm( input, scale, bias)-> output |
| 113 | +该示例展示了如何在 IR 中构造必要的张量/参数并调用 `fused_linear_norm`,其中 `out` 的类型为 `tensor<2x3xf32>`,与 `W` 的列数和 `A` 的行数对应。 |
| 114 | + |
| 115 | +### 示例 2:融合 Attention score + Softmax |
| 116 | +``` |
| 117 | +deepxir fused_attention_scores( |
| 118 | + Q: tensor<?x?xf32>, |
| 119 | + K: tensor<?x?xf32>, |
| 120 | + axis: list<i32>, |
| 121 | + keepdims: bool, |
| 122 | + shape_scores: list<i32>, |
| 123 | + shape_sum: list<i32> |
| 124 | +) -> (out: tensor<?x?xf32>) { |
| 125 | + newtensor(shape_scores, f32)-> scores_tmp |
| 126 | + matmul(Q, K)-> scores_tmp |
| 127 | + newtensor(shape_scores, f32)-> exp_tmp |
| 128 | + exp(scores_tmp)-> exp_tmp |
| 129 | + deltensor(scores_tmp)-> scores_tmp |
| 130 | + newtensor(shape_sum, f32)-> sum_tmp |
| 131 | + sum(exp_tmp, axis, keepdims)-> sum_tmp |
| 132 | + div(exp_tmp, sum_tmp)-> out |
| 133 | + deltensor(exp_tmp)-> exp_tmp |
| 134 | + deltensor(sum_tmp)-> sum_tmp |
59 | 135 | } |
| 136 | +``` |
0 commit comments