blob: c46d3e166a3383c3a269f65ecd543a330b31d5fc [file] [log] [blame]
Matthew Sloyan65c21a12023-04-04 12:06:14 +01001//
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
Matthew Sloyan65c21a12023-04-04 12:06:14 +010010
11namespace delegateTestInterpreter
12{
13
14DelegateTestInterpreter::DelegateTestInterpreter(std::vector<char>& modelBuffer,
15 const std::vector<armnn::BackendId>& backends,
16 const std::string& customOp,
17 bool disableFallback)
18{
Matthew Sloyan65c21a12023-04-04 12:06:14 +010019 TfLiteModel* tfLiteModel = delegateTestInterpreter::CreateTfLiteModel(modelBuffer);
20
21 TfLiteInterpreterOptions* options = delegateTestInterpreter::CreateTfLiteInterpreterOptions();
22 if (!customOp.empty())
23 {
24 options->mutable_op_resolver = delegateTestInterpreter::GenerateCustomOpResolver(customOp);
25 }
26
Teresa Charlin3e4b6082023-10-19 19:13:29 +010027 // Disable fallback by default for unit tests unless specified.
28 armnnDelegate::DelegateOptions delegateOptions(backends);
29 delegateOptions.DisableTfLiteRuntimeFallback(disableFallback);
30
31 auto armnnDelegate = armnnOpaqueDelegate::TfLiteArmnnOpaqueDelegateCreate(delegateOptions);
Matthew Sloyan65c21a12023-04-04 12:06:14 +010032 TfLiteInterpreterOptionsAddDelegate(options, armnnDelegate);
33
34 m_TfLiteDelegate = armnnDelegate;
35 m_TfLiteInterpreter = TfLiteInterpreterCreate(tfLiteModel, options);
36
37 // The options and model can be deleted after the interpreter is created.
38 TfLiteInterpreterOptionsDelete(options);
39 TfLiteModelDelete(tfLiteModel);
40}
41
42DelegateTestInterpreter::DelegateTestInterpreter(std::vector<char>& modelBuffer,
43 const armnnDelegate::DelegateOptions& delegateOptions,
44 const std::string& customOp)
45{
Matthew Sloyan65c21a12023-04-04 12:06:14 +010046 TfLiteModel* tfLiteModel = delegateTestInterpreter::CreateTfLiteModel(modelBuffer);
47
48 TfLiteInterpreterOptions* options = delegateTestInterpreter::CreateTfLiteInterpreterOptions();
49 if (!customOp.empty())
50 {
51 options->mutable_op_resolver = delegateTestInterpreter::GenerateCustomOpResolver(customOp);
52 }
53
Teresa Charlin3e4b6082023-10-19 19:13:29 +010054 auto armnnDelegate = armnnOpaqueDelegate::TfLiteArmnnOpaqueDelegateCreate(delegateOptions);
Matthew Sloyan65c21a12023-04-04 12:06:14 +010055 TfLiteInterpreterOptionsAddDelegate(options, armnnDelegate);
56
57 m_TfLiteDelegate = armnnDelegate;
58 m_TfLiteInterpreter = TfLiteInterpreterCreate(tfLiteModel, options);
59
60 // The options and model can be deleted after the interpreter is created.
61 TfLiteInterpreterOptionsDelete(options);
62 TfLiteModelDelete(tfLiteModel);
63}
64
65void DelegateTestInterpreter::Cleanup()
66{
67 TfLiteInterpreterDelete(m_TfLiteInterpreter);
68
69 if (m_TfLiteDelegate)
70 {
71 armnnOpaqueDelegate::TfLiteArmnnOpaqueDelegateDelete(static_cast<TfLiteOpaqueDelegate*>(m_TfLiteDelegate));
72 }
73}
74
75} // anonymous namespace