Skip to content

Commit 6588d0e

Browse files
committed
Update MKLDNN to 0.15, fix conv integration
1 parent 1c3e66b commit 6588d0e

File tree

5 files changed

+39
-23
lines changed

5 files changed

+39
-23
lines changed

cmake/external/mkldnn.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ ExternalProject_Add(
5454
${EXTERNAL_PROJECT_LOG_ARGS}
5555
DEPENDS ${MKLDNN_DEPENDS}
5656
GIT_REPOSITORY "https://github.com/01org/mkl-dnn.git"
57-
GIT_TAG "a29d8487a63afca3d5b8c5bbdbb473cf8ccc6e51"
57+
GIT_TAG "64e03a1939e0d526aa8e9f2e3f7dc0ad8d372944"
5858
PREFIX ${MKLDNN_SOURCES_DIR}
5959
UPDATE_COMMAND ""
6060
CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}

paddle/fluid/framework/tensor.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ size_t Tensor::memory_size() const {
3131
return holder_ == nullptr ? 0UL : holder_->size() - offset_;
3232
}
3333

34-
void* Tensor::mutable_data(platform::Place place, std::type_index type) {
34+
void* Tensor::mutable_data(platform::Place place, std::type_index type,
35+
int64_t requested_size) {
3536
if (holder_ != nullptr) {
3637
holder_->set_type(type);
3738
}
3839
PADDLE_ENFORCE_GE(numel(), 0,
3940
"When calling this method, the Tensor's numel must be "
4041
"equal or larger than zero. "
4142
"Please check Tensor::Resize has been called first.");
42-
int64_t size = numel() * SizeOfType(type);
43+
int64_t size = requested_size ? requested_size : numel() * SizeOfType(type);
4344
/* some versions of boost::variant don't have operator!= */
4445
if (holder_ == nullptr || !(holder_->place() == place) ||
4546
holder_->size() < size + offset_) {
@@ -68,10 +69,10 @@ void* Tensor::mutable_data(platform::Place place, std::type_index type) {
6869
offset_);
6970
}
7071

71-
void* Tensor::mutable_data(platform::Place place) {
72+
void* Tensor::mutable_data(platform::Place place, int64_t requested_size) {
7273
PADDLE_ENFORCE(this->holder_ != nullptr,
7374
"Cannot invoke mutable data if current hold nothing.");
74-
return mutable_data(place, holder_->type());
75+
return mutable_data(place, holder_->type(), requested_size);
7576
}
7677

7778
Tensor& Tensor::ShareDataWith(const Tensor& src) {

paddle/fluid/framework/tensor.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,24 @@ class Tensor {
8989
* @note If not exist, then allocation.
9090
*/
9191
template <typename T>
92-
T* mutable_data(platform::Place place);
92+
T* mutable_data(platform::Place place, int64_t requested_size = 0);
9393

94-
void* mutable_data(platform::Place place, std::type_index type);
94+
void* mutable_data(platform::Place place, std::type_index type,
95+
int64_t requested_size = 0);
9596

96-
void* mutable_data(platform::Place place);
97+
void* mutable_data(platform::Place place, int64_t requested_size = 0);
9798

9899
/**
99100
* @brief Return a pointer to mutable memory block.
100101
*
101-
* @param[in] dims The dimensions of the memory block.
102-
* @param[in] place The place of the memory block.
102+
* @param[in] dims The dimensions of the memory block.
103+
* @param[in] place The place of the memory block.
104+
* @param[in] requested_size The size of the block in bytes.
103105
*
104106
* @note If not exist, then allocation.
105107
*/
106108
template <typename T>
107-
T* mutable_data(DDim dims, platform::Place place);
109+
T* mutable_data(DDim dims, platform::Place place, int64_t requested_size = 0);
108110

109111
/*! Return the dimensions of the memory block. */
110112
const DDim& dims() const;

paddle/fluid/framework/tensor_impl.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,17 @@ inline T* Tensor::data() {
4646
}
4747

4848
template <typename T>
49-
inline T* Tensor::mutable_data(DDim dims, platform::Place place) {
49+
inline T* Tensor::mutable_data(DDim dims, platform::Place place,
50+
int64_t requested_size) {
5051
static_assert(std::is_pod<T>::value, "T must be POD");
5152
Resize(dims);
52-
return mutable_data<T>(place);
53+
return mutable_data<T>(place, requested_size);
5354
}
5455

5556
template <typename T>
56-
inline T* Tensor::mutable_data(platform::Place place) {
57+
inline T* Tensor::mutable_data(platform::Place place, int64_t requested_size) {
5758
static_assert(std::is_pod<T>::value, "T must be POD");
58-
return reinterpret_cast<T*>(mutable_data(place, typeid(T)));
59+
return reinterpret_cast<T*>(mutable_data(place, typeid(T), requested_size));
5960
}
6061

6162
inline Tensor ReshapeToMatrix(const Tensor& src, int num_col_dims) {

paddle/fluid/operators/conv_mkldnn_op.cc

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ class ConvMKLDNNHandler : public platform::MKLDNNHandler {
5353
key_ += "-BWD";
5454
}
5555

56+
size_t GetDstMemorySize() {
57+
return conv_pd_->dst_primitive_desc().get_size();
58+
}
59+
60+
size_t GetDiffWeightsMemorySize() {
61+
return conv_bwd_weights_pd_->diff_weights_primitive_desc().get_size();
62+
}
63+
64+
size_t GetDiffSourceMemorySize() {
65+
return conv_bwd_data_pd_->diff_src_primitive_desc().get_size();
66+
}
67+
5668
std::shared_ptr<mkldnn::memory> AcquireSrcMemoryFromWeightsPrimitive(
5769
const std::shared_ptr<mkldnn::memory> user_memory_p,
5870
std::vector<mkldnn::primitive>& pipeline) { // NOLINT
@@ -251,7 +263,6 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
251263

252264
const T* input_data = input->data<T>();
253265
const T* filter_data = filter->data<T>();
254-
T* output_data = output->mutable_data<T>(ctx.GetPlace());
255266

256267
PADDLE_ENFORCE(input->dims().size() == 4,
257268
"Input must be with 4 dimensions, i.e. NCHW");
@@ -306,6 +317,8 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
306317
auto user_weights_memory_p = handler.AcquireWeightsMemory(
307318
user_weights_md, to_void_cast<T>(filter_data));
308319

320+
T* output_data =
321+
output->mutable_data<T>(ctx.GetPlace(), handler.GetDstMemorySize());
309322
// create reorder primitive if the input format is not the preferred one
310323
auto src_memory_p =
311324
handler.AcquireSrcMemoryFromPrimitive(user_src_memory_p, pipeline);
@@ -393,13 +406,6 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> {
393406
T* input_grad_data = nullptr;
394407
T* filter_grad_data = nullptr;
395408

396-
if (input_grad) {
397-
input_grad_data = input_grad->mutable_data<T>(ctx.GetPlace());
398-
}
399-
if (filter_grad) {
400-
filter_grad_data = filter_grad->mutable_data<T>(ctx.GetPlace());
401-
}
402-
403409
std::vector<int> src_tz = paddle::framework::vectorize2int(input->dims());
404410
std::vector<int> weights_tz =
405411
paddle::framework::vectorize2int(filter->dims());
@@ -485,6 +491,9 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> {
485491
handler.AcquireDiffDstMemoryFromWeightsPrimitive(
486492
user_diff_dst_memory_p, pipeline);
487493

494+
size_t size = handler.GetDiffWeightsMemorySize();
495+
filter_grad_data = filter_grad->mutable_data<T>(ctx.GetPlace(), size);
496+
488497
auto diff_weights_memory_p =
489498
handler.AcquireDiffWeightsMemoryFromWeightsPrimitive(
490499
reinterpret_cast<void*>(filter_grad_data));
@@ -507,6 +516,9 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> {
507516
handler.AcquireDiffDstMemoryFromDataPrimitive(user_diff_dst_memory_p,
508517
pipeline);
509518

519+
size_t size = handler.GetDiffSourceMemorySize();
520+
input_grad_data = input_grad->mutable_data<T>(ctx.GetPlace(), size);
521+
510522
auto diff_src_memory_p = handler.AcquireDiffSrcMemoryFromDataPrimitive(
511523
reinterpret_cast<void*>(input_grad_data));
512524

0 commit comments

Comments
 (0)