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

Add nose #29

Merged
merged 4 commits into from
Aug 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ Debug
# Emacs
.clang_complete
.dir-locals.el
__pycache__
*.pkl
*
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ env:
- TASK=lint LINT_LANG=python
- TASK=doc
- TASK=build CXX=g++
- TASK=python CXX=g++
- TASK=python3 CXX=g++
- TASK=unittest_gtest CXX=g++

# dependent apt packages
Expand All @@ -27,20 +29,25 @@ addons:
- g++-4.8
- clang
- python-numpy
- python-nose
- python3-numpy
- python3-dev
- python3-nose


before_install:
- export NVCC_PREFIX=${HOME}
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi
- scripts/build_dmlc.sh
- export TRAVIS=dmlc-core/scripts/travis
- export PYTHONPATH=${PYTHONPATH}:${PWD}/python
- source ${TRAVIS}/travis_setup_env.sh


install:
- pip install cpplint pylint --user `whoami`
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi


script:
- scripts/travis_script.sh

Expand Down
2 changes: 1 addition & 1 deletion doc/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -1925,7 +1925,7 @@ PERLMOD_MAKEVAR_PREFIX =
# C-preprocessor directives found in the sources and include files.
# The default value is: YES.

ENABLE_PREPROCESSING = YES
ENABLE_PREPROCESSING = NO

# If the MACRO_EXPANSION tag is set to YES doxygen will expand all macro names
# in the source code. If set to NO only conditional compilation will be
Expand Down
1 change: 1 addition & 0 deletions doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Contents
--------
* [Python User Guide](python/python_guide.md)
* [C++ Developer Guide](cpp/cpp_guide.md)
* [Doxygen Version of C++ API](https://mxnet.readthedocs.org/en/latest/doxygen)

Indices and tables
------------------
Expand Down
2 changes: 1 addition & 1 deletion python/mxnet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# pylint: disable=invalid-name, protected-access
# coding: utf-8
"""MXNet: a concise, fast and flexible framework for deep learning

Expand All @@ -10,6 +9,7 @@
from __future__ import absolute_import

from .context import Context, current_context
from .base import MXNetError
from . import narray
from . import symbol

Expand Down
19 changes: 14 additions & 5 deletions python/mxnet/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
import platform
import numpy as np

__all__ = ['MXNetError']
#----------------------------
# library loading
#----------------------------
if sys.version_info[0] == 3:
string_types = str,
# this function is needed for python3
# to convert ctypes.char_p .value back to python str
py_str = lambda x: x.decode('utf-8')
else:
string_types = basestring,
py_str = lambda x: x


class MXNetError(Exception):
Expand Down Expand Up @@ -86,8 +91,7 @@ def check_call(ret):
return value from API calls
"""
if ret != 0:
raise MXNetError(_LIB.MXGetLastError())

raise MXNetError(py_str(_LIB.MXGetLastError()))

def c_str(string):
"""Create ctypes char * from a python string
Expand All @@ -98,7 +102,8 @@ def c_str(string):

Returns
-------
a char pointer that can be passed to C API
str : c_char_p
A char pointer that can be passed to C API
"""
return ctypes.c_char_p(string.encode('utf-8'))

Expand All @@ -116,7 +121,8 @@ def c_array(ctype, values):

Returns
-------
created ctypes array
out : ctypes array
Created ctypes array
"""
return (ctype * len(values))(*values)

Expand All @@ -136,7 +142,8 @@ def ctypes2numpy_shared(cptr, shape):

Returns
-------
a numpy array : numpy array
out : numpy_array
A numpy array : numpy array
"""
if not isinstance(cptr, ctypes.POINTER(mx_float)):
raise RuntimeError('expected float pointer')
Expand All @@ -145,3 +152,5 @@ def ctypes2numpy_shared(cptr, shape):
size *= s
dbuffer = (mx_float * size).from_address(ctypes.addressof(cptr.contents))
return np.frombuffer(dbuffer, dtype=np.float32).reshape(shape)


53 changes: 25 additions & 28 deletions python/mxnet/narray.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# coding: utf-8
# pylint: disable=invalid-name, protected-access, too-many-locals, fixme, no-member
"""NArray interface of mxnet"""
from __future__ import absolute_import

import ctypes
import sys
from .base import _LIB
from .base import c_array
from .base import c_array, py_str
from .base import mx_uint, mx_float, NArrayHandle, FunctionHandle
from .base import ctypes2numpy_shared
from .base import check_call
Expand Down Expand Up @@ -49,7 +48,7 @@ class NArray(object):

NArray is basic ndarray/Tensor like data structure in mxnet.
"""

# pylint: disable= no-member
def __init__(self, handle):
"""initialize a new NArray

Expand Down Expand Up @@ -165,7 +164,7 @@ def copyto(self, other):
return NArray._copyto(self, out=hret)
else:
raise TypeError('copyto do not support type ' + type(other))

# pylint: enable= no-member

def create(shape, ctx=Context.default_ctx):
"""Create a new NArray, with specified shape.
Expand All @@ -181,10 +180,9 @@ def create(shape, ctx=Context.default_ctx):
"""
return NArray(handle=_new_alloc_handle(shape, ctx, False))


# pylint: disable=too-many-locals, invalid-name
def _make_narray_function(handle):
"""Create a NArray function from the FunctionHandle."""
# Constants for type masks.
NARRAY_ARG_BEFORE_SCALAR = 1
ACCEPT_EMPTY_MUTATE_TARGET = 1 << 2
# Get the property of NArray
Expand All @@ -193,12 +191,12 @@ def _make_narray_function(handle):
n_scalars = mx_uint()
n_mutate_vars = mx_uint()
type_mask = ctypes.c_int()
check_call(_LIB.MXFuncDescribe( \
handle, \
ctypes.byref(n_used_vars), \
ctypes.byref(n_scalars), \
ctypes.byref(n_mutate_vars), \
ctypes.byref(type_mask)))
check_call(_LIB.MXFuncDescribe(
handle,
ctypes.byref(n_used_vars),
ctypes.byref(n_scalars),
ctypes.byref(n_mutate_vars),
ctypes.byref(type_mask)))
n_mutate_vars = n_mutate_vars.value
n_used_vars = n_used_vars.value
n_scalars = n_scalars.value
Expand All @@ -220,19 +218,19 @@ def _make_narray_function(handle):
arg_types = ctypes.POINTER(ctypes.c_char_p)()
arg_descs = ctypes.POINTER(ctypes.c_char_p)()

check_call(_LIB.MXFuncGetInfo( \
handle, ctypes.byref(name), ctypes.byref(desc), \
ctypes.byref(num_args), \
ctypes.byref(arg_names), \
ctypes.byref(arg_types), \
ctypes.byref(arg_descs)))
func_name = name.value
check_call(_LIB.MXFuncGetInfo(
handle, ctypes.byref(name), ctypes.byref(desc),
ctypes.byref(num_args),
ctypes.byref(arg_names),
ctypes.byref(arg_types),
ctypes.byref(arg_descs)))
func_name = py_str(name.value)

param_str = []
for i in range(num_args.value):
ret = '%s : %s' % (arg_names[i], arg_types[i])
ret = '%s : %s' % (py_str(arg_names[i]), py_str(arg_types[i]))
if len(arg_descs[i]) != 0:
ret += '\n ' + arg_descs[i]
ret += '\n ' + py_str(arg_descs[i])
param_str.append(ret)

doc_str = ('%s\n\n' +
Expand All @@ -245,7 +243,7 @@ def _make_narray_function(handle):
'-------\n' +
'out : NArray\n'+
' The output of binary function.')
doc_str = doc_str % (desc.value, '\n'.join(param_str))
doc_str = doc_str % (py_str(desc.value), '\n'.join(param_str))

# Definition of internal functions.
def binary_narray_function(lhs, rhs, out=None):
Expand All @@ -258,11 +256,10 @@ def binary_narray_function(lhs, rhs, out=None):
if not accept_empty_mutate:
raise TypeError('argument out is required to call %s' % func_name)
out = NArray(_new_empty_handle())
check_call(_LIB.MXFuncInvoke( \
handle, \
c_array(NArrayHandle, (lhs.handle, rhs.handle)), \
c_array(mx_float, ()), \
c_array(NArrayHandle, (out.handle,))))
check_call(_LIB.MXFuncInvoke(handle,
c_array(NArrayHandle, (lhs.handle, rhs.handle)),
c_array(mx_float, ()),
c_array(NArrayHandle, (out.handle,))))
return out

def unary_narray_function(src, out=None):
Expand Down Expand Up @@ -327,7 +324,7 @@ def generic_narray_function(*args, **kwargs):
ret_function.__name__ = func_name
ret_function.__doc__ = doc_str
return ret_function

# pylint: enable=too-many-locals, invalid-name

def _init_narray_module():
"""List and add all the narray functions to current module."""
Expand Down
Loading