22using namespace facebook::velox;
23using namespace facebook::velox::test;
24using namespace facebook::velox::exec::test;
25using namespace facebook::velox::memory;
51 bu_ =
new float[numUser];
52 bi_ =
new float[numItem];
53 pu_ =
new float[numUser * latentDims];
54 qi_ =
new float[numItem * latentDims];
55 std::memcpy(bu_, bu, numUser *
sizeof(
float));
56 std::memcpy(bi_, bi, numItem *
sizeof(
float));
57 std::memcpy(pu_, pu, numUser * latentDims *
sizeof(
float));
58 std::memcpy(qi_, qi, numItem * latentDims *
sizeof(
float));
59 dims.push_back(numUser);
60 dims.push_back(numItem);
61 dims.push_back(latentDims);
73 const SelectivityVector& rows,
74 std::vector<VectorPtr>& args,
75 const TypePtr& outputType,
76 exec::EvalCtx& context,
77 VectorPtr& output)
const override {
78 BaseVector::ensureWritable(rows, outputType, context.pool(), output);
80 exec::DecodedArgs decodedArgs(rows, args, context);
81 auto decodedUser = decodedArgs.at(0);
82 auto decodedItem = decodedArgs.at(1);
84 auto arrayOutput = output->asFlatVector<
float>();
85 float* outputValues = arrayOutput->mutableRawValues<
float>();
87 rows.applyToSelected([&](vector_size_t i) {
88 auto userId = decodedUser->valueAt<
int>(i);
89 auto itemId = decodedItem->valueAt<
int>(i);
91 if (userId >
dims[0]) {
92 LOG(WARNING) <<
"User id out of bound: " << userId <<
" / " <<
dims[0];
94 }
else if (itemId >
dims[1]) {
96 LOG(WARNING) <<
"Item id out of bound: " << itemId <<
" / " <<
dims[1];
99 Eigen::Map<Eigen::VectorXf> qiVec(qi_ + itemId *
dims[2],
dims[2]);
100 Eigen::Map<Eigen::VectorXf> puVec(pu_ + userId *
dims[2],
dims[2]);
102 float prediction = bu_[userId] + bi_[itemId] + puVec.dot(qiVec);
103 outputValues[i] = prediction;
111 static std::vector<std::shared_ptr<exec::FunctionSignature>>
signatures() {
112 return {exec::FunctionSignatureBuilder()
114 .argumentType(
"INTEGER")
115 .argumentType(
"INTEGER")
164 CostEstimate
getCost(std::vector<int> inputDims) {
166 float cost = coefficientVector[0] * inputDims[0] * inputDims[1];
168 return CostEstimate(cost, inputDims[0], inputDims[1]);
177 std::string weightsFile_;
A base class for machine learning functions, inheriting from Velox's VectorFunction.
Definition BaseFunction.h:9
std::vector< double > getCoefficientVector(std::string name)
Retrieves the cost coefficients for the function.
Definition BaseFunction.h:83
std::vector< int > dims
Dimensions of the function.
Definition BaseFunction.h:61
static std::string getName()
Returns the name of the function.
Definition SVD.h:139
float * getTensor() const override
Returns the tensor associated with the function.
Definition SVD.h:123
void setWeights(float *weights)
Sets the weights for the function.
Definition SVD.h:155
CostEstimate getCost(std::vector< int > inputDims)
Estimates the cost of the function.
Definition SVD.h:164
std::string getFuncName()
Returns the name of the function.
Definition SVD.h:131
static std::vector< std::shared_ptr< exec::FunctionSignature > > signatures()
Returns the function signatures.
Definition SVD.h:111
SVD(float *bu, float *bi, float *pu, float *qi, int numUser, int numItem, int latentDims)
Constructor for SVD.
Definition SVD.h:43
void apply(const SelectivityVector &rows, std::vector< VectorPtr > &args, const TypePtr &outputType, exec::EvalCtx &context, VectorPtr &output) const override
Applies the SVD function to the input data.
Definition SVD.h:72
std::string getWeightsFile()
Returns the path to the weights file.
Definition SVD.h:147