ArmNN
 24.08
ArmnnPreparedModel.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022, 2024 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "ArmnnDriver.hpp"
9 #include "ArmnnDriverImpl.hpp"
11 
12 #include <armnn/ArmNN.hpp>
13 
14 #include <BufferTracker.h>
15 #include <CpuExecutor.h>
16 #include <nnapi/IExecution.h>
17 #include <nnapi/IPreparedModel.h>
18 #include <nnapi/Result.h>
19 #include <nnapi/Types.h>
20 
21 #include <memory>
22 #include <tuple>
23 #include <utility>
24 #include <vector>
25 #include <string>
26 
27 namespace armnn_driver
28 {
30  {
31  ::android::nn::MeasureTiming measureTimings =
32  ::android::nn::MeasureTiming::NO;
33  android::nn::TimePoint driverStart;
34  android::nn::TimePoint driverEnd;
35  android::nn::TimePoint deviceStart;
36  android::nn::TimePoint deviceEnd;
37  };
38 class ArmnnPreparedModel final : public IPreparedModel,
39  public std::enable_shared_from_this<ArmnnPreparedModel>
40 {
41 public:
42  ARMNN_DEPRECATED_MSG_REMOVAL_DATE("The Shim and support library will be removed from Arm NN in 24.08", "24.08")
44  armnn::IRuntime* runtime,
45  const Model& model,
46  const std::string& requestInputsAndOutputsDumpDir,
47  const bool gpuProfilingEnabled,
48  Priority priority = Priority::MEDIUM);
49 
50  ARMNN_DEPRECATED_MSG_REMOVAL_DATE("The Shim and support library will be removed from Arm NN in 24.08", "24.08")
52  armnn::IRuntime* runtime,
53  const std::string& requestInputsAndOutputsDumpDir,
54  const bool gpuProfilingEnabled,
55  Priority priority = Priority::MEDIUM,
56  const bool prepareModelFromCache = false);
57 
58  virtual ~ArmnnPreparedModel();
59 
60  ExecutionResult<std::pair<std::vector<OutputShape>, Timing>> execute(
61  const Request& request,
62  MeasureTiming measureTiming,
63  const OptionalTimePoint& deadline,
64  const OptionalDuration& loopTimeoutDuration,
65  const std::vector<android::nn::TokenValuePair>& hints,
66  const std::vector<android::nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const override;
67 
68  GeneralResult<std::pair<SyncFence, ExecuteFencedInfoCallback>> executeFenced(
69  const Request& request,
70  const std::vector<SyncFence>& waitFor,
71  MeasureTiming measureTiming,
72  const OptionalTimePoint& deadline,
73  const OptionalDuration& loopTimeoutDuration,
74  const OptionalDuration& timeoutDurationAfterFence,
75  const std::vector<android::nn::TokenValuePair>& hints,
76  const std::vector<android::nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const override;
77 
78  GeneralResult<android::nn::SharedExecution> createReusableExecution(
79  const Request& request,
80  MeasureTiming measureTiming,
81  const OptionalDuration& loopTimeoutDuration,
82  const std::vector<android::nn::TokenValuePair>& hints,
83  const std::vector<android::nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const override;
84 
85  GeneralResult<SharedBurst> configureExecutionBurst() const override;
86 
87  std::any getUnderlyingResource() const override;
88 
89  /// execute the graph prepared from the request
91  std::shared_ptr<std::vector<android::nn::RunTimePoolInfo>>& pMemPools,
92  armnn::InputTensors& inputTensors,
93  armnn::OutputTensors& outputTensors,
95  const bool pointerMemory = false) const;
96 
97  Priority GetModelPriority() const;
98 
99  /// Executes this model with dummy inputs (e.g. all zeroes).
100  /// \return false on failure, otherwise true
101  bool ExecuteWithDummyInputs(unsigned int numInputs, unsigned int numOutputs) const;
102 
103 private:
104  void Init();
105  ErrorStatus PrepareMemoryForInputs(
106  armnn::InputTensors& inputs,
107  const Request& request,
108  const std::vector<android::nn::RunTimePoolInfo>& memPools) const;
109 
110  ErrorStatus PrepareMemoryForOutputs(
111  armnn::OutputTensors& outputs,
112  std::vector<OutputShape> &outputShapes,
113  const Request& request,
114  const std::vector<android::nn::RunTimePoolInfo>& memPools) const;
115 
116  ErrorStatus PrepareMemoryForIO(armnn::InputTensors& inputs,
117  armnn::OutputTensors& outputs,
118  std::vector<android::nn::RunTimePoolInfo>& memPools,
119  const Request& request,
120  const bool pointerMemory = false) const;
121 
122  template <typename TensorBindingCollection>
123  void DumpTensorsIfRequired(char const* tensorNamePrefix, const TensorBindingCollection& tensorBindings) const;
124 
125  /// schedule the graph prepared from the request for execution
126  armnn::NetworkId m_NetworkId;
127  armnn::IRuntime* m_Runtime;
128 
129  const Model m_Model;
130  const std::string& m_RequestInputsAndOutputsDumpDir;
131  const bool m_GpuProfilingEnabled;
132  Priority m_ModelPriority;
133  const bool m_PrepareFromCache;
134 };
135 
136 }
armnn_driver::ArmnnPreparedModel::GetModelPriority
Priority GetModelPriority() const
Definition: ArmnnPreparedModel.cpp:471
armnn::InputTensors
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:394
ArmnnDriverImpl.hpp
armnn_driver::CanonicalExecutionContext::deviceEnd
android::nn::TimePoint deviceEnd
Definition: ArmnnPreparedModel.hpp:36
armnn::OutputTensors
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:395
armnn_driver::ArmnnPreparedModel::ArmnnPreparedModel
ArmnnPreparedModel(armnn::NetworkId networkId, armnn::IRuntime *runtime, const Model &model, const std::string &requestInputsAndOutputsDumpDir, const bool gpuProfilingEnabled, Priority priority=Priority::MEDIUM)
Definition: ArmnnPreparedModel.cpp:126
armnn::IRuntime
Definition: IRuntime.hpp:75
ModelToINetworkTransformer.hpp
armnn::NetworkId
int NetworkId
Definition: IRuntime.hpp:35
armnn_driver::ArmnnPreparedModel::getUnderlyingResource
std::any getUnderlyingResource() const override
Definition: ArmnnPreparedModel.cpp:604
armnn_driver::ErrorStatus
::android::nn::ErrorStatus ErrorStatus
Definition: ConversionUtils.hpp:51
armnn_driver::CanonicalExecutionContext::driverEnd
android::nn::TimePoint driverEnd
Definition: ArmnnPreparedModel.hpp:34
armnn_driver
Helper classes.
Definition: ArmnnDevice.cpp:37
armnn_driver::Model
::android::nn::Model Model
Helper classes.
Definition: ConversionUtils.hpp:45
armnn_driver::ArmnnPreparedModel::ExecuteWithDummyInputs
bool ExecuteWithDummyInputs(unsigned int numInputs, unsigned int numOutputs) const
Executes this model with dummy inputs (e.g.
Definition: ArmnnPreparedModel.cpp:646
armnn_driver::CanonicalExecutionContext
Definition: ArmnnPreparedModel.hpp:29
armnn_driver::ArmnnPreparedModel::~ArmnnPreparedModel
virtual ~ArmnnPreparedModel()
Definition: ArmnnPreparedModel.cpp:626
ArmnnDriver.hpp
armnn_driver::CanonicalExecutionContext::driverStart
android::nn::TimePoint driverStart
Definition: ArmnnPreparedModel.hpp:33
armnn_driver::CanonicalExecutionContext::deviceStart
android::nn::TimePoint deviceStart
Definition: ArmnnPreparedModel.hpp:35
armnn_driver::CanonicalExecutionContext::measureTimings
::android::nn::MeasureTiming measureTimings
Definition: ArmnnPreparedModel.hpp:31
armnn_driver::ArmnnPreparedModel::execute
ExecutionResult< std::pair< std::vector< OutputShape >, Timing > > execute(const Request &request, MeasureTiming measureTiming, const OptionalTimePoint &deadline, const OptionalDuration &loopTimeoutDuration, const std::vector< android::nn::TokenValuePair > &hints, const std::vector< android::nn::ExtensionNameAndPrefix > &extensionNameToPrefix) const override
Definition: ArmnnPreparedModel.cpp:294
ARMNN_DEPRECATED_MSG_REMOVAL_DATE
#define ARMNN_DEPRECATED_MSG_REMOVAL_DATE(message, removed_in_release)
Definition: Deprecated.hpp:44
armnn_driver::ArmnnPreparedModel
Definition: ArmnnPreparedModel.hpp:38
armnn_driver::ArmnnPreparedModel::executeFenced
GeneralResult< std::pair< SyncFence, ExecuteFencedInfoCallback > > executeFenced(const Request &request, const std::vector< SyncFence > &waitFor, MeasureTiming measureTiming, const OptionalTimePoint &deadline, const OptionalDuration &loopTimeoutDuration, const OptionalDuration &timeoutDurationAfterFence, const std::vector< android::nn::TokenValuePair > &hints, const std::vector< android::nn::ExtensionNameAndPrefix > &extensionNameToPrefix) const override
Definition: ArmnnPreparedModel.cpp:477
armnn_driver::ArmnnPreparedModel::createReusableExecution
GeneralResult< android::nn::SharedExecution > createReusableExecution(const Request &request, MeasureTiming measureTiming, const OptionalDuration &loopTimeoutDuration, const std::vector< android::nn::TokenValuePair > &hints, const std::vector< android::nn::ExtensionNameAndPrefix > &extensionNameToPrefix) const override
Definition: ArmnnPreparedModel.cpp:585
ArmNN.hpp
armnn_driver::ArmnnPreparedModel::ExecuteGraph
ErrorStatus ExecuteGraph(std::shared_ptr< std::vector< android::nn::RunTimePoolInfo >> &pMemPools, armnn::InputTensors &inputTensors, armnn::OutputTensors &outputTensors, CanonicalExecutionContext callback, const bool pointerMemory=false) const
execute the graph prepared from the request
Definition: ArmnnPreparedModel.cpp:374
armnn_driver::ArmnnPreparedModel::configureExecutionBurst
GeneralResult< SharedBurst > configureExecutionBurst() const override
Definition: ArmnnPreparedModel.cpp:599