-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PYTHON] Enable cython ndarray API (dmlc#113)
- Loading branch information
Showing
10 changed files
with
282 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Runtime NDArray api""" | ||
from __future__ import absolute_import | ||
|
||
import ctypes | ||
from ..base import _LIB, check_call | ||
from ..runtime_ctypes import TVMArrayHandle | ||
|
||
class NDArrayBase(object): | ||
"""A simple Device/CPU Array object in runtime.""" | ||
__slots__ = ["handle", "is_view"] | ||
# pylint: disable=no-member | ||
def __init__(self, handle, is_view=False): | ||
"""Initialize the function with handle | ||
Parameters | ||
---------- | ||
handle : TVMArrayHandle | ||
the handle to the underlying C++ TVMArray | ||
""" | ||
self.handle = handle | ||
self.is_view = is_view | ||
|
||
def __del__(self): | ||
if not self.is_view: | ||
check_call(_LIB.TVMArrayFree(self.handle)) | ||
|
||
def _make_array(handle, is_view): | ||
handle = ctypes.cast(handle, TVMArrayHandle) | ||
return _CLASS_NDARRAY(handle, is_view) | ||
|
||
_CLASS_NDARRAY = None | ||
|
||
def _set_class_ndarray(cls): | ||
global _CLASS_NDARRAY | ||
_CLASS_NDARRAY = cls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
include "./base.pxi" | ||
include "./node.pxi" | ||
include "./function.pxi" | ||
include "./ndarray.pxi" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from ..runtime_ctypes import TVMArrayHandle | ||
|
||
cdef class NDArrayBase: | ||
cdef DLTensor* chandle | ||
cdef int c_is_view | ||
|
||
cdef inline _set_handle(self, handle): | ||
cdef unsigned long long ptr | ||
if handle is None: | ||
self.chandle = NULL | ||
else: | ||
ptr = ctypes.addressof(handle.contents) | ||
self.chandle = <DLTensor*>(ptr) | ||
|
||
property handle: | ||
def __get__(self): | ||
if self.chandle == NULL: | ||
return None | ||
else: | ||
return ctypes.cast( | ||
<unsigned long long>self.chandle, TVMArrayHandle) | ||
|
||
def __set__(self, value): | ||
self._set_handle(value) | ||
|
||
|
||
def __init__(self, handle, is_view): | ||
self._set_handle(handle) | ||
self.c_is_view = is_view | ||
|
||
|
||
def __dealloc__(self): | ||
if self.c_is_view == 0: | ||
CALL(TVMArrayFree(self.chandle)) | ||
|
||
|
||
cdef c_make_array(void* chandle, is_view): | ||
ret = _CLASS_NDARRAY(None, is_view) | ||
(<NDArrayBase>ret).chandle = <DLTensor*>chandle | ||
return ret | ||
|
||
|
||
def _make_array(handle, is_view): | ||
handle = ctypes.cast(handle, TVMArrayHandle) | ||
return _CLASS_NDARRAY(handle, is_view) | ||
|
||
_CLASS_NDARRAY = None | ||
|
||
def _set_class_ndarray(cls): | ||
global _CLASS_NDARRAY | ||
_CLASS_NDARRAY = cls |
Oops, something went wrong.