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/delegates/xnnpack/xnnpack_delegate.h" 19 #include "tensorflow/lite/experimental/acceleration/configuration/configuration_generated.h" 20 #include "tensorflow/lite/experimental/acceleration/configuration/delegate_registry.h" 21 22 namespace tflite { 23 namespace delegates { 24 class XNNPackPlugin : public DelegatePluginInterface { 25 public: Create()26 TfLiteDelegatePtr Create() override { 27 return TfLiteDelegatePtr(TfLiteXNNPackDelegateCreate(&options_), 28 TfLiteXNNPackDelegateDelete); 29 } GetDelegateErrno(TfLiteDelegate * from_delegate)30 int GetDelegateErrno(TfLiteDelegate* from_delegate) override { return 0; } New(const TFLiteSettings & acceleration)31 static std::unique_ptr<DelegatePluginInterface> New( 32 const TFLiteSettings& acceleration) { 33 return std::make_unique<XNNPackPlugin>(acceleration); 34 } XNNPackPlugin(const TFLiteSettings & tflite_settings)35 explicit XNNPackPlugin(const TFLiteSettings& tflite_settings) 36 : options_(TfLiteXNNPackDelegateOptionsDefault()) { 37 const auto* xnnpack_settings = tflite_settings.xnnpack_settings(); 38 if (xnnpack_settings) { 39 options_.num_threads = xnnpack_settings->num_threads(); 40 options_.flags = xnnpack_settings->flags(); 41 } 42 } 43 44 private: 45 TfLiteXNNPackDelegateOptions options_; 46 }; 47 48 TFLITE_REGISTER_DELEGATE_FACTORY_FUNCTION(XNNPackPlugin, XNNPackPlugin::New); 49 50 } // namespace delegates 51 } // namespace tflite 52