|
| 1 | +/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <string> |
| 18 | +#include <unordered_map> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +#include "boost/variant.hpp" |
| 22 | +#include "glog/logging.h" |
| 23 | +#include "nlohmann/json.hpp" |
| 24 | +#include "postprocessors/postprocessor.h" |
| 25 | + |
| 26 | +namespace tokenizers { |
| 27 | +namespace postprocessors { |
| 28 | + |
| 29 | +enum SequenceType { SEQ_A, SEQ_B }; |
| 30 | +NLOHMANN_JSON_SERIALIZE_ENUM(SequenceType, |
| 31 | + { |
| 32 | + {SEQ_A, "A"}, {SEQ_B, "B"}, |
| 33 | + }); |
| 34 | +// The template indicate `${Id} : ${TypeId}` |
| 35 | +using TemplateSequence = std::pair<SequenceType, uint>; |
| 36 | +using TemplateSpecialToken = std::pair<std::string, uint>; |
| 37 | + |
| 38 | +using TemplatePiece = boost::variant<TemplateSequence, TemplateSpecialToken>; |
| 39 | +void to_json(nlohmann::json& j, const TemplatePiece& template_piece); |
| 40 | +void from_json(const nlohmann::json& j, TemplatePiece& template_piece); |
| 41 | + |
| 42 | +void ParseIdFromString(const std::string& template_id_string, |
| 43 | + TemplatePiece* template_piece); |
| 44 | +void SetTypeId(uint type_id, TemplatePiece* template_piece); |
| 45 | +void GetTemplatePieceFromString(const std::string& template_string, |
| 46 | + TemplatePiece* template_piece); |
| 47 | + |
| 48 | +struct SpecialToken { |
| 49 | + std::string id_; |
| 50 | + std::vector<uint> ids_; |
| 51 | + std::vector<std::string> tokens_; |
| 52 | + SpecialToken() = default; |
| 53 | + SpecialToken(const std::string& id, |
| 54 | + const std::vector<uint>& ids, |
| 55 | + const std::vector<std::string>& tokens) |
| 56 | + : id_(id), ids_(ids), tokens_(tokens) {} |
| 57 | + SpecialToken(const std::string& token, uint id) { |
| 58 | + id_ = token; |
| 59 | + ids_.push_back(id); |
| 60 | + tokens_.push_back(token); |
| 61 | + } |
| 62 | + friend void to_json(nlohmann::json& j, const SpecialToken& special_token); |
| 63 | + friend void from_json(const nlohmann::json& j, SpecialToken& special_token); |
| 64 | +}; |
| 65 | + |
| 66 | +struct Template { |
| 67 | + std::vector<TemplatePiece> pieces_; |
| 68 | + Template() = default; |
| 69 | + explicit Template(const std::string& template_str) { |
| 70 | + std::vector<std::string> pieces; |
| 71 | + |
| 72 | + // Parse the pieces |
| 73 | + size_t start = template_str.find_first_not_of(" "); |
| 74 | + size_t pos; |
| 75 | + while ((pos = template_str.find_first_of(" ", start)) != |
| 76 | + std::string::npos) { |
| 77 | + pieces.push_back(template_str.substr(start, pos - start)); |
| 78 | + start = template_str.find_first_not_of(" ", pos); |
| 79 | + } |
| 80 | + if (start != std::string::npos) { |
| 81 | + pieces.push_back(template_str.substr(start)); |
| 82 | + } |
| 83 | + AddStringPiece(pieces); |
| 84 | + } |
| 85 | + |
| 86 | + explicit Template(const std::vector<TemplatePiece>& pieces) |
| 87 | + : pieces_(pieces) {} |
| 88 | + explicit Template(const std::vector<std::string>& pieces) { |
| 89 | + AddStringPiece(pieces); |
| 90 | + } |
| 91 | + |
| 92 | + void GetPiecesFromVec(const std::vector<std::string>& pieces) { |
| 93 | + AddStringPiece(pieces); |
| 94 | + } |
| 95 | + |
| 96 | + void GetPiecesFromStr(const std::string& template_str) { |
| 97 | + std::vector<std::string> pieces; |
| 98 | + |
| 99 | + // Parse the pieces |
| 100 | + size_t start = template_str.find_first_not_of(" "); |
| 101 | + size_t pos; |
| 102 | + while ((pos = template_str.find_first_of(" ", start)) != |
| 103 | + std::string::npos) { |
| 104 | + pieces.push_back(template_str.substr(start, pos - start)); |
| 105 | + start = template_str.find_first_not_of(" ", pos); |
| 106 | + } |
| 107 | + if (start != std::string::npos) { |
| 108 | + pieces.push_back(template_str.substr(start)); |
| 109 | + } |
| 110 | + AddStringPiece(pieces); |
| 111 | + } |
| 112 | + |
| 113 | + void Clean() { pieces_.clear(); } |
| 114 | + |
| 115 | +private: |
| 116 | + void AddStringPiece(const std::vector<std::string>& pieces) { |
| 117 | + for (auto&& piece : pieces) { |
| 118 | + TemplatePiece template_piece; |
| 119 | + GetTemplatePieceFromString(piece, &template_piece); |
| 120 | + if (boost::get<TemplateSequence>(&template_piece)) { |
| 121 | + pieces_.push_back(boost::get<TemplateSequence>(template_piece)); |
| 122 | + } else { |
| 123 | + pieces_.push_back(boost::get<TemplateSpecialToken>(template_piece)); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + friend void to_json(nlohmann::json& j, const Template& template_); |
| 129 | + friend void from_json(const nlohmann::json& j, Template& template_); |
| 130 | +}; |
| 131 | + |
| 132 | +struct SpecialTokensMap { |
| 133 | + std::unordered_map<std::string, SpecialToken> tokens_map_; |
| 134 | + SpecialTokensMap() = default; |
| 135 | + explicit SpecialTokensMap(const std::vector<SpecialToken>& special_tokens) { |
| 136 | + SetTokensMap(special_tokens); |
| 137 | + } |
| 138 | + void SetTokensMap(const std::vector<SpecialToken>& special_tokens) { |
| 139 | + tokens_map_.clear(); |
| 140 | + for (const auto& special_token : special_tokens) { |
| 141 | + tokens_map_.insert({special_token.id_, special_token}); |
| 142 | + } |
| 143 | + } |
| 144 | + friend void to_json(nlohmann::json& j, const SpecialTokensMap& tokens_map); |
| 145 | + friend void from_json(const nlohmann::json& j, SpecialTokensMap& tokens_map); |
| 146 | +}; |
| 147 | + |
| 148 | +struct TemplatePostProcessor : public PostProcessor { |
| 149 | + TemplatePostProcessor(); |
| 150 | + TemplatePostProcessor(const Template&, |
| 151 | + const Template&, |
| 152 | + const std::vector<SpecialToken>&); |
| 153 | + |
| 154 | + virtual void operator()(core::Encoding* encoding, |
| 155 | + core::Encoding* pair_encoding, |
| 156 | + bool add_special_tokens, |
| 157 | + core::Encoding* result_encoding) const override; |
| 158 | + virtual size_t AddedTokensNum(bool is_pair) const override; |
| 159 | + |
| 160 | + void UpdateSinglePieces(const std::string& template_str); |
| 161 | + void UpdateSinglePieces(const std::vector<std::string>& pieces); |
| 162 | + void UpdatePairPieces(const std::string& template_str); |
| 163 | + void UpdatePairPieces(const std::vector<std::string>& pieces); |
| 164 | + void UpdateAddedTokensNum(); |
| 165 | + void SetTokensMap(const std::vector<SpecialToken>& special_tokens); |
| 166 | + size_t CountAdded(Template* template_, |
| 167 | + const SpecialTokensMap& special_tokens_map); |
| 168 | + size_t DefaultAdded(bool is_single = true); |
| 169 | + void ApplyTemplate(const Template& pieces, |
| 170 | + core::Encoding* encoding, |
| 171 | + core::Encoding* pair_encoding, |
| 172 | + bool add_special_tokens, |
| 173 | + core::Encoding* result_encoding) const; |
| 174 | + |
| 175 | + friend void to_json(nlohmann::json& j, |
| 176 | + const TemplatePostProcessor& template_postprocessor); |
| 177 | + friend void from_json(const nlohmann::json& j, |
| 178 | + TemplatePostProcessor& template_postprocessor); |
| 179 | + |
| 180 | + Template single_; |
| 181 | + Template pair_; |
| 182 | + size_t added_single_; |
| 183 | + size_t added_pair_; |
| 184 | + SpecialTokensMap special_tokens_map_; |
| 185 | +}; |
| 186 | + |
| 187 | +} // postprocessors |
| 188 | +} // tokenizers |
0 commit comments