ndarray.py 3.77 KB
Newer Older
1
"""TVM Runtime NDArray API.
2

3 4
tvm.ndarray provides a minimum runtime array API to test
the correctness of the program.
5
"""
6
# pylint: disable=invalid-name,unused-import
7 8 9
from __future__ import absolute_import as _abs
import numpy as _np

10
from ._ffi.ndarray import TVMContext, TVMType, NDArrayBase
11
from ._ffi.ndarray import context, empty
12 13
from ._ffi.ndarray import _set_class_ndarray
from ._ffi.ndarray import register_extension, free_extension_handle
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

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

    Strictly this is only an Array Container(a buffer object)
    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.
    """
    pass


29 30 31 32 33 34 35
def cpu(dev_id=0):
    """Construct a CPU device

    Parameters
    ----------
    dev_id : int, optional
        The integer device id
36 37 38 39 40

    Returns
    -------
    ctx : TVMContext
        The created context
41 42 43 44 45 46 47 48 49 50 51
    """
    return TVMContext(1, dev_id)


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

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

    Returns
    -------
    ctx : TVMContext
        The created context
57 58 59
    """
    return TVMContext(2, dev_id)

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
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)

75 76 77 78 79 80 81 82

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

    Parameters
    ----------
    dev_id : int, optional
        The integer device id
83 84 85 86 87

    Returns
    -------
    ctx : TVMContext
        The created context
88 89 90 91 92 93 94 95 96 97 98
    """
    return TVMContext(4, dev_id)


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

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

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


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

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

    Returns
    -------
    ctx : TVMContext
        The created context
120 121 122
    """
    return TVMContext(9, dev_id)

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

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)


140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
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)

155

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
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)


177 178 179 180
cl = opencl
mtl = metal


181 182 183 184 185 186 187 188
def array(arr, ctx=cpu(0)):
    """Create an array from source arr.

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

189
    ctx : TVMContext, optional
190 191 192 193
        The device context to create the array

    Returns
    -------
194
    ret : NDArray
195 196
        The created array
    """
197
    if not isinstance(arr, (_np.ndarray, NDArray)):
198
        arr = _np.array(arr)
199
    return empty(arr.shape, arr.dtype, ctx).copyfrom(arr)
200

201
_set_class_ndarray(NDArray)