ndarray.py 4.54 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
"""TVM Runtime NDArray API.
18

19 20
tvm.ndarray provides a minimum runtime array API to test
the correctness of the program.
21
"""
22
# pylint: disable=invalid-name,unused-import
23 24 25
from __future__ import absolute_import as _abs
import numpy as _np

26
from ._ffi.ndarray import TVMContext, TVMType, NDArrayBase
eqy committed
27
from ._ffi.ndarray import context, empty, from_dlpack
28 29
from ._ffi.ndarray import _set_class_ndarray
from ._ffi.ndarray import register_extension, free_extension_handle
30 31 32 33

class NDArray(NDArrayBase):
    """Lightweight NDArray class of TVM runtime.

34
    Strictly this is only an Array Container (a buffer object)
35 36 37 38 39 40 41 42 43
    No arthimetic operations are defined.
    All operations are performed by TVM functions.

    The goal is not to re-build yet another array library.
    Instead, this is a minimal data structure to demonstrate
    how can we use TVM in existing project which might have their own array containers.
    """


44 45 46 47 48 49 50
def cpu(dev_id=0):
    """Construct a CPU device

    Parameters
    ----------
    dev_id : int, optional
        The integer device id
51 52 53 54 55

    Returns
    -------
    ctx : TVMContext
        The created context
56 57 58 59 60 61 62 63 64 65 66
    """
    return TVMContext(1, dev_id)


def gpu(dev_id=0):
    """Construct a CPU device

    Parameters
    ----------
    dev_id : int, optional
        The integer device id
67 68 69 70 71

    Returns
    -------
    ctx : TVMContext
        The created context
72 73 74
    """
    return TVMContext(2, dev_id)

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
def rocm(dev_id=0):
    """Construct a ROCM device

    Parameters
    ----------
    dev_id : int, optional
        The integer device id

    Returns
    -------
    ctx : TVMContext
        The created context
    """
    return TVMContext(10, dev_id)

90 91 92 93 94 95 96 97

def opencl(dev_id=0):
    """Construct a OpenCL device

    Parameters
    ----------
    dev_id : int, optional
        The integer device id
98 99 100 101 102

    Returns
    -------
    ctx : TVMContext
        The created context
103 104 105 106 107 108 109 110 111 112 113
    """
    return TVMContext(4, dev_id)


def metal(dev_id=0):
    """Construct a metal device

    Parameters
    ----------
    dev_id : int, optional
        The integer device id
114 115 116 117 118

    Returns
    -------
    ctx : TVMContext
        The created context
119 120 121 122 123 124 125 126 127 128 129
    """
    return TVMContext(8, dev_id)


def vpi(dev_id=0):
    """Construct a VPI simulated device

    Parameters
    ----------
    dev_id : int, optional
        The integer device id
130 131 132 133 134

    Returns
    -------
    ctx : TVMContext
        The created context
135 136 137
    """
    return TVMContext(9, dev_id)

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

def vulkan(dev_id=0):
    """Construct a Vulkan device

    Parameters
    ----------
    dev_id : int, optional
        The integer device id

    Returns
    -------
    ctx : TVMContext
        The created context
    """
    return TVMContext(7, dev_id)


155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
def opengl(dev_id=0):
    """Construct a OpenGL device

    Parameters
    ----------
    dev_id : int, optional
        The integer device id

    Returns
    -------
    ctx : TVMContext
        The created context
    """
    return TVMContext(11, dev_id)

170

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
def ext_dev(dev_id=0):
    """Construct a extension device

    Parameters
    ----------
    dev_id : int, optional
        The integer device id

    Returns
    -------
    ctx : TVMContext
        The created context

    Note
    ----
    This API is reserved for quick testing of new
    device by plugin device API as ext_dev.
    """
    return TVMContext(12, dev_id)


192 193 194 195
cl = opencl
mtl = metal


196 197 198 199 200 201 202 203
def array(arr, ctx=cpu(0)):
    """Create an array from source arr.

    Parameters
    ----------
    arr : numpy.ndarray
        The array to be copied from

204
    ctx : TVMContext, optional
205 206 207 208
        The device context to create the array

    Returns
    -------
209
    ret : NDArray
210 211
        The created array
    """
212
    if not isinstance(arr, (_np.ndarray, NDArray)):
213
        arr = _np.array(arr)
214
    return empty(arr.shape, arr.dtype, ctx).copyfrom(arr)
215

216
_set_class_ndarray(NDArray)