fsp_op.h 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.

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
#include "paddle/fluid/framework/op_registry.h"
17 18
#include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/phi/kernels/funcs/math_function.h"
19 20 21 22 23 24 25 26

namespace paddle {
namespace operators {

template <typename DeviceContext, typename T>
class FSPOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
27 28 29
    auto* x = context.Input<phi::DenseTensor>("X");
    auto* y = context.Input<phi::DenseTensor>("Y");
    auto* output = context.Output<phi::DenseTensor>("Out");
30 31 32 33 34 35 36 37 38 39
    output->mutable_data<T>(context.GetPlace());
    auto x_dims = x->dims();
    auto y_dims = y->dims();

    auto batch_size = x_dims[0];
    auto x_channel = x_dims[1];
    auto y_channel = y_dims[1];
    auto height = x_dims[2];
    auto width = x_dims[3];

40
    auto blas = phi::funcs::GetBlas<DeviceContext, T>(context);
41

42
    phi::funcs::MatDescriptor x_mat_desc;
43 44 45 46
    x_mat_desc.height_ = x_channel;
    x_mat_desc.width_ = height * width;
    x_mat_desc.batch_size_ = batch_size;
    x_mat_desc.stride_ = x_channel * height * width;
47
    x_mat_desc.trans_ = false;
48

49
    phi::funcs::MatDescriptor y_mat_desc;
50 51 52 53 54 55
    y_mat_desc.height_ = height * width;
    y_mat_desc.width_ = y_channel;
    y_mat_desc.batch_size_ = batch_size;
    y_mat_desc.stride_ = y_channel * height * width;
    y_mat_desc.trans_ = true;

56 57 58 59 60 61
    blas.MatMul(*x,
                x_mat_desc,
                *y,
                y_mat_desc,
                static_cast<T>(1.0 / (height * width)),
                output,
62 63 64 65 66 67 68 69
                static_cast<T>(0.0));
  }
};

template <typename DeviceContext, typename T>
class FSPGradOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
70 71
    auto* d_x = context.Output<phi::DenseTensor>(framework::GradVarName("X"));
    auto* d_y = context.Output<phi::DenseTensor>(framework::GradVarName("Y"));
72 73 74
    if (d_x == nullptr && d_y == nullptr) {
      return;
    }
75 76
    auto* d_out =
        context.Input<phi::DenseTensor>(framework::GradVarName("Out"));
77 78 79 80 81 82 83
    auto d_out_dims = d_out->dims();
    auto batch_size = d_out_dims[0];
    auto x_channel = d_out_dims[1];
    auto y_channel = d_out_dims[2];
    int64_t h = 0;
    int64_t w = 0;

84 85
    auto blas = phi::funcs::GetBlas<DeviceContext, T>(context);
    phi::funcs::SetConstant<DeviceContext, T> set_zero;
86 87
    if (d_x != nullptr) {
      d_x->mutable_data<T>(context.GetPlace());
88 89
      set_zero(context.template device_context<DeviceContext>(),
               d_x,
90
               static_cast<T>(0));
91
      auto* y = context.Input<phi::DenseTensor>("Y");
92 93 94 95
      auto y_dims = y->dims();
      h = y_dims[2];
      w = y_dims[3];

96
      phi::funcs::MatDescriptor d_out_mat_desc;
97 98 99 100
      d_out_mat_desc.height_ = x_channel;
      d_out_mat_desc.width_ = y_channel;
      d_out_mat_desc.batch_size_ = batch_size;
      d_out_mat_desc.stride_ = x_channel * y_channel;
101
      d_out_mat_desc.trans_ = false;
102

103
      phi::funcs::MatDescriptor y_mat_desc;
104 105 106 107
      y_mat_desc.height_ = y_channel;
      y_mat_desc.width_ = h * w;
      y_mat_desc.batch_size_ = batch_size;
      y_mat_desc.stride_ = y_channel * h * w;
108
      y_mat_desc.trans_ = false;
109

110 111 112 113 114 115 116
      blas.MatMul(*d_out,
                  d_out_mat_desc,
                  *y,
                  y_mat_desc,
                  static_cast<T>(1.0 / (h * w)),
                  d_x,
                  static_cast<T>(0.0));
117 118 119 120
    }

    if (d_y != nullptr) {
      d_y->mutable_data<T>(context.GetPlace());
121 122
      set_zero(context.template device_context<DeviceContext>(),
               d_y,
123
               static_cast<T>(0));
124
      auto* x = context.Input<phi::DenseTensor>("X");
125 126 127 128
      auto x_dims = x->dims();
      h = x_dims[2];
      w = x_dims[3];

129
      phi::funcs::MatDescriptor d_out_mat_desc;
130 131 132 133 134 135
      d_out_mat_desc.height_ = y_channel;
      d_out_mat_desc.width_ = x_channel;
      d_out_mat_desc.batch_size_ = batch_size;
      d_out_mat_desc.stride_ = x_channel * y_channel;
      d_out_mat_desc.trans_ = true;

136
      phi::funcs::MatDescriptor x_mat_desc;
137 138 139 140
      x_mat_desc.height_ = x_channel;
      x_mat_desc.width_ = h * w;
      x_mat_desc.batch_size_ = batch_size;
      x_mat_desc.stride_ = x_channel * h * w;
141
      x_mat_desc.trans_ = false;
142

143 144 145 146 147 148 149
      blas.MatMul(*d_out,
                  d_out_mat_desc,
                  *x,
                  x_mat_desc,
                  static_cast<T>(1.0 / (h * w)),
                  d_y,
                  static_cast<T>(0.0));
150 151 152 153 154 155
    }
  }
};

}  // namespace operators
}  // namespace paddle