From 89a8f8b2928793300da69105686b942c8200187d Mon Sep 17 00:00:00 2001 From: Karim Nosir Date: Tue, 21 Jul 2020 14:10:13 -0700 Subject: [PATCH] Update hexagon delegate to check for library version PiperOrigin-RevId: 322438763 Change-Id: Ice9390e2216ec958f16f3c92713606dba66f59ac --- .../delegates/hexagon/hexagon_delegate.cc | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tensorflow/lite/delegates/hexagon/hexagon_delegate.cc b/tensorflow/lite/delegates/hexagon/hexagon_delegate.cc index ef3162739a9..f03f300e380 100644 --- a/tensorflow/lite/delegates/hexagon/hexagon_delegate.cc +++ b/tensorflow/lite/delegates/hexagon/hexagon_delegate.cc @@ -74,6 +74,39 @@ class HexagonDelegate : public SimpleDelegateInterface { return options; } + bool VerifyDelegate() { + auto* hexagon_nn = HexagonNNImplementation(); + if (hexagon_nn == nullptr) { + return false; + } + if (hexagon_nn->hexagon_nn_version != nullptr && + hexagon_nn->hexagon_nn_hexagon_interface_version) { + int hexagon_nn_version = -1; + int hexagon_interface_version = + hexagon_nn->hexagon_nn_hexagon_interface_version(); + if (hexagon_nn->hexagon_nn_version(&hexagon_nn_version) != 0) { + TFLITE_LOG_PROD(tflite::TFLITE_LOG_WARNING, + "Failed to fetch Hexagon NN version. This might be " + "because you're using incompatible versions of " + "libhexagon_interface and libhexagon_nn_skel. " + "You must use compatible versions. " + "Refer to Tensorflow Lite Hexagon Delegate Guide."); + return false; + } + if (hexagon_nn_version != hexagon_interface_version) { + TFLITE_LOG_PROD( + tflite::TFLITE_LOG_WARNING, + "Incompatible versions between interface library and " + "libhexagon_skel %d vs %d. You must use compatible versions. " + "Refer to Tensorflow Lite Hexagon Delegate Guide.", + hexagon_interface_version, hexagon_nn_version); + return false; + } + } + return hexagon_nn->hexagon_nn_is_device_supported && + hexagon_nn->hexagon_nn_is_device_supported(); + } + private: TfLiteHexagonDelegateOptions params_; }; @@ -83,9 +116,16 @@ class HexagonDelegate : public SimpleDelegateInterface { TfLiteDelegate* TfLiteHexagonDelegateCreate( const TfLiteHexagonDelegateOptions* options) { + auto hexagon_delegate_interface = + std::make_unique(options); + if (!hexagon_delegate_interface->VerifyDelegate()) { + TFLITE_LOG_PROD_ONCE(tflite::TFLITE_LOG_INFO, + "Hexagon Delegate is not supported.\n"); + return nullptr; + } auto* initialized_delegate = tflite::TfLiteDelegateFactory::CreateSimpleDelegate( - std::make_unique(options)); + std::move(hexagon_delegate_interface)); if (options->enable_dynamic_batch_size) { initialized_delegate->flags |= kTfLiteDelegateFlagsAllowDynamicTensors; }