Views
No views yet
1Embedding dimension: 384
2Max sequence length: 2561input_names=["input_ids", "attention_mask"],
2output_names=["token_embeddings", "sentence_embedding"],
3dynamic_axes={
4 "input_ids": {0: "batch_size", 1: "sequence_length"},
5 "attention_mask": {0: "batch_size", 1: "sequence_length"},
6 "token_embeddings": {0: "batch_size", 1: "sequence_length"},
7 "sentence_embedding": {0: "batch_size"}1#include <iostream>
2#include <vector>
3#include <onnxruntime_cxx_api.h>
4
5int main() {
6 Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "embedding");
7 Ort::SessionOptions session_options;
8 session_options.SetIntraOpNumThreads(1);
9
10 // Load the ONNX model
11 const char* model_path = "your_model.onnx";
12 Ort::Session session(env, model_path, session_options);
13
14 Ort::AllocatorWithDefaultOptions allocator;
15
16 // Input and output names
17 const char* input_names[] = {"input_ids", "attention_mask"};
18 const char* output_names[] = {"token_embeddings", "sentence_embedding"};
19
20 // Mock input data (e.g., batch_size=1, sequence_length=5)
21 std::vector<int64_t> input_ids = {101, 2009, 2003, 1037, 2742}; // Example token IDs, Use proper tokenization library to convert string to input_ids (e.g. tokenizers-cpp)
22 std::vector<int64_t> attention_mask = {1, 1, 1, 1, 1};
23
24 std::vector<int64_t> input_shape = {1, 5}; // batch_size=1, seq_len=5
25
26 // Create input tensors
27 Ort::Value input_ids_tensor = Ort::Value::CreateTensor<int64_t>(
28 allocator, input_ids.data(), input_ids.size(), input_shape.data(), input_shape.size());
29
30 Ort::Value attention_mask_tensor = Ort::Value::CreateTensor<int64_t>(
31 allocator, attention_mask.data(), attention_mask.size(), input_shape.data(), input_shape.size());
32
33 std::vector<Ort::Value> input_tensors;
34 input_tensors.push_back(std::move(input_ids_tensor));
35 input_tensors.push_back(std::move(attention_mask_tensor));
36
37 // Run inference
38 auto output_tensors = session.Run(Ort::RunOptions{nullptr},
39 input_names, input_tensors.data(), 2,
40 output_names, 2);
41
42 // Extract sentence embedding
43 float* sentence_embedding = output_tensors[1].GetTensorMutableData<float>();
44 size_t embedding_size = output_tensors[1].GetTensorTypeAndShapeInfo().GetElementCount();
45
46 std::cout << "Sentence Embedding:\n";
47 for (size_t i = 0; i < embedding_size; ++i) {
48 std::cout << sentence_embedding[i] << " ";
49 }
50 std::cout << std::endl;
51
52 return 0;
53}
54