Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sentencepiece_trainer.cc « src - github.com/marian-nmt/sentencepiece.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f56f003a277e7c9d98127ddcccb2d7660c9bb4f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.!

#include "sentencepiece_trainer.h"
#include <string>
#include <vector>

#include "builder.h"
#include "common.h"
#include "flags.h"
#include "normalizer.h"
#include "sentencepiece.pb.h"
#include "sentencepiece_model.pb.h"
#include "trainer_factory.h"
#include "util.h"

namespace sentencepiece {
namespace {
static constexpr char kDefaultNormalizerName[] = "nmt_nfkc";
}  // namespace

// static
util::Status SentencePieceTrainer::Train(const TrainerSpec &trainer_spec) {
  NormalizerSpec normalizer_spec;
  return Train(trainer_spec, normalizer_spec);
}

// static
util::Status SentencePieceTrainer::Train(
    const TrainerSpec &trainer_spec, const NormalizerSpec &normalizer_spec) {
  auto copied_normalizer_spec = normalizer_spec;
  RETURN_IF_ERROR(PopulateNormalizerSpec(&copied_normalizer_spec));
  auto trainer = TrainerFactory::Create(trainer_spec, copied_normalizer_spec);
  return trainer->Train();
}

// static
NormalizerSpec SentencePieceTrainer::GetNormalizerSpec(
    util::min_string_view name) {
  NormalizerSpec spec;
  spec.set_name(name.data(), name.size());
  CHECK_OK(normalizer::Builder::GetPrecompiledCharsMap(
      spec.name(), spec.mutable_precompiled_charsmap()));
  return spec;
}

// static
util::Status SentencePieceTrainer::SetProtoField(
    util::min_string_view _field_name, util::min_string_view _value,
    google::protobuf::Message *message) {
  const absl::string_view field_name(_field_name.data(), _field_name.size());
  const absl::string_view value(_value.data(), _value.size());

  const auto *descriptor = message->GetDescriptor();
  const auto *reflection = message->GetReflection();

  CHECK_OR_RETURN(descriptor != nullptr && reflection != nullptr)
      << "reflection is not supported.";

  const auto *field = descriptor->FindFieldByName(
      std::string(field_name.data(), field_name.size()));

  if (field == nullptr) {
    return util::StatusBuilder(util::error::NOT_FOUND)
           << "unknown field name \"" << field_name << "\" in\n"
           << descriptor->DebugString();
  }

  std::vector<std::string> values = {std::string(value)};
  if (field->is_repeated())
    values = string_util::Split(std::string(value), ",");

#define SET_FIELD(METHOD_TYPE, v)                    \
  if (field->is_repeated())                          \
    reflection->Add##METHOD_TYPE(message, field, v); \
  else                                               \
    reflection->Set##METHOD_TYPE(message, field, v);

#define DEFINE_SET_FIELD(PROTO_TYPE, CPP_TYPE, FUNC_PREFIX, METHOD_TYPE,       \
                         EMPTY)                                                \
  case google::protobuf::FieldDescriptor::CPPTYPE_##PROTO_TYPE: {              \
    CPP_TYPE v;                                                                \
    if (!string_util::lexical_cast(value.empty() ? EMPTY : value, &v))         \
      return util::StatusBuilder(util::error::INVALID_ARGUMENT)                \
             << "cannot parse \"" << value << "\" as \"" << field->type_name() \
             << "\".";                                                         \
    SET_FIELD(METHOD_TYPE, v);                                                 \
    break;                                                                     \
  }

  for (const auto &value : values) {
    switch (field->cpp_type()) {
      DEFINE_SET_FIELD(INT32, int32, i, Int32, "");
      DEFINE_SET_FIELD(INT64, int64, i, Int64, "");
      DEFINE_SET_FIELD(UINT32, uint32, i, UInt32, "");
      DEFINE_SET_FIELD(UINT64, uint64, i, UInt64, "");
      DEFINE_SET_FIELD(DOUBLE, double, d, Double, "");
      DEFINE_SET_FIELD(FLOAT, float, f, Float, "");
      DEFINE_SET_FIELD(BOOL, bool, b, Bool, "true");
      case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
        SET_FIELD(String, value);
        break;
      case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
        const auto *enum_value =
            field->enum_type()->FindValueByName(string_util::ToUpper(value));
        if (enum_value == nullptr)
          return util::StatusBuilder(util::error::INVALID_ARGUMENT)
                 << "unknown enumeration value of \"" << value
                 << "\" for field \"" << field->name() << "\".";
        SET_FIELD(Enum, enum_value);
        break;
      }
      default:
        return util::StatusBuilder(util::error::UNIMPLEMENTED)
               << "proto type \"" << field->cpp_type_name()
               << "\" is not supported.";
    }
  }

  return util::OkStatus();
}

// static
util::Status SentencePieceTrainer::MergeSpecsFromArgs(
    util::min_string_view _args, TrainerSpec *trainer_spec,
    NormalizerSpec *normalizer_spec) {
  CHECK_OR_RETURN(trainer_spec) << "`trainer_spec` must not be null.";
  CHECK_OR_RETURN(normalizer_spec) << "`normalizer_spec` must not be null.";

  absl::string_view args(_args.data(), _args.size());
  if (args.empty()) return util::OkStatus();

  for (auto arg : string_util::SplitPiece(args, " ")) {
    string_util::ConsumePrefix(&arg, "--");
    std::string key, value;
    auto pos = arg.find("=");
    if (pos == absl::string_view::npos) {
      key = std::string(arg);
    } else {
      key = std::string(arg.substr(0, pos));
      value = std::string(arg.substr(pos + 1));
    }

    // Exception.
    if (key == "normalization_rule_name") {
      normalizer_spec->set_name(value);
      continue;
    }

    const auto status_train = SetProtoField(key, value, trainer_spec);
    if (status_train.ok()) continue;
    if (!util::IsNotFound(status_train)) return status_train;

    const auto status_norm = SetProtoField(key, value, normalizer_spec);
    if (status_norm.ok()) continue;
    if (!util::IsNotFound(status_norm)) return status_norm;

    // Not found both in trainer_spec and normalizer_spec.
    if (util::IsNotFound(status_train) && util::IsNotFound(status_norm)) {
      return status_train;
    }
  }

  return util::OkStatus();
}

// static
util::Status SentencePieceTrainer::Train(util::min_string_view args) {
  TrainerSpec trainer_spec;
  NormalizerSpec normalizer_spec;
  RETURN_IF_ERROR(MergeSpecsFromArgs(args, &trainer_spec, &normalizer_spec));
  return Train(trainer_spec, normalizer_spec);
}

// static
util::Status SentencePieceTrainer::PopulateNormalizerSpec(
    NormalizerSpec *normalizer_spec) {
  CHECK_OR_RETURN(normalizer_spec);

  if (!normalizer_spec->normalization_rule_tsv().empty()) {
    CHECK_OR_RETURN(normalizer_spec->precompiled_charsmap().empty())
        << "precompiled_charsmap is already defined.";
    normalizer::Builder::CharsMap chars_map;
    RETURN_IF_ERROR(normalizer::Builder::LoadCharsMap(
        normalizer_spec->normalization_rule_tsv(), &chars_map));
    RETURN_IF_ERROR(normalizer::Builder::CompileCharsMap(
        chars_map, normalizer_spec->mutable_precompiled_charsmap()));
    normalizer_spec->set_name("user_defined");
  } else {
    if (normalizer_spec->name().empty()) {
      normalizer_spec->set_name(kDefaultNormalizerName);
    }
    if (normalizer_spec->precompiled_charsmap().empty()) {
      RETURN_IF_ERROR(normalizer::Builder::GetPrecompiledCharsMap(
          normalizer_spec->name(),
          normalizer_spec->mutable_precompiled_charsmap()));
    }
  }

  return util::OkStatus();
}

}  // namespace sentencepiece