ML functions
 
Loading...
Searching...
No Matches
PositionEncoding.h
Go to the documentation of this file.
1
15
16#pragma once
17
18#include <Eigen/Dense>
19#include <cmath>
20#include <iostream>
21#include "BaseFunction.h"
22#include "velox/exec/tests/utils/AssertQueryBuilder.h"
23#include "velox/exec/tests/utils/PlanBuilder.h"
24#include "velox/exec/tests/utils/TempDirectoryPath.h"
25#include "velox/vector/tests/utils/VectorTestBase.h"
26
27using namespace facebook::velox;
28using namespace facebook::velox::test;
29using namespace facebook::velox::exec::test;
30using namespace facebook::velox::memory;
31
37 public:
42 PositionEncoding(int inputDims) {
43 inputDims_ = inputDims;
44 }
45
54 void apply(
55 const SelectivityVector& rows,
56 std::vector<VectorPtr>& args,
57 const TypePtr& type,
58 exec::EvalCtx& context,
59 VectorPtr& output) const override {
60 BaseVector::ensureWritable(rows, type, context.pool(), output);
61
62 // Decoder is required to handle address error, reference code:
63 // ArrayIntersectExcept.cpp
64 BaseVector* input = args[0].get();
65
66 exec::LocalDecodedVector inputHolder(context, *input, rows);
67 auto decodedInputArray = inputHolder.get();
68 auto baseInputArray =
69 decodedInputArray->base()->as<ArrayVector>()->elements();
70
71 float* inputValues = baseInputArray->values()->asMutable<float>();
72
73 auto numInput = rows.size();
74
75 Eigen::Map<
76 Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
77 m(inputValues, numInput, inputDims_);
78
79 std::vector<std::vector<float>> results;
80
81 if (inputDims_ % 2 != 0) {
82 throw std::runtime_error(fmt::format(
83 "Position Encoding Dims has to be a even number, current value is: {}",
84 inputDims_));
85 }
86 for (int i = 0; i < numInput; i++) {
87 for (int j = 0; j < inputDims_ / 2; j++) {
88 float angle = i / std::pow(10000.0, 2.0 * j / inputDims_);
89 int dataShift = i * inputDims_ + j * 2;
90 inputValues[dataShift] += std::sin(angle);
91 inputValues[dataShift + 1] += std::cos(angle);
92 }
93 std::vector<float> row(m.row(i).data(), m.row(i).data() + m.cols());
94 results.push_back(row);
95 }
96
97 VectorMaker maker{context.pool()};
98 output = maker.arrayVector<float>(results, REAL());
99 }
100
105 static std::vector<std::shared_ptr<exec::FunctionSignature>> signatures() {
106 return {exec::FunctionSignatureBuilder()
107 .argumentType("array(REAL)")
108 .returnType("array(REAL)")
109 .build()};
110 }
111
116 static std::string getName() {
117 return "position_encoding";
118 }
119
124 float* getTensor() const override {
125 // TODO: need to implement
126 return nullptr;
127 }
128
134 CostEstimate getCost(std::vector<int> inputDims) {
135 // TODO: need to implement
136 return CostEstimate(0, inputDims[0], inputDims[1]);
137 }
138
139 private:
140 int inputDims_;
141};
A base class for machine learning functions, inheriting from Velox's VectorFunction.
Definition BaseFunction.h:9
CostEstimate getCost(std::vector< int > inputDims)
Estimates the cost of the function.
Definition PositionEncoding.h:134
void apply(const SelectivityVector &rows, std::vector< VectorPtr > &args, const TypePtr &type, exec::EvalCtx &context, VectorPtr &output) const override
Applies the position encoding function to the input data.
Definition PositionEncoding.h:54
static std::string getName()
Returns the name of the function.
Definition PositionEncoding.h:116
float * getTensor() const override
Returns the tensor associated with the function.
Definition PositionEncoding.h:124
static std::vector< std::shared_ptr< exec::FunctionSignature > > signatures()
Returns the function signatures.
Definition PositionEncoding.h:105
PositionEncoding(int inputDims)
Constructor for PositionEncoding.
Definition PositionEncoding.h:42