如何在TVM上集成Codegen(下)
如何在TVM上集成Codegen(下)
Bring DNNL to TVM: JSON Codegen/Runtime
现在实现将中继图序列化为JSON表示的DNNL codegen,然后实现DNNL JSON runtime来反序列化和执行该图。请注意,如果尝试实现codegen来生成C兼容的程序,可能需要直接进入下一节。
要使TVM中的DNNL JSON codegen/runtime在本例中工作,请确保DNNL在计算机上可用,并在中使用set(USE_DNNL_CODEGEN ON)构建TVM配置文件制作。
DNNL codegen在src/relay/backend/contrib/dnnl/codegen.cc
。 因为在这个文件中的两个表单中都实现了DNNL codegen,所以在跟踪代码时,可以将注意力集中在USE_JSON_RUNTIME宏所涵盖的部分。
首先用TVM注册API(L510)注册codegen。此注册使TVM编译引擎将Compiler=<your codegen>的中继函数分派到relay.ext.<your codegen>。然后实现了DNNL编译器(L490)的入口函数。有关详细信息,请阅读代码片段中嵌入的注释:
runtime::Module DNNLCompiler(const ObjectRef& ref) {
// "ref" should be the paritioned Relay function with kCompiler=dnnl.
CHECK(ref->IsInstance<FunctionNode>());
auto func = Downcast<Function>(ref);
// Get the function name as the symbol to match in runtime.
auto func_name = GetExtSymbol(func);
// Serialize the function to a JSON string (introduce later).
DNNLJSONSerializer serializer(func_name, func);
serializer.serialize();
std::string graph_json = serializer.GetJSON();
// The constant tensor names that have been bound to the module.
// All constant tensors will be serialzied along with the JSON graph
// when export_library is invoked.
auto params = serializer.GetParams();
// The function to create DNNL JSON runtime (introduce later).
const auto* pf = runtime::Registry::Get("runtime.DNNLJSONRuntimeCreate");
CHECK(pf != nullptr) << "Cannot find JSON runtime module to create";
// Create a DNNL runtime module that can run the serialized function.
auto mod = (*pf)(func_name, graph_json, params);
return mod;
}
TVM_REGISTER_GLOBAL("relay.ext.dnnl").set_body_typed(DNNLCompiler);
每个 runtime模块只负责一个中继函数,这意味着您可能在一个single .so文件中有多个DNNL runtime模块。
DNNL JSON序列化
接下来,实现dnnljson序列化器(L429)。
我们从BYOC JSON codegen (src/relay/backend/contrib/codegen_json/codegen_json.h)派生而来。DNNL JSON serializer中的特殊进程尝试序列化对可由DNNL JSON runtime解释的JSON节点的复合函数调用。假设我们有一个与模式匹配的复合函数dnnl.conv2d_relu公司,则BYOC JSON codegen将生成以下JSON节点:
{
op: "kernel",
name: "dnnl.conv2d_relu",
inputs: [[0, 0, 0], [1, 0, 0]],
attrs: {
PartitionedFromPattern: ["nn.conv2d_nn.relu_"],
shape: [1, 32, 14, 14]
}
}
问题是在runtime仍然需要Conv2D属性,比如padding和stripes,但是BYOC JSON序列化程序只附加复合函数的属性,而不附加body算子。另一方面,定制的DNNL JSON序列化程序在复合函数中附加第一个也是唯一一个Conv2D的属性,以生成以下JSON节点:
{
op: "kernel",
name: "dnnl.conv2d_relu",
inputs: [[0, 0, 0], [1, 0, 0]],
attrs: {
shape: [1, 32, 14, 14],
data_layout: ["NCHW"],
kernel_layout: ["OIHW"],
strides: [1, 1],
padding: [1, 1, 1, 1]
}
}
从DNNL JSON序列化程序可以看出,只要JSON runtime能够解释,就可以定制序列化程序以生成JSON格式的任何表单。
DNNL JSON Runtime
实现一个DNNL JSON runtime来解释和执行序列化的JSON图。把它放在src/runtime/contrib/dnnl/dnnl_json_runtime.cc
。
同样,首先注册两个api来创建 runtime,这样就可以在任何地方使用。这个runtime.DNNLJSONRuntimeCreate序列化后在上一部分中使用,并且runtime.module.loadbinary_dnnl_json可以在加载.so back时使用。
// Create a DNNL JSON runtime to interpret and execute the given JSON graph.
runtime::ModuleDNNLJSONRuntimeCreate(String
symbol_name,
String
graph_json,
const
Array<String>&
const_names)
{
auto
n
=
make_object<DNNLJSONRuntime>(symbol_name,
graph_json,
const_names);
return
runtime::Module(n);
}
TVM_REGISTER_GLOBAL("runtime.DNNLJSONRuntimeCreate")
.set_body_typed(DNNLJSONRuntimeCreate);
TVM_REGISTER_GLOBAL("runtime.module.loadbinary_dnnl_json")
.set_body_typed(JSONRuntimeBase::LoadFromBinary<DNNLJSONRuntime>);
Now we explain DNNL JSON runtime implementation. The basic class structure is:
classDNNLJSONRuntime
:
public
JSONRuntimeBase
{
const
char*
type_key()
const
{
return
"dnnl_json";
}
void
Init(const
Array<NDArray>&
consts)
override
{
// Initialize the DNNL graph engine.
BuildEngine();
// Setup constants entries for weights.
CHECK_EQ(consts.size(),
const_idx_.size())
<<
"The number of input constants must match the number of required.";
SetupConstants(consts);
}
void
Run()
override
{
// 1. Fill in the input buffers.
// 2. Invoke the engine through intepreting the stream.
// 3. Read and fill output buffers.
}
}
Init函数负责通过解释JSON图形字符串来构建DNNL引擎(BuildEngine请参阅L93),并将常量权重填充到相应的数据输入缓冲区(SetupConstant在JSON runtime基类中实现,只需在Init中调用它)。
即使我们运行多次推断,这个函数也只会被调用一次。
接下来,Run函数(L64)首先将输入张量(可能来自用户输入或恒定权重)写入构建DNNL引擎时初始化的相应DNNL内存缓冲区。然后启动DNNL引擎来执行JSON图。最后,它将DNNL输出内存缓冲区写回相应的输出张量。
由于DNNL JSON runtime中的rest实现太过DNNL特定,因此在本文中我们将停止讨论。要强调的是,虽然DNNL JSON runtime是一个很好的参考,但是JSON runtime可以完全定制以满足需求。
Bring DNNL to TVM: C Source Codegen
现在让我们实现DNNL codegen,它生成C源代码,它调用dnnlapi来执行中继图表。注释如果试图实现一个codegen来生成JSON格式的其他图形表示,那么可能需要阅读DNNL to TVM: JSON Codegen/Runtime并跳过这一节。
要使TVM中的DNNL C源代码生成在本例中工作,确保DNNL在计算机上可用,并在中使用set(USE_DNNL_CODEGEN C_SRC)构建TVM配置文件制作.
DNNL codegen在src/relay/backend/contrib/dnnl/codegen.cc。由于在这个文件中的两个表单中都实现了DNNL codegen,所以在跟踪代码时,可以将注意力集中在USE_JSON_RUNTIME runtime宏未涵盖的部分。
首先用TVM注册API(L510)注册codegen。此注册使TVM编译引擎将Compiler=<your codegen>的中继函数分派到relay.ext.<your codegen>。然后实现DNNL编译器(L490)的entry函数:
runtime::Module DNNLCompiler(const ObjectRef& ref) {
DNNLModuleCodegen dnnl;
return dnnl.CreateCSourceModule(ref);
}
TVM_REGISTER_GLOBAL("relay.ext.dnnl").set_body_typed(DNNLCompiler);
每个 runtime模块只负责一个中继函数,这意味着您可能在single .so文件中有多个DNNL runtime模块。
然后,推导了CSourceModuleCodegenBase,在L362中实现了DNNLModuleCodegen。虽然CSourceModuleCodegenBase负责序列化等其他模块级流程,只需要在CreateCSourceModule函数(L389)中实现DNNL代码生成:
runtime::Module CreateCSourceModule(const ObjectRef& ref) override {
// Include headers
// ...skip...
code_stream_ << "#include <dnnl/dnnl_kernel.h>\n";
// ...skip...
// "ref" should be the paritioned Relay function with kCompiler=dnnl.
CHECK(ref->IsInstance<FunctionNode>());
auto res = GenDNNLFunc(Downcast<Function>(ref));
// "code" is the generated C code with DNNL APIs.
std::string code = code_stream_.str();
// "res" is a tuple of constant weights (symbols, values).
// All constant tensors will be serialzied along with the generated C code
// when export_library is invoked.
String sym = std::get<0>(res);
Array<String> variables = std::get<1>(res);
// Create a CSource module with all above artifacts.
const auto* pf = runtime::Registry::Get("runtime.CSourceModuleCreate");
CHECK(pf != nullptr) << "Cannot find csource module to create the external runtime module";
return (*pf)(code, "c", sym, variables);
}
接下来,实现GenDNNLFunc(L365),用DNN API生成可编译的C代码,如下所示。有关TVM C source runtime模块兼容函数接口的说明,请参阅嵌入的注释。
// The example Relay graph: conv2d -> add -> relu.
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <tvm/runtime/c_runtime_api.h>
#include <tvm/runtime/container.h>
#include <tvm/runtime/packed_func.h>
#include <dlpack/dlpack.h>
#include <dnnl/dnnl_kernel.h>
using namespace tvm::runtime;
using namespace tvm::runtime::contrib;
// Execute the conv2d->add->relu graph with DNNL.
extern "C" void dnnl_0_(float* dnnl_0_i0, float* dnnl_0_i1,
float* dnnl_0_i2, float* out0) {
// Allocate intermediate buffers.
float* buf_0 = (float*)std::malloc(4 * 4608);
float* buf_1 = (float*)std::malloc(4 * 4608);
float* buf_2 = (float*)std::malloc(4 * 4608);
// Pre-implemented op-based DNNL functions.
dnnl_conv2d(dnnl_0_i0, dnnl_0_i1, buf_0, 1, 32, 14, 14, 32, 1, 0, 0, 3, 3, 1, 1);
dnnl_add(buf_0, dnnl_0_i2, buf_1, 1, 32, 12, 12);
dnnl_relu(buf_1, buf_2, 1, 32, 12, 12);
// Copy the final output to the corresponding buffer.
std::memcpy(out0, buf_2, 4 * 4608);
std::free(buf_0);
std::free(buf_1);
std::free(buf_2);
}
// The wrapper function with all arguments in DLTensor type.
extern "C" int dnnl_0_wrapper_(DLTensor* arg0,
DLTensor* arg1,
DLTensor* arg2,
DLTensor* out0) {
// Cast all DLTensor to primitive type buffers and invoke the above
// execution function.
dnnl_0_(static_cast<float*>(arg0->data),
static_cast<float*>(arg1->data),
static_cast<float*>(arg2->data),
static_cast<float*>(out0->data));
return 0;
}
// The TVM macro to generate TVM runtime compatible function "dnnl_0"
// from our generated "dnnl_0_wrapper_".
TVM_DLL_EXPORT_TYPED_FUNC(dnnl_0, dnnl_0_wrapper_);
预先实现的基于算子的DNNL函数位于src/runtime/contrib/dnnl/dnnl.cc。
因为rest实现在src/relay/backend/contrib/dnnl/codegen.cc太DNNL的具体细节,本文就到此为止。其主要思想是实现一个中继图访问者(L138)来访问给定的中继函数并生成上面的C代码。只要codegen能够生成与TVM运行时兼容的C代码,就可以完全定制codegen以满足您的需求。
C Source Compilation
DNNLCompiler的输出是一个模块,其中生成的C代码是文本格式的,GCC尚未将其编译为可执行的二进制文件。实际上,当用户调用export_libray(mod)
时,会编译生成的C代码,如下面的代码片段:
defupdate_lib(lib):
# Include the path of src/runtime/contrib/dnnl/dnnl.cc
test_dir
=
os.path.dirname(os.path.realpath(os.path.expanduser(__file__)))
source_dir
=
os.path.join(test_dir,
"..",
"..",
"..")
contrib_path
=
os.path.join(source_dir,
"src",
"runtime",
"contrib")
# Setup the gcc flag to compile DNNL code.
kwargs
=
{}
kwargs["options"]
=
["-O2",
"-std=c++14",
"-I"
+
contrib_path]
tmp_path
=
util.tempdir()
lib_name
=
'lib.so'
lib_path
=
tmp_path.relpath(lib_name)
# The generated C code with DNNL APIs is compiled to a binary lib.so.
lib.export_library(lib_path,
fcompile=False,
**kwargs)
# Load the lib.so back to a runtime module.
lib
=
runtime.load_module(lib_path)
return
lib
withtvm.transform.PassContext(opt_level=3):
json,
lib,
param
=
relay.build(mod,
target=target,
params=params)
lib=
update_lib(lib)
rt_mod=
tvm.contrib.graph_runtime.create(json,
lib,
ctx)
Bring DNNL to TVM: Build TVM with DNNL Codegen/Runtime
最后,创建cmake/modules/contrib/DNNL.cmake在构建TVM时包括DNNL codegen。为了演示,DNNL codegen在同一个cmake文件中有两个实现。只能根据需要专注于其中的一个。
cmake文件就绪后,现在用户可以在其构建中指定set(USE_DNNL_CODEGEN ON)的build/config.cmake
配置文件制作启用DNNL codegen。
如何在TVM上集成Codegen(下)的更多相关文章
- 如何在TVM上集成Codegen(上)
如何在TVM上集成Codegen(上) 许多常用的深度学习内核,或者提供DNNL或TensorRT等框架和图形引擎,让用户以某种方式描述他们的模型,从而获得高性能.此外,新兴的深度学习加速器也有自己的 ...
- 如何在CPU上优化GEMM(下)
如何在CPU上优化GEMM(下) Array Packing 另一个重要的技巧是数组打包.这个技巧是对数组的存储维度进行重新排序,将某个维度上的连续访问模式在平滑后转换为顺序模式. 如上图所示,在阻塞 ...
- 中小研发团队架构实践之生产环境诊断工具WinDbg 三分钟学会.NET微服务之Polly 使用.Net Core+IView+Vue集成上传图片功能 Fiddler原理~知多少? ABP框架(asp.net core 2.X+Vue)模板项目学习之路(一) C#程序中设置全局代理(Global Proxy) WCF 4.0 使用说明 如何在IIS上发布,并能正常访问
中小研发团队架构实践之生产环境诊断工具WinDbg 生产环境偶尔会出现一些异常问题,WinDbg或GDB是解决此类问题的利器.调试工具WinDbg如同医生的听诊器,是系统生病时做问题诊断的逆向分析工具 ...
- Jenkins持续集成(上)-Windows下安装Jenkins
环境:Windows 2008 R2.Jenkins2.235.1: 概要 前面写过一篇文章,<自动发布-asp.net自动发布.IIS站点自动发布(集成SLB.配置管理.Jenkins)> ...
- 如何在windows7上安装启明星系统。
启明星系统提供多种安装方式.安装包里自带了setup.exe.每个程序的 install下有在线安装(例如请假应用程序为book,则默认为 http://localhost/book/install ...
- 如何在 Linux 上安装应用程序
如何在 Linux 上安装应用程序 编译自:https://opensource.com/article/18/1/how-install-apps-linux作者: Seth Kenlon原创:LC ...
- 如何在GitHub上大显身手?
推荐一篇良许大佬的文章,如何在github上大显身手.拥有自己的github,且有所贡献,这是一件很有意义的的事情,在面试上也是加分项哦,赶紧搞起来. 转载至http://uee.me/aHAfN 这 ...
- 实例演示:如何在Kubernetes上大规模运行CI/CD
本周四晚上8:30,第二期k3s在线培训如约开播!本期课程将介绍k3s的核心架构,如高可用架构以及containerd.一起来进阶探索k3s吧! 报名及观看链接:http://z-mz.cn/PmwZ ...
- TensorRT宏碁自建云(BYOC, BuildYourOwnCloud)上集成
TensorRT宏碁自建云(BYOC, BuildYourOwnCloud)上集成 这个PR增加了对分区.编译和运行TensorRT BYOC目标的支持. Building 有两个新的cmake标志: ...
随机推荐
- hdu4515 小模拟
题意: 给你当期日期,问前D天和后D天的年月日. 思路: 直接模拟就行了水题,对了别忘了题目2013,3,5要输出这样的2013/03/05. #include<stdio ...
- 路由选择协议(RIP/OSPF)
目录 IGP RIP协议 OSPF协议 IS-IS协议 EIGRP协议 EGP BGP 我们可能会想,在偌大的网络中,我们是如何跟其他人通信的呢?我们是如何跟远在太平洋对面的美国小伙伴对话的呢? 这就 ...
- Windows Pe 第三章 PE头文件(中)
这一章的上半部分大体介绍了下PE文件头,下半部分是详细介绍里面的内容,这一章一定要多读几遍,好好记记基础概念和知识,方便之后的学习. 简单回忆一下: 3.4 PE文件头部解析 3.4.1 DOS M ...
- .NET 在信创常用软件适配清单之中?
2020年8月份写了一篇文章<.NET Core也是国产化信息系统开发的重要选项>, 这又过去了大半年了,在信创领域发生了很大的变化,今天写这篇文章主要是想从信创常用软件适配清单 看一看. ...
- MySQL如何快速插入数据
前言: 日常学习和工作中,经常会遇到导数据的需求.比如数据迁移.数据恢复.新建从库等,这些操作可能都会涉及大量数据的导入.有时候导入进度慢,电脑风扇狂转真的很让人崩溃,其实有些小技巧是可以让导入更快速 ...
- c#RSA 私钥加签公钥解签
/// RSA签名 /// </summary> /// <param name="data">待签名数据</param> /// <pa ...
- C++ primer plus读书笔记——第8章 函数探幽
第8章 函数探幽 1. 对于内联函数,编译器将使用相应的函数代码替换函数调用,程序无需跳到一个位置执行代码,再调回来.因此,内联函数的运行速度比常规函数稍快,但代价是需要占用更多内存. 2. 要使用内 ...
- Spring Cloud Gateway之全局过滤器在工作中的使用场景
一.使用注意事项 1.全局过滤器作用于所有的路由,不需要单独配置. 2.通过@Order来指定执行的顺序,数字越小,优先级越高. 二.默认全局拦截器的整体架构 三.实战场景,例如,校验token.记录 ...
- 人人都爱Kubernetes,Docker难道就不香了吗?
开篇 提起Docker,有很多人第一印象会认为它就是一个虚拟化容器,所以大家特别容易陷入到一种误区,就是觉得Docker只是在Linux操作系统之上又增加了一层,就跟OS上跑了一个VMWare一样.D ...
- xxl-job的一些感悟与规范
后台计划任务设计思路: 日志埋点处理,便于prd排查问题 2种主动job搭配规范(正向job.反查job) 1种消息接收的处理规范,重试机制,返回状态 job开关维度 数据流图 线上暗job-便捷性- ...