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 #include <memory> 16 17 #include "absl/memory/memory.h" 18 #include "tensorflow/lite/experimental/acceleration/configuration/configuration_generated.h" 19 #include "tensorflow/lite/experimental/acceleration/configuration/delegate_registry.h" 20 #include "tensorflow/lite/minimal_logging.h" 21 22 // Guarding anyway although this file not expected to be compiled for non-Apple. 23 #if defined(__APPLE__) 24 #include "tensorflow/lite/delegates/coreml/coreml_delegate.h" 25 26 namespace tflite { 27 namespace delegates { 28 class CoreMLPlugin : public DelegatePluginInterface { 29 public: Create()30 TfLiteDelegatePtr Create() override { 31 TfLiteDelegate* delegate_ptr = TfLiteCoreMlDelegateCreate(&options_); 32 TfLiteDelegatePtr delegate(delegate_ptr, [](TfLiteDelegate* delegate) { 33 TfLiteCoreMlDelegateDelete(delegate); 34 }); 35 return delegate; 36 } GetDelegateErrno(TfLiteDelegate *)37 int GetDelegateErrno(TfLiteDelegate* /* from_delegate */) override { 38 return 0; 39 } New(const TFLiteSettings & tflite_settings)40 static std::unique_ptr<CoreMLPlugin> New( 41 const TFLiteSettings& tflite_settings) { 42 return absl::make_unique<CoreMLPlugin>(tflite_settings); 43 } CoreMLPlugin(const TFLiteSettings & tflite_settings)44 explicit CoreMLPlugin(const TFLiteSettings& tflite_settings) { 45 const CoreMLSettings* settings = tflite_settings.coreml_settings(); 46 options_ = TfLiteCoreMlDelegateOptions({}); 47 // Using the proto defaults if the settings were not set. 48 switch (settings->enabled_devices()) { 49 case tflite::CoreMLSettings_::EnabledDevices_DEVICES_ALL: 50 options_.enabled_devices = TfLiteCoreMlDelegateAllDevices; 51 break; 52 case tflite::CoreMLSettings_::EnabledDevices_DEVICES_WITH_NEURAL_ENGINE: 53 options_.enabled_devices = TfLiteCoreMlDelegateDevicesWithNeuralEngine; 54 break; 55 default: 56 TFLITE_LOG_PROD(TFLITE_LOG_ERROR, "Invalid devices enum: %d", 57 settings->enabled_devices()); 58 } 59 options_.coreml_version = settings->coreml_version(); 60 options_.max_delegated_partitions = settings->max_delegated_partitions(); 61 options_.min_nodes_per_partition = settings->min_nodes_per_partition(); 62 } 63 64 private: 65 TfLiteCoreMlDelegateOptions options_; 66 }; 67 68 TFLITE_REGISTER_DELEGATE_FACTORY_FUNCTION(CoreMLPlugin, CoreMLPlugin::New); 69 70 } // namespace delegates 71 } // namespace tflite 72 73 #endif // __APPLE__ 74