microsoft/onnxruntime-extensions

Public

mirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
13d9e27ccd8a0de9a1225756fbf6860a1931484f

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

operators/tokenizer/bert_tokenizer_decoder.hpp

52lines · modecode

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