ninetoothed 0.6.0__tar.gz → 0.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/PKG-INFO +10 -2
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/README.md +8 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/docs/README.zh.md +8 -0
- ninetoothed-0.8.0/docs/source/_static/matmul-tiling.png +0 -0
- ninetoothed-0.8.0/docs/source/_static/vecadd-tiling.png +0 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/pyproject.toml +1 -1
- ninetoothed-0.8.0/src/ninetoothed/__init__.py +5 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/src/ninetoothed/jit.py +304 -90
- ninetoothed-0.8.0/src/ninetoothed/naming.py +50 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/src/ninetoothed/symbol.py +39 -44
- ninetoothed-0.8.0/src/ninetoothed/tensor.py +334 -0
- ninetoothed-0.8.0/tests/test_naming.py +51 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/tests/test_softmax.py +1 -2
- ninetoothed-0.6.0/src/ninetoothed/__init__.py +0 -5
- ninetoothed-0.6.0/src/ninetoothed/tensor.py +0 -250
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/.github/workflows/pytest.yml +0 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/.github/workflows/ruff.yml +0 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/.gitignore +0 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/LICENSE +0 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/docs/source/_static/ninetoothed-logo.png +0 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/requirements.txt +0 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/src/ninetoothed/language.py +0 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/src/ninetoothed/torchifier.py +0 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/tests/__init__.py +0 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/tests/skippers.py +0 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/tests/test_add.py +0 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/tests/test_addmm.py +0 -0
- {ninetoothed-0.6.0 → ninetoothed-0.8.0}/tests/test_matmul.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
|
-
Metadata-Version: 2.
|
1
|
+
Metadata-Version: 2.4
|
2
2
|
Name: ninetoothed
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.8.0
|
4
4
|
Summary: A domain-specific language based on Triton but providing higher-level abstraction.
|
5
5
|
Project-URL: Homepage, https://github.com/InfiniTensor/ninetoothed
|
6
6
|
Project-URL: Issues, https://github.com/InfiniTensor/ninetoothed/issues
|
@@ -51,6 +51,8 @@ def add_kernel(
|
|
51
51
|
|
52
52
|
In this code, we first define `BLOCK_SIZE`, which is a `Symbol`. You can think of `"BLOCK_SIZE"` as its name. We see that `meta` is set to `True`, indicating to the compiler that it is a meta-parameter and its value can be determined by the compiler. The `Tensor(1)` constructs a one-dimensional tensor (vector), and `Tensor(1).tile((BLOCK_SIZE,))` means we want to create a vector and divide it into blocks of size `BLOCK_SIZE`. Suppose the size of this vector is `8192` and `BLOCK_SIZE` is `1024`, then the vector will be divided into `8` blocks, each of size `1024`.
|
53
53
|
|
54
|
+

|
55
|
+
|
54
56
|
By using type annotations, we tell the compiler that we will have three tensor parameters, which will be divided into blocks, and `x`, `y`, and `z` are these blocks. It's important to understand that `x`, `y`, and `z` are the blocks, not the tensors themselves. In the function body, `x`, `y`, and `z` are also the blocks. The rest is straightforward (only one line `z = x + y` left, haha), we add each block of `x` and `y` and store it in `z`. Since each block of the parameter tensors undergoes this operation, the addition is completed for the whole tensors as well.
|
55
57
|
|
56
58
|
### Matrix Multiplication
|
@@ -82,4 +84,10 @@ def matmul_kernel(a: a_tiled, b: b_tiled, c: c_tiled):
|
|
82
84
|
|
83
85
|
For matrix multiplication, we also have three tensor parameters, but the tiling method is more complex than vector addition. We denote the three matrices as $A$, $B$, and $C$, where $A$ and $B$ are inputs, and $C$ is the output. Tiling $C$ is simple; we just need to divide it into blocks of size `(BLOCK_SIZE_M, BLOCK_SIZE_N)` by rows and columns. Once each block computes its result, the entire $C$ is computed. However, how should we tile $A$ and $B$? The answer is to introduce another meta-parameter `BLOCK_SIZE_K`. This way, we can divide $A$ into blocks of size `(BLOCK_SIZE_M, BLOCK_SIZE_K)` and $B$ into blocks of size `(BLOCK_SIZE_K, BLOCK_SIZE_N)`. However, for matrix multiplication, $A$ and $B$ do not correspond block by block; each row of $A$ needs to correspond to each column of $B$. Therefore, we need to further `tile` $A$ and $B$ by rows and columns, respectively. Up to this point, we have a set of row blocks of $A$ and column blocks of $B$. However, each row block of $A$ must correspond to every column block of $B$. This is where `expand` comes in. We `expand` the row blocks of $A$ along the columns to the number of columns of $C$ and the column blocks of $B$ along the rows to the number of rows of $C$. This way, we successfully tile $A$, $B$, and $C$. In fact, our meta-operations up to this point have already enabled us to write kernel functions. However, we notice that the levels where the row blocks and column blocks reside, which we mentioned earlier, are two-dimensional, and their sizes are of the forms `(1, ...)` and `(..., 1)`. This means that if no other operations are performed, the way we access row blocks and column blocks would have to be `a[0, k]` and `b[k, 0]`. If we want to use `a` to find the range of `k`, we would need to use `a.shape[1]`, but we know that dimensions of size `1` can actually be removed completely. This is why we added two lines of `squeeze`. The `dtype` refers to the data type, which in PyTorch can generally be some integer or floating-point type, such as `torch.float32`. However, since meta-operations like `tile` can be performed in NineToothed, `dtype` can also be a `Tensor`. In other words, there is a concept of "tensors that store tensors" in NineToothed. In summary, these two lines perform operations on the tensors stored in the outmost tensor, removing the dimensions of size `1`. This way, when we access the row and column blocks, we can use `a[k]` and `b[k]`, and when finding the range of `k`, we can use `a.shape[0]`.
|
84
86
|
|
87
|
+

|
88
|
+
|
85
89
|
With tiling done, the rest is simple. In the function body, we define an `accumulator` to accumulate intermediate results. We then iterate through the corresponding row blocks of $A$ and column blocks of $B$, multiplying them and accumulating the results in `accumulator`. Finally, we place the `accumulator` in the corresponding block of $C$. Since each block of the parameter tensors undergoes this operation, the multiplication is completed for the whole tensors as well.
|
90
|
+
|
91
|
+
## License
|
92
|
+
|
93
|
+
This project is distributed under the Apache-2.0 license. See the included [LICENSE](LICENSE) file for details.
|
@@ -36,6 +36,8 @@ def add_kernel(
|
|
36
36
|
|
37
37
|
In this code, we first define `BLOCK_SIZE`, which is a `Symbol`. You can think of `"BLOCK_SIZE"` as its name. We see that `meta` is set to `True`, indicating to the compiler that it is a meta-parameter and its value can be determined by the compiler. The `Tensor(1)` constructs a one-dimensional tensor (vector), and `Tensor(1).tile((BLOCK_SIZE,))` means we want to create a vector and divide it into blocks of size `BLOCK_SIZE`. Suppose the size of this vector is `8192` and `BLOCK_SIZE` is `1024`, then the vector will be divided into `8` blocks, each of size `1024`.
|
38
38
|
|
39
|
+

|
40
|
+
|
39
41
|
By using type annotations, we tell the compiler that we will have three tensor parameters, which will be divided into blocks, and `x`, `y`, and `z` are these blocks. It's important to understand that `x`, `y`, and `z` are the blocks, not the tensors themselves. In the function body, `x`, `y`, and `z` are also the blocks. The rest is straightforward (only one line `z = x + y` left, haha), we add each block of `x` and `y` and store it in `z`. Since each block of the parameter tensors undergoes this operation, the addition is completed for the whole tensors as well.
|
40
42
|
|
41
43
|
### Matrix Multiplication
|
@@ -67,4 +69,10 @@ def matmul_kernel(a: a_tiled, b: b_tiled, c: c_tiled):
|
|
67
69
|
|
68
70
|
For matrix multiplication, we also have three tensor parameters, but the tiling method is more complex than vector addition. We denote the three matrices as $A$, $B$, and $C$, where $A$ and $B$ are inputs, and $C$ is the output. Tiling $C$ is simple; we just need to divide it into blocks of size `(BLOCK_SIZE_M, BLOCK_SIZE_N)` by rows and columns. Once each block computes its result, the entire $C$ is computed. However, how should we tile $A$ and $B$? The answer is to introduce another meta-parameter `BLOCK_SIZE_K`. This way, we can divide $A$ into blocks of size `(BLOCK_SIZE_M, BLOCK_SIZE_K)` and $B$ into blocks of size `(BLOCK_SIZE_K, BLOCK_SIZE_N)`. However, for matrix multiplication, $A$ and $B$ do not correspond block by block; each row of $A$ needs to correspond to each column of $B$. Therefore, we need to further `tile` $A$ and $B$ by rows and columns, respectively. Up to this point, we have a set of row blocks of $A$ and column blocks of $B$. However, each row block of $A$ must correspond to every column block of $B$. This is where `expand` comes in. We `expand` the row blocks of $A$ along the columns to the number of columns of $C$ and the column blocks of $B$ along the rows to the number of rows of $C$. This way, we successfully tile $A$, $B$, and $C$. In fact, our meta-operations up to this point have already enabled us to write kernel functions. However, we notice that the levels where the row blocks and column blocks reside, which we mentioned earlier, are two-dimensional, and their sizes are of the forms `(1, ...)` and `(..., 1)`. This means that if no other operations are performed, the way we access row blocks and column blocks would have to be `a[0, k]` and `b[k, 0]`. If we want to use `a` to find the range of `k`, we would need to use `a.shape[1]`, but we know that dimensions of size `1` can actually be removed completely. This is why we added two lines of `squeeze`. The `dtype` refers to the data type, which in PyTorch can generally be some integer or floating-point type, such as `torch.float32`. However, since meta-operations like `tile` can be performed in NineToothed, `dtype` can also be a `Tensor`. In other words, there is a concept of "tensors that store tensors" in NineToothed. In summary, these two lines perform operations on the tensors stored in the outmost tensor, removing the dimensions of size `1`. This way, when we access the row and column blocks, we can use `a[k]` and `b[k]`, and when finding the range of `k`, we can use `a.shape[0]`.
|
69
71
|
|
72
|
+

|
73
|
+
|
70
74
|
With tiling done, the rest is simple. In the function body, we define an `accumulator` to accumulate intermediate results. We then iterate through the corresponding row blocks of $A$ and column blocks of $B$, multiplying them and accumulating the results in `accumulator`. Finally, we place the `accumulator` in the corresponding block of $C$. Since each block of the parameter tensors undergoes this operation, the multiplication is completed for the whole tensors as well.
|
75
|
+
|
76
|
+
## License
|
77
|
+
|
78
|
+
This project is distributed under the Apache-2.0 license. See the included [LICENSE](LICENSE) file for details.
|
@@ -36,6 +36,8 @@ def add_kernel(
|
|
36
36
|
|
37
37
|
在这段代码当中,我们首先定义了 `BLOCK_SIZE`,它是一个 `Symbol`,我们可以把 `"BLOCK_SIZE"` 理解成它的名字。我们可以看到 `meta` 被设成了 `True`,这是在告诉编译器,它是一个元参数,可以由编译器决定它的取值。之后出现的 `Tensor(1)` 则是在构造一个一维的张量(向量),`Tensor(1).tile((BLOCK_SIZE,))` 的意思就是说,我们想要构造一个向量,并且把它分成大小为 `BLOCK_SIZE` 的块。假设这个向量的大小为 `8192`,而 `BLOCK_SIZE` 是 `1024`,那么这个向量就会被分成 `8` 块,每一块的大小都是 `1024`。
|
38
38
|
|
39
|
+

|
40
|
+
|
39
41
|
我们通过类型标注的方式,告诉了编译器,我们将会有三个参数张量,并且每个参数张量,都会被按照这样的方式分块,而 `x`、`y`、`z` 就是被分成的块。这一点很重要,我们要意识到,`x`、`y`、`z` 是被分成的块,而不是被分块的张量本身,并且函数体当中的 `x`、`y`、`z` 也都是被分成的块。剩下的就很好理解了(也就剩下 `z = x + y` 一行了,哈哈哈),我们把每一块 `x` 和 `y` 相加,放到了 `z` 中,由于参数张量被分成的每一块都被执行了这样的操作,因此即便对于整体而言,加法也被完成了。
|
40
42
|
|
41
43
|
### 矩阵乘法
|
@@ -67,4 +69,10 @@ def matmul_kernel(a: a_tiled, b: b_tiled, c: c_tiled):
|
|
67
69
|
|
68
70
|
对于矩阵乘法来说,我们也有三个参数张量,但是分块的方式肯定比向量加法要复杂一些。我们将三个矩阵分别记作 $A$、$B$、$C$,其中 $A$ 和 $B$ 为输入,$C$ 为输出。其中 $C$ 的分块操作很简单,我们只需要按照行和列,将其分成大小为 `(BLOCK_SIZE_M, BLOCK_SIZE_N)` 的块即可,这样只要每个这样的块都算出了结果,整个 $C$ 也就都算出了结果。那么该如何分 $A$ 和 $B$ 呢?答案是再引入一个元参数 `BLOCK_SIZE_K`,这样我们就可以把 $A$ 分成 `(BLOCK_SIZE_M, BLOCK_SIZE_K)` 大小的块,把 $B$ 分成 `(BLOCK_SIZE_K, BLOCK_SIZE_N)` 的块。但是对于矩阵乘法,$A$ 和 $B$ 并不是块块对应,而是需要对应 $A$ 的每一行和 $B$ 的每一列,所以我们还需要继续 `tile`,把 $A$ 和 $B$ 进一步分成以行为单位和以列为单位的块。到目前为止,我们有了一堆 $A$ 的行块和 $B$ 的列块,但是对于每一个 $A$ 的行块,我们都要对应 $B$ 的每一个列块。这个时候,我们就需要进行 `expand` 了,我们把 $A$ 的行块沿着列 `expand` 成 $C$ 的列数那么多列,把 $B$ 的列块沿着行 `expand` 成 $C$ 的行数那么多行。这样,我们就成功地将 $A$、$B$、$C$ 三者都分好了块,并且对于每一个 $C$ 的块,我们都有对应好的 $A$ 的行块和 $B$ 的列块。其实我们的元操作到此为止,已经能够编写出核函数了,但是我们发现,刚才所提到的行块和列块所在的层级,是二维的,而且大小是 `(1, ...)` 和 `(..., 1)` 这样的形式。也就是说,如果不进行其它操作,那么我们访问行块和列块的方式就得是 `a[0, k]` 和 `b[k, 0]`,如果我们想要依靠 `a` 找到 `k` 的范围,那就得是 `a.shape[1]`。但是我们知道,大小为 `1` 的维度,其实完全可以被去掉,这就是为什么我们加了两行 `squeeze`,其中的 `dtype` 是数据类型的意思,在 PyTorch 中一般可以是某些整数类型或者浮点类型之类的,比如 `torch.float32`,但是由于九齿当中可以进行 `tile` 等元操作,所以 `dtype` 也可以是 `Tensor`。也就是说,在九齿当中,存在着“存储张量的张量”这样的概念。总而言之,这两行就是对最外层张量所存储的下一层的张量进行操作,把大小为 `1` 的维度去掉了,这样,我们在访问行块和列块时就可以使用 `a[k]` 和 `b[k]`,找 `k` 的范围时也可以使用 `a.shape[0]` 了。
|
69
71
|
|
72
|
+

|
73
|
+
|
70
74
|
对应好了分块,后续的部分就简单多了。在函数体当中,我们定义了一个 `accumulator`,用于累加中间结果,之后就遍历了对应好的 $A$ 的行块和 $B$ 的列块,并且把他们相乘的结果累加到了 `accumulator` 当中,最后再将 `accumulator` 放到了对应的 $C$ 的分块当中。由于参数张量被分成的每一块都被执行了这样的操作,因此即便对于整体而言,乘法也被完成了。
|
75
|
+
|
76
|
+
## 许可证
|
77
|
+
|
78
|
+
本项目采用 Apache-2.0 许可证发布。详情请参见随附的 [LICENSE](LICENSE) 文件。
|
Binary file
|
Binary file
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
4
4
|
|
5
5
|
[project]
|
6
6
|
name = "ninetoothed"
|
7
|
-
version = "0.
|
7
|
+
version = "0.8.0"
|
8
8
|
authors = [{ name = "Jiacheng Huang", email = "huangjiacheng0709@outlook.com" }]
|
9
9
|
description = "A domain-specific language based on Triton but providing higher-level abstraction."
|
10
10
|
readme = "README.md"
|