pytorch/caffe2/operators/sinusoid_position_encoding_op.cc
Nikita Shulga a9b0a921d5 Disable avoid-non-const-global-variables lint check (#62008)
Summary:
As GoogleTest `TEST` macro is non-compliant with it as well as `DEFINE_DISPATCH`

All changes but the ones to `.clang-tidy` are generated using following script:
```
for i in `find . -type f -iname "*.c*" -or -iname "*.h"|xargs grep cppcoreguidelines-avoid-non-const-global-variables|cut -f1 -d:|sort|uniq`;  do sed -i "/\/\/ NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)/d" $i; done
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/62008

Reviewed By: driazati, r-barnes

Differential Revision: D29838584

Pulled By: malfet

fbshipit-source-id: 1b2f8602c945bd4ce50a9bfdd204755556e31d13
2021-07-22 18:04:40 -07:00

35 lines
1.4 KiB
C++

#include "caffe2/operators/sinusoid_position_encoding_op.h"
namespace caffe2 {
REGISTER_CPU_OPERATOR(
SinusoidPositionEncoding,
SinusoidPositionEncodingOp<CPUContext>);
OPERATOR_SCHEMA(SinusoidPositionEncoding)
.NumInputs(1)
.NumOutputs(1)
.SetDoc(R"DOC(
Calculates a sinusoid position encoding tensor as described
in https://arxiv.org/abs/1706.03762. Takes a 2-D tensor
(of size M x K) of positions as input, the embedding size
as an argument, and outputs a position encoding tensor of
size (M x K x embedding_size). Here M is typically the max
sequence length and K is typically the batch size.
The input tensor must satisfy input[m, 0] == input[m, k] for all k.
Encoded as amplitude * SIN(pos/alpha^(i/embedding_size)) if i is even,
else amplitude * COS(pos/alpha^(i/embedding_size)). Here, pos is the position,
alpha and amplitude are tuning parameters, i is the current dimension for
the embedding, and embedding_size is the number of total dimensions in
the embedding.
)DOC")
.Arg(
"embedding_size",
"Desired embedding size/number of dimensions -- defaults to 100")
.Arg("alpha", "Sinusoid tuning parameter -- defaults to 10000")
.Arg("amplitude", "Amplitude of Sin/Cos output")
.Input(0, "positions", "2-D tensor of positions to be encoded")
.Output(0, "output", "3-D tensor representing the positional encoding");
} // namespace caffe2