ArmNN
 23.05
ClBackendContext Class Reference

#include <ClBackendContext.hpp>

Inheritance diagram for ClBackendContext:
IBackendContext

Public Member Functions

 ClBackendContext (const IRuntime::CreationOptions &options)
 
bool BeforeLoadNetwork (NetworkId networkId) override
 Before and after Load network events. More...
 
bool AfterLoadNetwork (NetworkId networkId) override
 
bool BeforeUnloadNetwork (NetworkId networkId) override
 Before and after Unload network events. More...
 
bool AfterUnloadNetwork (NetworkId networkId) override
 
bool AfterEnqueueWorkload (NetworkId networkId) override
 
 ~ClBackendContext () override
 
- Public Member Functions inherited from IBackendContext
virtual ~IBackendContext ()
 

Protected Attributes

arm_compute::CLGEMMHeuristicsHandle m_MLGOTuner
 
std::string m_MLGOTuningFile
 

Additional Inherited Members

- Protected Member Functions inherited from IBackendContext
 IBackendContext (const IRuntime::CreationOptions &)
 

Detailed Description

Definition at line 17 of file ClBackendContext.hpp.

Constructor & Destructor Documentation

◆ ClBackendContext()

Definition at line 62 of file ClBackendContext.cpp.

63  : IBackendContext(options)
64  , m_TuningFile()
65 {
66  bool kernelProfiling = options.m_EnableGpuProfiling;
67 
68  arm_compute::CLTuner* tuner = nullptr;
69  arm_compute::CLGEMMHeuristicsHandle* mlgoTuner = nullptr;
70  bool useLegacyTunerAPI = options.m_GpuAccTunedParameters.get() != nullptr;
71  if (useLegacyTunerAPI)
72  {
73  auto clTunerParams = PolymorphicDowncast<ClTunedParameters*>(
74  options.m_GpuAccTunedParameters.get());
75  tuner = &clTunerParams->m_Tuner;
76 
77  if (tuner)
78  {
79  auto ConvertTuningLevel = [](IGpuAccTunedParameters::TuningLevel level,
81  {
83  {
84  return TuningLevel::None;
85  }
86 
87  switch(level)
88  {
90  return TuningLevel::Rapid;
92  return TuningLevel::Normal;
95  default:
96  {
97  ARMNN_ASSERT_MSG(false, "Tuning level not recognised.");
98  return TuningLevel::None;
99  }
100  }
101  };
102 
103  TuningLevel tuningLevel = ConvertTuningLevel(clTunerParams->m_TuningLevel, clTunerParams->m_Mode);
104  ConfigureTuner(*tuner, tuningLevel);
105  }
106  }
107  else //New backend options API
108  {
109  const TuningLevel defaultTuningLevel = TuningLevel::None;
110  auto tuningLevel = defaultTuningLevel;
111 
112  ParseOptions(options.m_BackendOptions, "GpuAcc", [&](std::string name, const BackendOptions::Var& value)
113  {
114  if (name == "KernelProfilingEnabled")
115  {
116  kernelProfiling |= ParseBooleanBackendOption(value, false);
117  } else if (name == "TuningFile")
118  {
119  m_TuningFile = ParseStringBackendOption(value, "");
120  } else if (name == "TuningLevel")
121  {
122  tuningLevel = ParseTuningLevel(value, defaultTuningLevel);
123  }
124  else if (name == "MLGOTuningFilePath")
125  {
127  }
128  });
129 
130  // Create the tuner, in tuning mode initially.
131  m_Tuner = std::make_unique<arm_compute::CLTuner>(true);
132 
133  ConfigureTuner(*(m_Tuner.get()), tuningLevel);
134 
135  if (!m_TuningFile.empty())
136  {
137  try
138  {
139  ARMNN_LOG(info) << "Loading Gpu tuning data from file: " << m_TuningFile;
140  m_Tuner->load_from_file(m_TuningFile.c_str());
141  }
142  catch (const std::exception& e)
143  {
144  // Warn if not tuning, otherwise tuning will generate new params
145  if (tuningLevel == TuningLevel::None)
146  {
147  ARMNN_LOG(warning) << "Could not load GpuAcc tuner data file.";
148  }
149  }
150  }
151 
152  if (!m_MLGOTuningFile.empty())
153  {
154  try
155  {
156  ARMNN_LOG(info) << "Loading Gpu MLGO tuning data from file: " << m_TuningFile;
157  if(m_MLGOTuner.reload_from_file(m_MLGOTuningFile.c_str()))
158  {
159  mlgoTuner = &m_MLGOTuner;
160  }
161  }
162  catch (const std::exception& e)
163  {
164  ARMNN_LOG(warning) << "Could not load GpuAcc MLGO tuner data file.";
165  }
166  }
167 
168  tuner = m_Tuner.get();
169  }
170 
171  m_ClContextControlWrapper = std::make_unique<ClContextControlWrapper>(
172  tuner,
173  mlgoTuner,
174  kernelProfiling
175  );
176 }

References ARMNN_ASSERT_MSG, ARMNN_LOG, armnn::ConfigureTuner(), armnn::Exhaustive, IGpuAccTunedParameters::Exhaustive, armnn::info, IRuntime::CreationOptions::m_BackendOptions, IRuntime::CreationOptions::m_EnableGpuProfiling, IRuntime::CreationOptions::m_GpuAccTunedParameters, ClBackendContext::m_MLGOTuner, ClBackendContext::m_MLGOTuningFile, armnn::None, armnn::Normal, IGpuAccTunedParameters::Normal, armnn::ParseOptions(), armnn::ParseStringBackendOption(), armnn::ParseTuningLevel(), armnn::Rapid, IGpuAccTunedParameters::Rapid, IGpuAccTunedParameters::UseTunedParameters, and armnn::warning.

◆ ~ClBackendContext()

~ClBackendContext ( )
override

Definition at line 219 of file ClBackendContext.cpp.

220 {
221  if (m_Tuner && !m_TuningFile.empty())
222  {
223  try
224  {
225  m_Tuner->save_to_file(m_TuningFile.c_str());
226  }
227  catch(const std::exception& e)
228  {
229  ARMNN_LOG(warning) << "Could not save GpuAcc tuner data to file " << m_TuningFile;
230  }
231  }
232 }

References ARMNN_LOG, and armnn::warning.

Member Function Documentation

◆ AfterEnqueueWorkload()

bool AfterEnqueueWorkload ( NetworkId  networkId)
overridevirtual

Implements IBackendContext.

Definition at line 214 of file ClBackendContext.cpp.

215 {
216  return m_ClContextControlWrapper->Sync();
217 }

◆ AfterLoadNetwork()

bool AfterLoadNetwork ( NetworkId  networkId)
overridevirtual

Implements IBackendContext.

Definition at line 183 of file ClBackendContext.cpp.

184 {
185  {
186  std::lock_guard<std::mutex> lockGuard(m_Mutex);
187  m_NetworkIds.insert(networkId);
188  }
189  return true;
190 }

◆ AfterUnloadNetwork()

bool AfterUnloadNetwork ( NetworkId  networkId)
overridevirtual

Implements IBackendContext.

Definition at line 197 of file ClBackendContext.cpp.

198 {
199  bool clearCache = false;
200  {
201  std::lock_guard<std::mutex> lockGuard(m_Mutex);
202  m_NetworkIds.erase(networkId);
203  clearCache = m_NetworkIds.empty();
204  }
205 
206  if (clearCache)
207  {
208  m_ClContextControlWrapper->ClearClCache();
209  }
210 
211  return true;
212 }

◆ BeforeLoadNetwork()

bool BeforeLoadNetwork ( NetworkId  networkId)
overridevirtual

Before and after Load network events.

Implements IBackendContext.

Definition at line 178 of file ClBackendContext.cpp.

179 {
180  return true;
181 }

◆ BeforeUnloadNetwork()

bool BeforeUnloadNetwork ( NetworkId  networkId)
overridevirtual

Before and after Unload network events.

Implements IBackendContext.

Definition at line 192 of file ClBackendContext.cpp.

193 {
194  return m_ClContextControlWrapper->Sync();
195 }

Member Data Documentation

◆ m_MLGOTuner

arm_compute::CLGEMMHeuristicsHandle m_MLGOTuner
protected

Definition at line 43 of file ClBackendContext.hpp.

Referenced by ClBackendContext::ClBackendContext().

◆ m_MLGOTuningFile

std::string m_MLGOTuningFile
protected

Definition at line 44 of file ClBackendContext.hpp.

Referenced by ClBackendContext::ClBackendContext().


The documentation for this class was generated from the following files:
armnn::IGpuAccTunedParameters::TuningLevel::Rapid
@ Rapid
armnn::TuningLevel::None
@ None
armnn::ParseOptions
void ParseOptions(const std::vector< BackendOptions > &options, BackendId backend, F f)
Definition: BackendOptions.hpp:297
armnn::TuningLevel::Rapid
@ Rapid
armnn::ConfigureTuner
void ConfigureTuner(arm_compute::CLTuner &tuner, TuningLevel level)
Definition: ArmComputeTuningUtils.hpp:44
ARMNN_LOG
#define ARMNN_LOG(severity)
Definition: Logging.hpp:212
armnn::IGpuAccTunedParameters::Mode
Mode
Definition: IRuntime.hpp:323
armnn::IGpuAccTunedParameters::TuningLevel::Exhaustive
@ Exhaustive
armnn::IGpuAccTunedParameters::TuningLevel
TuningLevel
Definition: IRuntime.hpp:329
armnn::TuningLevel
TuningLevel
Definition: ArmComputeTuningUtils.hpp:18
ARMNN_ASSERT_MSG
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
armnn::TuningLevel::Exhaustive
@ Exhaustive
armnn::TuningLevel::Normal
@ Normal
armnn::IGpuAccTunedParameters::TuningLevel::Normal
@ Normal
armnn::IBackendContext::IBackendContext
IBackendContext(const IRuntime::CreationOptions &)
Definition: IBackendContext.hpp:17
armnn::ClBackendContext::m_MLGOTuningFile
std::string m_MLGOTuningFile
Definition: ClBackendContext.hpp:44
armnn::ParseStringBackendOption
std::string ParseStringBackendOption(const armnn::BackendOptions::Var &value, std::string defaultValue)
Definition: BackendOptions.hpp:321
armnn::IGpuAccTunedParameters::Mode::UseTunedParameters
@ UseTunedParameters
armnn::ParseTuningLevel
TuningLevel ParseTuningLevel(const BackendOptions::Var &value, TuningLevel defaultValue)
Definition: ArmComputeTuningUtils.hpp:26
armnn::ClBackendContext::m_MLGOTuner
arm_compute::CLGEMMHeuristicsHandle m_MLGOTuner
Definition: ClBackendContext.hpp:43