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 winstywang/master
Browse files Browse the repository at this point in the history
rename
  • Loading branch information
winstywang committed Jul 17, 2015
2 parents 93f0801 + a5b8e48 commit 8eb61be
Show file tree
Hide file tree
Showing 19 changed files with 395 additions and 161 deletions.
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ endif

#BIN = test/test_threaded_engine test/api_registry_test
BIN = test/api_registry_test
OBJ = storage.o narray_op_cpu.o operator.o operator_cpu.o
OBJ = storage.o narray_op_cpu.o static_operator.o static_operator_cpu.o
# add threaded engine after it is done
OBJCXX11 = engine.o narray.o mxnet_api.o api_registry.o symbol.o
OBJCXX11 = engine.o narray.o mxnet_api.o api_registry.o symbol.o operator.o
CUOBJ =
SLIB = api/libmxnet.so
ALIB = api/libmxnet.a
LIB_DEP = $(DMLC_CORE)/libdmlc.a

ifeq ($(USE_CUDA), 1)
CUOBJ += narray_op_gpu.o operator_gpu.o
CUOBJ += narray_op_gpu.o static_operator_gpu.o
endif

.PHONY: clean all test lint doc
Expand All @@ -81,12 +81,13 @@ engine.o: src/dag_engine/simple_engine.cc
narray.o: src/narray/narray.cc
narray_op_cpu.o: src/narray/narray_op_cpu.cc src/narray/narray_op-inl.h
narray_op_gpu.o: src/narray/narray_op_gpu.cu src/narray/narray_op-inl.h
operator.o: src/operator/operator.cc
operator_cpu.o: src/operator/operator_cpu.cc
operator_gpu.o: src/operator/operator_gpu.cu
static_operator.o: src/static_operator/static_operator.cc
static_operator_cpu.o: src/static_operator/static_operator_cpu.cc
static_operator_gpu.o: src/static_operator/static_operator_gpu.cu
symbol.o: src/symbol/symbol.cc
api_registry.o: src/api_registry.cc
mxnet_api.o: api/mxnet_api.cc
operator.o: src/operator/operator.cc

api/libmxnet.a: $(OBJ) $(OBJCXX11) $(CUOBJ)
api/libmxnet.so: $(OBJ) $(OBJCXX11) $(CUOBJ)
Expand Down
34 changes: 34 additions & 0 deletions include/mxnet/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,39 @@ typedef mshadow::gpu gpu;
typedef mshadow::index_t index_t;
/*! \brief data type that will be used to store ndarray */
typedef mshadow::default_real_t real_t;

/*! \brief option to pass into the forward function */
struct Option {
/*! \brief whether it is training phase*/
int is_train;
};
/*! \brief gradient request type the request can have */
enum GradReqType {
/*! \brief no operation, do not write gradient */
kNullOp = 0,
/*! \brief write gradient to provided space */
kWriteTo = 1,
/*! \brief same as kWriteTo, but provided space is same as space of input-data */
kWriteInplace = 2,
/*! \brief add to the provided space */
kAddTo = 3
};
/*! \brief input argument type of the operator have */
enum ArgType {
/*! \brief data argument */
kDataArg = 0,
/*! \brief weight argument */
kWeightArg = 1,
/*! \brief bias argument */
kBiasArg = 2
};
/*! \brief Property for engine schedule */
enum Property {
/*! \brief Op contains interanl state, won't influence engine schedule */
kContainInteralState = 1,
/*! \brief Op forward require random number, will influence engine schedule */
kForwardRequireRnd = 2,
};

} // namespace mxnet
#endif // MXNET_BASE_H_
4 changes: 4 additions & 0 deletions include/mxnet/narray.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class NArray {
if (is_none()) return;
DAGEngine::Get()->WaitForVar(ptr_->var);
}
/*! \return the associated DAG variable of the narray.*/
inline DAGEngine::Variable Var() const {
return ptr_->var;
}
/*!
* \brief set all the elements in narray to be scalar
* \param scalar the scalar to set
Expand Down
81 changes: 24 additions & 57 deletions include/mxnet/operator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*!
* Copyright (c) 2015 by Contributors
* \file operator.h
* \brief static operator interface of mxnet
* \brief operator interface of mxnet
* \author Naiyan Wang
*/
#ifndef MXNET_OPERATOR_H_
#define MXNET_OPERATOR_H_
Expand All @@ -10,6 +11,9 @@
#include <vector>
#include "./base.h"
#include "./tensor_blob.h"
#include "./static_operator.h"
#include "./narray.h"
#include "./dag_engine.h"

namespace mxnet {
/*!
Expand All @@ -19,65 +23,28 @@ namespace mxnet {
* This interface relies on pre-allocated memory in TBlob, the caller need to set
* the memory region in TBlob correctly before calling Forward and Backward
*
* \sa TBlob, TShape
* \sa Operator
*/
class Operator {
public:
/*! \brief option to pass into the forward function */
struct Option {
/*! \brief whether it is training phase*/
int is_train;
};
/*! \brief gradient request type the request can have */
enum GradReqType {
/*! \brief no operation, do not write gradient */
kNullOp = 0,
/*! \brief write gradient to provided space */
kWriteTo = 1,
/*! \brief same as kWriteTo, but provided space is same as space of input-data */
kWriteInplace = 2,
/*! \brief add to the provided space */
kAddTo = 3
};
/*! \brief input argument type of the operator have */
enum ArgType {
/*! \brief data argument */
kDataArg = 0,
/*! \brief weight argument */
kWeightArg = 1,
/*! \brief bias argument */
kBiasArg = 2
};
/*! \brief Property for engine schedule */
enum Property {
/*! \brief Op contains interanl state, won't influence engine schedule */
kContainInteralState = 1,
/*! \brief Op forward require random number, will influence engine schedule */
kForwardRequireRnd = 2,
};
Operator(StaticOperator* op, Context ctx);
/*!
* \brief get types of input argument of this oeprator
* \return a vector corresponding to type of each argument
* this order is same as the order of inputs in Forward, InferShape and Backward
*/
virtual std::vector<ArgType> DescribeArgs() const {
// default most of layers only have one data argument
return std::vector<ArgType>(1, kDataArg);
}
virtual std::vector<ArgType> DescribeArgs() const;
/*!
* \brief describe property of op
* \return a bit map in int
*/
virtual int DescribeProperty() const {
// default most of layer only conatin internal state
return kContainInteralState;
}
virtual int DescribeProperty() const;
/*!
* \brief set param for the operator from string
* \param name parameter name
* \param val string for configuration
*/
virtual void SetParam(const char *name, const char *val) {}
virtual void SetParam(const char *name, const char *val);
/*!
* \brief inter the shapes of outputs and unknown input arguments
* \param in_shape the shape of input arguments of the operator
Expand All @@ -92,7 +59,9 @@ class Operator {
* InferShape will modify the vector to fill output TShape
*/
virtual void InferShape(std::vector<TShape> *in_shape,
std::vector<TShape> *out_shape) = 0;
std::vector<TShape> *out_shape);

virtual void SetContext(Context ctx);
/*!
* \brief perform a forward operation of operator, save the output to TBlob
* \param opt option on Forward such as whether this is training phase
Expand All @@ -103,8 +72,8 @@ class Operator {
*/
virtual void Forward(Option opt,
RunContext ctx,
const std::vector<TBlob> &in_data,
const std::vector<TBlob> &out_data) = 0;
const std::vector<NArray> &in_data,
const std::vector<NArray> &out_data);
/*!
* \brief perform a backward operation of the operator to get the gradient
* \param ctx runtime context
Expand All @@ -117,17 +86,15 @@ class Operator {
* \sa GradReqType
*/
virtual void Backward(RunContext ctx,
const std::vector<TBlob> &grad_next,
const std::vector<TBlob> &in_data,
const std::vector<TBlob> &out_grad,
const std::vector<GradReqType> &req) = 0;
/*!
* \brief factory unction, create a new operator
* \param type the type of operator
* \param ctx the context device type of operator
* \return a pointer of Operator object
*/
static Operator *Create(const char *type, Context ctx);
const std::vector<NArray> &grad_next,
const std::vector<NArray> &in_data,
const std::vector<NArray> &out_grad,
const std::vector<GradReqType> &req);

private:
/* \brief the static operator */
StaticOperator* op;
Context global_ctx;
};
} // namespace mxnet
#endif // MXNET_OPERATOR_H_
101 changes: 101 additions & 0 deletions include/mxnet/static_operator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*!
* Copyright (c) 2015 by Contributors
* \file static_operator.h
* \brief static operator interface of mxnet
*/
#ifndef MXNET_STATIC_OPERATOR_H_
#define MXNET_STATIC_OPERATOR_H_
// this file will be seen by cuda, no c++11 for now
#include <dmlc/base.h>
#include <vector>
#include "./base.h"
#include "./tensor_blob.h"

namespace mxnet {
/*!
* \brief static StaticOperator interface (current interface have not yet todo with scheduler),
* StaticOperator is a stateful object that can be used to call forward and backprop
*
* This interface relies on pre-allocated memory in TBlob, the caller need to set
* the memory region in TBlob correctly before calling Forward and Backward
*
* \sa TBlob, TShape
*/
class StaticOperator {
public:
/*!
* \brief get types of input argument of this oeprator
* \return a vector corresponding to type of each argument
* this order is same as the order of inputs in Forward, InferShape and Backward
*/
virtual std::vector<ArgType> DescribeArgs() const {
// default most of layers only have one data argument
return std::vector<ArgType>(1, kDataArg);
}
/*!
* \brief describe property of op
* \return a bit map in int
*/
virtual int DescribeProperty() const {
// default most of layer only conatin internal state
return kContainInteralState;
}
/*!
* \brief set param for the StaticOperator from string
* \param name parameter name
* \param val string for configuration
*/
virtual void SetParam(const char *name, const char *val) {}
/*!
* \brief inter the shapes of outputs and unknown input arguments
* \param in_shape the shape of input arguments of the StaticOperator
* this should be of same length as the vector returned by DescribeArgs
* in_shape allows unknown elements, which are checked by shape.ndim() == 0.
* For unknown shapes, InferShape will try to fill in the correct Shape in in_shape
* For known shapes, InferShape will check shape consistency
*
* common practice: set the shape of data input, and usually weight's shape can be infered
*
* \param out_shape the shape of outputs of the StaticOperator
* InferShape will modify the vector to fill output TShape
*/
virtual void InferShape(std::vector<TShape> *in_shape,
std::vector<TShape> *out_shape) = 0;
/*!
* \brief perform a forward operation of StaticOperator, save the output to TBlob
* \param opt option on Forward such as whether this is training phase
* \param ctx runtime context
* \param in_data array of input data, it is const
* \param out_data array of output data,
* the space of TBlob in out_data must be pre-allocated with InferShape
*/
virtual void Forward(Option opt,
RunContext ctx,
const std::vector<TBlob> &in_data,
const std::vector<TBlob> &out_data) = 0;
/*!
* \brief perform a backward operation of the StaticOperator to get the gradient
* \param ctx runtime context
* \param grad_next the gradient value we get from output of the StaticOperator
* \param in_data the array of input data
* \param out_grad array of output gradient, there could be three possible TBlob
* in the each element in the array
* \param req request types of the gradient saving operation
* only inplace will change input data
* \sa GradReqType
*/
virtual void Backward(RunContext ctx,
const std::vector<TBlob> &grad_next,
const std::vector<TBlob> &in_data,
const std::vector<TBlob> &out_grad,
const std::vector<GradReqType> &req) = 0;
/*!
* \brief factory unction, create a new StaticOperator
* \param type the type of StaticOperator
* \param ctx the context device type of StaticOperator
* \return a pointer of StaticOperator object
*/
static StaticOperator *Create(const char *type, Context ctx);
};
} // namespace mxnet
#endif // MXNET_STATIC_OPERATOR_H_
Loading

0 comments on commit 8eb61be

Please sign in to comment.