microsoft/onnxruntime-extensions

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
rel-0.7

Branches

Tags

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

Clone

HTTPS

Download ZIP

operators/tokenizer/bert_tokenizer_decoder.hpp

49lines · modecode

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
11class BertTokenizerDecoder {
12 public:
13 BertTokenizerDecoder(std::string vocab, std::string unk_token, std::string sep_token, std::string pad_token,
14 std::string cls_token, std::string mask_token, std::string suffix_indicator);
15 std::string Decode(const std::vector<int64_t>& ids, bool skip_special_tokens, bool clean_up_tokenization_spaces);
16
17 private:
18 std::string unk_token_;
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;
24 std::string suffix_indicator_;
25 std::vector<std::string_view> vocab_;
26 std::string raw_vocab_;
27 std::vector<bool> is_substr_;
28
29 bool RemoveTokenizeSpace(int64_t pre_token_id, int64_t new_token_id);
30};
31
32struct KernelBertTokenizerDecoder : BaseKernel {
33 KernelBertTokenizerDecoder(const OrtApi& api, const OrtKernelInfo& info);
34 void Compute(OrtKernelContext* context);
35
36 private:
37 std::shared_ptr<BertTokenizerDecoder> decoder_;
38 bool use_indices_;
39 bool skip_special_tokens_;
40 bool clean_up_tokenization_spaces_;
41};
42
43struct CustomOpBertTokenizerDecoder : OrtW::CustomOpBase<CustomOpBertTokenizerDecoder, KernelBertTokenizerDecoder> {
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;
49};
50