1 #ifndef CAFFE2_OPERATORS_SINUSOID_POSITION_ENCODING_OP_H_ 2 #define CAFFE2_OPERATORS_SINUSOID_POSITION_ENCODING_OP_H_ 5 #define _USE_MATH_DEFINES 9 #include "caffe2/core/operator.h" 15 template <
class Context>
20 embedding_size_(OperatorBase::template GetSingleArgument<int>(
23 alpha_(OperatorBase::template GetSingleArgument<float>(
"alpha", 10000)),
25 OperatorBase::template GetSingleArgument<float>(
"amplitude", 1)) {}
26 USE_OPERATOR_CONTEXT_FUNCTIONS;
28 bool RunOnDevice()
override {
30 this, OperatorBase::Input<TensorCPU>(0));
33 template <
typename Index>
34 bool DoRunWithType() {
35 auto& positions = Input(0);
36 auto* output = Output(0);
38 CAFFE_ENFORCE_EQ(positions.ndim(), 2,
"POSITIONS should be a 2-D tensor");
40 auto shape = positions.dims();
41 shape.push_back(embedding_size_);
42 output->Resize(shape);
46 const Index* idxs = positions.template data<Index>();
47 float* out = output->template mutable_data<float>();
49 float log_alpha = std::log(alpha_);
51 ((float)embedding_size_ - 1.0f) / (float)embedding_size_;
53 for (
int i = 0; i < M; ++i) {
54 float pos = (float)idxs[i * K];
57 float* row = &out[i * K * embedding_size_];
58 Eigen::Map<Eigen::VectorXf> row_map(row, embedding_size_, 1);
59 auto row_array = row_map.array();
61 float log_pos = std::log(pos);
62 row_array.setLinSpaced(
63 embedding_size_, log_pos, log_pos - log_alpha * max_alpha_pow);
64 row_array = row_array.exp().eval();
68 for (
int k = 1; k < embedding_size_; k += 2) {
69 row[k] += (float)M_PI_2;
71 row_array = amplitude_ * row_array.sin().eval();
74 for (
int j = 1; j < K; ++j) {
75 int base = i * K * embedding_size_;
78 &out[base + embedding_size_],
79 &out[base + j * embedding_size_]);
93 #endif // CAFFE2_OPERATORS_SINUSOID_POSITION_ENCODING_OP_H_
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...