2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "nnkit/support/tflite/AbstractBackend.h"
19 #include <tensorflow/lite/kernels/register.h>
20 #include <tensorflow/lite/model.h>
27 class GenericBackend final : public nnkit::support::tflite::AbstractBackend
30 GenericBackend(const std::string &path)
32 ::tflite::StderrReporter error_reporter;
34 _model = ::tflite::FlatBufferModel::BuildFromFile(path.c_str(), &error_reporter);
36 ::tflite::ops::builtin::BuiltinOpResolver resolver;
37 ::tflite::InterpreterBuilder builder(*_model, resolver);
39 if (kTfLiteOk != builder(&_interp))
41 throw std::runtime_error{"Failed to build a tflite interpreter"};
44 _interp->SetNumThreads(1);
48 ::tflite::Interpreter &interpreter(void) override { return *_interp; }
51 std::unique_ptr<::tflite::FlatBufferModel> _model;
52 std::unique_ptr<::tflite::Interpreter> _interp;
56 #include <nnkit/CmdlineArguments.h>
57 #include <stdex/Memory.h>
59 extern "C" std::unique_ptr<nnkit::Backend> make_backend(const nnkit::CmdlineArguments &args)
61 return stdex::make_unique<GenericBackend>(args.at(0));