1 /* Copyright 2019 The TensorFlow 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
16 #include "tensorflow_lite_support/codegen/metadata_helper.h"
17
18 #include "tensorflow_lite_support/codegen/utils.h"
19 #include "tensorflow_lite_support/metadata/metadata_schema_generated.h"
20
21 namespace tflite {
22 namespace support {
23 namespace codegen {
24
25 constexpr char BUFFER_KEY[] = "TFLITE_METADATA";
GetMetadataFromModel(const Model * model)26 const ModelMetadata* GetMetadataFromModel(const Model* model) {
27 if (model == nullptr || model->metadata() == nullptr) {
28 return nullptr;
29 }
30 for (auto i = 0; i < model->metadata()->size(); i++) {
31 const auto* name = model->metadata()->Get(i)->name();
32 if (name != nullptr && name->str() == BUFFER_KEY) {
33 const auto buffer_index = model->metadata()->Get(i)->buffer();
34 if (model->buffers() == nullptr ||
35 model->buffers()->size() <= buffer_index) {
36 continue;
37 }
38 const auto* buffer_vec = model->buffers()->Get(buffer_index)->data();
39 if (buffer_vec == nullptr || buffer_vec->data() == nullptr) {
40 continue;
41 }
42 return GetModelMetadata(buffer_vec->data());
43 }
44 }
45 return nullptr;
46 }
47
FindAssociatedFile(const TensorMetadata * metadata,const AssociatedFileType file_type,const std::string & tensor_identifier,ErrorReporter * err)48 int FindAssociatedFile(const TensorMetadata* metadata,
49 const AssociatedFileType file_type,
50 const std::string& tensor_identifier,
51 ErrorReporter* err) {
52 int result = -1;
53 if (metadata->associated_files() == nullptr ||
54 metadata->associated_files()->size() == 0) {
55 return result;
56 }
57 for (int i = 0; i < metadata->associated_files()->size(); i++) {
58 const auto* file_metadata = metadata->associated_files()->Get(i);
59 if (file_metadata->type() == file_type) {
60 if (result >= 0) {
61 err->Warning(
62 "Multiple associated file of type %d found on tensor %s. Only the "
63 "first one will be used.",
64 file_type, tensor_identifier.c_str());
65 continue;
66 }
67 result = i;
68 }
69 }
70 return result;
71 }
72
FindNormalizationUnit(const TensorMetadata * metadata,const std::string & tensor_identifier,ErrorReporter * err)73 int FindNormalizationUnit(const TensorMetadata* metadata,
74 const std::string& tensor_identifier,
75 ErrorReporter* err) {
76 int result = -1;
77 if (metadata->process_units() == nullptr ||
78 metadata->process_units()->size() == 0) {
79 return result;
80 }
81 for (int i = 0; i < metadata->process_units()->size(); i++) {
82 const auto* process_uint = metadata->process_units()->Get(i);
83 if (process_uint->options_type() ==
84 ProcessUnitOptions_NormalizationOptions) {
85 if (result >= 0) {
86 err->Warning(
87 "Multiple normalization unit found in tensor %s. Only the first "
88 "one will be effective.",
89 tensor_identifier.c_str());
90 continue;
91 }
92 result = i;
93 }
94 }
95 return result;
96 }
97
98 } // namespace codegen
99 } // namespace support
100 } // namespace tflite
101