mirror of
https://github.com/RustyRaptor/incubator-mxnet.git
synced 2026-12-06 09:15:17 -07:00
* python -> Python (fixed capitalization typo) * flavours -> flavors for internal consistency with American spellings. Changed 'good flabors' to specify imperative and symbolic programming, better not to tease the reader * Improved writing of 'History' section * Improved writing of 'History' section * fixed symbol.md doc which previously used deprecated / incorrect mxnet.sym.var instead of the real/working mxnet.sym.Variable * fixed typos and readability in parts of the README.md * removed awkward phrasing about 'flavours' * typo cleanup * Update NEWS.md Fixed typos * adding eval function for combining with , makes for quicker introspection in interactive interpreters * fixed docstrig * fixed docstring, cleaned up whitespace * clean up * Changed code to work with **kwargs, doesn't require dictionary explicitly. This also removes need for clumsy code to handle the case where there are no arguments * fixed too-long lines in the docstring * Update symbol.py * heavily revised docs in symbol.py * lightly revised content in ndarray.py * Update symbol.py * heavier pass for language on ndarray.py * fixed docstring line lengths per lint * fixed string continuation error * Improved docs for (alphabetically) model.py through visualization.py * fixing line-too-long pylint bugs * fixes pylint line length error * improved documentation further by making cosmetic edits to all other files in the Python API * edited module .py documents, also modified all .py docs to comply with numpy doc standard and to use double backticks around True, False, and None values * adding a few files that escaped last commit * Update name.py fixed speciry -> specify * changing binded to bound everywhere, warning for using old attribute name * debugging indentation error in base_module and Non-ASCII character in rtc.py * fixed pylint line-too-long errors * fixing docstring line in bucketing module * fixing docstring line in bucketing module * changed 'bound' back to 'binded' in code so that doc changes could be merged in without breaking code
92 lines
4.0 KiB
Python
92 lines
4.0 KiB
Python
"""Interface to runtime cuda kernel compile module."""
|
|
from __future__ import absolute_import
|
|
|
|
import ctypes
|
|
from .base import _LIB, NDArrayHandle, RtcHandle, mx_uint, c_array, check_call
|
|
|
|
class Rtc(object):
|
|
"""MXRtc object in mxnet.
|
|
This class allow you to write CUDA kernels in Python
|
|
and call them with NDArray.
|
|
|
|
Parameters
|
|
----------
|
|
name : str
|
|
Name of the kernel.
|
|
inputs : tuple of (str, mxnet.ndarray)
|
|
List of input names and ndarray.
|
|
outputs : tuple of (str, mxnet.ndarray)
|
|
List of output names and ndarray.
|
|
kernel : str
|
|
The actual kernel code.
|
|
Note that this is only the body of the kernel, i.e.
|
|
after { and before }. Rtc will decorate the kernel.
|
|
For example, if ``name = "mykernel"`` and
|
|
inputs = [('x', mx.nd.zeros((10,)))]
|
|
outputs = [('y', mx.nd.zeros((10,)))]
|
|
kernel = "y[threadIdx.x] = x[threadIdx.x];",
|
|
then the compiled kernel will be:
|
|
extern "C" __global__ mykernel(float *x, float *y) {
|
|
const int x_ndim = 1;
|
|
const int x_dims = { 10 };
|
|
const int y_ndim = 1;
|
|
const int y_dims = { 10 };
|
|
|
|
y[threadIdx.x] = x[threadIdx.x];
|
|
}
|
|
"""
|
|
def __init__(self, name, inputs, outputs, kernel):
|
|
self.handle = RtcHandle()
|
|
input_names = ctypes.cast(c_array(ctypes.c_char_p, [i[0] for i in inputs]),
|
|
ctypes.POINTER(ctypes.c_char_p))
|
|
output_names = ctypes.cast(c_array(ctypes.c_char_p, [i[0] for i in outputs]),
|
|
ctypes.POINTER(ctypes.c_char_p))
|
|
input_nds = ctypes.cast(c_array(NDArrayHandle, [i[1].handle for i in inputs]),
|
|
ctypes.POINTER(NDArrayHandle))
|
|
output_nds = ctypes.cast(c_array(NDArrayHandle, [i[1].handle for i in outputs]),
|
|
ctypes.POINTER(NDArrayHandle))
|
|
check_call(_LIB.MXRtcCreate(ctypes.c_char_p(name),
|
|
mx_uint(len(inputs)),
|
|
mx_uint(len(outputs)),
|
|
input_names,
|
|
output_names,
|
|
input_nds,
|
|
output_nds,
|
|
ctypes.c_char_p(kernel),
|
|
ctypes.byref(self.handle)))
|
|
|
|
def __del__(self):
|
|
check_call(_LIB.MXRtcFree(self.handle))
|
|
|
|
def push(self, inputs, outputs, grid_dims, block_dims):
|
|
"""Run the kernel.
|
|
|
|
Parameters
|
|
----------
|
|
inputs : list of NDArray
|
|
List of inputs. Can contain different NDArrays than those used for the constructor,
|
|
but its elements must have the same shapes and appear in the same order.
|
|
outputs : list of NDArray
|
|
List of outputs. Can contain different ndarrays than used for the constructor,
|
|
but must have the same shapes and appear in the same order.
|
|
grid_dims : tuple of 3 uint
|
|
Grid dimension for kernel launch.
|
|
block_dims : tuple of 3 uint
|
|
Block dimension for kernel launch.
|
|
"""
|
|
input_nds = ctypes.cast(c_array(NDArrayHandle, [i.handle for i in inputs]),
|
|
ctypes.POINTER(NDArrayHandle))
|
|
output_nds = ctypes.cast(c_array(NDArrayHandle, [i.handle for i in outputs]),
|
|
ctypes.POINTER(NDArrayHandle))
|
|
check_call(_LIB.MXRtcPush(self.handle,
|
|
mx_uint(len(inputs)),
|
|
mx_uint(len(outputs)),
|
|
input_nds,
|
|
output_nds,
|
|
mx_uint(grid_dims[0]),
|
|
mx_uint(grid_dims[1]),
|
|
mx_uint(grid_dims[2]),
|
|
mx_uint(block_dims[0]),
|
|
mx_uint(block_dims[1]),
|
|
mx_uint(block_dims[2])))
|