Skip to content

Commit 5972990

Browse files
authored
Merge pull request #11523 from mozga-intel/mozga-intel/Gausian_random_mkldnn_layout
MKLDNN layout: Gaussian random layout
2 parents 64045c2 + 7b9aa60 commit 5972990

File tree

4 files changed

+112
-3
lines changed

4 files changed

+112
-3
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
namespace paddle {
19+
namespace operators {
20+
21+
using framework::DataLayout;
22+
template <typename T>
23+
class GaussianMKLDNNKernel : public paddle::framework::OpKernel<T> {
24+
public:
25+
void Compute(const framework::ExecutionContext& context) const override {
26+
float mean = context.Attr<float>("mean");
27+
float std = context.Attr<float>("std");
28+
auto* tensor = context.Output<framework::Tensor>("Out");
29+
T* data = tensor->mutable_data<T>(context.GetPlace());
30+
31+
unsigned int seed = static_cast<unsigned int>(context.Attr<int>("seed"));
32+
std::minstd_rand engine;
33+
if (seed == 0) {
34+
seed = std::random_device()();
35+
}
36+
engine.seed(seed);
37+
std::normal_distribution<T> dist(mean, std);
38+
int64_t size = tensor->numel();
39+
for (int64_t i = 0; i < size; ++i) {
40+
data[i] = dist(engine);
41+
}
42+
43+
// The format of output is set as the mkldnn's format
44+
// TODO(@mozga-intel) The format of matrix sets inside the another layers.
45+
tensor->set_layout(DataLayout::kMKLDNN);
46+
tensor->set_format(mkldnn::memory::format::oihw);
47+
}
48+
};
49+
} // namespace operators
50+
} // namespace paddle
51+
52+
namespace ops = paddle::operators;
53+
54+
REGISTER_OP_KERNEL(gaussian_random, MKLDNN, ::paddle::platform::CPUPlace,
55+
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
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright (c) 2018 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+
import unittest
16+
17+
from test_gaussian_random_op import TestGaussianRandomOp
18+
19+
20+
class TestMKLDNN(TestGaussianRandomOp):
21+
def init_kernel_type(self):
22+
self.use_mkldnn = True
23+
24+
25+
if __name__ == '__main__':
26+
unittest.main()

python/paddle/fluid/tests/unittests/test_gaussian_random_op.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ class TestGaussianRandomOp(unittest.TestCase):
2525
def setUp(self):
2626
self.op_type = "gaussian_random"
2727
self.inputs = {}
28-
self.attrs = {"shape": [1000, 784], "mean": .0, "std": 1., "seed": 10}
28+
self.use_mkldnn = False
29+
self.init_kernel_type()
30+
self.attrs = {
31+
"shape": [1000, 784],
32+
"mean": .0,
33+
"std": 1.,
34+
"seed": 10,
35+
"use_mkldnn": self.use_mkldnn
36+
}
2937

3038
self.outputs = ["Out"]
3139

@@ -58,6 +66,9 @@ def gaussian_random_test(self, place):
5866
self.assertAlmostEqual(numpy.mean(tensor), .0, delta=0.1)
5967
self.assertAlmostEqual(numpy.std(tensor), 1., delta=0.1)
6068

69+
def init_kernel_type(self):
70+
pass
71+
6172

6273
if __name__ == "__main__":
6374
unittest.main()

0 commit comments

Comments
 (0)