CosSimOp.cpp 8.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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. */

#include "CosSimOp.h"
#include "paddle/math/Matrix.h"
#include "paddle/math/Vector.h"

namespace paddle {
X
xutianbing 已提交
20 21 22 23 24 25 26 27 28
/**
 * Cosine Similarity for CpuMatrix
 *
 * \param out_mat, output value, size: nSamples * 1.
 * \param in1_mat, input value 1, size: nSamples * dim.
 * \param in2_mat, input value 2, size: n2 * dim (n2 == 1 or n2 == nSamples).
 * \param scale, default 1.0
 *
 */
29
template <>
30 31 32
void CosSimForward<DEVICE_TYPE_CPU>(CpuMatrix& out_mat,
                                    const CpuMatrix& in1_mat,
                                    const CpuMatrix& in2_mat,
33
                                    real scale) {
34 35 36
  CHECK(out_mat.getData() && in1_mat.getData() && in2_mat.getData());
  size_t num_samples = out_mat.getHeight();
  size_t dim = in1_mat.getWidth();
37
  /// column vector [nSamples, 1]
38 39 40
  real* out = out_mat.getData();
  const real* x = in1_mat.getData();
  const real* y = in2_mat.getData();
41 42

  /// in2 might only have one row or full rows
43 44
  CHECK(in2_mat.getHeight() == 1LU || in2_mat.getHeight() == num_samples);
  size_t inc = (in2_mat.getHeight() == 1LU) ? 0 : dim;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  for (size_t i = 0; i < num_samples; ++i, x += dim, y += inc) {
    real square_sum_x = 0;
    real square_sum_y = 0;
    real xy = 0;
    for (size_t j = 0; j < dim; ++j) {
      square_sum_x += x[j] * x[j];
      square_sum_y += y[j] * y[j];
      xy += x[j] * y[j];
    }
    CHECK(square_sum_x > 0 && square_sum_y > 0);
    out[i] = scale * xy / (std::sqrt(square_sum_x) * std::sqrt(square_sum_y));
  }
}

/**
X
xutianbing 已提交
60 61 62 63 64 65 66
 * Cosine Similarity
 * for each row i,
 *   out[i] = scale * cos(input1[i], input2[i])
 *      = scale * <input1[i], input2[i]>/sqrt(|input1[i]|^2 * |input2[i]|^2)
 * when input2 only has one row, then for each row i,
 *   out[i] = cos(input1[i], input2[0])
 *
67 68 69 70 71 72 73 74 75 76 77
 * \param inputs[0] input matrix 1, size: nSamples * dim.
 * \param inputs[1] input matrix 2, size: n2 * dim (n2 == 1 or n2 == nSamples).
 * \param outputs[0] output matrix, size : nSamples * 1.
 */

template <DeviceType Device>
class CosSimForwardFunc : public FunctionBase {
  void init(const FuncConfig& config) override {
    scale_ = config.get<real>("scale");
  }

78
  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
79 80
    CHECK_EQ(inputs.size(), 2UL);
    CHECK_EQ(outputs.size(), 1UL);
81

82 83 84
    CHECK_EQ(inputs[0].shape().ndims(), 2UL);
    CHECK_EQ(inputs[1].shape().ndims(), 2UL);
    CHECK_EQ(outputs[0].shape().ndims(), 2UL);
85

86 87 88
    CHECK_EQ(inputs[0].shape()[0], outputs[0].shape()[0]);
    CHECK_EQ(inputs[0].shape()[1], inputs[1].shape()[1]);
    CHECK_EQ(outputs[0].shape()[1], 1UL);
89

90 91 92 93 94 95 96 97
    CHECK(outputs[0].data() && inputs[0].data() && inputs[1].data());

    CHECK_EQ(outputs[0].getArgType(), ASSIGN_TO);
    auto out_mat = outputs[0].matrix<Device>();
    const auto in1_mat = inputs[0].matrix<Device>();
    const auto in2_mat = inputs[1].matrix<Device>();

    CosSimForward<Device>(out_mat, in1_mat, in2_mat, scale_);
98 99 100 101 102 103
  }

private:
  real scale_;
};

X
xutianbing 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117
/**
 * Cosine Similarity Derivative for CpuMatrix
 *
 * \param in1_grad  forward input grad 1, size: nSamples * dim.
 * \param in2_grad  forward input grad 2,
 *                  size: n2 * dim (n2 == 1 or n2 == nSamples).
 *
 * \param out_grad  backward loss output grad, size : nSamples * 1.
 * \param out_val   forward output value, size: nSamples * 1.
 * \param in1_val   forward input value 1, size: nSamples * dim.
 * \param in2_val   forward input value 2,
 *                  size: n2 * dim (n2 == 1 or n2 == nSamples).
 * \param scale,    default 1.0
 */
118
template <>
119 120 121 122 123 124
void CosSimBackward<DEVICE_TYPE_CPU>(const CpuMatrix& out_grad,
                                     const CpuMatrix& out_val,
                                     const CpuMatrix& in1_val,
                                     const CpuMatrix& in2_val,
                                     CpuMatrix& in1_grad,
                                     CpuMatrix& in2_grad,
125
                                     real scale) {
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  CHECK(out_grad.getData() && out_val.getData() && in1_val.getData() &&
        in2_val.getData() && in1_grad.getData() && in2_grad.getData());
  CHECK_EQ(out_val.useGpu_, false) << "Matrix type are GPU, CPU required";

  const real* grad = out_grad.getData();
  const real* out = out_val.getData();
  const real* prev_out_x = in1_val.getData();
  const real* prev_out_y = in2_val.getData();
  real* prev_grad_x = in1_grad.getData();
  real* prev_grad_y = in2_grad.getData();

  size_t num_samples = out_grad.getHeight();
  size_t dim = in1_val.getWidth();
  CHECK_EQ(in2_val.getHeight(), in2_grad.getHeight());
  CHECK(in2_val.getHeight() == 1LU || in2_val.getHeight() == num_samples);
  size_t inc = (in2_val.getHeight() == 1LU) ? 0 : dim;
142 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 172 173 174 175 176 177 178 179
  for (size_t i = 0; i < num_samples; ++i,
              prev_out_x += dim,
              prev_out_y += inc,
              prev_grad_x += dim,
              prev_grad_y += inc) {
    real square_sum_x = 0;
    real square_sum_y = 0;
    real xy = 0;
    for (size_t j = 0; j < dim; ++j) {
      square_sum_x += prev_out_x[j] * prev_out_x[j];
      square_sum_y += prev_out_y[j] * prev_out_y[j];
      xy += prev_out_x[j] * prev_out_y[j];
    }
    CHECK(square_sum_x > 0 && square_sum_y > 0);
    if (xy == 0) {
      real reciprocal =
          1.0f / (std::sqrt(square_sum_x) * std::sqrt(square_sum_y));
      for (size_t j = 0; j < dim; ++j) {
        prev_grad_x[j] += scale * grad[i] * prev_out_y[j] * reciprocal;
        prev_grad_y[j] += scale * grad[i] * prev_out_x[j] * reciprocal;
      }
    } else {
      real reciprocal_xy = 1.0f / xy;
      real reciprocal_square_sum_x = 1.0f / square_sum_x;
      real reciprocal_square_sum_y = 1.0f / square_sum_y;
      for (size_t j = 0; j < dim; ++j) {
        prev_grad_x[j] +=
            out[i] * grad[i] * (prev_out_y[j] * reciprocal_xy -
                                prev_out_x[j] * reciprocal_square_sum_x);
        prev_grad_y[j] +=
            out[i] * grad[i] * (prev_out_x[j] * reciprocal_xy -
                                prev_out_y[j] * reciprocal_square_sum_y);
      }
    }
  }
}

/**
X
xutianbing 已提交
180 181
 * Cosine Similarity backward Derivative
 *
182 183
 * \param outputs[0] forward input grad 1, size: nSamples * dim.
 * \param outputs[1] forward input grad 2,
X
xutianbing 已提交
184 185 186 187 188 189 190
 *                  size: n2 * dim (n2 == 1 or n2 == nSamples).
 *
 * \param inputs[0] backward loss output grad, size : nSamples * 1.
 * \param inputs[1] forward output value, size: nSamples * 1.
 * \param inputs[2] forward input value 1, size: nSamples * dim.
 * \param inputs[3] forward input value 2,
 *                  size: n2 * dim (n2 == 1 or n2 == nSamples).
191 192 193 194 195 196 197
 */
template <DeviceType Device>
class CosSimBackwardFunc : public FunctionBase {
  void init(const FuncConfig& config) override {
    scale_ = config.get<real>("scale");
  }

198
  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
199 200
    CHECK_EQ(inputs.size(), 4UL);
    CHECK_EQ(outputs.size(), 2UL);
201
    /// dim of out_grad and out_val == 1, column vector
202 203
    CHECK_EQ(inputs[0].shape()[1], 1UL);
    CHECK_EQ(inputs[1].shape()[1], 1UL);
204
    /// nSamples of out_grad == out_val == in_val1 == in_grad1
205 206 207
    CHECK_EQ(inputs[1].shape()[0], inputs[0].shape()[0]);
    CHECK_EQ(inputs[0].shape()[0], inputs[0].shape()[0]);
    CHECK_EQ(outputs[0].shape()[0], inputs[0].shape()[0]);
208
    /// dim of in1_val1 == in_val2 == in_grad1 == in_grad2
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    CHECK_EQ(inputs[3].shape()[1], inputs[2].shape()[1]);
    CHECK_EQ(outputs[0].shape()[1], inputs[2].shape()[1]);
    CHECK_EQ(outputs[1].shape()[1], inputs[2].shape()[1]);

    CHECK(inputs[0].data() && inputs[1].data() && inputs[2].data() &&
          inputs[3].data() && outputs[0].data() && outputs[1].data());

    CHECK_EQ(outputs[0].getArgType(), ADD_TO);
    CHECK_EQ(outputs[1].getArgType(), ADD_TO);

    const auto out_grad = inputs[0].matrix<Device>();
    const auto out_val = inputs[1].matrix<Device>();
    const auto in1_val = inputs[2].matrix<Device>();
    const auto in2_val = inputs[3].matrix<Device>();
    auto in1_grad = outputs[0].matrix<Device>();
    auto in2_grad = outputs[1].matrix<Device>();

    CosSimBackward<Device>(
        out_grad, out_val, in1_val, in2_val, in1_grad, in2_grad, scale_);
228 229 230 231 232 233
  }

private:
  real scale_;
};

234
REGISTER_TYPED_FUNC(CosSimForward, CPU, CosSimForwardFunc);
235
REGISTER_TYPED_FUNC(CosSimBackward, CPU, CosSimBackwardFunc);
236
#ifdef PADDLE_WITH_CUDA
237
REGISTER_TYPED_FUNC(CosSimForward, GPU, CosSimForwardFunc);
238
REGISTER_TYPED_FUNC(CosSimBackward, GPU, CosSimBackwardFunc);
239 240
#endif
}  // namespace paddle