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 namespace tflite {
19
GetRegistrationFromOpCode(const OperatorCode * opcode,const OpResolver & op_resolver,ErrorReporter * error_reporter,const TfLiteRegistration ** registration)20 TfLiteStatus GetRegistrationFromOpCode(
21 const OperatorCode* opcode, const OpResolver& op_resolver,
22 ErrorReporter* error_reporter, const TfLiteRegistration** registration) {
23 TfLiteStatus status = kTfLiteOk;
24 *registration = nullptr;
25 auto builtin_code = opcode->builtin_code();
26 int version = opcode->version();
27
28 if (builtin_code > BuiltinOperator_MAX ||
29 builtin_code < BuiltinOperator_MIN) {
30 error_reporter->Report(
31 "Op builtin_code out of range: %d. Are you using old TFLite binary "
32 "with newer model?",
33 builtin_code);
34 status = kTfLiteError;
35 } else if (builtin_code != BuiltinOperator_CUSTOM) {
36 *registration = op_resolver.FindOp(builtin_code, version);
37 if (*registration == nullptr) {
38 error_reporter->Report(
39 "Didn't find op for builtin opcode '%s' version '%d'\n",
40 EnumNameBuiltinOperator(builtin_code), version);
41 status = kTfLiteError;
42 }
43 } else if (!opcode->custom_code()) {
44 error_reporter->Report(
45 "Operator with CUSTOM builtin_code has no custom_code.\n");
46 status = kTfLiteError;
47 } else {
48 const char* name = opcode->custom_code()->c_str();
49 *registration = op_resolver.FindOp(name, version);
50 if (*registration == nullptr) {
51 error_reporter->Report(
52 "Didn't find custom op for name '%s' with version %d\n", name,
53 version);
54 status = kTfLiteError;
55 }
56 }
57 return status;
58 }
59
60 } // namespace tflite
61