type.py 6.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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.
"""Unified type system in the project."""
from enum import IntEnum
19
import tvm
20 21 22 23 24 25 26 27 28 29
import tvm._ffi

from .base import Node
from . import _ffi_api


class Type(Node):
    """The base class of all types."""
    def __eq__(self, other):
        """Compare two types for structural equivalence."""
30
        return bool(tvm.ir.structural_equal(self, other))
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

    def __ne__(self, other):
        return not self.__eq__(other)

    def same_as(self, other):
        """Compares two Relay types by referential equality."""
        return super().__eq__(other)


class TypeKind(IntEnum):
    """Possible kinds of TypeVars."""
    Type = 0
    ShapeVar = 1
    BaseType = 2
    Constraint = 4
    AdtHandle = 5
    TypeData = 6


50
@tvm._ffi.register_object("PrimType")
51 52 53 54 55 56 57 58 59 60 61 62 63
class PrimType(Type):
    """Primitive data type in the low level IR

    Parameters
    ----------
    dtype : str
        The runtime data type relates to the primtype.
    """
    def __init__(self, dtype):
        self.__init_handle_by_constructor__(
            _ffi_api.PrimType, dtype)


64 65 66 67 68 69 70 71 72 73 74 75 76 77
@tvm._ffi.register_object("PointerType")
class PointerType(Type):
    """PointerType used in the low-level TIR.

    Parameters
    ----------
    element_type : tvm.ir.Type
        The type of pointer's element.
    """
    def __init__(self, element_type):
        self.__init_handle_by_constructor__(
            _ffi_api.PointerType, element_type)


78
@tvm._ffi.register_object("TypeVar")
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
class TypeVar(Type):
    """Type parameter in functions.

    A type variable represents a type placeholder which will
    be filled in later on. This allows the user to write
    functions which are generic over types.

    Parameters
    ----------
    name_hint: str
        The name of the type variable. This name only acts as a hint, and
        is not used for equality.

    kind : Optional[TypeKind]
        The kind of the type parameter.
    """
    def __init__(self, name_hint, kind=TypeKind.Type):
        self.__init_handle_by_constructor__(
            _ffi_api.TypeVar, name_hint, kind)

    def __call__(self, *args):
        """Create a type call from this type.

        Parameters
        ----------
        args: List[Type]
            The arguments to the type call.

        Returns
        -------
        call: Type
            The result type call.
        """
        # pylint: disable=import-outside-toplevel
        from .type_relation import TypeCall
        return TypeCall(self, args)


117
@tvm._ffi.register_object("GlobalTypeVar")
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
class GlobalTypeVar(Type):
    """A global type variable that is used for defining new types or type aliases.

    Parameters
    ----------
    name_hint: str
        The name of the type variable. This name only acts as a hint, and
        is not used for equality.

    kind : Optional[TypeKind]
        The kind of the type parameter.
    """
    def __init__(self, name_hint, kind=TypeKind.AdtHandle):
        self.__init_handle_by_constructor__(
            _ffi_api.GlobalTypeVar, name_hint, kind)

    def __call__(self, *args):
        """Create a type call from this type.

        Parameters
        ----------
        args: List[Type]
            The arguments to the type call.

        Returns
        -------
        call: Type
            The result type call.
        """
        # pylint: disable=import-outside-toplevel
        from .type_relation import TypeCall
        return TypeCall(self, args)


152
@tvm._ffi.register_object("TupleType")
153 154 155 156 157 158 159 160 161 162 163 164 165 166
class TupleType(Type):
    """The type of tuple values.

    Parameters
    ----------
    fields : List[Type]
        The fields in the tuple
    """

    def __init__(self, fields):
        self.__init_handle_by_constructor__(
            _ffi_api.TupleType, fields)


167
@tvm._ffi.register_object("TypeConstraint")
168 169 170 171
class TypeConstraint(Type):
    """Abstract class representing a type constraint."""


172
@tvm._ffi.register_object("FuncType")
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
class FuncType(Type):
    """Function type.

    A function type consists of a list of type parameters to enable
    the definition of generic functions,
    a set of type constraints which we omit for the time being,
    a sequence of argument types, and a return type.

    We can informally write them as:
    `forall (type_params), (arg_types) -> ret_type where type_constraints`

    Parameters
    ----------
    arg_types : List[tvm.relay.Type]
        The argument types

    ret_type : tvm.relay.Type
        The return type.

    type_params : Optional[List[tvm.relay.TypeVar]]
        The type parameters

    type_constraints : Optional[List[tvm.relay.TypeConstraint]]
        The type constraints.
    """
    def __init__(self,
                 arg_types,
                 ret_type,
                 type_params=None,
                 type_constraints=None):
        if type_params is None:
            type_params = []
        if type_constraints is None:
            type_constraints = []
        self.__init_handle_by_constructor__(
            _ffi_api.FuncType, arg_types, ret_type, type_params, type_constraints)


211
@tvm._ffi.register_object("IncompleteType")
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
class IncompleteType(Type):
    """Incomplete type during type inference.

    kind : Optional[TypeKind]
        The kind of the incomplete type.
    """
    def __init__(self, kind=TypeKind.Type):
        self.__init_handle_by_constructor__(
            _ffi_api.IncompleteType, kind)


@tvm._ffi.register_object("relay.RefType")
class RelayRefType(Type):
    """Reference Type in relay.

    Parameters
    ----------
    value: Type
        The value type.
    """
    def __init__(self, value):
        self.__init_handle_by_constructor__(_ffi_api.RelayRefType, value)