|
| 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>); |
0 commit comments