Skip to content

Commit

Permalink
重组文档结构
Browse files Browse the repository at this point in the history
  • Loading branch information
xinetzone committed Aug 26, 2023
1 parent f81a9aa commit 12e903f
Show file tree
Hide file tree
Showing 246 changed files with 4,849 additions and 1,769 deletions.
1 change: 0 additions & 1 deletion doc/chaos/tasks/index.md

This file was deleted.

1,087 changes: 1,087 additions & 0 deletions doc/deploy/build.ipynb

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,34 @@
"#include \"tvm/runtime/c_runtime_api.h\"\n",
"#include \"tvm/runtime/c_backend_api.h\"\n",
"#include <math.h>\n",
"#include <stdbool.h>\n",
"#ifdef __cplusplus\n",
"extern \"C\"\n",
"#endif\n",
"TVM_DLL int32_t tvmgen_default_fused_add(void* args, int32_t* arg_type_ids, int32_t num_args, void* out_ret_value, int32_t* out_ret_tcode, void* resource_handle) {\n",
" void* arg_p0 = (((TVMValue*)args)[0].v_handle);\n",
" int32_t arg_p0_code = arg_type_ids[0];\n",
" void* arg_p1 = (((TVMValue*)args)[1].v_handle);\n",
" int32_t arg_p1_code = arg_type_ids[1];\n",
" void* arg_T_add = (((TVMValue*)args)[2].v_handle);\n",
" int32_t arg_T_add_code = arg_type_ids[2];\n",
" void* p0 = (((DLTensor*)arg_p0)[0].data);\n",
" void* arg_p0_shape = (((DLTensor*)arg_p0)[0].shape);\n",
" void* arg_p0_strides = (((DLTensor*)arg_p0)[0].strides);\n",
" int32_t dev_id = (((DLTensor*)arg_p0)[0].device.device_id);\n",
" void* p1 = (((DLTensor*)arg_p1)[0].data);\n",
" void* arg_p1_shape = (((DLTensor*)arg_p1)[0].shape);\n",
" void* arg_p1_strides = (((DLTensor*)arg_p1)[0].strides);\n",
" void* T_add = (((DLTensor*)arg_T_add)[0].data);\n",
" void* arg_T_add_shape = (((DLTensor*)arg_T_add)[0].shape);\n",
" void* arg_T_add_strides = (((DLTensor*)arg_T_add)[0].strides);\n",
" if (!(arg_p0_strides == NULL)) {\n",
" int32_t p0_code = arg_type_ids[0];\n",
" int32_t p1_code = arg_type_ids[1];\n",
" int32_t T_add_code = arg_type_ids[2];\n",
" void* p0 = (((TVMValue*)args)[0].v_handle);\n",
" void* p1 = (((TVMValue*)args)[1].v_handle);\n",
" void* T_add = (((TVMValue*)args)[2].v_handle);\n",
" void* p0_1 = (((DLTensor*)p0)[0].data);\n",
" void* tvmgen_default_fused_add_p0_shape = (((DLTensor*)p0)[0].shape);\n",
" void* tvmgen_default_fused_add_p0_strides = (((DLTensor*)p0)[0].strides);\n",
" int32_t dev_id = (((DLTensor*)p0)[0].device.device_id);\n",
" void* p1_1 = (((DLTensor*)p1)[0].data);\n",
" void* tvmgen_default_fused_add_p1_shape = (((DLTensor*)p1)[0].shape);\n",
" void* tvmgen_default_fused_add_p1_strides = (((DLTensor*)p1)[0].strides);\n",
" void* T_add_1 = (((DLTensor*)T_add)[0].data);\n",
" void* tvmgen_default_fused_add_T_add_shape = (((DLTensor*)T_add)[0].shape);\n",
" void* tvmgen_default_fused_add_T_add_strides = (((DLTensor*)T_add)[0].strides);\n",
" if (!(tvmgen_default_fused_add_p0_strides == NULL)) {\n",
" }\n",
" if (!(arg_p1_strides == NULL)) {\n",
" if (!(tvmgen_default_fused_add_p1_strides == NULL)) {\n",
" }\n",
" if (!(arg_T_add_strides == NULL)) {\n",
" if (!(tvmgen_default_fused_add_T_add_strides == NULL)) {\n",
" }\n",
" *(float2*)(((float*)T_add) + 0) = (*(float2*)(((float*)p0) + 0) + *(float2*)(((float*)p1) + 0));\n",
" *(float2*)(((float*)T_add_1) + 0) = (*(float2*)(((float*)p0_1) + 0) + *(float2*)(((float*)p1_1) + 0));\n",
" return 0;\n",
"}\n",
"\n",
Expand All @@ -97,6 +98,13 @@
"compiled_module = relay.build(mod, \"c\", params=params)\n",
"print(compiled_module.lib.get_source())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -115,7 +123,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
"version": "3.10.12"
},
"orig_nbformat": 4,
"vscode": {
Expand Down
File renamed without changes
File renamed without changes.
31 changes: 16 additions & 15 deletions doc/tutorials/deploy/python.ipynb → doc/deploy/python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/media/pc/data/lxw/ai/tvm\n"
]
}
],
"source": [
"import set_env"
]
Expand Down Expand Up @@ -44,11 +52,11 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"mod_dylib = tvm.runtime.load_module(\"lib/test_addone_dll.so\")"
"mod_dylib = tvm.runtime.load_module(\"libs/test_addone_dll.so\")"
]
},
{
Expand All @@ -60,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -76,7 +84,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -94,7 +102,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -110,21 +118,14 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"np_x = x.numpy()\n",
"np_y = y.numpy()\n",
"assert np.all([xi + 1 == yi for xi, yi in zip(np_x, np_y)])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -143,7 +144,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.10.12"
},
"orig_nbformat": 4,
"vscode": {
Expand Down
7 changes: 7 additions & 0 deletions doc/deploy/set_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from pathlib import Path
from tvm_book.config.env import set_tvm
TVM_ROOT = Path(__file__).absolute().parents[4]
print(TVM_ROOT)
# TVM_ROOT = "/media/pc/data/lxw/ai/tvm/"
# print(TVM_ROOT)
set_tvm(TVM_ROOT)
1 change: 1 addition & 0 deletions doc/dev/attrs/a.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

5 changes: 5 additions & 0 deletions doc/dev/attrs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 属性

```{toctree}
ir-attrs
```
120 changes: 120 additions & 0 deletions doc/dev/attrs/ir-attrs.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 对象属性辅助函数\n",
"\n",
"```{topic} 核心\n",
"使用 TVM 中的 `AttrsNode` 类和相关宏来声明和使用具有默认值和边界检查的命名属性。\n",
"```\n",
"\n",
"示例:\n",
"\n",
"```c++\n",
"struct MyAttrs : public tvm::AttrsNode<MyAttrs> {\n",
" float learning_rate;\n",
" int num_hidden;\n",
" String name;\n",
" // 声明属性字段和头文件\n",
" TVM_DECLARE_ATTRS(MyAttrs, \"attrs.MyAttrs\") {\n",
" TVM_ATTR_FIELD(num_hidden).set_lower_bound(1);\n",
" TVM_ATTR_FIELD(learning_rate).set_default(0.01f);\n",
" TVM_ATTR_FIELD(name).set_default(\"hello\");\n",
" }\n",
"};\n",
"// 在 cc 文件中注册\n",
"TVM_REGISTER_NODE_TYPE(MyAttrs);\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"首先,定义了名为 `MyAttrs` 的结构体,它继承自 `tvm::AttrsNode<MyAttrs>`。这个结构体包含了三个属性:`learning_rate`、`num_hidden` 和 `name`。在头文件中,可以使用 `TVM_DECLARE_ATTRS` 宏来声明这些属性。在这个例子中,声明了三个属性字段:`num_hidden`、`learning_rate` 和 `name`。每个字段都使用了 `TVM_ATTR_FIELD` 宏来指定属性的名称和一些选项。\n",
"\n",
"- 对于 `num_hidden` 字段,使用 `set_lower_bound(1)` 来设置它的下界为 `1`,表示该属性的有效取值范围是大于等于 $1$ 的整数。\n",
"- 对于 `learning_rate` 字段,使用 `set_default(0.01f)` 来设置它的默认值为 `0.01`。这意味着如果在代码中没有显式地给 `learning_rate` 赋值,那么它将被初始化为 `0.01`。\n",
"- 对于 `name` 字段,使用 `set_default(\"hello\")` 来设置它的默认值为 `\"hello\"`。同样地,如果在代码中没有显式地给 `name` 赋值,那么它将被初始化为 `\"hello\"`。\n",
"\n",
"在源文件(通常是 C++)中,需要注册这个新的节点类型。使用 `TVM_REGISTER_NODE_TYPE(MyAttrs)` 宏可以将 `MyAttrs` 节点类型注册到 TVM 中,使其可以在后续的编译和执行过程中被识别和使用。\n",
"\n",
"总结起来,这段代码演示了如何使用TVM的 `AttrsNode` 类和相关宏来声明和使用具有默认值和边界检查的命名属性。通过这种方式,可以更方便地管理和使用模型的属性信息。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `TVM_DECLARE_ATTRS` 和 `TVM_ATTR_FIELD`\n",
"\n",
"```c++\n",
"#define TVM_DECLARE_ATTRS(ClassName, TypeKey) \\\n",
" static constexpr const char* _type_key = TypeKey; \\\n",
" TVM_DECLARE_FINAL_OBJECT_INFO(ClassName, ::tvm::BaseAttrsNode) \\\n",
" template <typename FVisit> \\\n",
" void _tvm_VisitAttrs(FVisit& _tvm_fvisit) // NOLINT(*)\n",
"```\n",
"\n",
"```c++\n",
"#define TVM_ATTR_FIELD(FieldName) _tvm_fvisit(#FieldName, &FieldName)\n",
"```\n",
"\n",
"这段代码定义了两个宏,用于在 TVM 中声明属性函数和属性字段。\n",
"\n",
"第一个宏 `TVM_DECLARE_ATTRS` 用于声明属性函数。它接受两个参数:`ClassName` 表示类的名称,`TypeKey` 表示类型键,用于在 TVM 节点系统中标识该属性的类型。\n",
"\n",
"- 在宏的定义中,首先使用 `static constexpr` 关键字将 `TypeKey` 赋值给了名为 `_type_key` 的静态常量字符指针。这样做的目的是确保 `_type_key` 的值在编译时就已经确定,并且只初始化一次。\n",
"- 接下来,通过调用 `TVM_DECLARE_FINAL_OBJECT_INFO` 宏来声明最终对象信息,其中包含了类名 `ClassName` 和基类 `::tvm::BaseAttrsNode`。这个宏的作用是将类名和基类传递给 TVM 的元编程系统,以便正确地生成派生类的元数据。\n",
"- 最后,通过模板函数 `_tvm_VisitAttrs` 来实现属性访问的机制。这个函数接受类型为 `FVisit` 的函数对象作为参数,用于遍历和访问属性。函数体中的注释 `// NOLINT(*)` 表示编译器在编译时应忽略该函数未被使用的错误警告。\n",
"\n",
"第二个宏 `TVM_ATTR_FIELD` 用于声明属性字段。它也接受两个参数:`FieldName` 表示属性的字段名,`&FieldName` 表示对应的变量。\n",
"\n",
"- 在宏的定义中,使用之前定义的模板函数 `_tvm_fvisit` 来处理属性字段的访问。`_tvm_fvisit(#FieldName, &FieldName)` 将属性字段的名称和对应的变量传递给 `_tvm_fvisit` 函数。这样,在后续的编译和执行过程中,就可以正确地访问和操作该属性字段了。\n",
"\n",
"```{note}\n",
"在 `#FieldName` 中,`#` 符号是 C++ 中的预处理器指令的开始。它用于指示编译器将紧随其后的文本视为预处理指令。\n",
"\n",
"在这种情况下,`#FieldName` 被视为宏定义的开始。宏定义是一种在编译之前进行文本替换的技术。通过使用 `#FieldName`,我们可以定义名为 `FieldName` 的宏。\n",
"\n",
"在给定的代码片段中,`#FieldName` 被用作参数传递给宏 `TVM_ATTR_FIELD`。这意味着当该宏被调用时,`FieldName` 将被替换为实际的值。\n",
"\n",
"总结起来,`#FieldName` 是 C++ 中预处理指令的语法,用于定义宏。在这个特定的代码片段中,它被用作参数传递给宏 `TVM_ATTR_FIELD`,并在宏展开时被替换为相应的值。\n",
"```\n",
"\n",
"总结起来,这段代码定义了两个宏,用于在 TVM 中声明属性函数和属性字段。通过使用这些宏,可以在 C++ 代码中方便地声明和使用带有类型键的属性。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `BaseAttrsNode`\n",
"\n",
"`BaseAttrsNode` 类是所有属性类的基类。下面是对代码中各部分的解释:\n",
"- `virtual ~BaseAttrsNode() {}` 是虚析构函数,用于在删除派生类对象时进行适当的清理。\n",
"- `virtual void VisitAttrs(AttrVisitor* v) {}` 是纯虚函数,用于在派生类中实现访问属性的功能。\n",
"- `template <typename... Args> inline void InitBySeq(Args&&... args);` 是模板函数,用于通过一系列参数初始化属性。它使用了可变参数模板,可以接受任意数量的参数。\n",
"- `inline void PrintDocString(std::ostream& os) const;` 是内联函数,用于将文档字符串打印到给定的输出流中,并在末尾添加换行符。\n",
"- `virtual void VisitNonDefaultAttrs(AttrVisitor* v) = 0;` 是纯虚函数,用于在派生类中实现访问非默认属性的功能。\n",
"- `virtual Array<AttrFieldInfo> ListFieldInfo() const = 0;` 是纯虚函数,用于获取属性列表的信息。它返回包含属性字段信息的数组。\n",
"- `virtual void InitByPackedArgs(const TVMArgs& kwargs, bool allow_unknown = false) = 0;` 用于通过参数初始化属性的纯虚函数。该函数的作用是根据传入的键值对参数来初始化属性,并根据 `allow_unknown` 参数决定是否允许存在未知字段。如果所需字段不存在,则会抛出异常。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"language_info": {
"name": "python"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 12e903f

Please sign in to comment.