ArmNN
 22.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 140 of file ClBackendContext.cpp.

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::ParseFile(), armnn::ParseOptions(), armnn::ParseTuningLevel(), armnn::Rapid, IGpuAccTunedParameters::Rapid, IGpuAccTunedParameters::UseTunedParameters, and armnn::warning.

Referenced by TEST_SUITE().

141  : IBackendContext(options)
142  , m_TuningFile()
143 {
144  bool kernelProfiling = options.m_EnableGpuProfiling;
145 
146  arm_compute::CLTuner* tuner = nullptr;
147  arm_compute::CLGEMMHeuristicsHandle* mlgoTuner = nullptr;
148  bool useLegacyTunerAPI = options.m_GpuAccTunedParameters.get() != nullptr;
149  if (useLegacyTunerAPI)
150  {
151  auto clTunerParams = PolymorphicDowncast<ClTunedParameters*>(
152  options.m_GpuAccTunedParameters.get());
153  tuner = &clTunerParams->m_Tuner;
154 
155  if (tuner)
156  {
157  auto ConvertTuningLevel = [](IGpuAccTunedParameters::TuningLevel level,
159  {
161  {
162  return TuningLevel::None;
163  }
164 
165  switch(level)
166  {
168  return TuningLevel::Rapid;
170  return TuningLevel::Normal;
173  default:
174  {
175  ARMNN_ASSERT_MSG(false, "Tuning level not recognised.");
176  return TuningLevel::None;
177  }
178  }
179  };
180 
181  TuningLevel tuningLevel = ConvertTuningLevel(clTunerParams->m_TuningLevel, clTunerParams->m_Mode);
182  ConfigureTuner(*tuner, tuningLevel);
183  }
184  }
185  else //New backend options API
186  {
187  const TuningLevel defaultTuningLevel = TuningLevel::None;
188  auto tuningLevel = defaultTuningLevel;
189 
190  ParseOptions(options.m_BackendOptions, "GpuAcc", [&](std::string name, const BackendOptions::Var& value)
191  {
192  if (name == "KernelProfilingEnabled")
193  {
194  kernelProfiling |= ParseBoolean(value, false);
195  } else if (name == "TuningFile")
196  {
197  m_TuningFile = ParseFile(value, "");
198  } else if (name == "TuningLevel")
199  {
200  tuningLevel = ParseTuningLevel(value, defaultTuningLevel);
201  }
202  else if (name == "MLGOTuningFilePath")
203  {
204  m_MLGOTuningFile = ParseFile(value, "");
205  }
206  });
207 
208  // Create the tuner, in tuning mode initially.
209  m_Tuner = std::make_unique<arm_compute::CLTuner>(true);
210 
211  ConfigureTuner(*(m_Tuner.get()), tuningLevel);
212 
213  if (!m_TuningFile.empty())
214  {
215  try
216  {
217  ARMNN_LOG(info) << "Loading Gpu tuning data from file: " << m_TuningFile;
218  m_Tuner->load_from_file(m_TuningFile.c_str());
219  }
220  catch (const std::exception& e)
221  {
222  // Warn if not tuning, otherwise tuning will generate new params
223  if (tuningLevel == TuningLevel::None)
224  {
225  ARMNN_LOG(warning) << "Could not load GpuAcc tuner data file.";
226  }
227  }
228  }
229 
230  if (!m_MLGOTuningFile.empty())
231  {
232  try
233  {
234  ARMNN_LOG(info) << "Loading Gpu MLGO tuning data from file: " << m_TuningFile;
235  if(m_MLGOTuner.reload_from_file(m_MLGOTuningFile.c_str()))
236  {
237  mlgoTuner = &m_MLGOTuner;
238  }
239  }
240  catch (const std::exception& e)
241  {
242  ARMNN_LOG(warning) << "Could not load GpuAcc MLGO tuner data file.";
243  }
244  }
245 
246  tuner = m_Tuner.get();
247  }
248 
249  m_ClContextControlWrapper = std::make_unique<ClContextControlWrapper>(
250  tuner,
251  mlgoTuner,
252  kernelProfiling
253  );
254 }
void ParseOptions(const std::vector< BackendOptions > &options, BackendId backend, F f)
#define ARMNN_LOG(severity)
Definition: Logging.hpp:205
void ConfigureTuner(arm_compute::CLTuner &tuner, TuningLevel level)
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
std::string ParseFile(const BackendOptions::Var &value, std::string defaultValue)
TuningLevel ParseTuningLevel(const BackendOptions::Var &value, TuningLevel defaultValue)
arm_compute::CLGEMMHeuristicsHandle m_MLGOTuner
IBackendContext(const IRuntime::CreationOptions &)

◆ ~ClBackendContext()

~ClBackendContext ( )
override

Definition at line 297 of file ClBackendContext.cpp.

References ARMNN_LOG, and armnn::warning.

298 {
299  if (m_Tuner && !m_TuningFile.empty())
300  {
301  try
302  {
303  m_Tuner->save_to_file(m_TuningFile.c_str());
304  }
305  catch(const std::exception& e)
306  {
307  ARMNN_LOG(warning) << "Could not save GpuAcc tuner data to file " << m_TuningFile;
308  }
309  }
310 }
#define ARMNN_LOG(severity)
Definition: Logging.hpp:205

Member Function Documentation

◆ AfterEnqueueWorkload()

bool AfterEnqueueWorkload ( NetworkId  networkId)
overridevirtual

Implements IBackendContext.

Definition at line 292 of file ClBackendContext.cpp.

293 {
294  return m_ClContextControlWrapper->Sync();
295 }

◆ AfterLoadNetwork()

bool AfterLoadNetwork ( NetworkId  networkId)
overridevirtual

Implements IBackendContext.

Definition at line 261 of file ClBackendContext.cpp.

262 {
263  {
264  std::lock_guard<std::mutex> lockGuard(m_Mutex);
265  m_NetworkIds.insert(networkId);
266  }
267  return true;
268 }

◆ AfterUnloadNetwork()

bool AfterUnloadNetwork ( NetworkId  networkId)
overridevirtual

Implements IBackendContext.

Definition at line 275 of file ClBackendContext.cpp.

276 {
277  bool clearCache = false;
278  {
279  std::lock_guard<std::mutex> lockGuard(m_Mutex);
280  m_NetworkIds.erase(networkId);
281  clearCache = m_NetworkIds.empty();
282  }
283 
284  if (clearCache)
285  {
286  m_ClContextControlWrapper->ClearClCache();
287  }
288 
289  return true;
290 }

◆ BeforeLoadNetwork()

bool BeforeLoadNetwork ( NetworkId  networkId)
overridevirtual

Before and after Load network events.

Implements IBackendContext.

Definition at line 256 of file ClBackendContext.cpp.

257 {
258  return true;
259 }

◆ BeforeUnloadNetwork()

bool BeforeUnloadNetwork ( NetworkId  networkId)
overridevirtual

Before and after Unload network events.

Implements IBackendContext.

Definition at line 270 of file ClBackendContext.cpp.

271 {
272  return m_ClContextControlWrapper->Sync();
273 }

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: