fsp_op.h 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/* 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"
#include "paddle/fluid/operators/math/blas.h"
#include "paddle/fluid/operators/math/math_function.h"

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];

    auto blas = math::GetBlas<DeviceContext, T>(context);

    math::MatDescriptor x_mat_desc;
    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;

    math::MatDescriptor y_mat_desc;
    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;

    auto blas = math::GetBlas<DeviceContext, T>(context);
    math::SetConstant<DeviceContext, T> set_zero;
    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];

      math::MatDescriptor d_out_mat_desc;
      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;

      math::MatDescriptor y_mat_desc;
      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;

      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];

      math::MatDescriptor d_out_mat_desc;
      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;

      math::MatDescriptor x_mat_desc;
      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;

      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