| 61 | } |
| 62 | |
| 63 | void ModelInterface::InitializePieces() { |
| 64 | pieces_.clear(); |
| 65 | reserved_id_map_.clear(); |
| 66 | unk_id_ = -1; |
| 67 | |
| 68 | std::set<absl::string_view> user_defined_symbols; |
| 69 | std::vector<bool> byte_found(256, false); |
| 70 | |
| 71 | int pieces_size = 0; |
| 72 | int reserved_id_map_size = 0; |
| 73 | for (int i = 0; i < model_proto_->pieces_size(); ++i) { |
| 74 | const auto &sp = model_proto_->pieces(i); |
| 75 | const bool is_normal_piece = |
| 76 | (sp.type() == ModelProto::SentencePiece::NORMAL || |
| 77 | sp.type() == ModelProto::SentencePiece::USER_DEFINED || |
| 78 | sp.type() == ModelProto::SentencePiece::UNUSED); |
| 79 | if (is_normal_piece) { |
| 80 | ++pieces_size; |
| 81 | } else { |
| 82 | ++reserved_id_map_size; |
| 83 | } |
| 84 | } |
| 85 | pieces_.reserve(pieces_size); |
| 86 | reserved_id_map_.reserve(reserved_id_map_size); |
| 87 | |
| 88 | for (int i = 0; i < model_proto_->pieces_size(); ++i) { |
| 89 | const auto &sp = model_proto_->pieces(i); |
| 90 | if (sp.piece().empty()) { |
| 91 | status_ = util::InternalError("piece must not be empty."); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | const bool is_normal_piece = |
| 96 | (sp.type() == ModelProto::SentencePiece::NORMAL || |
| 97 | sp.type() == ModelProto::SentencePiece::USER_DEFINED || |
| 98 | sp.type() == ModelProto::SentencePiece::UNUSED); |
| 99 | if (!port::InsertIfNotPresent( |
| 100 | is_normal_piece ? &pieces_ : &reserved_id_map_, sp.piece(), i)) { |
| 101 | status_ = util::InternalError(sp.piece() + " is already defined."); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | if (sp.type() == ModelProto::SentencePiece::USER_DEFINED) { |
| 106 | user_defined_symbols.insert(sp.piece()); |
| 107 | } |
| 108 | |
| 109 | if (sp.type() == ModelProto::SentencePiece::UNKNOWN) { |
| 110 | if (unk_id_ >= 0) { |
| 111 | status_ = util::InternalError("unk is already defined."); |
| 112 | return; |
| 113 | } |
| 114 | unk_id_ = i; |
| 115 | } |
| 116 | |
| 117 | if (sp.type() == ModelProto::SentencePiece::BYTE) { |
| 118 | if (!model_proto_->trainer_spec().byte_fallback()) { |
| 119 | status_ = |
| 120 | util::InternalError("byte piece " + sp.piece() + |
nothing calls this directly
no test coverage detected