mean_op.h 3.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
L
liaogang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once
Y
Yi Wang 已提交
16 17
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
18 19 20 21 22 23
#include "paddle/fluid/framework/pten_utils.h"

// only can include the headers in paddle/top/api dirs
#include "paddle/pten/api/include/core.h"
#include "paddle/pten/api/include/math.h"
#include "paddle/pten/hapi/lib/utils/tensor_utils.h"
L
liaogang 已提交
24 25 26 27

namespace paddle {
namespace operators {

D
dongzhihong 已提交
28 29 30 31 32 33 34 35
using Tensor = framework::Tensor;
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenScalar = framework::EigenScalar<T, MajorType, IndexType>;
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenVector = framework::EigenVector<T, MajorType, IndexType>;

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/** [ Why still keep the original kernel implementation? ]
 *
 * Removal of the original kernel implementation and kernel registration needs
 * to ensure that the new kernel mechanism adapts to multiple sets of execution
 * mechanisms, including:
 *
 * 1. Executor and ParallelExecutor
 * 2. Dygraph OpBase (Tracer and Engine)
 * 3. New Executor
 * 4. Predictor
 * 5. NPU and XPU lack kernel and need to reuse CPU Kernel
 *
 * Removal of the original Kernel requires a more complete solution to ensure
 * that it will not affect the current execution system.
 * Currently, only the first two cases are adapted.
 *
 * The principle here is that the implementation in the kernel must reuse the
 * corresponding functions in the Tensor Operation library and cannot maintain
 * two copies of the code.
 */
Q
QI JUN 已提交
56
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
57
class MeanKernel : public framework::OpKernel<T> {
58
 public:
D
dongzhihong 已提交
59
  void Compute(const framework::ExecutionContext& context) const override {
60 61 62 63
    auto* x = context.Input<Tensor>("X");
    auto* out = context.Output<Tensor>("Out");
    auto& dev_ctx = context.device_context<DeviceContext>();
    out->mutable_data<T>(x->place());
L
liaogang 已提交
64

65 66
    auto pt_x = paddle::experimental::MakePtenDenseTensor(*x);
    auto pt_out = paddle::experimental::MakePtenDenseTensor(*out);
L
liaogang 已提交
67

68 69
    // call new kernel
    pten::Mean<T>(dev_ctx, *pt_x.get(), pt_out.get());
L
liaogang 已提交
70 71 72
  }
};

Q
QI JUN 已提交
73
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
74
class MeanGradKernel : public framework::OpKernel<T> {
75
 public:
D
dongzhihong 已提交
76
  void Compute(const framework::ExecutionContext& context) const override {
77
    auto OG = context.Input<Tensor>(framework::GradVarName("Out"));
78 79 80 81 82
    PADDLE_ENFORCE_EQ(OG->numel(), 1UL,
                      platform::errors::InvalidArgument(
                          "Mean Gradient should be scalar. But received "
                          "Out@Grad's elements num is %d.",
                          OG->numel()));
83
    auto IG = context.Output<Tensor>(framework::GradVarName("X"));
Y
Yu Yang 已提交
84 85
    IG->mutable_data<T>(context.GetPlace());

86
    T ig_size = static_cast<T>(IG->numel());
C
chengduo 已提交
87
    Eigen::DSizes<int, 1> bcast(static_cast<int>(ig_size));
Q
QI JUN 已提交
88 89
    EigenVector<T>::Flatten(*IG).device(
        *context.template device_context<DeviceContext>().eigen_device()) =
90
        (EigenVector<T>::From(*OG) / ig_size).broadcast(bcast);
Y
Yu Yang 已提交
91 92 93
  }
};

L
liaogang 已提交
94 95
}  // namespace operators
}  // namespace paddle