Caffe2 - C++ API
A deep learning, cross platform ML framework
glu_op.cc
1 #include <math.h>
2 
3 #include "caffe2/operators/glu_op.h"
4 
5 namespace caffe2 {
6 
7 namespace {
8 float sigmoid(const float x) {
9  if (x >= 0) {
10  return 1. / (1. + exp(-x));
11  } else {
12  const float exp_x = exp(x);
13  return exp_x / (1 + exp_x);
14  }
15 }
16 } // namespace
17 
18 template <>
19 void GluOp<float, CPUContext>::ComputeGlu(
20  const int M,
21  const int split_dim,
22  const int N,
23  const float* Xdata,
24  float* Ydata) {
25  const int xStride = 2 * split_dim * N;
26  const int yStride = split_dim * N;
27  for (int i = 0; i < M; ++i) {
28  const int idx = i * xStride;
29  const int idy = i * yStride;
30  for (int j = 0; j < split_dim; ++j) {
31  const int jN = j * N;
32  const int jdx1 = idx + jN;
33  const int jdx2 = idx + (j + split_dim) * N;
34  const int jdy = idy + jN;
35  for (int k = 0; k < N; ++k) {
36  const float x1 = Xdata[jdx1 + k];
37  const float x2 = Xdata[jdx2 + k];
38  Ydata[jdy + k] = x1 * sigmoid(x2);
39  }
40  }
41  }
42 }
43 
44 OPERATOR_SCHEMA(Glu)
45  .NumInputs(1)
46  .NumOutputs(1)
47  .SetDoc(R"DOC(
48 Applies gated linear unit to the input Tensor X. The output Y is half the size
49 of the input X, so if the shape of X is [d1, d2, ..., N] shape of Y will be
50 [d1, d2, ..., dn/2] and Y(:dn-1, i) = GLU(X(:dn-1, i), X(:dn-1, i+N/2)) =
51 X(dn-1, i) * sigmoid(X(dn-1, i+N/2))
52 )DOC")
53  .Input(0, "X", "1D input tensor")
54  .Output(0, "Y", "1D output tensor");
55 
56 REGISTER_CPU_OPERATOR(Glu, GluOp<float, CPUContext>);
57 } // namespace caffe2
Copyright (c) 2016-present, Facebook, Inc.