Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
add cython support for numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
Fan committed Jul 1, 2019
1 parent d60e105 commit eac2130
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
28 changes: 20 additions & 8 deletions python/mxnet/cython/ndarray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,29 @@ cdef class NDArrayBase:


_ndarray_cls = None
_np_ndarray_cls = None

def _set_ndarray_class(cls):
global _ndarray_cls
_ndarray_cls = cls


cdef NewArray(NDArrayHandle handle, int stype=-1):
def _set_np_ndarray_class(cls):
global _np_ndarray_cls
_np_ndarray_cls = cls


cdef NewArray(NDArrayHandle handle, int is_np_op, int stype=-1):
"""Create a new array given handle"""
return _ndarray_cls(_ctypes.cast(<unsigned long long>handle, _ctypes.c_void_p), stype=stype)
if is_np_op:
return _np_ndarray_cls(_ctypes.cast(<unsigned long long>handle, _ctypes.c_void_p), stype=stype)
else:
return _ndarray_cls(_ctypes.cast(<unsigned long long>handle, _ctypes.c_void_p), stype=stype)


cdef class CachedOp:
"""Cached operator handle."""
cdef CachedOpHandle chandle

cdef _set_handle(self, handle):
cdef unsigned long long ptr
if handle is None:
Expand All @@ -95,6 +103,7 @@ cdef class CachedOp:
return _ctypes.cast(<unsigned long long>self.chandle, _ctypes.c_void_p)
def __set__(self, value):
self._set_handle(value)
cdef int is_np_sym

def __init__(self, sym, flags=()):
cdef vector[string] s_flag_keys
Expand All @@ -106,6 +115,9 @@ cdef class CachedOp:
cdef vector[const char*] c_flag_keys = SVec2Ptr(s_flag_keys)
cdef vector[const char*] c_flag_vals = SVec2Ptr(s_flag_vals)

from ..symbol.numpy._symbol import _Symbol
self.is_np_sym = bool(isinstance(sym, _Symbol))

CALL(MXCreateCachedOpEx(
<SymbolHandle>(<unsigned long long>sym.handle.value),
len(flags),
Expand Down Expand Up @@ -154,12 +166,12 @@ cdef class CachedOp:
if original_output is not None:
return original_output
if num_output == 1:
return NewArray(p_output_vars[0], p_output_stypes[0])
return NewArray(p_output_vars[0], self.is_np_sym, p_output_stypes[0])
else:
return [NewArray(p_output_vars[i], p_output_stypes[i]) for i in range(num_output)]
return [NewArray(p_output_vars[i], self.is_np_sym, p_output_stypes[i]) for i in range(num_output)]


def _imperative_invoke(handle, ndargs, keys, vals, out):
def _imperative_invoke(handle, ndargs, keys, vals, out, is_np_op):
"""cython implementation of imperative invoke wrapper"""
cdef unsigned long long ihandle = handle
cdef OpHandle chandle = <OpHandle>ihandle
Expand Down Expand Up @@ -211,6 +223,6 @@ def _imperative_invoke(handle, ndargs, keys, vals, out):
if original_output is not None:
return original_output
if num_output == 1:
return NewArray(p_output_vars[0], p_output_stypes[0])
return NewArray(p_output_vars[0], is_np_op, p_output_stypes[0])
else:
return [NewArray(p_output_vars[i], p_output_stypes[i]) for i in range(num_output)]
return [NewArray(p_output_vars[i], is_np_op, p_output_stypes[i]) for i in range(num_output)]
18 changes: 14 additions & 4 deletions python/mxnet/cython/symbol.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,29 @@ cdef SymbolSetAttr(SymbolHandle handle, dict kwargs):


_symbol_cls = SymbolBase
_np_symbol_cls = None

def _set_symbol_class(cls):
global _symbol_cls
_symbol_cls = cls

cdef NewSymbol(SymbolHandle handle):

def _set_np_symbol_class(cls):
global _np_symbol_cls
_np_symbol_cls = cls


cdef NewSymbol(SymbolHandle handle, int is_np_op):
"""Create a new symbol given handle"""
sym = _symbol_cls(None)
if is_np_op:
sym = _np_symbol_cls(None)
else:
sym = _symbol_cls(None)
(<SymbolBase>sym).chandle = handle
return sym


def _symbol_creator(handle, args, kwargs, keys, vals, name):
def _symbol_creator(handle, args, kwargs, keys, vals, name, is_np_op):
cdef unsigned long long ihandle = handle
cdef OpHandle chandle = <OpHandle>ihandle
cdef vector[string] ckeys
Expand Down Expand Up @@ -143,4 +153,4 @@ def _symbol_creator(handle, args, kwargs, keys, vals, name):
&csym_keys[0] if csym_keys.size() != 0 else NULL,
&sym_args[0] if sym_args.size() != 0 else NULL))

return NewSymbol(ret_handle)
return NewSymbol(ret_handle, is_np_op)

0 comments on commit eac2130

Please sign in to comment.