microsoft/onnxruntime-extensions
Publicmirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable
tokenizer/sentencepiece_tokenizer.cc
145lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #include "sentencepiece_processor.h" |
| 5 | #include "sentencepiece_model.pb.h" |
| 6 | #include "sentencepiece_tokenizer.hpp" |
| 7 | #include "kernels/string_common.h" |
| 8 | #include "base64.h" |
| 9 | |
| 10 | KernelSentencepieceTokenizer::KernelSentencepieceTokenizer(OrtApi api, const OrtKernelInfo* info) : BaseKernel(api) { |
| 11 | std::string model_as_string = ort_.KernelInfoGetAttribute<std::string>(info, "model"); |
| 12 | sentencepiece::ModelProto model_proto; |
| 13 | std::vector<uint8_t> model_as_bytes; |
| 14 | if (base64_decode(model_as_string, model_as_bytes)) { |
| 15 | model_proto.ParseFromArray(model_as_bytes.data(), model_as_bytes.size()); |
| 16 | } else { |
| 17 | model_proto.ParseFromArray(model_as_string.c_str(), model_as_string.size()); |
| 18 | } |
| 19 | sentencepiece::util::Status status = tokenizer_.Load(model_proto); |
| 20 | if (!status.ok()) |
| 21 | throw std::runtime_error(MakeString( |
| 22 | "Failed to create SentencePieceProcessor instance. Error code is ", |
| 23 | (int)status.code(), ". Message is '", status.error_message(), "'.")); |
| 24 | } |
| 25 | |
| 26 | static void _check_dimension_constant(Ort::CustomOpApi ort, const OrtValue* ort_value, const char* name) { |
| 27 | OrtTensorDimensions dimensions(ort, ort_value); |
| 28 | if (dimensions.size() != 1 || dimensions[0] != 1) |
| 29 | throw std::runtime_error(MakeString( |
| 30 | name, " must contain only one element. It has ", dimensions.size(), " dimensions.")); |
| 31 | } |
| 32 | |
| 33 | void KernelSentencepieceTokenizer::Compute(OrtKernelContext* context) { |
| 34 | // Update with the new API |
| 35 | const OrtValue* ort_input = ort_.KernelContext_GetInput(context, 0); |
| 36 | std::vector<std::string> str_input; |
| 37 | GetTensorMutableDataString(api_, ort_, context, ort_input, str_input); |
| 38 | const OrtValue* ort_nbest_size = ort_.KernelContext_GetInput(context, 1); |
| 39 | const float* p_nbest_size = ort_.GetTensorData<float>(ort_nbest_size); |
| 40 | const OrtValue* ort_alpha = ort_.KernelContext_GetInput(context, 2); |
| 41 | const float* p_alpha = ort_.GetTensorData<float>(ort_alpha); |
| 42 | const OrtValue* ort_add_bos = ort_.KernelContext_GetInput(context, 3); |
| 43 | const bool* p_add_bos = ort_.GetTensorData<bool>(ort_add_bos); |
| 44 | const OrtValue* ort_add_eos = ort_.KernelContext_GetInput(context, 4); |
| 45 | const bool* p_add_eos = ort_.GetTensorData<bool>(ort_add_eos); |
| 46 | const OrtValue* ort_add_rev = ort_.KernelContext_GetInput(context, 5); |
| 47 | const bool* p_add_rev = ort_.GetTensorData<bool>(ort_add_rev); |
| 48 | |
| 49 | // Verifications |
| 50 | _check_dimension_constant(ort_, ort_nbest_size, "nbest_size"); |
| 51 | _check_dimension_constant(ort_, ort_alpha, "alpha"); |
| 52 | _check_dimension_constant(ort_, ort_add_bos, "add_bos"); |
| 53 | _check_dimension_constant(ort_, ort_add_eos, "add_eos"); |
| 54 | _check_dimension_constant(ort_, ort_add_rev, "add_rev"); |
| 55 | |
| 56 | // computation |
| 57 | |
| 58 | std::vector<int64_t> indices; |
| 59 | std::vector<int> content; |
| 60 | indices.reserve(str_input.size() + 1); |
| 61 | std::vector<int> inloop; |
| 62 | for (size_t i = 0; i < str_input.size(); ++i) { |
| 63 | if (!tokenizer_.Encode(str_input[i].c_str(), &inloop).ok()) |
| 64 | throw std::runtime_error(MakeString( |
| 65 | "Unable to encode string '", str_input[i], "'.")); |
| 66 | indices.push_back(content.size()); |
| 67 | |
| 68 | if (*p_add_rev) { |
| 69 | if (*p_add_eos) { |
| 70 | content.push_back(tokenizer_.eos_id()); |
| 71 | } |
| 72 | content.insert(content.end(), inloop.rbegin(), inloop.rend()); |
| 73 | if (*p_add_bos) { |
| 74 | content.push_back(tokenizer_.bos_id()); |
| 75 | } |
| 76 | } else { |
| 77 | if (*p_add_bos) { |
| 78 | content.push_back(tokenizer_.bos_id()); |
| 79 | } |
| 80 | content.insert(content.end(), inloop.begin(), inloop.end()); |
| 81 | if (*p_add_eos) { |
| 82 | content.push_back(tokenizer_.eos_id()); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | indices.push_back(content.size()); |
| 87 | |
| 88 | // Setup output |
| 89 | std::vector<int64_t> size_content(1); |
| 90 | size_content[0] = content.size(); |
| 91 | OrtValue* out_content = ort_.KernelContext_GetOutput(context, 0, size_content.data(), size_content.size()); |
| 92 | |
| 93 | std::vector<int64_t> size_indices(1); |
| 94 | size_indices[0] = indices.size(); |
| 95 | OrtValue* out_indices = ort_.KernelContext_GetOutput(context, 1, size_indices.data(), size_indices.size()); |
| 96 | |
| 97 | int* ptr_content = ort_.GetTensorMutableData<int>(out_content); |
| 98 | memcpy(ptr_content, content.data(), content.size() * sizeof(int)); |
| 99 | int64_t* ptr_indices = ort_.GetTensorMutableData<int64_t>(out_indices); |
| 100 | memcpy(ptr_indices, indices.data(), indices.size() * sizeof(int64_t)); |
| 101 | } |
| 102 | |
| 103 | void* CustomOpSentencepieceTokenizer::CreateKernel(OrtApi api, const OrtKernelInfo* info) const { |
| 104 | return new KernelSentencepieceTokenizer(api, info); |
| 105 | }; |
| 106 | |
| 107 | const char* CustomOpSentencepieceTokenizer::GetName() const { |
| 108 | return "SentencepieceTokenizer"; |
| 109 | }; |
| 110 | |
| 111 | size_t CustomOpSentencepieceTokenizer::GetInputTypeCount() const { |
| 112 | return 6; |
| 113 | }; |
| 114 | |
| 115 | ONNXTensorElementDataType CustomOpSentencepieceTokenizer::GetInputType(size_t index) const { |
| 116 | switch (index) { |
| 117 | case 0: |
| 118 | return ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING; |
| 119 | case 1: |
| 120 | return ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64; |
| 121 | case 2: |
| 122 | return ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT; |
| 123 | case 3: |
| 124 | case 4: |
| 125 | case 5: |
| 126 | return ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL; |
| 127 | default: |
| 128 | throw std::runtime_error(MakeString("Unexpected input index ", index)); |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | size_t CustomOpSentencepieceTokenizer::GetOutputTypeCount() const { |
| 133 | return 2; |
| 134 | }; |
| 135 | |
| 136 | ONNXTensorElementDataType CustomOpSentencepieceTokenizer::GetOutputType(size_t index) const { |
| 137 | switch (index) { |
| 138 | case 0: |
| 139 | return ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32; |
| 140 | case 1: |
| 141 | return ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64; |
| 142 | default: |
| 143 | throw std::runtime_error(MakeString("Unexpected output index ", index)); |
| 144 | } |
| 145 | }; |
| 146 | |