Skip to content

Commit 75f507c

Browse files
committed
Log embeddings on trace
1 parent 1285652 commit 75f507c

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

src/embeddings/embeddings_calculator_ov.cc

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,91 @@ const std::string EMBEDDINGS_SESSION_SIDE_PACKET_TAG = "EMBEDDINGS_NODE_RESOURCE
5353
using InputDataType = ovms::HttpPayload;
5454
using OutputDataType = std::string;
5555

56+
static void dumpTensorToFile(const ov::Tensor& tensor, const std::string& tensor_name, const std::string& filename) {
57+
// if log level is trace
58+
if (!embeddings_calculator_logger->should_log(spdlog::level::trace)) {
59+
return;
60+
}
61+
62+
std::ofstream output_file(filename);
63+
if (!output_file.is_open()) {
64+
SPDLOG_LOGGER_ERROR(embeddings_calculator_logger, "Failed to open file {} for writing tensor data", filename);
65+
return;
66+
}
67+
68+
const auto& shape = tensor.get_shape();
69+
70+
// Write tensor name and shape information
71+
output_file << "Tensor '" << tensor_name << "' shape: [";
72+
for (size_t i = 0; i < shape.size(); ++i) {
73+
output_file << shape[i];
74+
if (i < shape.size() - 1) output_file << ", ";
75+
}
76+
output_file << "]\n";
77+
output_file << "Tensor data:\n";
78+
79+
if (shape.size() == 2) {
80+
// Handle 2D tensors
81+
for (size_t i = 0; i < shape[0]; ++i) {
82+
for (size_t j = 0; j < shape[1]; ++j) {
83+
if (tensor.get_element_type() == ov::element::i32) {
84+
output_file << reinterpret_cast<const int32_t*>(tensor.data())[i * shape[1] + j] << "\t";
85+
} else if (tensor.get_element_type() == ov::element::i64) {
86+
output_file << reinterpret_cast<const int64_t*>(tensor.data())[i * shape[1] + j] << "\t";
87+
} else if (tensor.get_element_type() == ov::element::f32) {
88+
output_file << reinterpret_cast<const float*>(tensor.data())[i * shape[1] + j] << "\t";
89+
} else {
90+
output_file << "unsupported_type\t";
91+
}
92+
}
93+
output_file << "\n";
94+
}
95+
} else {
96+
output_file << "Tensor shape not supported for dumping (dimensions: " << shape.size() << ")\n";
97+
}
98+
}
99+
100+
static void dumpTensorToTrace(const ov::Tensor& tensor, const std::string& tensor_name) {
101+
// if log level is trace
102+
if (!embeddings_calculator_logger->should_log(spdlog::level::trace)) {
103+
return;
104+
}
105+
106+
const auto& shape = tensor.get_shape();
107+
std::ostringstream oss;
108+
109+
// Build shape string
110+
oss << "Tensor '" << tensor_name << "' shape: [";
111+
for (size_t i = 0; i < shape.size(); ++i) {
112+
oss << shape[i];
113+
if (i < shape.size() - 1) oss << ", ";
114+
}
115+
oss << "]\nTensor data:\n";
116+
117+
if (shape.size() == 2) {
118+
// Handle 2D tensors
119+
for (size_t i = 0; i < shape[0]; ++i) {
120+
for (size_t j = 0; j < shape[1]; ++j) {
121+
if (tensor.get_element_type() == ov::element::i32) {
122+
oss << reinterpret_cast<const int32_t*>(tensor.data())[i * shape[1] + j];
123+
} else if (tensor.get_element_type() == ov::element::i64) {
124+
oss << reinterpret_cast<const int64_t*>(tensor.data())[i * shape[1] + j];
125+
} else if (tensor.get_element_type() == ov::element::f32) {
126+
oss << reinterpret_cast<const float*>(tensor.data())[i * shape[1] + j];
127+
} else {
128+
oss << "unsupported_type";
129+
}
130+
if (j < shape[1] - 1) oss << " ";
131+
}
132+
oss << "\n";
133+
}
134+
} else {
135+
oss << "Tensor shape not supported for tracing (dimensions: " << shape.size() << ")\n";
136+
}
137+
138+
SPDLOG_LOGGER_TRACE(embeddings_calculator_logger, "{}", oss.str());
139+
}
140+
56141
class EmbeddingsCalculatorOV : public CalculatorBase {
57142
static const std::string INPUT_TAG_NAME;
58143
static const std::string OUTPUT_TAG_NAME;
@@ -127,12 +212,21 @@ class EmbeddingsCalculatorOV : public CalculatorBase {
127212
try {
128213
auto input = handler.getInput();
129214
if (auto strings = std::get_if<std::vector<std::string>>(&input)) {
215+
if (!embeddings_calculator_logger->should_log(spdlog::level::trace)) {
216+
std::ostringstream oss;
217+
oss << "Received " << strings->size() << " strings:\n";
218+
for (const auto& str : *strings) {
219+
oss << "[" << str << "]\n";
220+
}
221+
SPDLOG_INFO("{}", oss.str());
222+
}
130223
received_batch_size = strings->size();
131224
ov::AnyMap params = {};
132225
if (cc->Options<EmbeddingsCalculatorOVOptions>().truncate()) {
133226
params = {{"max_length", max_context_length}};
134227
}
135228
tokens = embeddings_session->getTokenizer().encode(*strings, params);
229+
// ------------- print tokens
136230
RET_CHECK(tokens.input_ids.get_shape().size() == 2);
137231
size_t input_ids_size = tokens.input_ids.get_shape()[1];
138232
if (input_ids_size > max_context_length) {
@@ -207,9 +301,17 @@ class EmbeddingsCalculatorOV : public CalculatorBase {
207301
auto executingStreamIdGuard = std::make_unique<ExecutingStreamIdGuard>(embeddings_session->getInferRequestsQueue(), unused);
208302
ov::InferRequest& inferRequest = executingStreamIdGuard->getInferRequest();
209303
inferRequest.set_tensor(EMBEDDINGS_MODEL_INPUT_IDS_NAME, tokens.input_ids);
304+
dumpTensorToTrace(tokens.input_ids, EMBEDDINGS_MODEL_INPUT_IDS_NAME);
305+
dumpTensorToFile(tokens.input_ids, EMBEDDINGS_MODEL_INPUT_IDS_NAME, "input_ids_tensor.txt");
306+
210307
inferRequest.set_tensor(EMBEDDINGS_MODEL_ATTENTION_MASK_NAME, tokens.attention_mask);
308+
dumpTensorToTrace(tokens.attention_mask, EMBEDDINGS_MODEL_ATTENTION_MASK_NAME);
309+
dumpTensorToFile(tokens.attention_mask, EMBEDDINGS_MODEL_ATTENTION_MASK_NAME, "attention_mask_tensor.txt");
310+
211311
if (embeddings_session->getNumberOfModelInputs() == 3) {
212312
inferRequest.set_tensor(EMBEDDINGS_MODEL_TOKEN_TYPE_IDS_NAME, typeIds);
313+
dumpTensorToTrace(typeIds, EMBEDDINGS_MODEL_TOKEN_TYPE_IDS_NAME);
314+
dumpTensorToFile(typeIds, EMBEDDINGS_MODEL_TOKEN_TYPE_IDS_NAME, "token_type_ids_tensor.txt");
213315
}
214316
inferRequest.start_async();
215317
inferRequest.wait();

0 commit comments

Comments
 (0)