microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
operators/tokenizer/bert_tokenizer_decoder.hpp
49lines · modeblame
97ec9507Mojimi4 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. | |
| 3 | | |
| 4 | #pragma once | |
| 5 | | |
| 6 | #include "ocos.h" | |
| 7 | #include "ustring.h" | |
| 8 | #include "string_utils.h" | |
| 9 | #include "string_tensor.h" | |
| 10 | | |
| 11 | class BertTokenizerDecoder { | |
| 12 | public: | |
d8cdb8e0Mojimi4 years ago | 13 | BertTokenizerDecoder(std::string vocab, std::string unk_token, std::string sep_token, std::string pad_token, |
5e44a7c3Scott McKay3 years ago | 14 | std::string cls_token, std::string mask_token, std::string suffix_indicator); |
d8cdb8e0Mojimi4 years ago | 15 | std::string Decode(const std::vector<int64_t>& ids, bool skip_special_tokens, bool clean_up_tokenization_spaces); |
97ec9507Mojimi4 years ago | 16 | |
| 17 | private: | |
d8cdb8e0Mojimi4 years ago | 18 | std::string unk_token_; |
5320af1eWenbing Li3 years ago | 19 | int32_t unk_token_id_ = -1; |
| 20 | int32_t sep_token_id_ = -1; | |
| 21 | int32_t pad_token_id_ = -1; | |
| 22 | int32_t cls_token_id_ = -1; | |
| 23 | int32_t mask_token_id_ = -1; | |
d8cdb8e0Mojimi4 years ago | 24 | std::string suffix_indicator_; |
| 25 | std::vector<std::string_view> vocab_; | |
| 26 | std::string raw_vocab_; | |
97ec9507Mojimi4 years ago | 27 | std::vector<bool> is_substr_; |
cce66310Mojimi4 years ago | 28 | |
d8cdb8e0Mojimi4 years ago | 29 | bool RemoveTokenizeSpace(int64_t pre_token_id, int64_t new_token_id); |
97ec9507Mojimi4 years ago | 30 | }; |
| 31 | | |
| 32 | struct KernelBertTokenizerDecoder : BaseKernel { | |
5e44a7c3Scott McKay3 years ago | 33 | KernelBertTokenizerDecoder(const OrtApi& api, const OrtKernelInfo& info); |
97ec9507Mojimi4 years ago | 34 | void Compute(OrtKernelContext* context); |
5e44a7c3Scott McKay3 years ago | 35 | |
97ec9507Mojimi4 years ago | 36 | private: |
| 37 | std::shared_ptr<BertTokenizerDecoder> decoder_; | |
cce66310Mojimi4 years ago | 38 | bool use_indices_; |
| 39 | bool skip_special_tokens_; | |
| 40 | bool clean_up_tokenization_spaces_; | |
97ec9507Mojimi4 years ago | 41 | }; |
| 42 | | |
c599b00dWenbing Li3 years ago | 43 | struct CustomOpBertTokenizerDecoder : OrtW::CustomOpBase<CustomOpBertTokenizerDecoder, KernelBertTokenizerDecoder> { |
97ec9507Mojimi4 years ago | 44 | const char* GetName() const; |
| 45 | size_t GetInputTypeCount() const; | |
| 46 | ONNXTensorElementDataType GetInputType(size_t index) const; | |
| 47 | size_t GetOutputTypeCount() const; | |
| 48 | ONNXTensorElementDataType GetOutputType(size_t index) const; | |
5e44a7c3Scott McKay3 years ago | 49 | }; |