-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathONNXEmbedder.cpp
More file actions
159 lines (138 loc) · 4.33 KB
/
ONNXEmbedder.cpp
File metadata and controls
159 lines (138 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "ONNXEmbedder.h"
#include <cmath>
#include <numeric>
#include <vector>
#include <utility>
#include <string>
#include <cstdint>
#include <array>
#include <thread>
#include "packages/Microsoft.ML.OnnxRuntime.1.23.2/build/native/include/onnxruntime_c_api.h"
#include "packages/Microsoft.ML.OnnxRuntime.1.23.2/build/native/include/onnxruntime_cxx_api.h"
// Convert std::string to ORTCHAR_T*, needed for Windows compatibility
#ifdef _WIN32
inline static const ORTCHAR_T* ToOrtString(const std::string& s) {
static std::wstring ws;
ws.assign(s.begin(), s.end());
return ws.c_str();
}
#else
inline static const ORTCHAR_T* ToOrtString(const std::string& s) {
return s.c_str();
}
#endif
// Constructor
ONNXEmbedder::ONNXEmbedder(
const std::string& modelPath,
const std::string& vocabPath,
size_t maxLen)
: env(ORT_LOGGING_LEVEL_WARNING, "ONNXEmbedder"),
sessionOptions(),
session(nullptr),
tokenizer(vocabPath),
maxLen(maxLen)
{
sessionOptions.SetIntraOpNumThreads(std::thread::hardware_concurrency());
sessionOptions.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);
session = Ort::Session(
env,
ToOrtString(modelPath),
sessionOptions);
}
// Embed a batch of texts
std::vector<std::vector<float>> ONNXEmbedder::embedBatch(const std::vector<std::string>& texts) {
size_t B = texts.size();
if (B == 0) return {};
std::vector<int64_t> flat_ids(B * maxLen);
std::vector<int64_t> flat_mask(B * maxLen);
std::vector<int64_t> flat_types(B * maxLen, 0);
// Tokenize each text
for (size_t i = 0; i < B; ++i) {
auto encoded = tokenizer.encode(texts[i], maxLen);
for (size_t j = 0; j < maxLen; ++j) {
int64_t id = encoded[j];
flat_ids[i * maxLen + j] = id;
flat_mask[i * maxLen + j] = (id != tokenizer.pad_id);
}
}
// Create input tensors
Ort::MemoryInfo mem = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
std::array<int64_t, 2> shape{
static_cast<int64_t>(B),
static_cast<int64_t>(maxLen)
};
Ort::Value idsTensor = Ort::Value::CreateTensor<int64_t>(
mem, flat_ids.data(), flat_ids.size(), shape.data(), 2);
Ort::Value maskTensor = Ort::Value::CreateTensor<int64_t>(
mem, flat_mask.data(), flat_mask.size(), shape.data(), 2);
Ort::Value typeTensor = Ort::Value::CreateTensor<int64_t>(
mem, flat_types.data(), flat_types.size(), shape.data(), 2);
// Run inference
const char* inputNames[] = {
"input_ids",
"attention_mask",
"token_type_ids"
};
Ort::Value inputTensors[] = {
std::move(idsTensor),
std::move(maskTensor),
std::move(typeTensor)
};
const char* outputNames[] = {
"token_embeddings"
};
// Execute the model
auto outputs = session.Run(
Ort::RunOptions{ nullptr },
inputNames,
inputTensors,
3,
outputNames,
1
);
// Process output tensor
auto& out = outputs[0];
auto info = out.GetTensorTypeAndShapeInfo();
auto outShape = info.GetShape();
int64_t hidden = outShape.back();
float* data = out.GetTensorMutableData<float>();
// Prepare result vector
std::vector<std::vector<float>> result(
B, std::vector<float>(hidden)
);
// Compute mean pooling, ignoring padding tokens
for (size_t i = 0; i < B; ++i) {
float* start = data + i * maxLen * hidden;
std::vector<float> sum(hidden, 0.0f);
int count = 0;
for (size_t j = 0; j < maxLen; ++j) {
int64_t id = flat_ids[i * maxLen + j];
if (id != tokenizer.pad_id) {
for (size_t k = 0; k < hidden; ++k) {
sum[k] += start[j * hidden + k];
}
count++;
}
}
for (auto& x : sum) x /= count;
normalize(sum);
result[i] = std::move(sum);
}
return result;
}
// Normalize a vector to unit length
void ONNXEmbedder::normalize(std::vector<float>& v) {
float norm = std::sqrt(
std::accumulate(
v.begin(), v.end(), 0.0f,
[](float sum, float val) {
return sum + val * val;
}
)
);
if (norm > 0.0f) {
for (auto& x : v) {
x /= norm;
}
}
}