TVM中的调度原语

TVM是一种用于高效内核构造的领域专用语言。

本文将展示如何通过TVM提供的各种原语来调度计算。

from __future__ import absolute_import, print_function

import tvm

from tvm import te

import numpy as np

通常存在多种方法来计算相同的结果,但是不同的方法会导致不同的局部性和性能。因此TVM要求用户提供如何执行称为Schedule的计算。

调度是一组计算转换,它转换程序中的计算循环。

# declare some variables for use later

n = te.var("n")

m = te.var("m")

可以从操作列表中创建计划,默认情况下,调度按行主要顺序以串行方式计算张量。

# declare a matrix element-wise multiply

A = te.placeholder((m, n), name="A")

B = te.placeholder((m, n), name="B")

C = te.compute((m, n), lambda i, j: A[i, j] * B[i, j], name="C")

s = te.create_schedule([C.op])

# lower will transform the computation from definition to the real

# callable function. With argument `simple_mode=True`, it will

# return you a readable C like statement, we use it here to print the

# schedule result.

print(tvm.lower(s, [A, B, C], simple_mode=True))

Out:

primfn(A_1: handle, B_1: handle, C_1: handle) -> ()

attr = {"global_symbol": "main", "tir.noalias": True}

buffers = {B: Buffer(B_2: Pointer(float32), float32, [m: int32, n: int32], [stride: int32, stride_1: int32], type="auto"),

C: Buffer(C_2: Pointer(float32), float32, [m, n], [stride_2: int32, stride_3: int32], type="auto"),

A: Buffer(A_2: Pointer(float32), float32, [m, n], [stride_4: int32, stride_5: int32], type="auto")}

buffer_map = {A_1: A, B_1: B, C_1: C} {

for (i: int32, 0, m) {

for (j: int32, 0, n) {

C_2[((i*stride_2) + (j*stride_3))] = ((float32*)A_2[((i*stride_4) + (j*stride_5))]*(float32*)B_2[((i*stride) + (j*stride_1))])

}

}

}

一个计划由多个阶段组成,一个阶段代表一个操作的进度。提供各种方法来分派每个阶段。

split分裂

split拆分”可以按因子factor将指定的轴拆分为两个轴。

A = te.placeholder((m,), name="A")
B = te.compute((m,), lambda i: A[i] * 2, name="B")
 
s = te.create_schedule(B.op)
xo, xi = s[B].split(B.op.axis[0], factor=32)
print(tvm.lower(s, [A, B], simple_mode=True))

Out:

primfn(A_1: handle, B_1: handle) -> ()

attr = {"global_symbol": "main", "tir.noalias": True}

buffers = {B: Buffer(B_2: Pointer(float32), float32, [m: int32], [stride: int32], type="auto"),

A: Buffer(A_2: Pointer(float32), float32, [m], [stride_1: int32], type="auto")}

buffer_map = {A_1: A, B_1: B} {

for (i.outer: int32, 0, floordiv((m + 31), 32)) {

for (i.inner: int32, 0, 32) {

if @tir.likely((((i.outer*32) + i.inner) < m), dtype=bool) {

B_2[(((i.outer*32) + i.inner)*stride)] = ((float32*)A_2[(((i.outer*32) + i.inner)*stride_1)]*2f32)

}

}

}

}

也可以按nparts拆分轴,这将与factor相反地拆分轴。

A = te.placeholder((m,), name="A")
B = te.compute((m,), lambda i: A[i], name="B")
 
s = te.create_schedule(B.op)
bx, tx = s[B].split(B.op.axis[0], nparts=32)
print(tvm.lower(s, [A, B], simple_mode=True))

Out:

primfn(A_1: handle, B_1: handle) -> ()

attr = {"global_symbol": "main", "tir.noalias": True}

buffers = {B: Buffer(B_2: Pointer(float32), float32, [m: int32], [stride: int32], type="auto"),

A: Buffer(A_2: Pointer(float32), float32, [m], [stride_1: int32], type="auto")}

buffer_map = {A_1: A, B_1: B} {

for (i.outer: int32, 0, 32) {

for (i.inner: int32, 0, floordiv((m + 31), 32)) {

if @tir.likely(((i.inner + (i.outer*floordiv((m + 31), 32))) < m), dtype=bool) {

B_2[((i.inner + (i.outer*floordiv((m + 31), 32)))*stride)] = (float32*)A_2[((i.inner + (i.outer*floordiv((m + 31), 32)))*stride_1)]

}

}

}

}

tile

tile help you execute the computation tile by tile over two axises.

A = te.placeholder((m, n), name="A")
B = te.compute((m, n), lambda i, j: A[i, j], name="B")
 
s = te.create_schedule(B.op)
xo, yo, xi, yi = s[B].tile(B.op.axis[0], B.op.axis[1], x_factor=10, y_factor=5)
print(tvm.lower(s, [A, B], simple_mode=True))

Out:

primfn(A_1: handle, B_1: handle) -> ()

attr = {"global_symbol": "main", "tir.noalias": True}

buffers = {B: Buffer(B_2: Pointer(float32), float32, [m: int32, n: int32], [stride: int32, stride_1: int32], type="auto"),

A: Buffer(A_2: Pointer(float32), float32, [m, n], [stride_2: int32, stride_3: int32], type="auto")}

buffer_map = {A_1: A, B_1: B} {

for (i.outer: int32, 0, floordiv((m + 9), 10)) {

for (j.outer: int32, 0, floordiv((n + 4), 5)) {

for (i.inner: int32, 0, 10) {

if @tir.likely((((i.outer*10) + i.inner) < m), dtype=bool) {

for (j.inner: int32, 0, 5) {

if @tir.likely((((j.outer*5) + j.inner) < n), dtype=bool) {

B_2[((((i.outer*10) + i.inner)*stride) + (((j.outer*5) + j.inner)*stride_1))] = (float32*)A_2[((((i.outer*10) + i.inner)*stride_2) + (((j.outer*5) + j.inner)*stride_3))]

}

}

}

}

}

}

}

fuse

fuse can fuse two consecutive axises of one computation.

A = te.placeholder((m, n), name="A")
B = te.compute((m, n), lambda i, j: A[i, j], name="B")
 
s = te.create_schedule(B.op)
# tile to four axises first: (i.outer, j.outer, i.inner, j.inner)
xo, yo, xi, yi = s[B].tile(B.op.axis[0], B.op.axis[1], x_factor=10, y_factor=5)
# then fuse (i.inner, j.inner) into one axis: (i.inner.j.inner.fused)
fused = s[B].fuse(xi, yi)
print(tvm.lower(s, [A, B], simple_mode=True))

Out:

primfn(A_1: handle, B_1: handle) -> ()

attr = {"global_symbol": "main", "tir.noalias": True}

buffers = {B: Buffer(B_2: Pointer(float32), float32, [m: int32, n: int32], [stride: int32, stride_1: int32], type="auto"),

A: Buffer(A_2: Pointer(float32), float32, [m, n], [stride_2: int32, stride_3: int32], type="auto")}

buffer_map = {A_1: A, B_1: B} {

for (i.outer: int32, 0, floordiv((m + 9), 10)) {

for (j.outer: int32, 0, floordiv((n + 4), 5)) {

for (i.inner.j.inner.fused: int32, 0, 50) {

if @tir.likely((((i.outer*10) + floordiv(i.inner.j.inner.fused, 5)) < m), dtype=bool) {

if @tir.likely((((j.outer*5) + floormod(i.inner.j.inner.fused, 5)) < n), dtype=bool) {

B_2[((((i.outer*10) + floordiv(i.inner.j.inner.fused, 5))*stride) + (((j.outer*5) + floormod(i.inner.j.inner.fused, 5))*stride_1))] = (float32*)A_2[((((i.outer*10) + floordiv(i.inner.j.inner.fused, 5))*stride_2) + (((j.outer*5) + floormod(i.inner.j.inner.fused, 5))*stride_3))]

}

}

}

}

}

}

reorder

reorder can reorder the axises in the specified order.

A = te.placeholder((m, n), name="A")
B = te.compute((m, n), lambda i, j: A[i, j], name="B")
 
s = te.create_schedule(B.op)
# tile to four axises first: (i.outer, j.outer, i.inner, j.inner)
xo, yo, xi, yi = s[B].tile(B.op.axis[0], B.op.axis[1], x_factor=10, y_factor=5)
# then reorder the axises: (i.inner, j.outer, i.outer, j.inner)
s[B].reorder(xi, yo, xo, yi)
print(tvm.lower(s, [A, B], simple_mode=True))

Out:

primfn(A_1: handle, B_1: handle) -> ()

attr = {"global_symbol": "main", "tir.noalias": True}

buffers = {B: Buffer(B_2: Pointer(float32), float32, [m: int32, n: int32], [stride: int32, stride_1: int32], type="auto"),

A: Buffer(A_2: Pointer(float32), float32, [m, n], [stride_2: int32, stride_3: int32], type="auto")}

buffer_map = {A_1: A, B_1: B} {

for (i.inner: int32, 0, 10) {

for (j.outer: int32, 0, floordiv((n + 4), 5)) {

for (i.outer: int32, 0, floordiv((m + 9), 10)) {

if @tir.likely((((i.outer*10) + i.inner) < m), dtype=bool) {

for (j.inner: int32, 0, 5) {

if @tir.likely((((j.outer*5) + j.inner) < n), dtype=bool) {

B_2[((((i.outer*10) + i.inner)*stride) + (((j.outer*5) + j.inner)*stride_1))] = (float32*)A_2[((((i.outer*10) + i.inner)*stride_2) + (((j.outer*5) + j.inner)*stride_3))]

}

}

}

}

}

}

}

bind

bind can bind a specified axis with a thread axis, often used in gpu programming.

A = te.placeholder((n,), name="A")
B = te.compute(A.shape, lambda i: A[i] * 2, name="B")
 
s = te.create_schedule(B.op)
bx, tx = s[B].split(B.op.axis[0], factor=64)
s[B].bind(bx, te.thread_axis("blockIdx.x"))
s[B].bind(tx, te.thread_axis("threadIdx.x"))
print(tvm.lower(s, [A, B], simple_mode=True))

Out:

primfn(A_1: handle, B_1: handle) -> ()
  attr = {"global_symbol": "main", "tir.noalias": True}
  buffers = {B: Buffer(B_2: Pointer(float32), float32, [n: int32], [stride: int32], type="auto"),
             A: Buffer(A_2: Pointer(float32), float32, [n], [stride_1: int32], type="auto")}
  buffer_map = {A_1: A, B_1: B} {
  attr [IterVar(blockIdx.x: int32, (nullptr), "ThreadIndex", "blockIdx.x")] "thread_extent" = floordiv((n + 63), 64);
  attr [IterVar(threadIdx.x: int32, (nullptr), "ThreadIndex", "threadIdx.x")] "thread_extent" = 64;
  if @tir.likely((((blockIdx.x*64) + threadIdx.x) < n), dtype=bool) {
    B_2[(((blockIdx.x*64) + threadIdx.x)*stride)] = ((float32*)A_2[(((blockIdx.x*64) + threadIdx.x)*stride_1)]*2f32)
  }
}

compute_at

For a schedule that consists of multiple operators, TVM will compute tensors at the root separately by default.

A = te.placeholder((m,), name="A")
B = te.compute((m,), lambda i: A[i] + 1, name="B")
C = te.compute((m,), lambda i: B[i] * 2, name="C")
 
s = te.create_schedule(C.op)
print(tvm.lower(s, [A, B, C], simple_mode=True))

Out:

primfn(A_1: handle, B_1: handle, C_1: handle) -> ()

attr = {"global_symbol": "main", "tir.noalias": True}

buffers = {C: Buffer(C_2: Pointer(float32), float32, [m: int32], [stride: int32], type="auto"),

B: Buffer(B_2: Pointer(float32), float32, [m], [stride_1: int32], type="auto"),

A: Buffer(A_2: Pointer(float32), float32, [m], [stride_2: int32], type="auto")}

buffer_map = {A_1: A, B_1: B, C_1: C} {

for (i: int32, 0, m) {

B_2[(i*stride_1)] = ((float32*)A_2[(i*stride_2)] + 1f32)

}

for (i_1: int32, 0, m) {

C_2[(i_1*stride)] = ((float32*)B_2[(i_1*stride_1)]*2f32)

}

}

compute_at can move computation of B into the first axis of computation of C.

A = te.placeholder((m,), name="A")

B = te.compute((m,), lambda i: A[i] + 1, name="B")

C = te.compute((m,), lambda i: B[i] * 2, name="C")

s = te.create_schedule(C.op)

s[B].compute_at(s[C], C.op.axis[0])

print(tvm.lower(s, [A, B, C], simple_mode=True))

Out:

primfn(A_1: handle, B_1: handle, C_1: handle) -> ()

attr = {"global_symbol": "main", "tir.noalias": True}

buffers = {B: Buffer(B_2: Pointer(float32), float32, [m: int32], [stride: int32], type="auto"),

C: Buffer(C_2: Pointer(float32), float32, [m], [stride_1: int32], type="auto"),

A: Buffer(A_2: Pointer(float32), float32, [m], [stride_2: int32], type="auto")}

buffer_map = {A_1: A, B_1: B, C_1: C} {

for (i: int32, 0, m) {

B_2[(i*stride)] = ((float32*)A_2[(i*stride_2)] + 1f32)

C_2[(i*stride_1)] = ((float32*)B_2[(i*stride)]*2f32)

}

}

compute_inline

compute_inline can mark one stage as inline, then the body of computation will be expanded and inserted at the address where the tensor is required.

A = te.placeholder((m,), name="A")

B = te.compute((m,), lambda i: A[i] + 1, name="B")

C = te.compute((m,), lambda i: B[i] * 2, name="C")

s = te.create_schedule(C.op)

s[B].compute_inline()

print(tvm.lower(s, [A, B, C], simple_mode=True))

Out:

primfn(A_1: handle, B_1: handle, C_1: handle) -> ()

attr = {"global_symbol": "main", "tir.noalias": True}

buffers = {C: Buffer(C_2: Pointer(float32), float32, [m: int32], [stride: int32], type="auto"),

B: Buffer(B_2: Pointer(float32), float32, [m], [stride_1: int32], type="auto"),

A: Buffer(A_2: Pointer(float32), float32, [m], [stride_2: int32], type="auto")}

buffer_map = {A_1: A, B_1: B, C_1: C} {

for (i: int32, 0, m) {

C_2[(i*stride)] = (((float32*)A_2[(i*stride_2)] + 1f32)*2f32)

}

}

compute_root

compute_root can move computation of one stage to the root.

A = te.placeholder((m,), name="A")

B = te.compute((m,), lambda i: A[i] + 1, name="B")

C = te.compute((m,), lambda i: B[i] * 2, name="C")

s = te.create_schedule(C.op)

s[B].compute_at(s[C], C.op.axis[0])

s[B].compute_root()

print(tvm.lower(s, [A, B, C], simple_mode=True))

Out:

primfn(A_1: handle, B_1: handle, C_1: handle) -> ()

attr = {"global_symbol": "main", "tir.noalias": True}

buffers = {B: Buffer(B_2: Pointer(float32), float32, [m: int32], [stride: int32], type="auto"),

C: Buffer(C_2: Pointer(float32), float32, [m], [stride_1: int32], type="auto"),

A: Buffer(A_2: Pointer(float32), float32, [m], [stride_2: int32], type="auto")}

buffer_map = {A_1: A, B_1: B, C_1: C} {

for (i: int32, 0, m) {

B_2[(i*stride)] = ((float32*)A_2[(i*stride_2)] + 1f32)

}

for (i_1: int32, 0, m) {

C_2[(i_1*stride_1)] = ((float32*)B_2[(i_1*stride)]*2f32)

}

}

Summary

本文介绍tvm中的调度原语,它允许用户轻松灵活地调度计算。

为了获得性能良好的内核实现,一般的工作流程通常是:

通过一系列的运算来描述计算。

尝试用基元来调度计算。

编译并运行以查看性能差异。

根据运行结果调整你的日程安排。

下载Python源代码:schedule_primitives.py

下载Jupyter笔记本:schedule_primitives.ipynb

TVM中的调度原语的更多相关文章

  1. OS中处理机调度模型和调度算法

    OS中处理机调度模型和调度算法 调度层次 1.1. 高级调度(长程调度,作业调度) 功能:依据某种算法.把在外存队列上处于后备队列的那些作业调入内存.以作业为操做对象. 作业:比程序更为广泛的概念,不 ...

  2. 自主数据类型:在TVM中启用自定义数据类型探索

    自主数据类型:在TVM中启用自定义数据类型探索 介绍 在设计加速器时,一个重要的决定是如何在硬件中近似地表示实数.这个问题有一个长期的行业标准解决方案:IEEE 754浮点标准.1.然而,当试图通过构 ...

  3. Spring中Quartz调度器的使用

    一.Quartz的特点 * 按作业类的继承方式来分,主要有以下两种: 1.作业类继承org.springframework.scheduling.quartz.QuartzJobBean类的方式 2. ...

  4. RxSwift 中的调度器

    与 ReactiveCocoa 相比,Rx 的一大优势就是更丰富的并发模型.提到并发,就不得不提多线程.在 RxSwift 中,与线程对应的概念就是调度器,本文就调度器做些介绍,包括并发调度器.串行调 ...

  5. DLPack构建跨框架的深度学习编译器

    DLPack构建跨框架的深度学习编译器 Tensorflow,PyTorch和ApacheMxNet等深度学习框架提供了一个功能强大的工具包,可用于快速进行原型设计和部署深度学习模型.易用性通常是以碎 ...

  6. TVM自动调度器

    TVM自动调度器 随着模型大小,算子多样性和硬件异构性的不断增长,优化深度神经网络的执行速度非常困难.从计算的角度来看,深度神经网络只是张量计算的一层又一层.这些张量计算(例如matmul和conv2 ...

  7. 在指定时间干,必须干(kbmmw 中的事件调度)

    从去年开始,kbmmw 慢慢增加内涵,除了完善各种服务外,陆续增加和扩展了作为一个中间件必须有的功能, 例如,权限管理.日志系统.调度系统.内存调试等功能. 今天给大家介绍一下kbmmw 的调度事件, ...

  8. 利刃 MVVMLight 8:DispatchHelper在多线程和调度中的使用

    在应用程序中,线程可以被看做是应用程序的一个较小的执行单位.每个应用程序都至少拥有一个线程,我们称为主线程,这是在启动时调用应用程序的主方法时由操作系统分配启动的线程. 当调用和操作主线程的时候,该操 ...

  9. 在linux系统中I/O 调度的选择

        I/O 调度算法再各个进程竞争磁盘I/O的时候担当了裁判的角色.他要求请求的次序和时机做最优化的处理,以求得尽可能最好的整体I/O性能. 在linux下面列出4种调度算法 CFQ (Compl ...

随机推荐

  1. PE文件加节感染之Win32.Loader.bx.V病毒分析

    一.病毒名称:Win32.Loader.bx.V 二.分析工具:IDA 5.5.OllyDebug.StudPE 三.PE文件加节感染病毒简介 PE病毒感染的方式比较多,也比较复杂也比较难分析,下面就 ...

  2. 汇编环境搭建(vs2010(2012)+masm32)

    我本地使用的环境VS2012(2010)+MASM32,下面的图是在网上找的几个博客拼在一起的,用的是vs2010,但是并不影响.(所有文件我都打包好了,如果懒的话可以直接下载这个包)地址是:http ...

  3. 三、postman持久化及批量运行

    一.设置环境变量 环境变量的引用为{{变量名}},运行脚本之前切记要在右上角选择对应脚本设置的环境变量后点击保存 二.设置全局变量 三.脚本导入导出及分享 1.导入脚本 2.导出脚本 3.分享脚本 四 ...

  4. XGBoost原理解析

    摘要:对xgboost论文中的细节进行记录. 算法原理 系统设计 基于column block的并行 树学习最耗时的部分通常是对数据进行排序,为了降低排序带来的计算负荷,xgb使用基于block的结构 ...

  5. Windows进程间通讯(IPC)----管道

    管道的分类 管道其实际就是一段共享内存,只不过Windows规定需要使用I/O的形式类访问这块共享内存,管道可以分为匿名管道和命名管道. 匿名管道就是没有名字的管道,其支持单向传输数据,如果需要双向传 ...

  6. 逆向工程初步160个crackme-------1

    放假在家学习的效率真的很低,看完看雪加密解密的前两章就迫不及待的找了几个crackme练习一下,顺便熟悉ollydbg的使用. 工具:exeinfope(查壳工具),ollydbg(2.10版) 1. ...

  7. Django(27)类视图

    类视图 在写视图的时候,Django除了使用函数作为视图,也可以使用类作为视图.使用类视图可以使用类的一些特性,比如继承等. View django.views.generic.base.View是主 ...

  8. 大数据开发-Flink-1.13新特性

    介绍 大概4月,Flink1.13就发布了,参加 了Flink1.13 的Meetup,收获还是挺多,从大的方面讲就是FlingSql的改进和优化,资源调度管理方面的优化,以及流批一体Flink在运行 ...

  9. ThreadLocal内存溢出代码演示和原因分析!

    ThreadLocal 翻译成中文是线程本地变量的意思,也就是说它是线程中的私有变量,每个线程只能操作自己的私有变量,所以不会造成线程不安全的问题. ​ 线程不安全是指,多个线程在同一时刻对同一个全局 ...

  10. JAVA的基本介绍和JDK的安装

    JAVA帝国 JAVA特性和优势 简单 面向对象 可复制性 高性能 分布式 动态性 多线性 安全性 健壮性 JAVA三大版本 javaSE:标准版(桌面程序.控制台开发) javaME(嵌入式开发) ...