1 /* Copyright 2018 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/core/api/op_resolver.h"
17 
18 #include "flatbuffers/flatbuffers.h"  // from @flatbuffers
19 #include "tensorflow/lite/c/common.h"
20 #include "tensorflow/lite/core/api/error_reporter.h"
21 #include "tensorflow/lite/schema/schema_utils.h"
22 
23 namespace tflite {
24 
GetRegistrationFromOpCode(const OperatorCode * opcode,const OpResolver & op_resolver,ErrorReporter * error_reporter,const TfLiteRegistration ** registration)25 TfLiteStatus GetRegistrationFromOpCode(
26     const OperatorCode* opcode, const OpResolver& op_resolver,
27     ErrorReporter* error_reporter, const TfLiteRegistration** registration) {
28   TfLiteStatus status = kTfLiteOk;
29   *registration = nullptr;
30   auto builtin_code = GetBuiltinCode(opcode);
31   int version = opcode->version();
32 
33   if (builtin_code > BuiltinOperator_MAX ||
34       builtin_code < BuiltinOperator_MIN) {
35     TF_LITE_REPORT_ERROR(
36         error_reporter,
37         "Op builtin_code out of range: %d. Are you using old TFLite binary "
38         "with newer model?",
39         builtin_code);
40     status = kTfLiteError;
41   } else if (builtin_code != BuiltinOperator_CUSTOM) {
42     *registration = op_resolver.FindOp(builtin_code, version);
43     if (*registration == nullptr) {
44       TF_LITE_REPORT_ERROR(
45           error_reporter,
46           "Didn't find op for builtin opcode '%s' version '%d'. "
47           "An older version of this builtin might be supported. "
48           "Are you using an old TFLite binary with a newer model?\n",
49           EnumNameBuiltinOperator(builtin_code), version);
50       status = kTfLiteError;
51     }
52   } else if (!opcode->custom_code()) {
53     TF_LITE_REPORT_ERROR(
54         error_reporter,
55         "Operator with CUSTOM builtin_code has no custom_code.\n");
56     status = kTfLiteError;
57   } else {
58     const char* name = opcode->custom_code()->c_str();
59     *registration = op_resolver.FindOp(name, version);
60     if (*registration == nullptr) {
61       // Do not report error for unresolved custom op, we do the final check
62       // while preparing ops.
63       status = kTfLiteError;
64     }
65   }
66   return status;
67 }
68 
69 }  // namespace tflite
70