Skip to content

Commit

Permalink
[TIR] Add VNNI dot product intrinsic for TIR
Browse files Browse the repository at this point in the history
  • Loading branch information
masahi committed Apr 6, 2022
1 parent 5ca528c commit 11a29c7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
9 changes: 4 additions & 5 deletions python/tvm/script/tir/special_stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@

import tvm.tir
from tvm.runtime import Object, String
from tvm import te
from tvm.target import Target
from tvm.ir import Span
from tvm.tir import IntImm, IterVar
from tvm.tir import IntImm, IterVar, Var

from .node import BufferSlice
from .utils import buffer_slice_to_region
Expand Down Expand Up @@ -800,7 +799,7 @@ def var(dtype, span):
self.context.report_error(
f"VarDef expected assign to only one var, but got {names}", span
)
v = te.var(names[0], dtype, span=span)
v = Var(names[0], dtype, span=span)
self.context.update_symbol(v.name, v, self.node)

super().__init__(var, def_symbol=True)
Expand All @@ -821,7 +820,7 @@ def buffer_var(dtype, storage_scope, span):
f"VarDef expected assign to only one var, but got {names}", span
)
ptr_type = tvm.ir.PointerType(tvm.ir.PrimType(dtype), storage_scope)
v = te.var(names[0], ptr_type, span=span)
v = Var(names[0], ptr_type, span=span)
self.context.update_symbol(v.name, v, self.node)

super().__init__(buffer_var, def_symbol=True)
Expand All @@ -841,7 +840,7 @@ def env_thread(env_name, span):
self.context.report_error(
f"VarDef expected assign to only one var, but got {names}", span
)
v = te.var(names[0], span=span)
v = Var(names[0], span=span)
self.context.func_var_env_dict[v] = env_name
self.context.update_symbol(v.name, v, self.node)

Expand Down
19 changes: 19 additions & 0 deletions python/tvm/tir/tensor_intrin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.
# pylint: disable=unused-import
"""Intrinsics for tensorization."""
from . import vnni
69 changes: 69 additions & 0 deletions python/tvm/tir/tensor_intrin/vnni.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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.
from .. import TensorIntrin
from tvm.script import tir as T


@T.prim_func
def dot_product_desc(a: T.handle, b: T.handle, c: T.handle) -> None:
A = T.match_buffer(a, (4,), "uint8", offset_factor=1)
B = T.match_buffer(b, (16, 4), "int8", offset_factor=1)
C = T.match_buffer(c, (16,), "int32", offset_factor=1)

with T.block("root"):
T.reads(C[0:16], A[0:4], B[0:16, 0:4])
T.writes(C[0:16])
for i in T.serial(0, 16):
with T.init():
C[i] = T.int32(0)
for k in T.serial(0, 4):
with T.block("update"):
vi, vk = T.axis.remap("SR", [i, k])
C[vi] = C[vi] + T.cast(A[vk], "int32") * T.cast(B[vi, vk], "int32")


@T.prim_func
def dot_product_intrin(a: T.handle, b: T.handle, c: T.handle) -> None:
A = T.match_buffer(a, (4,), "uint8", offset_factor=1)
B = T.match_buffer(b, (16, 4), "int8", offset_factor=1)
C = T.match_buffer(c, (16,), "int32", offset_factor=1)

with T.block("root"):
T.reads(C[0:16], A[0:4], B[0:16, 0:4])
T.writes(C[0:16])

A_u8x4 = A.vload([0], "uint8x4")
A_i32 = T.reinterpret(A_u8x4, dtype="int32")

B_i8x64 = B.vload([0, 0], dtype="int8x64")
B_i32x16 = T.reinterpret(B_i8x64, dtype="int32x16")

C[
T.ramp(T.int32(0), 1, 16)
] += T.call_llvm_pure_intrin( # Note: this is an update +=
T.llvm_lookup_intrinsic_id("llvm.x86.avx512.vpdpbusd.512"),
T.uint32(0),
T.int32x16(0),
T.broadcast(A_i32, 16),
B_i32x16,
dtype="int32x16",
)


TensorIntrin.register(
"dot_16x1x16_uint8_int8_int32_cascadelake", dot_product_desc, dot_product_intrin
)

0 comments on commit 11a29c7

Please sign in to comment.