Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion python/tvm/script/_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
# specific language governing permissions and limitations
# under the Licens.
"""The parser"""
from . import _core, ir
from . import _core, ir, tir
from .ir import ir_module
from .tir import prim_func
15 changes: 15 additions & 0 deletions python/tvm/script/_parser/core/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,21 @@ def visit_Assign(self, node: doc.Assign) -> Any: # pylint: disable=invalid-name
"""
return _dispatch(self, "Assign")(self, node)

def visit_AnnAssign(self, node: doc.AnnAssign) -> Any: # pylint: disable=invalid-name
"""The general annotated assign visiting method.

Parameters
----------
node : doc.Assign
The doc AST annotated assign node.

Returns
-------
res : Any
The visiting result.
"""
return _dispatch(self, "AnnAssign")(self, node)

def visit_Expr(self, node: doc.Expr) -> Any: # pylint: disable=invalid-name
"""The general expression visiting method.

Expand Down
25 changes: 25 additions & 0 deletions python/tvm/script/_parser/tir/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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.
"""The tir parser"""

from ...ir_builder.tir import * # pylint: disable=redefined-builtin
from ...ir_builder.tir import ir as _tir
from . import operation as _operation
from . import parser as _parser
from .entry import Buffer, Ptr, prim_func

__all__ = _tir.__all__ + ["Buffer", "Ptr", "prim_func"]
108 changes: 108 additions & 0 deletions python/tvm/script/_parser/tir/entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# 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.
"""The entry point of TVM parser for tir."""

import inspect
from typing import Callable, Union

from tvm.tir import Buffer, PrimFunc

from ...ir_builder.tir import buffer_decl, ptr
from .._core import parse, utils
from ..ir import is_defined_in_class


def prim_func(func: Callable) -> Union[PrimFunc, Callable]:
"""The parsing method for tir prim func, by using `@prim_func` as decorator.

Parameters
----------
func : Callable
The function to be parsed as prim func.

Returns
-------
res : Union[PrimFunc, Callable]
The parsed tir prim func.
"""
if not inspect.isfunction(func):
raise TypeError(f"Expect a function, but got: {func}")
if is_defined_in_class(inspect.stack()):
return func
return parse(func, utils.inspect_function_capture(func))


setattr(prim_func, "dispatch_token", "tir")


class BufferProxy:
"""Buffer proxy class for constructing tir buffer.
Overload __call__ and __getitem__ to support syntax as T.Buffer() and T.Buffer[].
"""

def __call__(
self,
shape,
dtype="float32",
data=None,
strides=None,
elem_offset=None,
scope="global",
align=0,
offset_factor=0,
buffer_type="",
axis_separators=None,
) -> Buffer:
return buffer_decl(
shape,
dtype=dtype,
data=data,
strides=strides,
elem_offset=elem_offset,
scope=scope,
align=align,
offset_factor=offset_factor,
buffer_type=buffer_type,
axis_separators=axis_separators,
)

def __getitem__(self, keys) -> Buffer:
if not isinstance(keys, tuple):
return self(keys)
if len(keys) >= 2 and not isinstance(keys[1], str):
return self(keys)
return self(*keys) # pylint: disable=no-member # type: ignore


class PtrProxy:
"""Ptr proxy class for constructing tir pointer.
Overload __call__ and __getitem__ to support syntax as T.Ptr() and T.Ptr[].
"""

def __call__(self, dtype, storage_scope="global"):
if callable(dtype):
dtype = dtype().dtype
return ptr(dtype, storage_scope) # pylint: disable=no-member # type: ignore

def __getitem__(self, keys):
if not isinstance(keys, tuple):
return self(keys)
return self(*keys)


Buffer = BufferProxy() # pylint: disable=invalid-name
Ptr = PtrProxy() # pylint: disable=invalid-name
85 changes: 85 additions & 0 deletions python/tvm/script/_parser/tir/operation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# 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.
"""The tir expression operation registration"""

from typing import Type

from tvm import tir
from tvm.tir import IntImm

from .._core import OpMethod, doc, register_op


def _register_expr_op(ty: Type): # pylint: disable=invalid-name
ty._dispatch_type = ty # pylint: disable=protected-access

def _and(a, b):
if isinstance(a, bool):
a = IntImm("bool", a)
if isinstance(b, bool):
b = IntImm("bool", b)
return tir.And(a, b)

def _or(a, b):
if isinstance(a, bool):
a = IntImm("bool", a)
if isinstance(b, bool):
b = IntImm("bool", b)
return tir.Or(a, b)

def r(op: Type, i: int, m: OpMethod): # pylint: disable=invalid-name
register_op(ty, op, i)(m)

for i in [0, 1]:
# Case 1. binop
r(doc.Add, i, lambda a, b: a + b)
r(doc.Sub, i, lambda a, b: a - b)
r(doc.Mult, i, lambda a, b: a * b)
r(doc.Div, i, lambda a, b: a / b)
r(doc.FloorDiv, i, lambda a, b: a // b)
r(doc.Mod, i, lambda a, b: a % b)
r(doc.LShift, i, lambda a, b: a << b)
r(doc.RShift, i, lambda a, b: a >> b)
r(doc.BitOr, i, lambda a, b: a | b)
r(doc.BitXor, i, lambda a, b: a ^ b)
r(doc.BitAnd, i, lambda a, b: a & b)
# doc.MatMult <-- not implemented
# doc.Pow <-- not implemented
# Case 2. cmpop
r(doc.Eq, i, tir.EQ)
r(doc.NotEq, i, tir.NE)
r(doc.Lt, i, tir.LT)
r(doc.LtE, i, tir.LE)
r(doc.Gt, i, tir.GT)
r(doc.GtE, i, tir.GE)
# doc.Is <-- not implemented
# doc.IsNot <-- not implemented
# doc.In <-- not implemented
# doc.NotIn <-- not implemented
# Case 3. boolop
r(doc.And, i, _and)
r(doc.Or, i, _or)
for i in [0]:
# Case 4. unaryop
r(doc.Invert, i, lambda a: ~a)
r(doc.Not, i, tir.Not)
r(doc.UAdd, i, lambda a: +a)
r(doc.USub, i, lambda a: -a)


_register_expr_op(tir.PrimExpr)
_register_expr_op(tir.IterVar)
Loading