David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 7 | #include <armnn/Types.hpp> |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 8 | #include <armnn/IRuntime.hpp> |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame^] | 9 | #include <armnn/Deprecated.hpp> |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 10 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 11 | #include <ISubgraphViewConverter.hpp> |
| 12 | #include <SubgraphView.hpp> |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 13 | |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 14 | #include "OptimizationViews.hpp" |
| 15 | |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 16 | #include <vector> |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 17 | |
| 18 | namespace armnn |
| 19 | { |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 20 | class IWorkloadFactory; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 21 | class IBackendContext; |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 22 | class IMemoryManager; |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 23 | class Optimization; |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 24 | class ILayerSupport; |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 25 | |
| 26 | class IBackendInternal : public IBackend |
| 27 | { |
| 28 | protected: |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 29 | // Creation must be done through a specific |
| 30 | // backend interface. |
David Beck | 6b779f0 | 2018-10-09 17:20:21 +0100 | [diff] [blame] | 31 | IBackendInternal() = default; |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 32 | |
| 33 | public: |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 34 | // Allow backends created by the factory function |
| 35 | // to be destroyed through IBackendInternal. |
| 36 | ~IBackendInternal() override = default; |
| 37 | |
| 38 | using IWorkloadFactoryPtr = std::unique_ptr<IWorkloadFactory>; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 39 | using IBackendContextPtr = std::unique_ptr<IBackendContext>; |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 40 | using OptimizationPtr = std::unique_ptr<Optimization>; |
| 41 | using Optimizations = std::vector<OptimizationPtr>; |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 42 | using ILayerSupportSharedPtr = std::shared_ptr<ILayerSupport>; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 43 | |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 44 | using IMemoryManagerUniquePtr = std::unique_ptr<IMemoryManager>; |
| 45 | using IMemoryManagerSharedPtr = std::shared_ptr<IMemoryManager>; |
| 46 | |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 47 | using GraphUniquePtr = std::unique_ptr<Graph>; |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 48 | using SubgraphViewUniquePtr = std::unique_ptr<SubgraphView>; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 49 | |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 50 | virtual IMemoryManagerUniquePtr CreateMemoryManager() const = 0; |
| 51 | |
| 52 | virtual IWorkloadFactoryPtr CreateWorkloadFactory( |
| 53 | const IMemoryManagerSharedPtr& memoryManager = nullptr) const = 0; |
| 54 | |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 55 | virtual IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const = 0; |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 56 | |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 57 | virtual Optimizations GetOptimizations() const = 0; |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 58 | virtual ILayerSupportSharedPtr GetLayerSupport() const = 0; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 59 | |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame^] | 60 | ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead") |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 61 | virtual SubgraphViewUniquePtr OptimizeSubgraphView(const SubgraphView& subgraph, bool& optimizationAttempted) const |
| 62 | { |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 63 | optimizationAttempted = false; |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 64 | return nullptr; |
| 65 | } |
| 66 | |
| 67 | // Default implementation of OptimizeSubgraphView for backward compatibility with old API. |
| 68 | // Override this method with a custom optimization implementation. |
Derek Lamberti | 8106b7c | 2019-05-07 21:33:30 +0100 | [diff] [blame] | 69 | virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 70 | { |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 71 | bool optimizationAttempted = false; |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame^] | 72 | |
| 73 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 74 | SubgraphViewUniquePtr optSubgraph = OptimizeSubgraphView(subgraph, optimizationAttempted); |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame^] | 75 | ARMNN_NO_DEPRECATE_WARN_END |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 76 | |
| 77 | OptimizationViews result; |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 78 | if (!optimizationAttempted) |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 79 | { |
| 80 | result.AddUntouchedSubgraph(SubgraphView(subgraph)); |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | if (optSubgraph) |
| 85 | { |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 86 | result.AddSubstituion({subgraph, SubgraphView(*optSubgraph.get())}); |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 87 | } |
| 88 | else |
| 89 | { |
| 90 | result.AddFailedSubgraph(SubgraphView(subgraph)); |
| 91 | } |
| 92 | } |
| 93 | return result; |
| 94 | } |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 95 | }; |
| 96 | |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 97 | using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>; |
| 98 | |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 99 | } // namespace armnn |