1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements.  See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership.  The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License.  You may obtain a copy of the License at
8#
9#   http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied.  See the License for the
15# specific language governing permissions and limitations
16# under the License.
17"""Container data structures used in TVM DSL."""
18from __future__ import absolute_import as _abs
19from ._ffi.node import NodeBase, register_node
20from . import _api_internal
21
22@register_node
23class Array(NodeBase):
24    """Array container of TVM.
25
26    You do not need to create Array explicitly.
27    Normally python list and tuple will be converted automatically
28    to Array during tvm function call.
29    You may get Array in return values of TVM function call.
30    """
31    def __getitem__(self, i):
32        if isinstance(i, slice):
33            start = i.start if i.start is not None else 0
34            stop = i.stop if i.stop is not None else len(self)
35            step = i.step if i.step is not None else 1
36            if start < 0:
37                start += len(self)
38            if stop < 0:
39                stop += len(self)
40            return [self[idx] for idx in range(start, stop, step)]
41
42        if i < -len(self) or i >= len(self):
43            raise IndexError("Array index out of range. Array size: {}, got index {}"
44                             .format(len(self), i))
45        if i < 0:
46            i += len(self)
47        return _api_internal._ArrayGetItem(self, i)
48
49    def __len__(self):
50        return _api_internal._ArraySize(self)
51
52
53@register_node
54class EnvFunc(NodeBase):
55    """Environment function.
56
57    This is a global function object that can be serialized by its name.
58    """
59    def __call__(self, *args):
60        return _api_internal._EnvFuncCall(self, *args)
61
62    @property
63    def func(self):
64        return _api_internal._EnvFuncGetPackedFunc(self)
65
66
67@register_node
68class Map(NodeBase):
69    """Map container of TVM.
70
71    You do not need to create Map explicitly.
72    Normally python dict will be converted automaticall to Map during tvm function call.
73    You can use convert to create a dict[NodeBase-> NodeBase] into a Map
74    """
75    def __getitem__(self, k):
76        return _api_internal._MapGetItem(self, k)
77
78    def __contains__(self, k):
79        return _api_internal._MapCount(self, k) != 0
80
81    def items(self):
82        """Get the items from the map"""
83        akvs = _api_internal._MapItems(self)
84        return [(akvs[i], akvs[i+1]) for i in range(0, len(akvs), 2)]
85
86    def __len__(self):
87        return _api_internal._MapSize(self)
88
89
90@register_node
91class StrMap(Map):
92    """A special map container that has str as key.
93
94    You can use convert to create a dict[str->NodeBase] into a Map.
95    """
96    def items(self):
97        """Get the items from the map"""
98        akvs = _api_internal._MapItems(self)
99        return [(akvs[i].value, akvs[i+1]) for i in range(0, len(akvs), 2)]
100
101
102@register_node
103class Range(NodeBase):
104    """Represent a range in TVM.
105
106    You do not need to create a Range explicitly.
107    Python lists and tuples will be converted automatically to a Range in API functions.
108    """
109
110
111@register_node
112class LoweredFunc(NodeBase):
113    """Represent a LoweredFunc in TVM."""
114    MixedFunc = 0
115    HostFunc = 1
116    DeviceFunc = 2
117