Coverage Report

Created: 2026-08-31 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sentencepiece/src/bpe_model.h
Line
Count
Source
1
// Copyright 2016 Google Inc.
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
#ifndef BPE_MODEL_H_
16
#define BPE_MODEL_H_
17
18
#include "absl/strings/string_view.h"
19
#include "model_interface.h"
20
#include "sentencepiece_model.pb.h"
21
22
namespace sentencepiece {
23
namespace bpe {
24
25
// Segmentation model with BPE (Byte Pair Encoding)
26
// Details:
27
// Neural Machine Translation of Rare Words with Subword Units
28
// https://arxiv.org/abs/1508.07909
29
//
30
// https://en.wikipedia.org/wiki/Byte_pair_encoding
31
class Model : public ModelInterface {
32
 public:
33
  explicit Model(const ModelProto& model_proto);
34
  ~Model() override;
35
36
0
  EncodeResult Encode(absl::string_view normalized) const override {
37
0
    return SampleEncode(normalized, 0.0);
38
0
  }
39
40
  // Sampling with BPE-dropout: https://arxiv.org/pdf/1910.13267.pdf
41
  // `alpha` is dropout probability in BPE-dropout paper.
42
  // Skips merge operation with `alpha` probability.
43
  // When alpha <= 0.0, no sampling is performed.
44
  EncodeResult SampleEncode(absl::string_view normalized,
45
                            float alpha) const override;
46
47
0
  bool IsSampleEncodeAvailable() const override { return true; }
48
49
0
  bool IsNBestEncodeAvailable() const override { return false; }
50
};
51
}  // namespace bpe
52
}  // namespace sentencepiece
53
#endif  // BPE_MODEL_H_