object_generic.py 4.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 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.
17
"""Common implementation of object generic related logic"""
18
# pylint: disable=unused-import, invalid-name
19
from numbers import Number, Integral
20
from tvm._ffi.base import string_types
21

22
from . import _ffi_node_api
23 24 25
from .object import ObjectBase, _set_class_object_generic
from .ndarray import NDArrayBase
from .packed_func import PackedFuncBase, convert_to_tvm_func
26
from .module import Module
27 28


29 30 31 32
class ObjectGeneric(object):
    """Base class for all classes that can be converted to object."""
    def asobject(self):
        """Convert value to object"""
33 34
        raise NotImplementedError()

35

36
ObjectTypes = (ObjectBase, NDArrayBase, Module)
37 38


39 40
def convert_to_object(value):
    """Convert a python value to corresponding object type.
41 42 43 44 45 46 47 48

    Parameters
    ----------
    value : str
        The value to be inspected.

    Returns
    -------
49 50
    obj : Object
        The corresponding object value.
51
    """
52
    if isinstance(value, ObjectTypes):
53
        return value
54
    if isinstance(value, bool):
Yizhi Liu committed
55
        return const(value, 'uint1x1')
56
    if isinstance(value, Number):
57
        return const(value)
58
    if isinstance(value, string_types):
59
        return _ffi_node_api.String(value)
60
    if isinstance(value, (list, tuple)):
61
        value = [convert_to_object(x) for x in value]
62
        return _ffi_node_api.Array(*value)
63
    if isinstance(value, dict):
64 65
        vlist = []
        for item in value.items():
66
            if (not isinstance(item[0], ObjectTypes) and
67
                    not isinstance(item[0], string_types)):
68 69
                raise ValueError("key of map must already been a container type")
            vlist.append(item[0])
70
            vlist.append(convert_to_object(item[1]))
71
        return _ffi_node_api.Map(*vlist)
72 73
    if isinstance(value, ObjectGeneric):
        return value.asobject()
74
    if value is None:
75
        return None
76

77
    raise ValueError("don't know how to convert type %s to object" % type(value))
78

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
def convert(value):
    """Convert value to TVM object or function.

    Parameters
    ----------
    value : python value

    Returns
    -------
    tvm_val : Object or Function
        Converted value in TVM
    """
    if isinstance(value, (PackedFuncBase, ObjectBase)):
        return value

    if callable(value):
        return convert_to_tvm_func(value)

    return convert_to_object(value)


def _scalar_type_inference(value):
    if hasattr(value, 'dtype'):
        dtype = str(value.dtype)
    elif isinstance(value, bool):
        dtype = 'bool'
    elif isinstance(value, float):
        # We intentionally convert the float to float32 since it's more common in DL.
        dtype = 'float32'
    elif isinstance(value, int):
        # We intentionally convert the python int to int32 since it's more common in DL.
        dtype = 'int32'
    else:
        raise NotImplementedError('Cannot automatically inference the type.'
                                  ' value={}'.format(value))
    return dtype

117
def const(value, dtype=None):
118
    """construct a constant
119 120 121

    Parameters
    ----------
122 123
    value : number
        The content of the constant number.
124

125
    dtype : str or None, optional
126 127 128 129
        The data type.

    Returns
    -------
130 131
    const_val: tvm.Expr
        The result expression.
132 133
    """
    if dtype is None:
134
        dtype = _scalar_type_inference(value)
135
    if dtype == "uint64" and value >= (1 << 63):
136
        return _ffi_node_api.LargeUIntImm(
137
            dtype, value & ((1 << 32) - 1), value >> 32)
138
    return _ffi_node_api._const(value, dtype)
139 140 141


_set_class_object_generic(ObjectGeneric, convert_to_object)