74 const SelectivityVector& rows,
75 std::vector<VectorPtr>& args,
77 exec::EvalCtx& context,
78 VectorPtr& output)
const override {
79 BaseVector::ensureWritable(rows, type, context.pool(), output);
80 output->clearNulls(rows);
81 auto arrayOutput = output->as<ArrayVector>();
82 auto sizes = arrayOutput->mutableSizes(rows.end());
83 auto rawSizes = sizes->asMutable<int32_t>();
84 auto offsets = arrayOutput->mutableOffsets(rows.end());
85 auto rawOffsets = offsets->asMutable<int32_t>();
88 std::fill(rawSizes, rawSizes + rows.end(), 0);
89 std::fill(rawOffsets, rawOffsets + rows.end(), 0);
91 auto elementsOutput = arrayOutput->elements();
92 auto elementsPool = context.pool();
94 exec::DecodedArgs decodedArgs(rows, args, context);
95 auto input = decodedArgs.at(0);
96 auto arrayVector = input->base()->as<ArrayVector>();
98 auto indicesVector = arrayVector->elements();
99 int* indicesValues = indicesVector->values()->asMutable<
int>();
100 int numInputs = rows.size();
104 int numEmbeddingToRetireve = 0;
105 rows.applyToSelected([&](vector_size_t row) {
106 int numSubIndices = arrayVector->sizeAt(row);
107 numEmbeddingToRetireve += numSubIndices;
110 auto baseOffset = elementsOutput->size();
113 elementsOutput->resize(baseOffset + numEmbeddingToRetireve *
dims[1]);
114 float* outputValues = elementsOutput->values()->asMutable<
float>();
116 vector_size_t outputOffset = 0;
117 rows.applyToSelected([&](vector_size_t row) {
118 int numSubIndices = arrayVector->sizeAt(row);
119 int indicesOffset = arrayVector->offsetAt(row);
120 rawOffsets[row] = outputOffset;
121 rawSizes[row] = numSubIndices *
dims[1];
122 for (
int i = 0; i < numSubIndices; i++) {
124 int embedIndex = indicesValues[indicesOffset + i];
125 if (embedIndex >=
dims[0]) {
126 throw std::runtime_error(fmt::format(
127 "[Embedding] Index out of bounds: {} >= {}",
132 outputValues + outputOffset,
133 weights_ + embedIndex *
dims[1],
134 dims[1] *
sizeof(
float));
135 outputOffset +=
dims[1];
138 arrayOutput->setElements(elementsOutput);