Skip to content

Commit 7011022

Browse files
committed
MKLDNN layouts: Gaussian random layout
1 parent a29cb4b commit 7011022

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include <string>
16+
#include "paddle/fluid/operators/mean_op.h"
17+
18+
#include "mkldnn.hpp"
19+
#include "paddle/fluid/framework/tensor.h"
20+
#include "paddle/fluid/operators/math/selected_rows_functor.h"
21+
#include "paddle/fluid/platform/device_context.h"
22+
#include "paddle/fluid/platform/mkldnn_helper.h"
23+
24+
#include "paddle/fluid/framework/eigen.h"
25+
namespace paddle {
26+
namespace operators {
27+
28+
using paddle::framework::Tensor;
29+
using paddle::platform::MKLDNNDeviceContext;
30+
using paddle::platform::MKLDNNMemDesc;
31+
using paddle::platform::CPUDeviceContext;
32+
33+
using mkldnn::memory; // Note: paddle has also "memory" namespace
34+
using mkldnn::primitive;
35+
using mkldnn::softmax_forward;
36+
using mkldnn::prop_kind;
37+
using mkldnn::stream;
38+
39+
using framework::DataLayout;
40+
template <typename T>
41+
class GaussianMKLDNNKernel : public paddle::framework::OpKernel<T> {
42+
public:
43+
void Compute(const framework::ExecutionContext& context) const override {
44+
float mean = context.Attr<float>("mean");
45+
float std = context.Attr<float>("std");
46+
auto* tensor = context.Output<framework::Tensor>("Out");
47+
T* data = tensor->mutable_data<T>(context.GetPlace());
48+
49+
unsigned int seed = static_cast<unsigned int>(context.Attr<int>("seed"));
50+
std::minstd_rand engine;
51+
if (seed == 0) {
52+
seed = std::random_device()();
53+
}
54+
engine.seed(seed);
55+
std::normal_distribution<T> dist(mean, std);
56+
int64_t size = tensor->numel();
57+
for (int64_t i = 0; i < size; ++i) {
58+
data[i] = dist(engine);
59+
}
60+
61+
// The format of output is set as the mkldnn's format
62+
// TODO(@mozga-intel) The format of matrix sets inside the another layers.
63+
tensor->set_layout(DataLayout::kMKLDNN);
64+
tensor->set_format(mkldnn::memory::format::Ohwi16o);
65+
}
66+
};
67+
} // namespace operators
68+
} // namespace paddle
69+
70+
namespace ops = paddle::operators;
71+
72+
REGISTER_OP_KERNEL(gaussian_random, MKLDNN, ::paddle::platform::CPUPlace,
73+
ops::GaussianMKLDNNKernel<float>);

paddle/fluid/operators/gaussian_random_op.cc

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ limitations under the License. */
1515
#include <random>
1616
#include "paddle/fluid/framework/op_registry.h"
1717

18+
#ifdef PADDLE_WITH_MKLDNN
19+
#include "paddle/fluid/platform/mkldnn_helper.h"
20+
#endif
21+
1822
namespace paddle {
1923
namespace operators {
2024

@@ -62,9 +66,20 @@ class GaussianRandomOp : public framework::OperatorWithKernel {
6266
protected:
6367
framework::OpKernelType GetExpectedKernelType(
6468
const framework::ExecutionContext& ctx) const override {
69+
framework::LibraryType library{framework::LibraryType::kPlain};
70+
framework::DataLayout layout{framework::DataLayout::kAnyLayout};
71+
72+
#ifdef PADDLE_WITH_MKLDNN
73+
if (library == framework::LibraryType::kPlain &&
74+
platform::CanMKLDNNBeUsed(ctx)) {
75+
library = framework::LibraryType::kMKLDNN;
76+
layout = framework::DataLayout::kMKLDNN;
77+
}
78+
#endif
79+
6580
return framework::OpKernelType(
6681
static_cast<framework::proto::VarType::Type>(ctx.Attr<int>("dtype")),
67-
ctx.device_context());
82+
ctx.device_context(), layout, library);
6883
}
6984
};
7085

@@ -95,7 +110,9 @@ class GaussianRandomOpMaker : public framework::OpProtoAndCheckerMaker {
95110
"(int, default 5(FP32)) "
96111
"Output data type.")
97112
.SetDefault(framework::proto::VarType::FP32);
98-
113+
AddAttr<bool>("use_mkldnn",
114+
"(bool, default false) Only used in mkldnn kernel")
115+
.SetDefault(false);
99116
AddComment(R"DOC(
100117
GaussianRandom Operator.
101118

0 commit comments

Comments
 (0)