Matthew Sloyan | 65c21a1 | 2023-04-04 12:06:14 +0100 | [diff] [blame] | 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 | |
Matthew Sloyan | 65c21a1 | 2023-04-04 12:06:14 +0100 | [diff] [blame] | 10 | |
| 11 | namespace delegateTestInterpreter |
| 12 | { |
| 13 | |
| 14 | DelegateTestInterpreter::DelegateTestInterpreter(std::vector<char>& modelBuffer, |
| 15 | const std::vector<armnn::BackendId>& backends, |
| 16 | const std::string& customOp, |
| 17 | bool disableFallback) |
| 18 | { |
Matthew Sloyan | 65c21a1 | 2023-04-04 12:06:14 +0100 | [diff] [blame] | 19 | 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 Charlin | 3e4b608 | 2023-10-19 19:13:29 +0100 | [diff] [blame] | 27 | // 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 Sloyan | 65c21a1 | 2023-04-04 12:06:14 +0100 | [diff] [blame] | 32 | 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 | |
| 42 | DelegateTestInterpreter::DelegateTestInterpreter(std::vector<char>& modelBuffer, |
| 43 | const armnnDelegate::DelegateOptions& delegateOptions, |
| 44 | const std::string& customOp) |
| 45 | { |
Matthew Sloyan | 65c21a1 | 2023-04-04 12:06:14 +0100 | [diff] [blame] | 46 | 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 Charlin | 3e4b608 | 2023-10-19 19:13:29 +0100 | [diff] [blame] | 54 | auto armnnDelegate = armnnOpaqueDelegate::TfLiteArmnnOpaqueDelegateCreate(delegateOptions); |
Matthew Sloyan | 65c21a1 | 2023-04-04 12:06:14 +0100 | [diff] [blame] | 55 | 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 | |
| 65 | void 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 |