|
| 1 | +#include <iostream> |
| 2 | +#include <algorithm> |
| 3 | +#include <vector> |
| 4 | +#include <chrono> |
| 5 | +#include <string> |
| 6 | +#include <vector> |
| 7 | +#include <onnxruntime_cxx_api.h> |
| 8 | +#include "util/tokenization.h" |
| 9 | + |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +const static std::vector<std::string> key = { |
| 13 | + "finance", |
| 14 | + "realty", |
| 15 | + "stocks", |
| 16 | + "education", |
| 17 | + "science", |
| 18 | + "society", |
| 19 | + "politics", |
| 20 | + "sports", |
| 21 | + "game", |
| 22 | + "entertainment" |
| 23 | +}; |
| 24 | + |
| 25 | +template <typename T> |
| 26 | +int argmax(const std::vector<T>& v) { |
| 27 | + if (v.empty()) { |
| 28 | + return -1; |
| 29 | + } |
| 30 | + return std::max_element(v.begin(), v.end()) - v.begin(); |
| 31 | +} |
| 32 | +template <typename T> |
| 33 | +int argmax(T a, T b) { |
| 34 | + return std::max_element(a, b) - a; |
| 35 | +} |
| 36 | +class Model { |
| 37 | +public: |
| 38 | + Model(const std::string& vocab_path) { |
| 39 | + tokenizer_ = new FullTokenizer(vocab_path); |
| 40 | + } |
| 41 | + |
| 42 | + std::vector<std::vector<int64_t>> build_input(const std::string& text) { |
| 43 | + auto tokens = tokenizer_->tokenize(text); |
| 44 | + auto token_ids = tokenizer_->convertTokensToIds(tokens); |
| 45 | + |
| 46 | + std::vector<std::vector<int64_t>> res; |
| 47 | + |
| 48 | + std::vector<int64_t> input(32); |
| 49 | + std::vector<int64_t> mask(32); |
| 50 | + input[0] = 101; |
| 51 | + mask[0] = 1; |
| 52 | + for (int i = 0; i < token_ids.size() && i < 31; ++i) { |
| 53 | + input[i+1] = token_ids[i]; |
| 54 | + mask[i+1] = token_ids[i] > 0; |
| 55 | + } |
| 56 | + res.push_back(std::move(input)); |
| 57 | + res.push_back(std::move(mask)); |
| 58 | + return res; |
| 59 | + } |
| 60 | + FullTokenizer* tokenizer_ = nullptr; |
| 61 | +}; |
| 62 | + |
| 63 | +int main() |
| 64 | +{ |
| 65 | + const char* text = "李稻葵:过去2年抗疫为每人增寿10天"; |
| 66 | + const char* vocab_path = "/home/guodong/bert_pretrain/vocab.txt"; |
| 67 | + Model model(vocab_path); |
| 68 | + auto res = model.build_input(text); |
| 69 | + |
| 70 | + Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "test"); |
| 71 | + Ort::SessionOptions session_options; |
| 72 | + |
| 73 | + OrtCUDAProviderOptions cuda_options; //= { |
| 74 | +// 0, |
| 75 | +// //OrtCudnnConvAlgoSearch::EXHAUSTIVE, |
| 76 | +// OrtCudnnConvAlgoSearchExhaustive, |
| 77 | +// std::numeric_limits<size_t>::max(), |
| 78 | +// 0, |
| 79 | +// true |
| 80 | +// }; |
| 81 | + |
| 82 | + session_options.AppendExecutionProvider_CUDA(cuda_options); |
| 83 | + const char* model_path = "/home/guodong/github/Bert-Chinese-Text-Classification-Pytorch/model.onnx"; |
| 84 | + |
| 85 | + |
| 86 | + Ort::Session session(env, model_path, session_options); |
| 87 | + // print model input layer (node names, types, shape etc.) |
| 88 | + Ort::AllocatorWithDefaultOptions allocator; |
| 89 | + |
| 90 | + // print number of model input nodes |
| 91 | + size_t num_input_nodes = session.GetInputCount(); |
| 92 | + std::cout<< num_input_nodes <<std::endl; |
| 93 | + std::cout<< session.GetOutputCount() <<std::endl; |
| 94 | + |
| 95 | + std::vector<int64_t> input_node_dims = {1, 32}; |
| 96 | + |
| 97 | + auto& input_tensor_values = res[0]; |
| 98 | + auto& mask_tensor_values = res[1]; |
| 99 | + |
| 100 | + //size_t input_tensor_size = 32; |
| 101 | + for (auto i : input_tensor_values) { |
| 102 | + std::cout << i << "\t" ; |
| 103 | + } |
| 104 | +std::cout<<std::endl; |
| 105 | + for (auto i : mask_tensor_values) { |
| 106 | + std::cout << i << "\t" ; |
| 107 | + } |
| 108 | +std::cout<<std::endl; |
| 109 | + |
| 110 | + // create input tensor object from data values !!!!!!!!!! |
| 111 | + auto memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault); |
| 112 | + |
| 113 | + Ort::Value input_tensor = Ort::Value::CreateTensor<int64_t>(memory_info, input_tensor_values.data(), |
| 114 | + input_tensor_values.size(), input_node_dims.data(), 2); |
| 115 | + |
| 116 | + Ort::Value mask_tensor = Ort::Value::CreateTensor<int64_t>(memory_info, mask_tensor_values.data(), |
| 117 | + mask_tensor_values.size(), input_node_dims.data(), 2); |
| 118 | + |
| 119 | + std::vector<Ort::Value> ort_inputs; |
| 120 | + ort_inputs.push_back(std::move(input_tensor)); |
| 121 | + ort_inputs.push_back(std::move(mask_tensor)); |
| 122 | + |
| 123 | + std::vector<const char*> input_node_names = {"ids", "mask"}; |
| 124 | + std::vector<const char*> output_node_names = {"output"}; |
| 125 | + auto output_tensors = session.Run(Ort::RunOptions{nullptr}, input_node_names.data(), ort_inputs.data(), |
| 126 | + ort_inputs.size(), output_node_names.data(), 1); |
| 127 | + |
| 128 | + float* floatarr = output_tensors[0].GetTensorMutableData<float>(); |
| 129 | + |
| 130 | + for (int i=0; i<10; i++) |
| 131 | + { |
| 132 | + std::cout<<floatarr[i]<<std::endl; |
| 133 | + } |
| 134 | + std::cout<< key[argmax(floatarr, floatarr+10)] << std::endl; |
| 135 | + |
| 136 | + return 0; |
| 137 | +} |
0 commit comments