-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TensorIR] TVMScript Parser/Printer (#317)
[TensorIR] TVMScript Parser/Printer Co-authored-by: Bohan Hou <32121147+spectrometerHBH@users.noreply.github.com> Co-authored-by: Junru Shao <junrushao1994@gmail.com> Co-authored-by: Tianqi Chen <tqchen@users.noreply.github.com> Co-authored-by: Ruihang Lai <lairuihangdongdong@qq.com> Co-authored-by: Hongyi Jin <3231950289@qq.com> Co-authored-by: Wuwei Lin <wuwei@apache.org>
- Loading branch information
1 parent
3a0e3a5
commit 99a62d1
Showing
17 changed files
with
2,256 additions
and
194 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
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,150 @@ | ||
# 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 | ||
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 convert 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): | ||
if index.dtype != "int32": | ||
report_error( | ||
"index expects an int32 type PrimExpr but gets " + str(index.dtype), | ||
index.span, | ||
) | ||
else: | ||
report_error( | ||
"Unsupported index type, expects int or tvm.tir.PrimExpr, but gets " | ||
+ str(type(index)), | ||
span, | ||
) | ||
|
||
slices: List[Slice] = [] | ||
for index in indices: | ||
if isinstance(index, Slice): | ||
check_index(index.start) | ||
check_index(index.stop) | ||
slices.append(index) | ||
elif isinstance(index, (PrimExpr, int)): | ||
check_index(index) | ||
slices.append(Slice(index)) | ||
else: | ||
report_error( | ||
"Unsupported index type for BufferSlice, " | ||
+ "expects int, tvm.tir.PrimExpr, tvm.tir.Slice, but gets " | ||
+ 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) |
Oops, something went wrong.