8813d0a2bd
## Context
When dealing with end-to-end models, we note that some tensors may have large shapes. Thus, when designing graph-level IR, we sometimes use `int64` instead of `int32` for the shape. Below is an dense GeMM example which has `int64` input tensor shape:
```python
@tvm.script.ir_module
class Module:
@T.prim_func
def main(rxplaceholder: T.Buffer[(1, 512), "float32"], rxplaceholder_1: T.Buffer[(T.int64(1000), T.int64(512)), "float32"], T_matmul_NT: T.Buffer[(1, T.int64(1000)), "float32"]) -> None:
# function attr dict
T.func_attr({"global_symbol": "dense", "tir.noalias": True, "op_pattern": 3})
# body
# with T.block("root")
for i0_0, i1_0, i0_1, i1_1, i2_0, i0_2, i1_2, i2_1, i0_3, i1_3 in T.grid(1, 4, 1, 25, 8, 1, 10, 64, 1, 1):
with T.block("T_matmul_NT"):
i = T.axis.spatial(1, 0)
j = T.axis.spatial(T.int64(1000), i1_0 * T.int64(250) + i1_1 * T.int64(10) + i1_2)
k = T.axis.reduce(512, i2_0 * 64 + i2_1)
T.reads(T_matmul_NT[i, j], rxplaceholder[i, k], rxplaceholder_1[j, k])
T.writes(T_matmul_NT[i, j])
T.block_attr({"layout_free_placeholders":[rxplaceholder_1], "meta_schedule.tiling_structure":"SSRSRS"})
with T.init():
T_matmul_NT[i, j] = T.float32(0)
T_matmul_NT[i, j] = T_matmul_NT[i, j] + rxplaceholder[i, k] * rxplaceholder_1[j, k]
```
## Problem
Though our TVMScript printer can easily print `int64` constants, the parser had poor support for `int64`. So this PR introduces some parser support for `int64`, basically about the data type of loop variables, block iterators and block read/write regions.
Besides the parser, most of the TIR schedule primitives didn't take `int64` into account in their implementations. These schedule primitives will be fixed and updated in recent future, in followup PRs.
160 lines
5.5 KiB
Python
160 lines
5.5 KiB
Python
# 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=redefined-builtin
|
|
"""TVM Script nodes."""
|
|
|
|
from typing import Optional, Union, List, Callable
|
|
import synr
|
|
|
|
from tvm.runtime import ObjectGeneric, convert
|
|
from tvm.tir import PrimExpr, Buffer, BufferLoad
|
|
from tvm.ir import Span
|
|
|
|
|
|
class Slice:
|
|
"""A helper class to present slice information for BufferSlice
|
|
|
|
Parameters
|
|
----------
|
|
start : Union[PrimExpr, int]
|
|
The start index.
|
|
|
|
stop : Optional[Union[PrimExpr, int]]
|
|
The stop index, None means the Slice is an element-wise index
|
|
|
|
span : Optional[Span]
|
|
The location of the slice in the source.
|
|
"""
|
|
|
|
start: Union[PrimExpr, int]
|
|
stop: Optional[Union[PrimExpr, int]]
|
|
span: Optional[Span]
|
|
|
|
def __init__(
|
|
self,
|
|
start: Union[PrimExpr, int],
|
|
stop: Optional[Union[PrimExpr, int]] = None,
|
|
span: Optional[Span] = None,
|
|
):
|
|
self.start = start
|
|
self.stop = stop
|
|
self.span = span
|
|
|
|
|
|
class BufferSlice(ObjectGeneric):
|
|
"""A generic object for representing general buffer access. Following cases are supported:
|
|
- element wise access buffer[i, j], which can be converted to BufferLoad if necessary
|
|
- slice access buffer[i: i + 1, j : j + 2]
|
|
- union of element and slice buffer[i, j: j + 2]
|
|
|
|
This node is used in TVMScript to parse BufferLoad, BufferRegion and Realize
|
|
|
|
Parameters
|
|
----------
|
|
buffer : Buffer
|
|
The buffer.
|
|
|
|
indices : List[Union[Slice, PrimExpr, int]]
|
|
The access indexes can be slice, PrimExpr or int.
|
|
|
|
report_error: Callable[[str, Union[Span, synr.ast.Span]], None]
|
|
The error report func
|
|
|
|
span : Optional[Span]
|
|
The location of the buffer access in the source.
|
|
"""
|
|
|
|
buffer: Buffer
|
|
slices: List[Slice]
|
|
report_error: Callable[[str, Union[Span, synr.ast.Span]], None]
|
|
span: Optional[Span]
|
|
|
|
def __init__(
|
|
self,
|
|
buffer: Buffer,
|
|
indices: List[Union[Slice, PrimExpr, int]],
|
|
report_error: Callable[[str, Union[Span, synr.ast.Span]], None],
|
|
span: Optional[Span] = None,
|
|
):
|
|
def check_index(index: Union[int, PrimExpr]):
|
|
"""Check input index is non-negative integer or PrimExpr"""
|
|
if isinstance(index, int):
|
|
if index < 0:
|
|
report_error("Negative index is not allowed during buffer access", span)
|
|
elif isinstance(index, PrimExpr):
|
|
element_dtype = index.dtype.split("x", maxsplit=1)[0]
|
|
if element_dtype[:3] != "int":
|
|
report_error(
|
|
"index expected an integer type PrimExpr but got " + str(index.dtype),
|
|
index.span,
|
|
)
|
|
else:
|
|
report_error(
|
|
"Unsupported index type, expected int or tvm.tir.PrimExpr, but got "
|
|
+ str(type(index)),
|
|
span,
|
|
)
|
|
|
|
slices: List[Union[Slice, BufferSlice]] = []
|
|
for index in indices:
|
|
if isinstance(index, Slice):
|
|
index.start, index.stop = [convert(_) for _ in [index.start, index.stop]]
|
|
check_index(index.start)
|
|
check_index(index.stop)
|
|
slices.append(index)
|
|
elif isinstance(index, (PrimExpr, int)):
|
|
check_index(index)
|
|
slices.append(Slice(index))
|
|
elif isinstance(index, BufferSlice):
|
|
buffer_load = index.asobject()
|
|
check_index(buffer_load)
|
|
slices.append(Slice(buffer_load))
|
|
else:
|
|
report_error(
|
|
"Unsupported index type for BufferSlice, "
|
|
+ "expected int, tvm.tir.PrimExpr, tvm.tir.Slice, but got "
|
|
+ str(type(index)),
|
|
span,
|
|
)
|
|
|
|
self.buffer = buffer
|
|
self.slices = slices
|
|
self.report_error = report_error
|
|
self.span = span
|
|
|
|
def __str__(self):
|
|
regions: List[str] = []
|
|
for s in self.slices:
|
|
if s.stop is None:
|
|
regions.append(str(s.start))
|
|
else:
|
|
regions.append(str(s.start) + ": " + str(s.stop))
|
|
|
|
return self.buffer.name + "[" + ", ".join(regions) + "]"
|
|
|
|
def asobject(self) -> BufferLoad:
|
|
"""Convert object."""
|
|
for s in self.slices:
|
|
if s.stop is not None:
|
|
self.report_error("BufferLoad only accepts elementwise access", self.span)
|
|
|
|
indices = [s.start for s in self.slices]
|
|
return BufferLoad(self.buffer, indices, span=self.span)
|
|
|
|
def astype(self, dtype: str, span: Optional[Span] = None) -> PrimExpr:
|
|
return self.asobject().astype(dtype, span)
|