6 #ifndef MXNET_NDARRAY_H_
7 #define MXNET_NDARRAY_H_
10 #include <dmlc/logging.h>
12 #include <dmlc/type_traits.h>
13 #include <dmlc/registry.h>
23 #if DMLC_USE_CXX11 == 0
24 #error "cxx11 was required for ndarray module"
43 bool delay_alloc =
false,
int dtype = mshadow::default_type_flag)
44 : ptr_(std::make_shared<Chunk>(shape.Size(), ctx, delay_alloc,
dtype)),
45 shape_(shape), offset_(0), dtype_(
dtype) {
55 : ptr_(std::make_shared<Chunk>(data, dev_id)), shape_(data.shape_), offset_(0),
56 dtype_(data.type_flag_) {
68 MSHADOW_TYPE_SWITCH(dtype_, DType, {
69 return TBlob(static_cast<DType*>(ptr_->shandle.dptr)
70 + offset_, shape_, ptr_->shandle.ctx.dev_mask());
78 return ptr_->shandle.ctx;
88 return ptr_.get() ==
nullptr;
119 void Save(dmlc::Stream *strm)
const;
125 bool Load(dmlc::Stream *strm);
229 CHECK(!
is_none()) <<
"NDArray is not initialized";
230 CHECK_GE(shape_[0], end) <<
"Slice end index out of range";
231 size_t length = shape_.ProdShape(1, shape_.ndim());
232 ret.offset_ += begin * length;
233 ret.shape_[0] = end - begin;
243 CHECK(!
is_none()) <<
"NDArray is not initialized";
244 CHECK_GE(shape_[0], idx) <<
"index out of range";
245 size_t length = shape_.ProdShape(1, shape_.ndim());
246 ret.offset_ += idx * length;
247 ret.shape_ =
TShape(shape_.data()+1, shape_.data()+shape_.ndim());
256 CHECK_GE(shape_.Size(), shape.Size())
257 <<
"NDArray.Reshape: target shape size is different from current shape";
267 ptr_->CheckAndAlloc();
275 static void Save(dmlc::Stream* fo,
276 const std::vector<NDArray>&
data,
277 const std::vector<std::string>& names);
284 static void Load(dmlc::Stream* fi,
285 std::vector<NDArray>*
data,
286 std::vector<std::string>* keys);
303 Chunk() : static_data(true), delay_alloc(false) {
311 if (data.dev_mask_ == cpu::kDevMask) {
314 CHECK_EQ(data.dev_mask_, gpu::kDevMask);
317 shandle.dptr = data.dptr_;
318 shandle.size = data.shape_.Size() * mshadow::mshadow_sizeof(data.type_flag_);
321 Chunk(uint64_t size, Context
ctx,
bool delay_alloc_,
int dtype)
322 : static_data(false), delay_alloc(true) {
324 shandle.size = size * mshadow::mshadow_sizeof(dtype);
337 if (static_data || delay_alloc) {
340 Storage::Handle h = this->shandle;
343 }, shandle.ctx,
var);
348 std::shared_ptr<Chunk> ptr_;
368 void CopyFromTo(
const NDArray &from, NDArray *to,
int priority = 0);
376 void ElementwiseSum(
const std::vector<NDArray> &source, NDArray *out,
int priority = 0);
384 NDArray
operator+(
const NDArray &lhs,
const NDArray &rhs);
398 NDArray
operator-(
const NDArray &lhs,
const NDArray &rhs);
412 NDArray
operator*(
const NDArray &lhs,
const NDArray &rhs); \
426 NDArray
operator/(
const NDArray &lhs,
const NDArray &rhs);
459 typedef std::function<void (NDArray **used_vars,
461 NDArray **mutate_vars,
483 :
public dmlc::FunctionRegEntryBase<NDArrayFunctionReg,
484 NDArrayAPIFunction> {
510 int num_params,
char **param_keys,
char **param_vals) {
511 (*fsetvalue)(s[0], mutate_vars[0]);
514 this->add_argument(
"src",
"real_t",
"Source input to the function.");
527 body = [fternary](
NDArray **used_vars,
529 int num_params,
char **param_keys,
char **param_vals) {
530 (*fternary)(*used_vars[0], *used_vars[1], *used_vars[2], mutate_vars[0]);
534 this->add_argument(
"lhs",
"NDArray",
"Left operand to the function.");
535 this->add_argument(
"mhs",
"NDArray",
"Middle operand to the function.");
536 this->add_argument(
"rhs",
"NDArray",
"Right operand to the function.");
549 int num_params,
char **param_keys,
char **param_vals) {
550 (*fbinary)(*used_vars[0], *used_vars[1], mutate_vars[0]);
554 this->add_argument(
"lhs",
"NDArray",
"Left operand to the function.");
555 this->add_argument(
"rhs",
"NDArray",
"Right operand to the function.");
568 int num_params,
char **param_keys,
char **param_vals) {
569 (*fscalar)(*used_vars[0], s[0], mutate_vars[0]);
573 this->add_argument(
"lhs",
"NDArray",
"Left operand to the function.");
574 this->add_argument(
"rhs",
"real_t",
"Right operand to the function.");
586 int num_params,
char **param_keys,
char **param_vals) {
587 (*funary)(*used_vars[0], mutate_vars[0]);
591 this->add_argument(
"src",
"NDArray",
"Source input to the function.");
601 void (*fgeneric)(
NDArray **used_vars,
604 const std::map<std::string, std::string>& param)) {
606 int num_params,
char **param_keys,
char **param_vals) {
607 std::map<std::string, std::string> param;
608 for (
int i = 0; i < num_params; ++i) {
609 param[param_keys[i]] = param_vals[i];
611 fgeneric(used_vars, s, mutate_vars, param);
660 #define MXNET_REGISTER_NDARRAY_FUN(name) \
661 DMLC_REGISTRY_REGISTER(::mxnet::NDArrayFunctionReg, NDArrayFunctionReg, name)
669 #endif // MXNET_NDARRAY_H_
bool Load(dmlc::Stream *strm)
load the content from binary stream
NDArrayFunctionReg & set_num_mutate_vars(unsigned n)
set the number of mutate variables
Definition: ndarray.h:628
Engine::VarHandle var() const
Definition: ndarray.h:112
void RandomSeed(uint32_t seed)
Seed the random number generator.
Engine that schedules all the operations according to dependency.
mshadow::TShape TShape
dynamic shape type
Definition: base.h:85
NDArray & operator/=(const NDArray &src)
elementwise division from current ndarray this mutate the current NDArray
void SyncCopyFromCPU(const void *data, size_t size) const
Do a synchronize copy from a continugous CPU memory region.
NDArrayFunctionReg()
constructor
Definition: ndarray.h:496
void PushSync(SyncFn exec_fn, Context exec_ctx, std::vector< VarHandle > const &const_vars, std::vector< VarHandle > const &mutable_vars, FnProperty prop=FnProperty::kNormal, int priority=0)
Push an synchronous operation to the engine.
Definition: engine.h:198
NDArray operator*(const NDArray &lhs, const NDArray &rhs)
elementwise multiplication
virtual void Free(Handle handle)=0
Free storage.
NDArray Slice(index_t begin, index_t end) const
Slice a NDArray.
Definition: ndarray.h:227
NDArrayFunctionReg & set_num_use_vars(unsigned n)
set the number of mutate variables
Definition: ndarray.h:620
DMLC_DECLARE_TRAITS(has_saveload, mxnet::NDArray, true)
traits
mshadow::default_real_t real_t
data type that will be used to store ndarray
Definition: base.h:82
int type_mask
information on how function should be called from API
Definition: ndarray.h:492
NDArray T() const
return transpose of current NDArray
NDArrayFunctionReg & set_function(void(*funary)(const NDArray &src, NDArray *out))
set the function body to a unary NDArray function this will also auto set the parameters correctly ...
Definition: ndarray.h:583
NDArrayFunctionReg & set_num_scalars(unsigned n)
set the number of scalar arguments
Definition: ndarray.h:636
mshadow::TBlob TBlob
storage container type
Definition: base.h:87
NDArray & operator+=(const NDArray &src)
elementwise add to current space this mutate the current NDArray
unsigned num_mutate_vars
number of variable mutated by this function
Definition: ndarray.h:488
execution time context. The information needed in runtime for actual execution.
Definition: base.h:181
NDArrayFunctionReg & set_function(void(*fscalar)(const NDArray &lhs, const real_t &rhs, NDArray *out))
set the function body to a binary NDArray function this will also auto set the parameters correctly ...
Definition: ndarray.h:564
void SyncCopyToCPU(void *data, size_t size) const
Do a synchronize copy to a continugous CPU memory region.
NDArray()
default cosntructor
Definition: ndarray.h:34
unsigned num_use_vars
number of variable used by this function
Definition: ndarray.h:486
NDArrayFunctionReg & set_function(void(*fternary)(const NDArray &lhs, const NDArray &mhs, const NDArray &rhs, NDArray *out))
set the function body to a ternary NDArray function this will also auto set the parameters correctly ...
Definition: ndarray.h:523
virtual Handle Alloc(size_t size, Context ctx)=0
Allocate a new contiguous memory for a given size.
NDArray Reshape(const TShape &shape) const
Get an reshaped NDArray.
Definition: ndarray.h:255
bool is_none() const
Definition: ndarray.h:87
all the scalar should go before use_vars
Definition: ndarray.h:470
virtual VarHandle NewVariable()=0
Allocate a new variable, the variable can then be used to schedule the operation concurrently via dep...
NDArray & operator=(real_t scalar)
set all the elements in ndarray to be scalar
whether this function allows the handles in the target to be empty NDArray that are not yet initializ...
Definition: ndarray.h:479
const TShape & shape() const
Definition: ndarray.h:61
virtual void WaitForVar(VarHandle var)=0
Wait for a variable.
NDArray & operator-=(const NDArray &src)
elementwise subtract from current ndarray this mutate the current NDArray
Context ctx() const
Definition: ndarray.h:77
void SampleGaussian(real_t mu, real_t sigma, NDArray *out)
Sample gaussian distribution for each elements of out.
Storage manager across multiple devices.
void WaitToRead() const
Block until all the pending write operations with respect to current NDArray are finished, and read can be performed.
Definition: ndarray.h:94
int dtype() const
Definition: ndarray.h:83
Storage handle.
Definition: storage.h:22
virtual void DeleteVariable(SyncFn delete_fn, Context exec_ctx, VarHandle var)=0
Schedule the deletion of a variable.
NDArrayFunctionReg & set_type_mask(int tmask)
set type mask
Definition: ndarray.h:644
TBlob data() const
Definition: ndarray.h:67
engine::VarHandle VarHandle
Variable pointer.
Definition: engine.h:83
void WaitToWrite() const
Block until all the pending read/write operations with respect to current NDArray are finished...
Definition: ndarray.h:102
NDArray operator-(const NDArray &lhs, const NDArray &rhs)
elementwise substraction
NDArrayFunctionReg & set_function(void(*fsetvalue)(const real_t &rhs, NDArray *out))
set the function body to a NDArray setvalue function this will also auto set the parameters correctly...
Definition: ndarray.h:507
NDArray operator+(const NDArray &lhs, const NDArray &rhs)
elementwise add
void SampleUniform(real_t begin, real_t end, NDArray *out)
Sample uniform distribution for each elements of out.
Registry entry for NDArrayFunction.
Definition: ndarray.h:482
NDArray At(index_t idx) const
Index a NDArray.
Definition: ndarray.h:241
NDArrayFunctionReg & set_function(void(*fbinary)(const NDArray &lhs, const NDArray &rhs, NDArray *out))
set the function body to a binary NDArray function this will also auto set the parameters correctly ...
Definition: ndarray.h:545
NDArray Copy(Context ctx) const
return a new copy this NDArray
configuation of mxnet as well as basic data structure.
NDArray & operator*=(const NDArray &src)
elementwise multiplication to current ndarray this mutate the current NDArray
all the use_vars should go before scalar
Definition: ndarray.h:468
unsigned num_scalars
number of scalars used by this function
Definition: ndarray.h:490
void Save(dmlc::Stream *strm) const
save the content into binary stream
void CheckAndAlloc() const
Allocate the space if it is delayed allocated. This is an internal function used by system that norma...
Definition: ndarray.h:266
mshadow::index_t index_t
index type usually use unsigned
Definition: base.h:80
static Context GPU(int32_t dev_id)
Context information about the execution enviroment.
Definition: base.h:90
ndarray interface
Definition: ndarray.h:31
void CopyFromTo(const NDArray &from, NDArray *to, int priority=0)
issue an copy operation from one NDArray to another the two ndarray can sit on different devices this...
NDArray(const TBlob &data, int dev_id)
constructing a static NDArray that shares data with TBlob Use with caution: allocate ONLY ONE NDArray...
Definition: ndarray.h:54
void ElementwiseSum(const std::vector< NDArray > &source, NDArray *out, int priority=0)
Perform elementwise sum over each data from source, store result into out.
std::function< void(NDArray **used_vars, real_t *scalars, NDArray **mutate_vars, int num_params, char **param_keys, char **param_vals)> NDArrayAPIFunction
definition of NDArray function
Definition: ndarray.h:464
NDArrayFunctionReg & set_function(void(*fgeneric)(NDArray **used_vars, real_t *s, NDArray **mutate_vars, const std::map< std::string, std::string > ¶m))
set the function body to a unary NDArray function this will also auto set the parameters correctly ...
Definition: ndarray.h:600
NDArray(const TShape &shape, Context ctx, bool delay_alloc=false, int dtype=mshadow::default_type_flag)
constructing a new dynamic NDArray
Definition: ndarray.h:42
NDArray operator/(const NDArray &lhs, const NDArray &rhs)
elementwise division
NDArrayFunctionTypeMask
mask information on how functions can be exposed
Definition: ndarray.h:466