forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TIR] Add TileWithTensorIntrin (apache#11075)
Co-authored-by: Siyuan Feng <Hzfengsy@sjtu.edu.cn> Co-authored-by: Bohan Hou <32121147+spectrometerHBH@users.noreply.github.com> Co-authored-by: Hongyi Jin <3231950289@qq.com> Co-authored-by: Ruihang Lai <lairuihangdongdong@qq.com> Co-authored-by: Wuwei Lin <wuwei@apache.org> Co-authored-by: Siyuan Feng <Hzfengsy@sjtu.edu.cn> Co-authored-by: Bohan Hou <32121147+spectrometerHBH@users.noreply.github.com> Co-authored-by: Hongyi Jin <3231950289@qq.com> Co-authored-by: Ruihang Lai <lairuihangdongdong@qq.com> Co-authored-by: Wuwei Lin <wuwei@apache.org>
- Loading branch information
1 parent
821f00e
commit 6749e09
Showing
5 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ | |
from .trace import Trace | ||
|
||
from . import analysis | ||
from . import transform |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""Transformation on TIR schedule.""" | ||
from typing import Optional | ||
|
||
from tvm.tir.schedule import Schedule, BlockRV, LoopRV | ||
from . import _ffi_api | ||
|
||
|
||
def tile_with_tensor_intrin(sch: Schedule, block: BlockRV, intrin_name: str) -> Optional[LoopRV]: | ||
"""Tile a subset of loops in the block according to the given tensor intrinsic. | ||
Parameters | ||
---------- | ||
sch : Schedule | ||
The schedule to which tiling is applied | ||
block : BlockRV | ||
The block whose subset of loops will be tiled | ||
intrin_name : str | ||
The name of a tensor intrinsic, must be registerd via TensorIntrin.register(...) beforehand | ||
Returns | ||
------- | ||
tiled_loop_rv : Optional[LoopRV] | ||
LoopRV corresponding to the outermost loop of a block tiled according to the given intrin | ||
NullOpt if no valid loop mapping is found | ||
""" | ||
return _ffi_api.TileWithTensorIntrin(sch, block, intrin_name) # type: ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
import tvm | ||
from tvm.tir.tensor_intrin.x86 import VNNI_DOT_16x4_INTRIN | ||
|
||
from tvm.tir import Schedule | ||
from tvm.script import tir as T | ||
from tvm.tir.schedule.transform import tile_with_tensor_intrin | ||
|
||
|
||
@tvm.script.ir_module | ||
class DenseVNNIModule: | ||
@T.prim_func | ||
def main( | ||
placeholder: T.Buffer[(1024, 1024), "uint8"], | ||
placeholder_1: T.Buffer[(64, 256, 16, 4), "int8"], | ||
compute: T.Buffer[(1024, 1024), "int32"], | ||
) -> None: | ||
T.func_attr({"global_symbol": "main", "tir.noalias": True}) | ||
with T.block("root"): | ||
T.reads() | ||
T.writes() | ||
for i0, i1, i2 in T.grid(1024, 1024, 1024): | ||
with T.block("compute"): | ||
i, j, k = T.axis.remap("SSR", [i0, i1, i2]) | ||
T.reads(placeholder[i, k], placeholder_1[j // 16, k // 4, j % 16, k % 4]) | ||
T.writes(compute[i, j]) | ||
with T.init(): | ||
compute[i, j] = 0 | ||
compute[i, j] = compute[i, j] + T.cast(placeholder[i, k], "int32") * T.cast( | ||
placeholder_1[j // 16, k // 4, j % 16, k % 4], "int32" | ||
) | ||
|
||
|
||
@tvm.script.ir_module | ||
class DenseVNNIModuleTiled: | ||
@T.prim_func | ||
def main( | ||
placeholder: T.Buffer[(1024, 1024), "uint8"], | ||
placeholder_1: T.Buffer[(64, 256, 16, 4), "int8"], | ||
compute: T.Buffer[(1024, 1024), "int32"], | ||
) -> None: | ||
# function attr dict | ||
T.func_attr({"global_symbol": "main", "tir.noalias": True}) | ||
# body | ||
# with T.block("root") | ||
for i0, i1_0, i2_0, i1_1, i2_1 in T.grid(1024, 64, 256, 16, 4): | ||
with T.block("compute"): | ||
i = T.axis.spatial(1024, i0) | ||
j = T.axis.spatial(1024, i1_0 * 16 + i1_1) | ||
k = T.axis.reduce(1024, i2_0 * 4 + i2_1) | ||
T.reads(placeholder[i, k], placeholder_1[j // 16, k // 4, j % 16, k % 4]) | ||
T.writes(compute[i, j]) | ||
with T.init(): | ||
compute[i, j] = 0 | ||
compute[i, j] = compute[i, j] + T.cast(placeholder[i, k], "int32") * T.cast( | ||
placeholder_1[j // 16, k // 4, j % 16, k % 4], "int32" | ||
) | ||
|
||
|
||
@tvm.script.ir_module | ||
class Conv2dNCHWcVNNIModule: | ||
@T.prim_func | ||
def main( | ||
placeholder: T.Buffer[(1, 4, 56, 56, 16), "uint8"], | ||
placeholder_1: T.Buffer[(16, 4, 1, 1, 4, 16, 4), "int8"], | ||
conv2d_NCHWc_int8: T.Buffer[(1, 16, 56, 56, 16), "int32"], | ||
) -> None: | ||
T.func_attr({"global_symbol": "main", "tir.noalias": True}) | ||
for i0, i1, i2, i3, i4, i5, i6, i7, i8, i9 in T.grid(1, 16, 56, 56, 16, 1, 1, 4, 4, 4): | ||
with T.block("conv2d_NCHWc_int8"): | ||
( | ||
n, | ||
oc_chunk, | ||
oh, | ||
ow, | ||
oc_block, | ||
kh, | ||
kw, | ||
ic_outer, | ||
ic_f_inner, | ||
ic_s_inner, | ||
) = T.axis.remap("SSSSSRRRRR", [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9]) | ||
T.reads( | ||
placeholder[n, ic_outer, oh + kh, ow + kw, ic_f_inner * 4 + ic_s_inner], | ||
placeholder_1[oc_chunk, ic_outer, kh, kw, ic_f_inner, oc_block, ic_s_inner], | ||
) | ||
T.writes(conv2d_NCHWc_int8[n, oc_chunk, oh, ow, oc_block]) | ||
with T.init(): | ||
conv2d_NCHWc_int8[n, oc_chunk, oh, ow, oc_block] = 0 | ||
conv2d_NCHWc_int8[n, oc_chunk, oh, ow, oc_block] = conv2d_NCHWc_int8[ | ||
n, oc_chunk, oh, ow, oc_block | ||
] + T.cast( | ||
placeholder[n, ic_outer, oh + kh, ow + kw, ic_f_inner * 4 + ic_s_inner], "int32" | ||
) * T.cast( | ||
placeholder_1[oc_chunk, ic_outer, kh, kw, ic_f_inner, oc_block, ic_s_inner], | ||
"int32", | ||
) | ||
|
||
|
||
@tvm.script.ir_module | ||
class Conv2dNCHWcVNNIModuleTiled: | ||
@T.prim_func | ||
def main( | ||
placeholder: T.Buffer[(1, 4, 56, 56, 16), "uint8"], | ||
placeholder_1: T.Buffer[(16, 4, 1, 1, 4, 16, 4), "int8"], | ||
conv2d_NCHWc_int8: T.Buffer[(1, 16, 56, 56, 16), "int32"], | ||
) -> None: | ||
# function attr dict | ||
T.func_attr({"global_symbol": "main", "tir.noalias": True}) | ||
# body | ||
# with T.block("root") | ||
for i0, i1, i2, i3, i4_0, i5, i6, i7, i8, i9_0, i4_1, i9_1 in T.grid( | ||
1, 16, 56, 56, 1, 1, 1, 4, 4, 1, 16, 4 | ||
): | ||
with T.block("conv2d_NCHWc_int8"): | ||
n = T.axis.spatial(1, 0) | ||
oc_chunk, oh, ow, oc_block = T.axis.remap("SSSS", [i1, i2, i3, i4_1]) | ||
kh = T.axis.reduce(1, 0) | ||
kw = T.axis.reduce(1, 0) | ||
ic_outer, ic_f_inner, ic_s_inner = T.axis.remap("RRR", [i7, i8, i9_1]) | ||
T.reads( | ||
placeholder[n, ic_outer, oh + kh, ow + kw, ic_f_inner * 4 + ic_s_inner], | ||
placeholder_1[oc_chunk, ic_outer, kh, kw, ic_f_inner, oc_block, ic_s_inner], | ||
) | ||
T.writes(conv2d_NCHWc_int8[n, oc_chunk, oh, ow, oc_block]) | ||
with T.init(): | ||
conv2d_NCHWc_int8[n, oc_chunk, oh, ow, oc_block] = 0 | ||
conv2d_NCHWc_int8[n, oc_chunk, oh, ow, oc_block] = conv2d_NCHWc_int8[ | ||
n, oc_chunk, oh, ow, oc_block | ||
] + T.cast( | ||
placeholder[n, ic_outer, oh + kh, ow + kw, ic_f_inner * 4 + ic_s_inner], "int32" | ||
) * T.cast( | ||
placeholder_1[oc_chunk, ic_outer, kh, kw, ic_f_inner, oc_block, ic_s_inner], | ||
"int32", | ||
) | ||
|
||
|
||
def test_tile_with_tensor_intrin_dense_vnni(): | ||
s = Schedule(DenseVNNIModule) | ||
block = s.get_block("compute") | ||
|
||
tiled_loop = tile_with_tensor_intrin(s, block, VNNI_DOT_16x4_INTRIN) | ||
|
||
_, _, _, i1_1, _ = s.get_loops(block) | ||
|
||
assert s.get(tiled_loop) == s.get(i1_1) | ||
tvm.ir.assert_structural_equal(s.mod, DenseVNNIModuleTiled) | ||
|
||
|
||
def test_tile_with_tensor_intrin_conv2d_nchwc_vnni(): | ||
s = Schedule(Conv2dNCHWcVNNIModule) | ||
block = s.get_block("conv2d_NCHWc_int8") | ||
|
||
tiled_loop = tile_with_tensor_intrin(s, block, VNNI_DOT_16x4_INTRIN) | ||
|
||
tiled_loops = s.get_loops(block) | ||
|
||
assert len(tiled_loops) == 12 | ||
assert s.get(tiled_loop) == s.get(tiled_loops[-2]) | ||
|
||
tvm.ir.assert_structural_equal(s.mod, Conv2dNCHWcVNNIModuleTiled) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_tile_with_tensor_intrin_dense_vnni() | ||
test_tile_with_tensor_intrin_conv2d_nchwc_vnni() |