fsp_op.h 5.1 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

namespace paddle {
namespace operators {

23
using Tensor = phi::DenseTensor;
24 25 26 27 28

template <typename DeviceContext, typename T>
class FSPOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
29 30 31
    auto* x = context.Input<phi::DenseTensor>("X");
    auto* y = context.Input<phi::DenseTensor>("Y");
    auto* output = context.Output<phi::DenseTensor>("Out");
32 33 34 35 36 37 38 39 40 41
    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];

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

44
    phi::funcs::MatDescriptor x_mat_desc;
45 46 47 48
    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;
49
    x_mat_desc.trans_ = false;
50

51
    phi::funcs::MatDescriptor y_mat_desc;
52 53 54 55 56 57
    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;

58 59 60 61 62 63
    blas.MatMul(*x,
                x_mat_desc,
                *y,
                y_mat_desc,
                static_cast<T>(1.0 / (height * width)),
                output,
64 65 66 67 68 69 70 71
                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 {
72 73
    auto* d_x = context.Output<phi::DenseTensor>(framework::GradVarName("X"));
    auto* d_y = context.Output<phi::DenseTensor>(framework::GradVarName("Y"));
74 75 76
    if (d_x == nullptr && d_y == nullptr) {
      return;
    }
77 78
    auto* d_out =
        context.Input<phi::DenseTensor>(framework::GradVarName("Out"));
79 80 81 82 83 84 85
    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;

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

98
      phi::funcs::MatDescriptor d_out_mat_desc;
99 100 101 102
      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;
103
      d_out_mat_desc.trans_ = false;
104

105
      phi::funcs::MatDescriptor y_mat_desc;
106 107 108 109
      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;
110
      y_mat_desc.trans_ = false;
111

112 113 114 115 116 117 118
      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));
119 120 121 122
    }

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

131
      phi::funcs::MatDescriptor d_out_mat_desc;
132 133 134 135 136 137
      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;

138
      phi::funcs::MatDescriptor x_mat_desc;
139 140 141 142
      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;
143
      x_mat_desc.trans_ = false;
144

145 146 147 148 149 150 151
      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));
152 153 154 155 156 157
    }
  }
};

}  // namespace operators
}  // namespace paddle