shingjan commented on a change in pull request #9432: URL: https://github.com/apache/tvm/pull/9432#discussion_r742304506
########## File path: tests/python/unittest/test_tvmscript_type.py ########## @@ -0,0 +1,61 @@ +# 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=missing-function-docstring,missing-module-docstring +import sys +import pytest +import tvm +from tvm import tir +from tvm.script import tir as T + +""" +This module tests the type of +T.prim_func, T.handle, T.match_buffer, T.block +T.reads, T.writes, T.alloc_buffer, T.serial +T.block_attr, T.float32 +""" + + [email protected]_testing Review comment: done ########## File path: python/tvm/script/tir/__init__.pyi ########## @@ -0,0 +1,239 @@ +# 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 +from typing import ( + Any, + Callable, + ContextManager, + Dict, + Iterable, + Object, + Optional, + Tuple, + Union, + Sequence, + List, + Mapping, +) +from tvm.tir.function import PrimFunc +from tvm.tir import PrimExpr, Buffer, IterVar, Var +from .node import BufferSlice + +""" +Variables and constants +""" + +def bool(imm: int) -> PrimExpr: ... Review comment: the definition of `bool` here actually shadows all use of native `bool` type annotation of Python in this file. My workaround is to not include type annotation for method/class parameter that may be typed `bool`. E.g. this line would just be `def bool(imm) -> PrimExpr: ...` ########## File path: python/tvm/script/tir/__init__.pyi ########## @@ -0,0 +1,239 @@ +# 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 +from typing import ( + Any, + Callable, + ContextManager, + Dict, + Iterable, + Object, + Optional, + Tuple, + Union, + Sequence, + List, + Mapping, +) +from tvm.tir.function import PrimFunc +from tvm.tir import PrimExpr, Buffer, IterVar, Var +from .node import BufferSlice + +""" +Variables and constants +""" + +def bool(imm: int) -> PrimExpr: ... +def int8(imm: int) -> PrimExpr: ... Review comment: done ########## File path: python/tvm/script/tir/__init__.pyi ########## @@ -0,0 +1,239 @@ +# 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 +from typing import ( + Any, + Callable, + ContextManager, + Dict, + Iterable, + Object, + Optional, + Tuple, + Union, + Sequence, + List, + Mapping, +) +from tvm.tir.function import PrimFunc +from tvm.tir import PrimExpr, Buffer, IterVar, Var +from .node import BufferSlice + +""" +Variables and constants +""" + +def bool(imm: int) -> PrimExpr: ... +def int8(imm: int) -> PrimExpr: ... +def int16(imm: int) -> PrimExpr: ... +def int32(imm: int) -> PrimExpr: ... +def int64(imm: int) -> PrimExpr: ... +def uint8(imm: int) -> PrimExpr: ... +def uint16(imm: int) -> PrimExpr: ... +def uint32(imm: int) -> PrimExpr: ... +def uint64(imm: int) -> PrimExpr: ... +def float8(imm: int) -> PrimExpr: ... +def float16(imm: int) -> PrimExpr: ... +def float32(imm: int) -> PrimExpr: ... +def float64(imm: int) -> PrimExpr: ... + +""" +Intrinsic +""" + +def min_value(dtype): ... +def max_value(dtype): ... +def floordiv(x: PrimExpr, y: PrimExpr): ... +def floormod(x: PrimExpr, y: PrimExpr): ... +def abs(x): ... +def load(dtype, var, index, predicate=None): ... +def cast(value, dtype): ... +def ramp(base, stride, lanes): ... +def broadcast(value, lanes): ... +def iter_var(var, dom, iter_type, thread_tag): ... +def max(a, b): ... +def min(a, b): ... +def get_axis(begin, end, iter_type): ... +def range(begin, end): ... Review comment: removed ########## File path: python/tvm/script/tir/__init__.pyi ########## @@ -0,0 +1,261 @@ +# 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 +from typing import ( + Any, + Callable, + ContextManager, + Dict, + Iterable, + Optional, + Tuple, + Union, + Sequence, + List, + Mapping, +) + +from tvm.tir.function import PrimFunc +from tvm.tir import PrimExpr, Range, IterVar, Var +from tvm.runtime import Object +from .node import BufferSlice +from . import axis +from .ty import ConcreteType + +""" +redefine types +""" + +class Buffer(Var): + def __getitem__(self: Buffer, pos: Tuple[Union[int, PrimExpr]]) -> Buffer: ... + @property + def data(self: Buffer) -> Ptr: ... + +class Ptr: ... + +""" +Variables and constants +""" + +def bool(imm: int) -> PrimExpr: ... +def int8(imm: int) -> PrimExpr: ... +def int16(imm: int) -> PrimExpr: ... +def int32(imm: int) -> PrimExpr: ... +def int64(imm: int) -> PrimExpr: ... +def uint8(imm: int) -> PrimExpr: ... +def uint16(imm: int) -> PrimExpr: ... +def uint32(imm: int) -> PrimExpr: ... +def uint64(imm: int) -> PrimExpr: ... +def float8(imm: int) -> PrimExpr: ... +def float16(imm: int) -> PrimExpr: ... +def float32(imm: int) -> PrimExpr: ... +def float64(imm: int) -> PrimExpr: ... + +""" +Intrinsic +""" + +def min_value(dtype): ... +def max_value(dtype): ... +def floordiv(x: PrimExpr, y: PrimExpr): ... +def floormod(x: PrimExpr, y: PrimExpr): ... +def abs(x): ... +def load(dtype, var, index, predicate=None): ... +def cast(value, dtype): ... +def ramp(base, stride, lanes): ... +def broadcast(value, lanes): ... +def iter_var(var, dom, iter_type, thread_tag): ... +def max(a, b): ... +def min(a, b): ... +def get_axis(begin, end, iter_type): ... +def Select(cond, if_body, else_body): ... +def evaluate(value): ... +def store(var, index, value, predicate=True): ... +def comm_reducer(lambda_io, identities): ... + +""" +Unary operator +""" + +def exp2(x: PrimExpr) -> PrimExpr: ... +def exp10(x: PrimExpr) -> PrimExpr: ... +def erf(x: PrimExpr) -> PrimExpr: ... +def tanh(x: PrimExpr) -> PrimExpr: ... +def sigmoid(x: PrimExpr) -> PrimExpr: ... +def log(x: PrimExpr) -> PrimExpr: ... +def log2(x: PrimExpr) -> PrimExpr: ... +def log10(x: PrimExpr) -> PrimExpr: ... +def log1p(x: PrimExpr) -> PrimExpr: ... +def tan(x: PrimExpr) -> PrimExpr: ... +def cos(x: PrimExpr) -> PrimExpr: ... +def cosh(x: PrimExpr) -> PrimExpr: ... +def acos(x: PrimExpr) -> PrimExpr: ... +def acosh(x: PrimExpr) -> PrimExpr: ... +def sin(x: PrimExpr) -> PrimExpr: ... +def sinh(x: PrimExpr) -> PrimExpr: ... +def asin(x: PrimExpr) -> PrimExpr: ... +def asinh(x: PrimExpr) -> PrimExpr: ... +def atan(x: PrimExpr) -> PrimExpr: ... +def atanh(x: PrimExpr) -> PrimExpr: ... +def atan2(x: PrimExpr) -> PrimExpr: ... +def sqrt(x: PrimExpr) -> PrimExpr: ... +def rsqrt(x: PrimExpr) -> PrimExpr: ... + +""" +Axis +""" + +def reduce_axis(begin: Union[PrimExpr, int], end: Union[PrimExpr, int]) -> IterVar: ... +def range(begin: Union[PrimExpr, int], end: Union[PrimExpr, int]) -> IterVar: ... +def scan_axis(begin: Union[PrimExpr, int], end: Union[PrimExpr, int]) -> IterVar: ... +def opaque_axis(begin: Union[PrimExpr, int], end: Union[PrimExpr, int]) -> IterVar: ... + +""" +special_stmt - Buffers +""" + +def match_buffer( + param: Union[Var, BufferSlice], + shape: Sequence[Union[PrimExpr, int]], + dtype: str = "float32", + data=None, + strides: Optional[Sequence[int]] = None, + elem_offset: Optional[int] = None, + scope: str = "global", + align: int = -1, + offset_factor: int = 0, + buffer_type: str = "default", +) -> Buffer: ... +def buffer_decl( + shape: Sequence[Union[PrimExpr, int]], + dtype: str = "float32", + data=None, + strides: Optional[Sequence[int]] = None, + elem_offset: Optional[int] = None, + scope: str = "global", + align: int = -1, + offset_factor: int = 0, + buffer_type: str = "default", +) -> Buffer: ... +def alloc_buffer( + shape: Sequence[Union[PrimExpr, int]], + dtype: str = "float32", + data=None, + strides: Optional[Sequence[int]] = None, + elem_offset: Optional[int] = None, + scope: str = "global", + align: int = -1, + offset_factor: int = 0, + buffer_type: str = "default", +) -> Buffer: ... + +""" +special_stmt - Reads/Writes +""" + +def reads(read_regions: Union[BufferSlice, List[BufferSlice]]) -> None: ... +def writes(write_region: Union[BufferSlice, List[BufferSlice]]) -> None: ... +def block_attr(attrs: Mapping[str, Object]) -> None: ... + +""" +special_stmt - Axis +""" + +def axis_spatial(dom: Union[PrimExpr, Tuple[PrimExpr, PrimExpr]], value: PrimExpr) -> IterVar: ... Review comment: done ########## File path: mypy.ini ########## @@ -23,6 +23,14 @@ follow_imports = skip ignore_errors = False strict_optional = False +# +# Note: not all tests under .tests/ are typed +# Therefore include test files that should be +# checked by mypy here +# +files = + tests/python/unittest/test_tvmscript_type.py Review comment: Good point. The reason why I include that test in `mypy.ini` is that PyTorch seems to add individual test to mypy this [way](https://github.com/pytorch/pytorch/blob/master/mypy.ini). But adding that to `task_mypy.sh` should also work. ########## File path: python/tvm/script/tir/__init__.pyi ########## @@ -0,0 +1,261 @@ +# 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 +from typing import ( + Any, + Callable, + ContextManager, + Dict, + Iterable, + Optional, + Tuple, + Union, + Sequence, + List, + Mapping, +) + +from tvm.tir.function import PrimFunc +from tvm.tir import PrimExpr, Range, IterVar, Var +from tvm.runtime import Object +from .node import BufferSlice +from . import axis +from .ty import ConcreteType + +""" +redefine types +""" + +class Buffer(Var): + def __getitem__(self: Buffer, pos: Tuple[Union[int, PrimExpr]]) -> Buffer: ... + @property + def data(self: Buffer) -> Ptr: ... + +class Ptr: ... + +""" +Variables and constants +""" + +def bool(imm: int) -> PrimExpr: ... +def int8(imm: int) -> PrimExpr: ... +def int16(imm: int) -> PrimExpr: ... +def int32(imm: int) -> PrimExpr: ... +def int64(imm: int) -> PrimExpr: ... +def uint8(imm: int) -> PrimExpr: ... +def uint16(imm: int) -> PrimExpr: ... +def uint32(imm: int) -> PrimExpr: ... +def uint64(imm: int) -> PrimExpr: ... +def float8(imm: int) -> PrimExpr: ... +def float16(imm: int) -> PrimExpr: ... +def float32(imm: int) -> PrimExpr: ... +def float64(imm: int) -> PrimExpr: ... + +""" +Intrinsic +""" + +def min_value(dtype): ... +def max_value(dtype): ... +def floordiv(x: PrimExpr, y: PrimExpr): ... +def floormod(x: PrimExpr, y: PrimExpr): ... +def abs(x): ... +def load(dtype, var, index, predicate=None): ... +def cast(value, dtype): ... +def ramp(base, stride, lanes): ... +def broadcast(value, lanes): ... +def iter_var(var, dom, iter_type, thread_tag): ... +def max(a, b): ... +def min(a, b): ... +def get_axis(begin, end, iter_type): ... +def Select(cond, if_body, else_body): ... +def evaluate(value): ... +def store(var, index, value, predicate=True): ... +def comm_reducer(lambda_io, identities): ... + +""" +Unary operator +""" + +def exp2(x: PrimExpr) -> PrimExpr: ... +def exp10(x: PrimExpr) -> PrimExpr: ... +def erf(x: PrimExpr) -> PrimExpr: ... +def tanh(x: PrimExpr) -> PrimExpr: ... +def sigmoid(x: PrimExpr) -> PrimExpr: ... +def log(x: PrimExpr) -> PrimExpr: ... +def log2(x: PrimExpr) -> PrimExpr: ... +def log10(x: PrimExpr) -> PrimExpr: ... +def log1p(x: PrimExpr) -> PrimExpr: ... +def tan(x: PrimExpr) -> PrimExpr: ... +def cos(x: PrimExpr) -> PrimExpr: ... +def cosh(x: PrimExpr) -> PrimExpr: ... +def acos(x: PrimExpr) -> PrimExpr: ... +def acosh(x: PrimExpr) -> PrimExpr: ... +def sin(x: PrimExpr) -> PrimExpr: ... +def sinh(x: PrimExpr) -> PrimExpr: ... +def asin(x: PrimExpr) -> PrimExpr: ... +def asinh(x: PrimExpr) -> PrimExpr: ... +def atan(x: PrimExpr) -> PrimExpr: ... +def atanh(x: PrimExpr) -> PrimExpr: ... +def atan2(x: PrimExpr) -> PrimExpr: ... +def sqrt(x: PrimExpr) -> PrimExpr: ... +def rsqrt(x: PrimExpr) -> PrimExpr: ... + +""" +Axis +""" + +def reduce_axis(begin: Union[PrimExpr, int], end: Union[PrimExpr, int]) -> IterVar: ... +def range(begin: Union[PrimExpr, int], end: Union[PrimExpr, int]) -> IterVar: ... +def scan_axis(begin: Union[PrimExpr, int], end: Union[PrimExpr, int]) -> IterVar: ... +def opaque_axis(begin: Union[PrimExpr, int], end: Union[PrimExpr, int]) -> IterVar: ... + +""" +special_stmt - Buffers +""" + +def match_buffer( + param: Union[Var, BufferSlice], + shape: Sequence[Union[PrimExpr, int]], + dtype: str = "float32", + data=None, + strides: Optional[Sequence[int]] = None, + elem_offset: Optional[int] = None, + scope: str = "global", + align: int = -1, + offset_factor: int = 0, + buffer_type: str = "default", +) -> Buffer: ... +def buffer_decl( + shape: Sequence[Union[PrimExpr, int]], + dtype: str = "float32", + data=None, + strides: Optional[Sequence[int]] = None, + elem_offset: Optional[int] = None, + scope: str = "global", + align: int = -1, + offset_factor: int = 0, + buffer_type: str = "default", +) -> Buffer: ... +def alloc_buffer( + shape: Sequence[Union[PrimExpr, int]], + dtype: str = "float32", + data=None, + strides: Optional[Sequence[int]] = None, + elem_offset: Optional[int] = None, + scope: str = "global", + align: int = -1, + offset_factor: int = 0, + buffer_type: str = "default", +) -> Buffer: ... + +""" +special_stmt - Reads/Writes +""" + +def reads(read_regions: Union[BufferSlice, List[BufferSlice]]) -> None: ... +def writes(write_region: Union[BufferSlice, List[BufferSlice]]) -> None: ... +def block_attr(attrs: Mapping[str, Object]) -> None: ... + +""" +special_stmt - Axis +""" + +def axis_spatial(dom: Union[PrimExpr, Tuple[PrimExpr, PrimExpr]], value: PrimExpr) -> IterVar: ... +def axis_reduce(dom: Union[PrimExpr, Tuple[PrimExpr, PrimExpr]], value: PrimExpr) -> IterVar: ... +def axis_scan(dom: Union[PrimExpr, Tuple[PrimExpr, PrimExpr]], value: PrimExpr) -> IterVar: ... +def axis_opaque(dom: Union[PrimExpr, Tuple[PrimExpr, PrimExpr]], value: PrimExpr) -> IterVar: ... +def axis_remap(iter_types: str, loop_vars: List[Var]) -> IterVar: ... + +""" +special_stmt - Annotations +""" + +def buffer_var(dtype, storage_scope) -> IterVar: ... +def func_attr(attrs: Dict) -> None: ... +def prim_func(input_func: Callable) -> PrimFunc: ... + +""" +special_stmt - Threads and Bindings +""" + +def env_thread(env_name: str) -> IterVar: ... +def bind(iter_var: IterVar, expr: PrimExpr) -> None: ... + +""" +Scope handler +""" + +class block(ContextManager): + def __init__(self, name_hint: str = "") -> None: ... + def __enter__(self) -> Sequence[IterVar]: ... + +class init(ContextManager): + def __init__(self) -> None: ... + +class let(ContextManager): + def __init__(self, var: Var, value: PrimExpr) -> None: ... + +def where(cond: PrimExpr) -> None: ... +def allocate(extents, dtype, scope: str, condition=True, annotations=None) -> None: ... +def launch_thread(env_var, extent): ... +def realize(buffer_slice: BufferSlice, scope: str, condition=True) -> None: ... +def attr(node: PrimExpr, attr_key: str, value: PrimExpr) -> None: ... +def Assert(condition, message): ... + +""" +Scope handler - Loops +""" + +def serial( + begin: Union[PrimExpr, int], + end: Union[PrimExpr, int], + annotations: Optional[Mapping[str, Object]] = None, +) -> Iterable[IterVar]: ... +def parallel( + begin: Union[PrimExpr, int], + end: Union[PrimExpr, int], + annotations: Optional[Mapping[str, Object]] = None, +) -> Iterable[IterVar]: ... +def vectorized( + begin: Union[PrimExpr, int], + end: Union[PrimExpr, int], + annotations: Optional[Mapping[str, Object]] = None, +) -> Iterable[IterVar]: ... +def unroll( + begin: Union[PrimExpr, int], + end: Union[PrimExpr, int], + annotations: Optional[Mapping[str, Object]] = None, +) -> Iterable[IterVar]: ... +def thread_binding( + begin: Union[PrimExpr, int], + end: Union[PrimExpr, int], + thread: str, + annotations: Optional[Mapping[str, Object]] = None, +) -> Iterable[IterVar]: ... +def for_range( + begin: Union[PrimExpr, int], + end: Union[PrimExpr, int] = None, + annotations: Optional[Mapping[str, Object]] = None, +) -> Iterable[IterVar]: ... +def grid(*extents: List[Union[PrimExpr, int]]) -> Iterable[Tuple[IterVar]]: ... + +""" +ty +""" +boolean = ConcreteType("bool") +handle = ConcreteType("handle") Review comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
