1 /* Copyright 2020 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 #ifndef TENSORFLOW_LITE_SUPPORT_CC_TASK_CORE_TASK_API_FACTORY_H_
17 #define TENSORFLOW_LITE_SUPPORT_CC_TASK_CORE_TASK_API_FACTORY_H_
18 
19 #include <memory>
20 
21 #include "absl/status/status.h"
22 #include "tensorflow/lite/core/api/op_resolver.h"
23 #include "tensorflow/lite/kernels/op_macros.h"
24 #include "tensorflow_lite_support/cc/port/status_macros.h"
25 #include "tensorflow_lite_support/cc/port/statusor.h"
26 #include "tensorflow_lite_support/cc/task/core/base_task_api.h"
27 #include "tensorflow_lite_support/cc/task/core/proto/external_file_proto_inc.h"
28 #include "tensorflow_lite_support/cc/task/core/tflite_engine.h"
29 
30 namespace tflite {
31 namespace task {
32 namespace core {
33 template <typename T>
34 using EnableIfBaseUntypedTaskApiSubclass = typename std::enable_if<
35     std::is_base_of<BaseUntypedTaskApi, T>::value>::type*;
36 
37 // Template creator for all subclasses of BaseTaskApi
38 class TaskAPIFactory {
39  public:
40   TaskAPIFactory() = delete;
41 
42   template <typename T, EnableIfBaseUntypedTaskApiSubclass<T> = nullptr>
43   static tflite::support::StatusOr<std::unique_ptr<T>> CreateFromBuffer(
44       const char* buffer_data, size_t buffer_size,
45       std::unique_ptr<tflite::OpResolver> resolver =
46           absl::make_unique<tflite::ops::builtin::BuiltinOpResolver>(),
47       int num_threads = 1) {
48     auto engine = absl::make_unique<TfLiteEngine>(std::move(resolver));
49     RETURN_IF_ERROR(engine->BuildModelFromFlatBuffer(buffer_data, buffer_size));
50     return CreateFromTfLiteEngine<T>(std::move(engine), num_threads);
51   }
52 
53   template <typename T, EnableIfBaseUntypedTaskApiSubclass<T> = nullptr>
54   static tflite::support::StatusOr<std::unique_ptr<T>> CreateFromFile(
55       const string& file_name,
56       std::unique_ptr<tflite::OpResolver> resolver =
57           absl::make_unique<tflite::ops::builtin::BuiltinOpResolver>(),
58       int num_threads = 1) {
59     auto engine = absl::make_unique<TfLiteEngine>(std::move(resolver));
60     RETURN_IF_ERROR(engine->BuildModelFromFile(file_name));
61     return CreateFromTfLiteEngine<T>(std::move(engine), num_threads);
62   }
63 
64   template <typename T, EnableIfBaseUntypedTaskApiSubclass<T> = nullptr>
65   static tflite::support::StatusOr<std::unique_ptr<T>> CreateFromFileDescriptor(
66       int file_descriptor,
67       std::unique_ptr<tflite::OpResolver> resolver =
68           absl::make_unique<tflite::ops::builtin::BuiltinOpResolver>(),
69       int num_threads = 1) {
70     auto engine = absl::make_unique<TfLiteEngine>(std::move(resolver));
71     RETURN_IF_ERROR(engine->BuildModelFromFileDescriptor(file_descriptor));
72     return CreateFromTfLiteEngine<T>(std::move(engine), num_threads);
73   }
74 
75   template <typename T, EnableIfBaseUntypedTaskApiSubclass<T> = nullptr>
76   static tflite::support::StatusOr<std::unique_ptr<T>>
77   CreateFromExternalFileProto(
78       const ExternalFile* external_file,
79       std::unique_ptr<tflite::OpResolver> resolver =
80           absl::make_unique<tflite::ops::builtin::BuiltinOpResolver>(),
81       int num_threads = 1) {
82     auto engine = absl::make_unique<TfLiteEngine>(std::move(resolver));
83     RETURN_IF_ERROR(engine->BuildModelFromExternalFileProto(external_file));
84     return CreateFromTfLiteEngine<T>(std::move(engine), num_threads);
85   }
86 
87  private:
88   template <typename T, EnableIfBaseUntypedTaskApiSubclass<T> = nullptr>
CreateFromTfLiteEngine(std::unique_ptr<TfLiteEngine> engine,int num_threads)89   static tflite::support::StatusOr<std::unique_ptr<T>> CreateFromTfLiteEngine(
90       std::unique_ptr<TfLiteEngine> engine, int num_threads) {
91     RETURN_IF_ERROR(engine->InitInterpreter(num_threads));
92     return absl::make_unique<T>(std::move(engine));
93   }
94 };
95 
96 }  // namespace core
97 }  // namespace task
98 }  // namespace tflite
99 
100 #endif  // TENSORFLOW_LITE_SUPPORT_CC_TASK_CORE_TASK_API_FACTORY_H_
101