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

Commit

Permalink
Change Interface of NDArray & TBlob for DLPack Compatible (#6345)
Browse files Browse the repository at this point in the history
* Change Interface of NDArray & TBlob for DLPack Compatible

Fix for cudnn operator

Fix cpp tests

* Update nnvm

* Fix for MKL mem

* Fix for windows macro

* Bump up version number to 0.10.1

* Update NDArray Save&Load

* trigger update

* Add test for legacy data load

* Use LegacyTShapeLoad

* trigger update

* Update tensor_blob.h
  • Loading branch information
ZihengJiang authored and piiswrong committed May 30, 2017
1 parent b35dc56 commit 05e0728
Show file tree
Hide file tree
Showing 52 changed files with 384 additions and 175 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Expand Up @@ -10,6 +10,9 @@
[submodule "nnvm"]
path = nnvm
url = https://github.com/dmlc/nnvm
[submodule "dlpack"]
path = dlpack
url = https://github.com/dmlc/dlpack
[submodule "cub"]
path = cub
url = https://github.com/NVlabs/cub
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -142,6 +142,7 @@ include_directories("mshadow")
include_directories("cub")
include_directories("nnvm/include")
include_directories("dmlc-core/include")
include_directories("dlpack/include")

if(NOT MSVC)
set(BEGIN_WHOLE_ARCHIVE -Wl,--whole-archive)
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Expand Up @@ -18,6 +18,10 @@ ifndef NNVM_PATH
NNVM_PATH = $(ROOTDIR)/nnvm
endif

ifndef DLPACK_PATH
DLPACK_PATH = $(ROOTDIR)/dlpack
endif

ifneq ($(USE_OPENMP), 1)
export NO_OPENMP = 1
endif
Expand Down Expand Up @@ -49,7 +53,7 @@ ifeq ($(DEBUG), 1)
else
CFLAGS += -O3 -DNDEBUG=1
endif
CFLAGS += -I$(ROOTDIR)/mshadow/ -I$(ROOTDIR)/dmlc-core/include -fPIC -I$(NNVM_PATH)/include -Iinclude $(MSHADOW_CFLAGS)
CFLAGS += -I$(ROOTDIR)/mshadow/ -I$(ROOTDIR)/dmlc-core/include -fPIC -I$(NNVM_PATH)/include -I$(DLPACK_PATH)/include -Iinclude $(MSHADOW_CFLAGS)
LDFLAGS = -pthread $(MSHADOW_LDFLAGS) $(DMLC_LDFLAGS)
ifeq ($(DEBUG), 1)
NVCCFLAGS = -std=c++11 -Xcompiler -D_FORCE_INLINES -g -G -O0 -ccbin $(CXX) $(MSHADOW_NVCCFLAGS)
Expand Down
2 changes: 1 addition & 1 deletion R-package/DESCRIPTION
@@ -1,7 +1,7 @@
Package: mxnet
Type: Package
Title: MXNet
Version: 0.10.0
Version: 0.10.1
Date: 2015-12-23
Author: Tianqi Chen, Qiang Kou, Tong He
Maintainer: Qiang Kou <qkou@umail.iu.edu>
Expand Down
1 change: 1 addition & 0 deletions dlpack
Submodule dlpack added at a6e09b
2 changes: 1 addition & 1 deletion include/mxnet/base.h
Expand Up @@ -87,7 +87,7 @@
/*! \brief minor version */
#define MXNET_MINOR 10
/*! \brief patch version */
#define MXNET_PATCH 0
#define MXNET_PATCH 1
/*! \brief mxnet version */
#define MXNET_VERSION (MXNET_MAJOR*10000 + MXNET_MINOR*100 + MXNET_PATCH)
/*! \brief helper for making version number */
Expand Down
2 changes: 1 addition & 1 deletion include/mxnet/c_api.h
Expand Up @@ -390,7 +390,7 @@ MXNET_DLL int MXNDArrayGetShape(NDArrayHandle handle,
const mx_uint **out_pdata);
/*!
* \brief get the content of the data in NDArray
* \param handle the handle to the narray
* \param handle the handle to the ndarray
* \param out_pdata pointer holder to get pointer of data
* \return 0 when success, -1 when failure happens
*/
Expand Down
47 changes: 29 additions & 18 deletions include/mxnet/ndarray.h
Expand Up @@ -57,10 +57,10 @@ class AutogradRuntime;
*/
class NDArray {
public:
/*! \brief default cosntructor */
/*! \brief default constructor */
NDArray() {
#if MKL_EXPERIMENTAL == 1
Mkl_mem_ = MKLMemHolder::create();
Mkl_mem_ = MKLMemHolder::create();
#endif
}
/*!
Expand All @@ -75,7 +75,7 @@ class NDArray {
: ptr_(std::make_shared<Chunk>(shape.Size(), ctx, delay_alloc, dtype)),
shape_(shape), offset_(0), dtype_(dtype), entry_({nullptr, 0, 0}) {
#if MKL_EXPERIMENTAL == 1
Mkl_mem_ = std::make_shared<MKLMemHolder>();
Mkl_mem_ = std::make_shared<MKLMemHolder>();
#endif
}
/*!
Expand All @@ -89,29 +89,32 @@ class NDArray {
: ptr_(std::make_shared<Chunk>(data, dev_id)), shape_(data.shape_), offset_(0),
dtype_(data.type_flag_), entry_({nullptr, 0, 0}) {
#if MKL_EXPERIMENTAL == 1
Mkl_mem_ = std::make_shared<MKLMemHolder>();
Mkl_mem_ = std::make_shared<MKLMemHolder>();
#endif
}
/*!
* \return the shape of current NDArray
*/
inline const TShape &shape() const {
inline const TShape& shape() const {
return shape_;
}
/*!
* \return the data TBlob
*/
inline TBlob data() const {
inline const TBlob& data() const {
CheckAndAlloc();
TBlob res;
#if MKL_EXPERIMENTAL == 1
MSHADOW_TYPE_SWITCH(dtype_, DType, {
res = TBlob(static_cast<DType*>(ptr_->shandle.dptr)
+ offset_, shape_, ptr_->shandle.ctx.dev_mask());
tblob_ = TBlob(static_cast<DType*>(ptr_->shandle.dptr) + offset_,
shape_, ptr_->shandle.ctx.dev_mask(), ptr_->shandle.ctx.dev_id, Mkl_mem_);
});
#else
MSHADOW_TYPE_SWITCH(dtype_, DType, {
tblob_ = TBlob(static_cast<DType*>(ptr_->shandle.dptr) + offset_,
shape_, ptr_->shandle.ctx.dev_mask(), ptr_->shandle.ctx.dev_id);
});
#if MKL_EXPERIMENTAL == 1
res.Mkl_mem_ = Mkl_mem_;
#endif
return res;
return tblob_;
}
/*!
* \return a chunk of raw data in TBlob
Expand All @@ -122,8 +125,8 @@ class NDArray {
TShape raw_shape(1);
raw_shape[0] = length;
MSHADOW_TYPE_SWITCH(dtype_, DType, {
res = TBlob(static_cast<DType*>(ptr_->shandle.dptr)
+ offset_ + offset, raw_shape, ptr_->shandle.ctx.dev_mask());
res = TBlob(static_cast<DType*>(ptr_->shandle.dptr) + offset_ + offset,
raw_shape, ptr_->shandle.ctx.dev_mask(), ptr_->shandle.ctx.dev_id);
});
#if MKL_EXPERIMENTAL == 1
res.Mkl_mem_ = Mkl_mem_;
Expand Down Expand Up @@ -326,7 +329,7 @@ class NDArray {
ptr_->CheckAndAlloc();
}
/*!
* \brief Save list of narray into the Stream.x
* \brief Save list of ndarray into the Stream.x
* \param fo The stream of output.
* \param data the NDArrays to be saved.
* \param names the name of the NDArray, optional, can be zero length.
Expand All @@ -335,7 +338,7 @@ class NDArray {
const std::vector<NDArray>& data,
const std::vector<std::string>& names);
/*!
* \brief Load list of narray into from the stream.
* \brief Load list of ndarray into from the stream.
* \param fi The stream of the input file.
* \param data the NDArrays to be loaded
* \param keys the name of the NDArray, if saved in the file.
Expand Down Expand Up @@ -368,10 +371,10 @@ class NDArray {
: static_data(true),
delay_alloc(false) {
var = Engine::Get()->NewVariable();
if (data.dev_mask_ == cpu::kDevMask) {
if (data.dev_mask() == cpu::kDevMask) {
shandle.ctx = Context::CPU();
} else {
CHECK_EQ(data.dev_mask_, gpu::kDevMask);
CHECK_EQ(data.dev_mask(), gpu::kDevMask);
shandle.ctx = Context::GPU(dev_id);
}
shandle.dptr = data.dptr_;
Expand Down Expand Up @@ -418,6 +421,14 @@ class NDArray {
int dtype_ = -1;
/*! \brief node entry for autograd */
autograd::AGNodeEntry entry_;
/*!
* \brief internal TBlob
* \note When user access tblob_ by some const methods like
* NDArray::data(), the dptr in tblob_ still need to be updated
* in case that allocation happens. So we make it mutable for
* this situation.
*/
mutable TBlob tblob_;
};

/*!
Expand Down

0 comments on commit 05e0728

Please sign in to comment.