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

Commit

Permalink
Merge pull request #4 from dmlc/master
Browse files Browse the repository at this point in the history
merge back
  • Loading branch information
hetong007 committed Oct 18, 2015
2 parents b2ab9e8 + dd37e67 commit fa782b0
Show file tree
Hide file tree
Showing 22 changed files with 307 additions and 130 deletions.
22 changes: 11 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8.7)
cmake_minimum_required(VERSION 2.8.7)

project(mxnet C CXX)

Expand Down Expand Up @@ -47,13 +47,13 @@ if(USE_OPENCV)
endif()

if(USE_OPENMP)
FIND_PACKAGE( OpenMP REQUIRED)
if(OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
FIND_PACKAGE( OpenMP REQUIRED)
if(OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
endif()

# cudnn detection
Expand Down Expand Up @@ -86,11 +86,11 @@ if(NOT MSVC)
# Only add c++11 flags and definitions after cuda compiling
add_definitions(-DDMLC_USE_CXX11)
add_definitions(-DMSHADOW_IN_CXX11)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c++0x")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c++0x")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
endif()

add_library(mxnet SHARED ${SOURCE})
add_library(mxnet SHARED ${SOURCE})
target_link_libraries(mxnet ${mshadow_LINKER_LIBS})
target_link_libraries(mxnet dmlccore)
target_link_libraries(mxnet pslite)
Expand Down
8 changes: 7 additions & 1 deletion R-package/R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ mx.io.extract <- function(iter, field) {
#' @param shuffle Whether shuffle the data
#'
#' @export
mx.io.arrayiter <- function(data, label=NULL,
mx.io.arrayiter <- function(data, label,
batch.size=128,
shuffle=FALSE) {
if (shuffle) {
unif.rnds <- as.array(mx.runif(c(length(label)), ctx=mx.cpu()));
} else {
unif.rnds <- mx.array(0)
}
mx.io.internal.arrayiter(as.array(data),
as.array(label),
unif.rnds,
batch.size,
shuffle)
}
17 changes: 11 additions & 6 deletions R-package/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
MXNet R-Package
===============
The MXNet R packages brings flexible and efficient GPU computing and deep learning to R.

You have find MXNet R Package! The MXNet R packages brings flexible and efficient GPU
computing and state-of-art deep learning to R.

- It enables you to write seamless tensor/matrix computation with multiple GPUs in R.
- It also enables you construct and customize the state-of-art deep learning models in R,
and apply them to tasks such as image classification and data science challenges.

Document
--------
Sounds exciting? This page contains links to all the related documents on R package.

Resources
---------
* [MXNet R Package Document](http://mxnet.readthedocs.org/en/latest/R-package/index.html)
- This is a online hosted documents for mxnet examples etc.
- Check this out for detailed documents, examples, installation guides.


Installation
------------
- First build ```../lib/libmxnet.so``` by following [Build Instruction](../doc/build.md)
- Type ```R CMD INSTALL R-package``` in the root folder.
Follow [Installation Guide](http://mxnet.readthedocs.org/en/latest/build.html)


8 changes: 4 additions & 4 deletions R-package/demo/basic_training.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ act2 <- mx.symbol.Activation(fc2, name="relu2", act_type="relu")
fc3 <- mx.symbol.FullyConnected(act2, name="fc3", num_hidden=10)
softmax <- mx.symbol.Softmax(fc3, name = "sm")

dtrain = mx.varg.io.MNISTIter(list(
dtrain = mx.io.MNISTIter(
image="data/train-images-idx3-ubyte",
label="data/train-labels-idx1-ubyte",
data.shape=c(784),
batch.size=batch.size,
flat=TRUE,
silent=0,
seed=10))
seed=10)

dtest = mx.varg.io.MNISTIter(list(
dtest = mx.io.MNISTIter(
image="data/t10k-images-idx3-ubyte",
label="data/t10k-labels-idx1-ubyte",
data.shape=c(784),
batch.size=batch.size,
shuffle=FALSE,
flat=TRUE,
silent=0))
silent=0)
# X is R's array, we load from mxnet's native iter structure, but you don't have to
X = mx.io.extract(dtrain, "data")
y = mx.io.extract(dtrain, "label")
Expand Down
14 changes: 2 additions & 12 deletions R-package/src/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ namespace R {

/*! \brief macro to be compatible with non c++11 env */
#if DMLC_USE_CXX11 == 0
#ifndef nullptr
#define nullptr NULL
#endif
#endif

/*!
* \brief Log that enables Stop and print message to R console
Expand Down Expand Up @@ -90,8 +92,6 @@ void SetSeed(int seed);
* \brief Base Movable class of MXNet Module object.
* This class will define several common functions.
* \tparam Class The class name of subclass
* \tparam HandleType The type of handle the object have.
* \tparam finalizer The free function used to delete the handle.
*/
template<typename Class>
class MXNetMovable {
Expand All @@ -116,15 +116,6 @@ class MXNetMovable {
protected:
/*! \brief default constructor */
MXNetMovable() : moved_(false) {}
/*!
* \brief the finalizer for Rcpp
* \param self the pointer to the class.
*/
inline static void Finalizer(Class* self) {
if (!static_cast<MXNetMovable<Class>*>(self)->moved_) {
self->DoFinalize();
}
}
/*!
* \brief Default implement to Move a existing R Class object to a new one.
* \param src The source R Object.
Expand All @@ -137,7 +128,6 @@ class MXNetMovable {
return Rcpp::internal::make_new_object(moved);
}

private:
/*! \brief Whether the object has been moved */
bool moved_;
};
Expand Down
1 change: 0 additions & 1 deletion R-package/src/executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ Executor::RObjectType Executor::Bind(const Symbol::RObjectType& symbol,
void Executor::InitRcppModule() {
using namespace Rcpp; // NOLINT(*)
class_<Executor>("MXExecutor")
.finalizer(&Executor::Finalizer)
.method("update.aux.arrays",
&Executor::UpdateAuxArray,
"Update auxilary states array of executor, this will mutate the executor")
Expand Down
11 changes: 5 additions & 6 deletions R-package/src/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,26 @@ class Executor : public MXNetMovable<Executor> {
static void InitRcppModule();
// destructor
~Executor() {
// delete can handle nullptr safely
delete out_arrays_;
delete arg_arrays_;
delete grad_arrays_;
delete aux_arrays_;

if (!this->moved_) {
MX_CALL(MXExecutorFree(handle_));
}
}

private:
// friend with symbol
friend class Symbol;
friend class MXNetMovable<Executor>;
// internal constructor, enable trivial operator=
Executor()
: out_arrays_(nullptr),
arg_arrays_(nullptr),
grad_arrays_(nullptr),
aux_arrays_(nullptr) {}

/*! \return a new Object that is moved from current one */
inline Executor* CreateMoveObject() {
Executor *moved = new Executor();
Expand All @@ -147,10 +150,6 @@ class Executor : public MXNetMovable<Executor> {
aux_arrays_ = nullptr;
return moved;
}
// finalizer that invoked on non-movable object
inline void DoFinalize() {
MX_CALL(MXExecutorFree(handle_));
}
/*!
* \brief Clone src into a new space.
* \param src source list of arrays to clone.
Expand Down
24 changes: 14 additions & 10 deletions R-package/src/io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@

namespace mxnet {
namespace R {
// Rcpp random wrapper
inline size_t RcppRandWrapper(const size_t n) {
return floor(unif_rand() * n);
}

void MXDataIter::Reset() {
MX_CALL(MXDataIterBeforeFirst(handle_));
Expand Down Expand Up @@ -44,14 +40,22 @@ Rcpp::List MXDataIter::Value() const {

ArrayDataIter::ArrayDataIter(const Rcpp::NumericVector& data,
const Rcpp::NumericVector& label,
const Rcpp::NumericVector& unif_rnds,
int batch_size,
bool shuffle) : counter_(0) {
std::vector<size_t> order(label.size());
for (size_t i = 0; i < order.size(); ++i) {
order[i] = i;
}

if (shuffle) {
std::random_shuffle(order.begin(), order.end(), RcppRandWrapper);
RCHECK(unif_rnds.size() == label.size());
for (size_t i = order.size() - 1; i != 0; --i) {
size_t idx = static_cast<size_t>(unif_rnds[i] * (i + 1));
if (idx < i) {
std::swap(order[i], order[idx]);
}
}
}
ArrayDataIter::Convert(data, order, batch_size, &data_);
ArrayDataIter::Convert(label, order, batch_size, &label_);
Expand Down Expand Up @@ -118,9 +122,11 @@ int ArrayDataIter::NumPad() const {

Rcpp::RObject ArrayDataIter::Create(const Rcpp::NumericVector& data,
const Rcpp::NumericVector& label,
const Rcpp::NumericVector& unif_rnds,
size_t batch_size,
bool shuffle) {
return Rcpp::internal::make_new_object(new ArrayDataIter(data, label, batch_size, shuffle));
return Rcpp::internal::make_new_object(
new ArrayDataIter(data, label, unif_rnds, batch_size, shuffle));
}

DataIterCreateFunction::DataIterCreateFunction
Expand Down Expand Up @@ -185,12 +191,10 @@ void DataIter::InitRcppModule() {
.method("num.pad", &DataIter::NumPad);

class_<MXDataIter>("MXNativeDataIter")
.derives<DataIter>("MXDataIter")
.finalizer(&MXDataIter::Finalizer);
.derives<DataIter>("MXDataIter");

class_<ArrayDataIter>("MXArrayDataIter")
.derives<DataIter>("MXDataIter")
.finalizer(&ArrayDataIter::Finalizer);
.derives<DataIter>("MXDataIter");

function("mx.io.internal.arrayiter", &ArrayDataIter::Create);
}
Expand Down
16 changes: 7 additions & 9 deletions R-package/src/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class MXDataIter : public DataIter {
virtual bool Next();
virtual int NumPad() const;
virtual Rcpp::List Value() const;
virtual ~MXDataIter() {
MX_CALL(MXDataIterFree(handle_));
}

private:
friend class DataIter;
Expand All @@ -76,10 +79,6 @@ class MXDataIter : public DataIter {
inline static Rcpp::RObject RObject(DataIterHandle handle) {
return Rcpp::internal::make_new_object(new MXDataIter(handle));
}
// finalizer that invoked on non-movable object
static void Finalizer(MXDataIter *iter) {
MX_CALL(MXDataIterFree(iter->handle_));
}
/*! \brief internal data iter handle */
DataIterHandle handle_;
};
Expand All @@ -100,11 +99,14 @@ class ArrayDataIter : public DataIter {
* \brief Construct a ArrayDataIter from data and label.
* \param data The data array.
* \param label The label array.
* \param unif_rnds Uniform [0,1] random number of same length as label.
* Only needed when shuffle=TRUE
* \param batch_size The size of the batch.
* \param shuffle Whether shuffle the data.
*/
ArrayDataIter(const Rcpp::NumericVector& data,
const Rcpp::NumericVector& label,
const Rcpp::NumericVector& unif_rnds,
int batch_size,
bool shuffle);
virtual void Reset() {
Expand All @@ -115,6 +117,7 @@ class ArrayDataIter : public DataIter {
virtual Rcpp::List Value() const;
static Rcpp::RObject Create(const Rcpp::NumericVector& data,
const Rcpp::NumericVector& label,
const Rcpp::NumericVector& unif_rnds,
size_t batch_size,
bool shuffle);

Expand All @@ -125,11 +128,6 @@ class ArrayDataIter : public DataIter {
const std::vector<size_t> &order,
size_t batch_size,
std::vector<NDArray> *out);
// finalizer that invoked on non-movable object
static void Finalizer(ArrayDataIter *iter) {
iter->data_.clear();
iter->label_.clear();
}
/*! \brief The counter */
size_t counter_;
/*! \brief number of pad instances*/
Expand Down
1 change: 0 additions & 1 deletion R-package/src/kvstore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ Rcpp::RObject KVStore::Create(const char *type) {
void KVStore::InitRcppModule() {
using namespace Rcpp; // NOLINT(*)
class_<KVStore>("MXKVStore")
.finalizer(&KVStore::Finalizer)
.method("init", &KVStore::Init)
.method("push", &KVStore::Push)
.method("pull", &KVStore::Pull)
Expand Down
7 changes: 4 additions & 3 deletions R-package/src/kvstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ class KVStore {
static Rcpp::RObject Create(const char *type);
/*! \brief initialize the R cpp Module */
static void InitRcppModule();
// destructor
~KVStore() {
MX_CALL(MXKVStoreFree(handle_));
}

private:
explicit KVStore(KVStoreHandle handle)
: handle_(handle), optimizer_set_(false) {}
static void Finalizer(KVStore *kv) {
MX_CALL(MXKVStoreFree(kv->handle_));
}
// the internal callback to kvstore.
NDArray CreateState(int index, const NDArray& weight) const;
/*! \brief internal KVStore handle */
Expand Down
1 change: 1 addition & 0 deletions R-package/src/ndarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class NDArray {
// operator overloading
inline NDArray& operator=(const NDArray& other) {
ptr_ = other.ptr_;
return *this;
}
inline NDBlob* operator->() {
return ptr_.get();
Expand Down
1 change: 0 additions & 1 deletion R-package/src/symbol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ SEXP SymbolFunction::operator() (SEXP* args) {
void Symbol::InitRcppModule() {
using namespace Rcpp; // NOLINT(*)
class_<Symbol>("MXSymbol")
.finalizer(&Symbol::Finalizer)
.method("debug.str", &Symbol::DebugStr,
"Return the debug string of internals of symbol")
.method("apply", &Symbol::Apply,
Expand Down
Loading

0 comments on commit fa782b0

Please sign in to comment.