aboutsummaryrefslogtreecommitdiff
path: root/tensorflow_lite_support/cc/task/text/nlclassifier/bert_nl_classifier.cc
blob: a246066b810feb0d350696052023048126cc1795 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.

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 "tensorflow_lite_support/cc/task/text/nlclassifier/bert_nl_classifier.h"

#include <stddef.h>

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "absl/status/status.h"
#include "absl/strings/ascii.h"
#include "absl/strings/str_format.h"
#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/core/api/op_resolver.h"
#include "tensorflow/lite/string_type.h"
#include "tensorflow_lite_support/cc/common.h"
#include "tensorflow_lite_support/cc/port/status_macros.h"
#include "tensorflow_lite_support/cc/task/core/category.h"
#include "tensorflow_lite_support/cc/task/core/task_api_factory.h"
#include "tensorflow_lite_support/cc/task/core/task_utils.h"
#include "tensorflow_lite_support/cc/task/text/nlclassifier/nl_classifier.h"
#include "tensorflow_lite_support/cc/text/tokenizers/tokenizer.h"
#include "tensorflow_lite_support/cc/text/tokenizers/tokenizer_utils.h"
#include "tensorflow_lite_support/metadata/cc/metadata_extractor.h"

namespace tflite {
namespace task {
namespace text {
namespace nlclassifier {

using ::tflite::support::CreateStatusWithPayload;
using ::tflite::support::StatusOr;
using ::tflite::support::TfLiteSupportStatus;
using ::tflite::support::text::tokenizer::CreateTokenizerFromProcessUnit;
using ::tflite::support::text::tokenizer::TokenizerResult;
using ::tflite::task::core::FindTensorByName;
using ::tflite::task::core::PopulateTensor;

namespace {
constexpr char kIdsTensorName[] = "ids";
constexpr char kMaskTensorName[] = "mask";
constexpr char kSegmentIdsTensorName[] = "segment_ids";
constexpr int kIdsTensorIndex = 0;
constexpr int kMaskTensorIndex = 1;
constexpr int kSegmentIdsTensorIndex = 2;
constexpr char kScoreTensorName[] = "probability";
constexpr char kClassificationToken[] = "[CLS]";
constexpr char kSeparator[] = "[SEP]";
constexpr int kTokenizerProcessUnitIndex = 0;
}  // namespace

// TODO(b/241507692) Add a unit test for a model with dynamic tensors.
absl::Status BertNLClassifier::Preprocess(
    const std::vector<TfLiteTensor*>& input_tensors, const std::string& input) {
  auto* input_tensor_metadatas =
      GetMetadataExtractor()->GetInputTensorMetadata();
  auto* ids_tensor =
      FindTensorByName(input_tensors, input_tensor_metadatas, kIdsTensorName);
  auto* mask_tensor =
      FindTensorByName(input_tensors, input_tensor_metadatas, kMaskTensorName);
  auto* segment_ids_tensor = FindTensorByName(
      input_tensors, input_tensor_metadatas, kSegmentIdsTensorName);

  std::string processed_input = input;
  absl::AsciiStrToLower(&processed_input);

  TokenizerResult input_tokenize_results;
  input_tokenize_results = tokenizer_->Tokenize(processed_input);

  // Offset by 2 to account for [CLS] and [SEP]
  int input_tokens_size =
      static_cast<int>(input_tokenize_results.subwords.size()) + 2;
  int input_tensor_length = input_tokens_size;
  if (!input_tensors_are_dynamic_) {
    input_tokens_size = std::min(kMaxSeqLen, input_tokens_size);
    input_tensor_length = kMaxSeqLen;
  } else {
    GetTfLiteEngine()->interpreter()->ResizeInputTensorStrict(kIdsTensorIndex,
                                                    {1, input_tensor_length});
    GetTfLiteEngine()->interpreter()->ResizeInputTensorStrict(kMaskTensorIndex,
                                                    {1, input_tensor_length});
    GetTfLiteEngine()->interpreter()->ResizeInputTensorStrict(kSegmentIdsTensorIndex,
                                                    {1, input_tensor_length});
    GetTfLiteEngine()->interpreter()->AllocateTensors();
  }

  std::vector<std::string> input_tokens;
  input_tokens.reserve(input_tokens_size);
  input_tokens.push_back(std::string(kClassificationToken));
  for (int i = 0; i < input_tokens_size - 2; ++i) {
    input_tokens.push_back(std::move(input_tokenize_results.subwords[i]));
  }
  input_tokens.push_back(std::string(kSeparator));

  std::vector<int> input_ids(input_tensor_length, 0);
  std::vector<int> input_mask(input_tensor_length, 0);
  // Convert tokens back into ids and set mask
  for (int i = 0; i < input_tokens.size(); ++i) {
    tokenizer_->LookupId(input_tokens[i], &input_ids[i]);
    input_mask[i] = 1;
  }
  //                           |<--------input_tensor_length------->|
  // input_ids                 [CLS] s1  s2...  sn [SEP]  0  0...  0
  // input_masks                 1    1   1...  1    1    0  0...  0
  // segment_ids                 0    0   0...  0    0    0  0...  0

  PopulateTensor(input_ids, ids_tensor);
  PopulateTensor(input_mask, mask_tensor);
  PopulateTensor(std::vector<int>(input_tensor_length, 0), segment_ids_tensor);

  return absl::OkStatus();
}

StatusOr<std::vector<core::Category>> BertNLClassifier::Postprocess(
    const std::vector<const TfLiteTensor*>& output_tensors,
    const std::string& /*input*/) {
  if (output_tensors.size() != 1) {
    return CreateStatusWithPayload(
        absl::StatusCode::kInvalidArgument,
        absl::StrFormat("BertNLClassifier models are expected to have only 1 "
                        "output, found %d",
                        output_tensors.size()),
        TfLiteSupportStatus::kInvalidNumOutputTensorsError);
  }
  const TfLiteTensor* scores = FindTensorByName(
      output_tensors, GetMetadataExtractor()->GetOutputTensorMetadata(),
      kScoreTensorName);

  // optional labels extracted from metadata
  return BuildResults(scores, /*labels=*/nullptr);
}

StatusOr<std::unique_ptr<BertNLClassifier>>
BertNLClassifier::CreateFromFile(
    const std::string& path_to_model_with_metadata,
    std::unique_ptr<tflite::OpResolver> resolver) {
  std::unique_ptr<BertNLClassifier> bert_nl_classifier;
  ASSIGN_OR_RETURN(bert_nl_classifier,
                   core::TaskAPIFactory::CreateFromFile<BertNLClassifier>(
                       path_to_model_with_metadata, std::move(resolver)));
  RETURN_IF_ERROR(bert_nl_classifier->InitializeFromMetadata());
  return std::move(bert_nl_classifier);
}

StatusOr<std::unique_ptr<BertNLClassifier>>
BertNLClassifier::CreateFromBuffer(
    const char* model_with_metadata_buffer_data,
    size_t model_with_metadata_buffer_size,
    std::unique_ptr<tflite::OpResolver> resolver) {
  std::unique_ptr<BertNLClassifier> bert_nl_classifier;
  ASSIGN_OR_RETURN(bert_nl_classifier,
                   core::TaskAPIFactory::CreateFromBuffer<BertNLClassifier>(
                       model_with_metadata_buffer_data,
                       model_with_metadata_buffer_size, std::move(resolver)));
  RETURN_IF_ERROR(bert_nl_classifier->InitializeFromMetadata());
  return std::move(bert_nl_classifier);
}

StatusOr<std::unique_ptr<BertNLClassifier>> BertNLClassifier::CreateFromFd(
    int fd, std::unique_ptr<tflite::OpResolver> resolver) {
  std::unique_ptr<BertNLClassifier> bert_nl_classifier;
  ASSIGN_OR_RETURN(
      bert_nl_classifier,
      core::TaskAPIFactory::CreateFromFileDescriptor<BertNLClassifier>(
          fd, std::move(resolver)));
  RETURN_IF_ERROR(bert_nl_classifier->InitializeFromMetadata());
  return std::move(bert_nl_classifier);
}

absl::Status BertNLClassifier::InitializeFromMetadata() {
  // Set up mandatory tokenizer.
  const ProcessUnit* tokenizer_process_unit =
      GetMetadataExtractor()->GetInputProcessUnit(kTokenizerProcessUnitIndex);
  if (tokenizer_process_unit == nullptr) {
    return CreateStatusWithPayload(
        absl::StatusCode::kInvalidArgument,
        "No input process unit found from metadata.",
        TfLiteSupportStatus::kMetadataInvalidTokenizerError);
  }
  ASSIGN_OR_RETURN(tokenizer_,
                   CreateTokenizerFromProcessUnit(tokenizer_process_unit,
                                                  GetMetadataExtractor()));

  // Set up optional label vector.
  TrySetLabelFromMetadata(
      GetMetadataExtractor()->GetOutputTensorMetadata(kOutputTensorIndex))
      .IgnoreError();

  auto* input_tensor_metadatas =
      GetMetadataExtractor()->GetInputTensorMetadata();
  const auto& input_tensors = GetInputTensors();
  const auto& ids_tensor = *FindTensorByName(input_tensors, input_tensor_metadatas,
                                             kIdsTensorName);
  const auto& mask_tensor = *FindTensorByName(input_tensors, input_tensor_metadatas,
                                              kMaskTensorName);
  const auto& segment_ids_tensor = *FindTensorByName(input_tensors, input_tensor_metadatas,
                                                     kSegmentIdsTensorName);
  if (ids_tensor.dims->size != 2 || mask_tensor.dims->size != 2 ||
      segment_ids_tensor.dims->size != 2) {
    return CreateStatusWithPayload(
        absl::StatusCode::kInternal,
        absl::StrFormat(
            "The three input tensors in Bert models are expected to have dim "
            "2, but got ids_tensor (%d), mask_tensor (%d), segment_ids_tensor "
            "(%d).",
            ids_tensor.dims->size, mask_tensor.dims->size,
            segment_ids_tensor.dims->size),
        TfLiteSupportStatus::kInvalidInputTensorDimensionsError);
  }
  if (ids_tensor.dims->data[0] != 1 || mask_tensor.dims->data[0] != 1 ||
      segment_ids_tensor.dims->data[0] != 1) {
    return CreateStatusWithPayload(
        absl::StatusCode::kInternal,
        absl::StrFormat(
            "The three input tensors in Bert models are expected to have same "
            "batch size 1, but got ids_tensor (%d), mask_tensor (%d), "
            "segment_ids_tensor (%d).",
            ids_tensor.dims->data[0], mask_tensor.dims->data[0],
            segment_ids_tensor.dims->data[0]),
        TfLiteSupportStatus::kInvalidInputTensorSizeError);
  }
  if (ids_tensor.dims->data[1] != mask_tensor.dims->data[1] ||
      ids_tensor.dims->data[1] != segment_ids_tensor.dims->data[1]) {
    return CreateStatusWithPayload(
        absl::StatusCode::kInternal,
        absl::StrFormat("The three input tensors in Bert models are "
                        "expected to have same length, but got ids_tensor "
                        "(%d), mask_tensor (%d), segment_ids_tensor (%d).",
                        ids_tensor.dims->data[1], mask_tensor.dims->data[1],
                        segment_ids_tensor.dims->data[1]),
        TfLiteSupportStatus::kInvalidInputTensorSizeError);
  }
  if (ids_tensor.dims_signature->data[1] == -1 &&
      mask_tensor.dims_signature->data[1] == -1 &&
      segment_ids_tensor.dims_signature->data[1] == -1) {
    input_tensors_are_dynamic_ = true;
  } else if (ids_tensor.dims_signature->data[1] == -1 ||
             mask_tensor.dims_signature->data[1] == -1 ||
             segment_ids_tensor.dims_signature->data[1] == -1) {
    return CreateStatusWithPayload(
        absl::StatusCode::kInternal,
        "Input tensors contain a mix of static and dynamic tensors",
        TfLiteSupportStatus::kInvalidInputTensorSizeError);
  }

  return absl::OkStatus();
}

}  // namespace nlclassifier
}  // namespace text
}  // namespace task
}  // namespace tflite