fsp_op.h 4.7 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
#include "paddle/pten/kernels/funcs/blas/blas.h"
18
#include "paddle/pten/kernels/funcs/math_function.h"
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

template <typename DeviceContext, typename T>
class FSPOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* x = context.Input<Tensor>("X");
    auto* y = context.Input<Tensor>("Y");
    auto* output = context.Output<Tensor>("Out");
    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 = pten::funcs::GetBlas<DeviceContext, T>(context);
43

44
    pten::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
    pten::funcs::MatDescriptor y_mat_desc;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    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;

    blas.MatMul(*x, x_mat_desc, *y, y_mat_desc,
                static_cast<T>(1.0 / (height * width)), output,
                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 {
    auto* d_x = context.Output<Tensor>(framework::GradVarName("X"));
    auto* d_y = context.Output<Tensor>(framework::GradVarName("Y"));
    if (d_x == nullptr && d_y == nullptr) {
      return;
    }
    auto* d_out = context.Input<Tensor>(framework::GradVarName("Out"));
    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;

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

92
      pten::funcs::MatDescriptor d_out_mat_desc;
93 94 95 96
      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;
97
      d_out_mat_desc.trans_ = false;
98

99
      pten::funcs::MatDescriptor y_mat_desc;
100 101 102 103
      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;
104
      y_mat_desc.trans_ = false;
105 106 107 108 109 110 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));
    }

    if (d_y != nullptr) {
      d_y->mutable_data<T>(context.GetPlace());
      set_zero(context.template device_context<DeviceContext>(), d_y,
               static_cast<T>(0));
      auto* x = context.Input<Tensor>("X");
      auto x_dims = x->dims();
      h = x_dims[2];
      w = x_dims[3];

119
      pten::funcs::MatDescriptor d_out_mat_desc;
120 121 122 123 124 125
      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;

126
      pten::funcs::MatDescriptor x_mat_desc;
127 128 129 130
      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;
131
      x_mat_desc.trans_ = false;
132 133 134 135 136 137 138 139 140

      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));
    }
  }
};

}  // namespace operators
}  // namespace paddle