jroesch commented on a change in pull request #4818: [REFACTOR][PY] Establish 
tvm.runtime
URL: https://github.com/apache/incubator-tvm/pull/4818#discussion_r375559169
 
 

 ##########
 File path: python/tvm/runtime/ndarray.py
 ##########
 @@ -18,132 +18,36 @@
 """Runtime NDArray api"""
 import ctypes
 import numpy as np
-from .base import _LIB, check_call, c_array, string_types, _FFI_MODE, c_str
-from .runtime_ctypes import TVMType, TVMContext, TVMArray, TVMArrayHandle
-from .runtime_ctypes import TypeCode, tvm_shape_index_t
+import tvm._ffi
+
+from tvm._ffi.base import _LIB, check_call, c_array, string_types, _FFI_MODE
+from tvm._ffi.runtime_ctypes import DataType, TVMContext, TVMArray, 
TVMArrayHandle
+from tvm._ffi.runtime_ctypes import TypeCode, tvm_shape_index_t
 
 try:
     # pylint: disable=wrong-import-position
     if _FFI_MODE == "ctypes":
         raise ImportError()
-    from ._cy3.core import _set_class_ndarray, _make_array, _from_dlpack
-    from ._cy3.core import NDArrayBase as _NDArrayBase
+    from tvm._ffi._cy3.core import _set_class_ndarray, _make_array, 
_from_dlpack
+    from tvm._ffi._cy3.core import NDArrayBase
 except (RuntimeError, ImportError):
     # pylint: disable=wrong-import-position
-    from ._ctypes.ndarray import _set_class_ndarray, _make_array, _from_dlpack
-    from ._ctypes.ndarray import NDArrayBase as _NDArrayBase
-
-
-def context(dev_type, dev_id=0):
-    """Construct a TVM context with given device type and id.
-
-    Parameters
-    ----------
-    dev_type: int or str
-        The device type mask or name of the device.
-
-    dev_id : int, optional
-        The integer device id
-
-    Returns
-    -------
-    ctx: TVMContext
-        The corresponding context.
-
-    Examples
-    --------
-    Context can be used to create reflection of context by
-    string representation of the device type.
-
-    .. code-block:: python
-
-      assert tvm.context("cpu", 1) == tvm.cpu(1)
-      assert tvm.context("gpu", 0) == tvm.gpu(0)
-      assert tvm.context("cuda", 0) == tvm.gpu(0)
-    """
-    if isinstance(dev_type, string_types):
-        if '-device=micro_dev' in dev_type:
-            dev_type = 'micro_dev'
-        else:
-            dev_type = dev_type.split()[0]
-            if dev_type not in TVMContext.STR2MASK:
-                raise ValueError("Unknown device type %s" % dev_type)
-            dev_type = TVMContext.STR2MASK[dev_type]
-    return TVMContext(dev_type, dev_id)
-
-
-def numpyasarray(np_data):
-    """Return a TVMArray representation of a numpy array.
-    """
-    data = np_data
-    assert data.flags['C_CONTIGUOUS']
-    arr = TVMArray()
-    shape = c_array(tvm_shape_index_t, data.shape)
-    arr.data = data.ctypes.data_as(ctypes.c_void_p)
-    arr.shape = shape
-    arr.strides = None
-    arr.dtype = TVMType(np.dtype(data.dtype).name)
-    arr.ndim = data.ndim
-    # CPU device
-    arr.ctx = context(1, 0)
-    return arr, shape
-
-
-def empty(shape, dtype="float32", ctx=context(1, 0)):
-    """Create an empty array given shape and device
-
-    Parameters
-    ----------
-    shape : tuple of int
-        The shape of the array
-
-    dtype : type or str
-        The data type of the array.
-
-    ctx : TVMContext
-        The context of the array
-
-    Returns
-    -------
-    arr : tvm.nd.NDArray
-        The array tvm supported.
-    """
-    shape = c_array(tvm_shape_index_t, shape)
-    ndim = ctypes.c_int(len(shape))
-    handle = TVMArrayHandle()
-    dtype = TVMType(dtype)
-    check_call(_LIB.TVMArrayAlloc(
-        shape, ndim,
-        ctypes.c_int(dtype.type_code),
-        ctypes.c_int(dtype.bits),
-        ctypes.c_int(dtype.lanes),
-        ctx.device_type,
-        ctx.device_id,
-        ctypes.byref(handle)))
-    return _make_array(handle, False, False)
+    from tvm._ffi._ctypes.ndarray import _set_class_ndarray, _make_array, 
_from_dlpack
+    from tvm._ffi._ctypes.ndarray import NDArrayBase
 
 
-def from_dlpack(dltensor):
-    """Produce an array from a DLPack tensor without memory copy.
-    Retreives the underlying DLPack tensor's pointer to create an array from 
the
-    data. Removes the original DLPack tensor's destructor as now the array is
-    responsible for destruction.
+@tvm._ffi.register_object
+class NDArray(NDArrayBase):
+    """Lightweight NDArray class of TVM runtime.
 
 Review comment:
   I think the comments here could use some rewording. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to