File: cosine_embedding_criterion_op.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (85 lines) | stat: -rw-r--r-- 2,570 bytes parent folder | download | duplicates (2)
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
#include "caffe2/operators/cosine_embedding_criterion_op.h"

#include <algorithm>

#include "caffe2/utils/math.h"

namespace caffe2 {

template <>
bool CosineEmbeddingCriterionOp<CPUContext>::RunOnDevice() {
  auto& S = Input(0);
  auto& Y = Input(1);

  CAFFE_ENFORCE(
      S.numel() == Y.numel(),
      "The embedding and label should have the same size.");
  auto* output = Output(0, S.sizes(), at::dtype<float>());

  const float* Sdata = S.data<float>();
  const int* Ydata = Y.data<int>();
  float* output_data = output->template mutable_data<float>();
  for (int i = 0; i < S.numel(); ++i) {
    output_data[i] =
        Ydata[i] == 1 ? (1.f - Sdata[i]) : std::max(0.f, Sdata[i] - margin_);
  }
  return true;
}

template <>
bool CosineEmbeddingCriterionGradientOp<CPUContext>::RunOnDevice() {
  auto& S = Input(0);
  auto& Y = Input(1);
  auto& dOutput = Input(2);

  auto* dS = Output(0, S.sizes(), at::dtype<float>());

  const float* Sdata = S.data<float>();
  const int* Ydata = Y.data<int>();
  const float* dOutput_data = dOutput.data<float>();
  float* dSdata = dS->template mutable_data<float>();
  for (int i = 0; i < S.numel(); ++i) {
    dSdata[i] = dOutput_data[i] *
        (Ydata[i] == 1 ? -1.f : static_cast<float>(Sdata[i] >= margin_));
  }
  return true;
}

REGISTER_CPU_OPERATOR(
    CosineEmbeddingCriterion,
    CosineEmbeddingCriterionOp<CPUContext>);
REGISTER_CPU_OPERATOR(
    CosineEmbeddingCriterionGradient,
    CosineEmbeddingCriterionGradientOp<CPUContext>);

OPERATOR_SCHEMA(CosineEmbeddingCriterion)
    .NumInputs(2)
    .NumOutputs(1)
    .SetDoc(R"DOC(
CosineEmbeddingCriterion takes two inputs: the similarity value and
the label, and computes the elementwise criterion output as

  output = 1 - s,               if y == 1
           max(0, s - margin),  if y == -1
)DOC")
    .Input(0, "S", "The cosine similarity as a 1-dim TensorCPU.")
    .Input(1, "Y", "The label as a 1-dim TensorCPU with int value of 1 or -1.")
    .Output(0, "loss", "The output loss with the same dimensionality as S.");

OPERATOR_SCHEMA(CosineEmbeddingCriterionGradient).NumInputs(3).NumOutputs(1);

class GetCosineEmbeddingCriterionGradient : public GradientMakerBase {
  using GradientMakerBase::GradientMakerBase;
  vector<OperatorDef> GetGradientDefs() override {
    return SingleGradientDef(
        "CosineEmbeddingCriterionGradient",
        "",
        vector<string>{I(0), I(1), GO(0)},
        vector<string>{GI(0)});
  }
};
REGISTER_GRADIENT(
    CosineEmbeddingCriterion,
    GetCosineEmbeddingCriterionGradient);

} // namespace caffe2