Skip to content

Commit ea32c5c

Browse files
authored
[Docs] Update the documentation for the coming release (#360)
1 parent 7c12631 commit ea32c5c

38 files changed

+1169
-144
lines changed

docs/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ sphinx
33
sphinx-gallery
44
sphinx-copybutton
55
autodocsumm
6-
sphinx-book-theme
6+
sphinx-book-theme==1.0.1
77
matplotlib
88
sphinxcontrib-bibtex
99
sphinxcontrib-googleanalytics

docs/source/_static/custom.css

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ div.sphx-glr-download-python {
1717
/* font-weight: 550;*/
1818
/*}*/
1919

20-
div.container-xl {
21-
max-width: 1500px;
22-
}
20+
/*div.container-xl {*/
21+
/* max-width: 1500px;*/
22+
/*}*/
2323

24-
.sphx-glr-script-out .highlight pre {
25-
background-color: #e9ecef !important;
26-
}
24+
/*.sphx-glr-script-out .highlight pre {*/
25+
/* background-color: #e9ecef !important;*/
26+
/*}*/
2727

2828
div.sphx-glr-download a {
2929
background-color: #0084c845 !important;
@@ -35,33 +35,49 @@ div.sphx-glr-download a:hover {
3535
background-image: none !important;
3636
}
3737

38-
dt.sig {
39-
letter-spacing: 0;
40-
font-family: Menlo,Monaco,Consolas,SFMono-Regular,"Liberation Mono","Courier New",monospace;
41-
/*font-family: ;*/
42-
/*font-size=1em;*/
43-
}
38+
/*dt.sig {*/
39+
/* letter-spacing: 0;*/
40+
/* font-family: Menlo,Monaco,Consolas,SFMono-Regular,"Liberation Mono","Courier New",monospace;*/
41+
/* !*font-family: ;*!*/
42+
/* !*font-size=1em;*!*/
43+
/*}*/
4444

45-
span.sig-name {
46-
color: #0558b7;
47-
}
45+
/*span.sig-name {*/
46+
/* color: #0558b7;*/
47+
/*}*/
4848
em.sig-param {
4949
font-style: normal;
5050
}
5151
em.property {
5252
font-style: normal;
5353
}
54-
code {
55-
font-family: monospace;
54+
a {
55+
text-decoration: unset;
5656
}
57-
code {
58-
color: #434552;
57+
58+
.navbar-brand img {
59+
width: 210px;
5960
}
6061

61-
.heading-style, h1, h2, h3, h4, h5, h6 {
62-
font-family: Ubuntu, system-ui;
62+
.navbar-brand {
63+
padding: 1.8rem 0;
6364
}
65+
/*code {*/
66+
/* font-family: monospace;*/
67+
/*}*/
68+
/*code {*/
69+
/* color: #434552;*/
70+
/*}*/
71+
72+
/*.heading-style, h1, h2, h3, h4, h5, h6 {*/
73+
/* font-family: Ubuntu, system-ui;*/
74+
/*}*/
6475

6576
dl.class, dl.function {
6677
margin-bottom: 3em;
78+
}
79+
80+
.bd-sidebar-primary .sidebar-primary-items__end {
81+
margin-bottom: 0;
82+
margin-top: 0
6783
}

docs/source/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@
114114
html_theme_options = {
115115
"repository_url": "https://github.com/hidet-org/hidet",
116116
"use_repository_button": True,
117-
'logo_only': True,
118-
"extra_navbar": r"<a href=/netron target=_blank>Customized Netron</a>",
117+
# 'logo_only': True,
118+
# "extra_navbar": r"<a href=/netron target=_blank>Customized Netron</a>",
119119
"show_navbar_depth": 1,
120120
# "home_page_in_toc": True
121121
}

docs/source/developer-guides/hidet-script/index.rst

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/source/getting-started/build-from-source.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ shared library:
3333
After building, you could find two libraries ``libhidet.so`` and ``libhidet_runtime.so`` under ``build/lib`` directory.
3434

3535
Install the Hidet Python package
36-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3737

3838
Next we will install the Python package of Hidet in the develop mode via pip:
3939

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Examples
2+
========
3+
4+
This section contains a collection of examples that demonstrate how to use Hidet Script to write kernel programs. Each
5+
example is a self-contained hidet script program that can be run directly.
6+
7+
.. _hidet script examples:
8+
9+
.. toctree::
10+
:maxdepth: 1
11+
:caption: Hidet Script Examples
12+
13+
../../gallery/hidet-script/0-hello-world
14+
../../gallery/hidet-script/1-scalar-addition
15+
../../gallery/hidet-script/2-vector-addition
16+
../../gallery/hidet-script/3-kernel-functions
17+
../../gallery/hidet-script/4-naive-matmul
18+
../../gallery/hidet-script/5-efficient-matmul

docs/source/hidet-script/index.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Introduction
2+
============
3+
4+
Hidet Script is a domain specific language (DSL) for writing tensor programs directly in python.
5+
The users can write the tensor programs with python's syntax with some constrains and extensions.
6+
A transpiler is used to translate the python abstract syntax tree (AST) to Hidet's tensor program IR.
7+
Then, the translated tensor programs in Hidet IR is optimized and compiled to the target binary for execution.
8+
The tensor program writer works in the python environment in the whole process.
9+
10+
11+
To get started, please refer to the :ref:`hidet script examples`.
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
Type System
2+
===========
3+
4+
In hidet script, we have a type system that contains scalar types, tensor type, as well as pointer types.
5+
6+
Scalar types
7+
------------
8+
9+
Hidet supports the following scalar types:
10+
- integer types: ``i8``, ``i16``, ``i32``, ``i64`` (``int8``, ``int16``, ``int32``, ``int64``)
11+
- floating point types: ``f16``, ``f32``, ``f64``, ``bf16``, ``tf32`` (``float16``, ``float32``, ``float64``, ``bfloat16``, ``tfloat32``)
12+
- boolean type: ``bool``
13+
- complex types: ``c64``, ``c128`` (``complex64``, ``complex128``)
14+
15+
Some types have both short names and long names. For example, ``i8`` and ``int8`` are the same type.
16+
17+
There are also vectorized scalar types:
18+
- vectorized integer types: ``i8x4`` (``int8x4``)
19+
- vectorized float types: ``f16x2``, ``f32x4`` (``float16x2``, ``float32x4``)
20+
21+
Tensor type
22+
-----------
23+
24+
Hidet is designed to simplify the tensor program writing. Therefore, we have a powerful tensor type that
25+
represents a tensor with a specific element data type, shape, and memory layout. More specifically, a
26+
tensor type has the following attributes:
27+
- ``dtype``: the data type of the tensor elements, can be any scalar type.
28+
- ``shape``: a list of expressions that represents the shape of the tensor.
29+
- ``layout``: the memory layout of the tensor.
30+
31+
The following code snippet shows how to define a tensor type:
32+
33+
.. code-block::
34+
35+
import hidet
36+
from hidet.lang import attrs, printf
37+
from hidet.lang.types import tensor, f32
38+
39+
with hidet.script_module() as script_module:
40+
@hidet.script
41+
def kernel():
42+
attrs.func_kind = 'cpu_kernel'
43+
44+
# by default, the layout is a row-major layout
45+
a = tensor(dtype=f32, shape=[1024, 1024])
46+
47+
a[0, 0] = 0.0
48+
49+
printf("a[%d, %d] = %.1f\n", 0, 0, a[0, 0])
50+
51+
module = script_module.build()
52+
module()
53+
54+
55+
Tensor shape
56+
~~~~~~~~~~~~
57+
58+
The shape of the tensor must be determined at the compile time. Therefore, the shape of the tensor can only
59+
be defined with constant expressions. If we want to access a tensor with shape determined at runtime with
60+
variable expressions, we can use *tensor pointer* (will be discussed later).
61+
62+
63+
Tensor layout
64+
~~~~~~~~~~~~~
65+
66+
The layout of a tensor defines how to map the coordinates of a tensor element to the linear position of the
67+
element in the memory space. Generally speaking, a layout maps a :math:`n`-dimensional coordinate
68+
:math:`(c_0, c_1, \dots, c_{n-1})` to a linear index:
69+
70+
.. math::
71+
72+
index = layout(c_0, c_1, ..., c_{n-1})
73+
74+
75+
The most commonly used layout is the row-major layout. In row-major layout, the linear index is calculated as:
76+
77+
78+
.. math::
79+
80+
index = c_0 \times s_1 \times s_2 \times \dots \times s_{n-1} + c_1 \times s_2 \times \dots \times s_{n-1} + \dots + c_{n-2} \times s_{n-1} + c_{n-1}
81+
82+
where :math:`s_i` is the size of the :math:`i`-th dimension of the tensor: :math:`shape=(s_0, s_1, \dots, s_{n-1})`.
83+
84+
85+
Similar to the row-major layout, we can also define a column-major layout as follows:
86+
87+
.. math::
88+
89+
index = c_{n-1} \times s_{n-2} \times \dots \times s_1 \times s_0 + c_{n-2} \times \dots \times s_1 \times s_0 + \dots + c_1 \times s_0 + c_0
90+
91+
The row-major layout is the default layout if we do not specify the layout of a tensor. We can also specify
92+
the layout of a tensor with the ``layout`` argument of the ``tensor`` type. For example, we can define a tensor with
93+
column-major layout as follows:
94+
95+
.. code-block::
96+
97+
from hidet.lang.layout import column_major
98+
from hidet.lang.types import tensor, f32
99+
# ...
100+
a = tensor(dtype=f32, shape=[1024, 1024], layout=column_major(1024, 1024))
101+
# or ignore shape if the layout is specified
102+
b = tensor(dtype=f32, layout=column_major(1024, 1024))
103+
104+
105+
Both row-major layout and column-major layout are special cases of the strided layout.
106+
In hidet, we can define a strided layout like
107+
108+
109+
.. code-block::
110+
111+
from hidet.lang.layout import strided_layout
112+
from hidet.lang.types import tensor, f32
113+
114+
# equivalent to row-major layout
115+
a = tensor(dtype=f32, layout=strided_layout(shape=[1024, 1024], ranks=[0, 1]))
116+
# equivalent to column-major layout
117+
b = tensor(dtype=f32, layout=strided_layout(shape=[1024, 1024], ranks=[1, 0]))
118+
# the ranks define the order of the dimensions from the one that changes the slowest to the one that changes the fastest
119+
c = tensor(dtype=f32, layout=strided_layout(shape=[2, 2, 2], ranks=[0, 2, 1]))
120+
# c[coordinate] -> index
121+
# c[0, 0, 0] -> 0
122+
# c[0, 1, 0] -> 1
123+
# c[0, 0, 1] -> 2
124+
# c[0, 1, 1] -> 3
125+
# c[1, 0, 0] -> 4
126+
# c[1, 1, 0] -> 5
127+
# c[1, 0, 1] -> 6
128+
# c[1, 1, 1] -> 7
129+
130+
Given two layouts $f$ and $g$, we can define a new layout $h$ as the composition of $f$ and $g$ with $f$ as the outer
131+
layout and $g$ as the inner layout:
132+
133+
.. math::
134+
135+
h(\textbf{c}) = f(\textbf{c}/\textbf{s}_{g}) * n_g + g(\textbf{c} \mod \textbf{s}_{g})
136+
137+
where :math:`\textbf{c}` is the coordinate of the tensor element, :math:`\textbf{s}_{g}` is the shape of the inner
138+
layout :math:`g`, and :math:`n_g` is the number of elements in the inner layout :math:`g`. The division and modulo
139+
operations are performed element-wise. The composed layout $h$ has the same number of dimensions as the outer and inner
140+
layouts, and the shape of the composed layout is the elementwise product of the shapes of the outer and inner layouts.
141+
142+
In hidet script, we can use the *multiply* operator ``*`` to compose two layouts. For example, we can define a
143+
composed layout as follows:
144+
145+
.. code-block::
146+
147+
from hidet.lang.layout import row_major, column_major
148+
149+
c = row_major(2, 1) * row_major(2, 2)
150+
# c shape=[4, 2]
151+
# c[0, 0] -> 0
152+
# c[0, 1] -> 1
153+
# c[1, 0] -> 2
154+
# c[1, 1] -> 3
155+
# c[2, 0] -> 4
156+
# c[2, 1] -> 5
157+
# c[3, 0] -> 6
158+
# c[3, 1] -> 7
159+
160+
d = row_major(2, 1) * column_major(2, 2)
161+
# d shape=[4, 2]
162+
# d[0, 0] -> 0
163+
# d[1, 0] -> 1
164+
# d[0, 1] -> 2
165+
# d[1, 1] -> 3
166+
# d[2, 0] -> 4
167+
# d[3, 0] -> 5
168+
# d[2, 1] -> 6
169+
# d[3, 1] -> 7
170+
171+
We can apply the composition operation multiple times to compose multiple layouts. For example,
172+
173+
.. code-block::
174+
175+
from hidet.lang.layout import row_major, column_major
176+
177+
e = row_major(2, 1) * row_major(2, 2) * column_major(2, 2) # e shape=[8, 4]
178+
179+
The composition operation is associative, i.e., :math:`(f * g) * h = f * (g * h)`, but not commutative, i.e.,
180+
it is highly likely :math:`f * g \neq g * f`.
181+
182+
183+
Pointer types
184+
~~~~~~~~~~~~~
185+
186+
In hidet, we can define a pointer type with the same semantics as the pointer type in C/C++.
187+
188+
To construct a pointer type, we use the ``~`` operator to apply to a scalar type or pointer type:
189+
190+
- ``~i32``: a pointer to ``i32`` type
191+
- ``~(~f16)``: a pointer to a pointer to ``f16`` type
192+
193+
194+
Void type
195+
~~~~~~~~~
196+
197+
The ``void`` type can be used as the return type of a function, or used to define a ``void`` pointer type
198+
(i.e., ``~void``).

0 commit comments

Comments
 (0)