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 #ifndef TENSORFLOW_LITE_DELEGATES_HEXAGON_BUILDERS_TESTS_HEXAGON_DELEGATE_OP_MODEL_H_
16 #define TENSORFLOW_LITE_DELEGATES_HEXAGON_BUILDERS_TESTS_HEXAGON_DELEGATE_OP_MODEL_H_
17 
18 #include <algorithm>
19 
20 #include <gtest/gtest.h>
21 #include "tensorflow/lite/c/common.h"
22 #include "tensorflow/lite/delegates/hexagon/hexagon_delegate.h"
23 #include "tensorflow/lite/interpreter.h"
24 #include "tensorflow/lite/kernels/internal/reference/reference_ops.h"
25 #include "tensorflow/lite/kernels/internal/tensor.h"
26 #include "tensorflow/lite/kernels/internal/types.h"
27 #include "tensorflow/lite/kernels/register.h"
28 #include "tensorflow/lite/kernels/test_util.h"
29 #include "tensorflow/lite/model.h"
30 #include "tensorflow/lite/schema/schema_generated.h"
31 
32 namespace tflite {
33 class SingleOpModelWithHexagon : public SingleOpModel {
34  public:
SingleOpModelWithHexagon()35   SingleOpModelWithHexagon() : delegate_(nullptr, [](TfLiteDelegate*) {}) {}
36 
ApplyDelegateAndInvoke()37   void ApplyDelegateAndInvoke() {
38     static const char kDelegateName[] = "TfLiteHexagonDelegate";
39 
40     // Make sure we set the environment.
41     setenv(
42         "ADSP_LIBRARY_PATH",
43         "/data/local/tmp/hexagon_delegate_test;/system/lib/rfsa/adsp;/system/"
44         "vendor/lib/rfsa/adsp;/dsp",
45         1 /*overwrite*/);
46 
47     // For tests, we use one-op-models.
48     params_.min_nodes_per_partition = 1;
49     auto* delegate_ptr = TfLiteHexagonDelegateCreate(&params_);
50     ASSERT_TRUE(delegate_ptr != nullptr);
51     delegate_ = Interpreter::TfLiteDelegatePtr(
52         delegate_ptr, [](TfLiteDelegate* delegate) {
53           TfLiteHexagonDelegateDelete(delegate);
54           // Turn off the fast rpc and cleanup.
55           // Any communication with the DSP will fail unless new
56           // HexagonDelegateInit called.
57           TfLiteHexagonTearDown();
58         });
59     TfLiteHexagonInit();
60     // Make sure we have valid interpreter.
61     ASSERT_TRUE(interpreter_ != nullptr);
62     // Add delegate.
63     EXPECT_TRUE(interpreter_->ModifyGraphWithDelegate(delegate_.get()) !=
64                 kTfLiteError);
65     // Make sure graph has one Op which is the delegate node.
66     ASSERT_EQ(1, interpreter_->execution_plan().size());
67     const int node = interpreter_->execution_plan()[0];
68     const auto* node_and_reg = interpreter_->node_and_registration(node);
69     ASSERT_TRUE(node_and_reg != nullptr);
70     ASSERT_TRUE(node_and_reg->second.custom_name != nullptr);
71     ASSERT_STREQ(kDelegateName, node_and_reg->second.custom_name);
72 
73     Invoke();
74   }
75 
76  protected:
77   using SingleOpModel::builder_;
78 
79  private:
80   Interpreter::TfLiteDelegatePtr delegate_;
81   TfLiteHexagonDelegateOptions params_ = {0};
82 };
83 }  // namespace tflite
84 
85 #endif  // TENSORFLOW_LITE_DELEGATES_HEXAGON_BUILDERS_TESTS_HEXAGON_DELEGATE_OP_MODEL_H_
86