xref: /aosp_15_r20/external/armnn/delegate/classic/src/test/DelegateTestInterpreter.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include <DelegateTestInterpreter.hpp>
7 
8 #include <armnn_delegate.hpp>
9 
10 namespace delegateTestInterpreter
11 {
12 
DelegateTestInterpreter(std::vector<char> & modelBuffer,const std::vector<armnn::BackendId> & backends,const std::string & customOp,bool disableFallback)13 DelegateTestInterpreter::DelegateTestInterpreter(std::vector<char>& modelBuffer,
14                                                  const std::vector<armnn::BackendId>& backends,
15                                                  const std::string& customOp,
16                                                  bool disableFallback)
17 {
18     TfLiteModel* tfLiteModel = delegateTestInterpreter::CreateTfLiteModel(modelBuffer);
19 
20     TfLiteInterpreterOptions* options = delegateTestInterpreter::CreateTfLiteInterpreterOptions();
21     if (!customOp.empty())
22     {
23         options->mutable_op_resolver = delegateTestInterpreter::GenerateCustomOpResolver(customOp);
24     }
25 
26     // Disable fallback by default for unit tests unless specified.
27     armnnDelegate::DelegateOptions delegateOptions(backends);
28     delegateOptions.DisableTfLiteRuntimeFallback(disableFallback);
29 
30     auto armnnDelegate = armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions);
31     TfLiteInterpreterOptionsAddDelegate(options, armnnDelegate);
32 
33     m_TfLiteDelegate = armnnDelegate;
34     m_TfLiteInterpreter = TfLiteInterpreterCreate(tfLiteModel, options);
35 
36     // The options and model can be deleted after the interpreter is created.
37     TfLiteInterpreterOptionsDelete(options);
38     TfLiteModelDelete(tfLiteModel);
39 }
40 
DelegateTestInterpreter(std::vector<char> & modelBuffer,const armnnDelegate::DelegateOptions & delegateOptions,const std::string & customOp)41 DelegateTestInterpreter::DelegateTestInterpreter(std::vector<char>& modelBuffer,
42                                                  const armnnDelegate::DelegateOptions& delegateOptions,
43                                                  const std::string& customOp)
44 {
45     TfLiteModel* tfLiteModel = delegateTestInterpreter::CreateTfLiteModel(modelBuffer);
46 
47     TfLiteInterpreterOptions* options = delegateTestInterpreter::CreateTfLiteInterpreterOptions();
48     if (!customOp.empty())
49     {
50         options->mutable_op_resolver = delegateTestInterpreter::GenerateCustomOpResolver(customOp);
51     }
52 
53     auto armnnDelegate = armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions);
54     TfLiteInterpreterOptionsAddDelegate(options, armnnDelegate);
55 
56     m_TfLiteDelegate = armnnDelegate;
57     m_TfLiteInterpreter = TfLiteInterpreterCreate(tfLiteModel, options);
58 
59     // The options and model can be deleted after the interpreter is created.
60     TfLiteInterpreterOptionsDelete(options);
61     TfLiteModelDelete(tfLiteModel);
62 }
63 
Cleanup()64 void DelegateTestInterpreter::Cleanup()
65 {
66     TfLiteInterpreterDelete(m_TfLiteInterpreter);
67 
68     if (m_TfLiteDelegate)
69     {
70         armnnDelegate::TfLiteArmnnDelegateDelete(static_cast<TfLiteDelegate*>(m_TfLiteDelegate));
71     }
72 }
73 
74 } // anonymous namespace