squared_l2_distance_op.h 5.7 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
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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/framework/eigen.h"
#include "paddle/framework/op_registry.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
23
template <typename T, size_t D, int MajorType = Eigen::RowMajor,
24
          typename IndexType = Eigen::DenseIndex>
25
using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;
26 27
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
28
using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;
29 30 31 32 33

template <typename Place, typename T>
class SquaredL2DistanceKernel : public framework::OpKernel {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
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
    auto* input0 = context.Input<Tensor>("X");
    const int rank = framework::arity(input0->dims());
    switch (rank) {
      case 2:
        Operate<2>(context);
        break;
      case 3:
        Operate<3>(context);
        break;
      case 4:
        Operate<4>(context);
        break;
      case 5:
        Operate<5>(context);
        break;
      case 6:
        Operate<6>(context);
        break;
      default:
        // already asserted in SquaredL2DistanceOpMaker
        break;
    }
  }

 private:
  template <int Dims>
  void Operate(const framework::ExecutionContext& context) const {
61 62 63 64 65 66 67 68
    auto* input0 = context.Input<Tensor>("X");
    auto* input1 = context.Input<Tensor>("Y");
    auto* output0 = context.Output<Tensor>("sub_result");
    auto* output1 = context.Output<Tensor>("Out");

    output0->mutable_data<T>(context.GetPlace());
    output1->mutable_data<T>(context.GetPlace());

69 70 71
    auto X = EigenTensor<T, Dims>::From(*input0);
    auto Y = EigenTensor<T, Dims>::From(*input1);
    auto subResult = EigenTensor<T, Dims>::From(*output0);
72 73
    auto Z = EigenMatrix<T>::From(*output1);

74 75 76
    auto xDims = X.dimensions();
    auto yDims = Y.dimensions();

77
    auto place = context.GetEigenDevice<Place>();
78

79
    // buffer the substraction result
80 81 82 83 84 85 86 87 88
    if (yDims[0] == 1 && xDims[0] != yDims[0]) {
      auto yBroadcastDims = yDims;
      yBroadcastDims[0] = xDims[0];
      subResult.device(place) = X - Y.broadcast(yBroadcastDims);
    } else {
      subResult.device(place) = X - Y;
    }

    // create matrix view for substraction result
89
    const auto& subResMat = subResult.reshape(Eigen::array<int, 2>(
90
        {static_cast<int>(xDims[0]), static_cast<int>(X.size() / xDims[0])}));
91 92 93 94 95 96 97 98 99
    Z.device(place) = subResMat.pow(2).sum(Eigen::array<int, 1>({1}));
  }
};

template <typename Place, typename T>
class SquaredL2DistanceGradKernel : public framework::OpKernel {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* input0 = context.Input<Tensor>("sub_result");
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    const int rank = framework::arity(input0->dims());
    switch (rank) {
      case 2:
        Operate<2>(context);
        break;
      case 3:
        Operate<3>(context);
        break;
      case 4:
        Operate<4>(context);
        break;
      case 5:
        Operate<5>(context);
        break;
      case 6:
        Operate<6>(context);
        break;
      default:
        // already asserted in SquaredL2DistanceOpMaker
        break;
    }
  }
122

123 124 125 126 127 128 129
 private:
  template <int Dims>
  void Operate(const framework::ExecutionContext& context) const {
    auto* input0 = context.Input<Tensor>("sub_result");
    auto* OG = context.Input<Tensor>(framework::GradVarName("Out"));
    auto* XG = context.Output<Tensor>(framework::GradVarName("X"));
    auto* YG = context.Output<Tensor>(framework::GradVarName("Y"));
130

131
    auto subResult = EigenTensor<T, Dims>::From(*input0);
132 133
    auto outGrad = EigenMatrix<T>::From(*OG);

134
    auto subResDims = subResult.dimensions();
135 136 137 138
    int firstDim = static_cast<int>(subResDims[0]);
    int cols = subResult.size() / firstDim;
    const auto subResMat =
        subResult.reshape(Eigen::array<int, 2>({firstDim, cols}));
139 140 141

    // calculate gradient
    auto gradMat =
142
        2 * (outGrad.broadcast(Eigen::array<int, 2>({1, cols}))) * subResMat;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

    // propagate back to input
    auto eigenPlace = context.GetEigenDevice<Place>();
    if (XG != nullptr) {
      XG->mutable_data<T>(context.GetPlace());
      auto xGrad = EigenTensor<T, Dims>::From(*XG);
      // dimensions are same with subResult
      auto xGradMat = xGrad.reshape(Eigen::array<int, 2>({firstDim, cols}));
      xGradMat.device(eigenPlace) = gradMat;
    }
    if (YG != nullptr) {
      YG->mutable_data<T>(context.GetPlace());
      auto yGrad = EigenTensor<T, Dims>::From(*YG);
      auto dimsYGrad = yGrad.dimensions();
      auto yGradMat = yGrad.reshape(Eigen::array<int, 2>(
          {static_cast<int>(dimsYGrad[0]),
           static_cast<int>(yGrad.size() / dimsYGrad[0])}));

      PADDLE_ENFORCE(dimsYGrad[0] <= firstDim,
                     "First dimension of gradient must be greater or "
                     "equal than first dimension of target");

      if (dimsYGrad[0] == firstDim) {
        yGradMat.device(eigenPlace) = -1 * gradMat;
      } else {
        yGradMat.device(eigenPlace) =
            -1 * (gradMat.sum(Eigen::array<int, 2>({0})));
      }
    }
172 173 174 175 176
  }
};

}  // namespace operators
}  // namespace paddle