ArmNN
 26.07
NeonBackendModelContext.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020, 2026 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #include <arm_compute/core/CPP/CPPTypes.h>
9 
10 namespace
11 {
12 
13 bool ParseBool(const armnn::BackendOptions::Var& value, bool defaultValue)
14 {
15  if (value.IsBool())
16  {
17  return value.AsBool();
18  }
19  return defaultValue;
20 }
21 
22 unsigned int ParseUnsignedInt(const armnn::BackendOptions::Var& value, unsigned int defaultValue)
23 {
24  if (value.IsUnsignedInt())
25  {
26  return value.AsUnsignedInt();
27  }
28  if (value.IsInt() && value.AsInt() >= 0)
29  {
30  return static_cast<unsigned int>(value.AsInt());
31  }
32  return defaultValue;
33 }
34 
35 // Some downstream Android builds consume ArmNN with a ComputeLibrary revision
36 // that does not yet expose runtime ISA masking. Keep the model option compatible
37 // with those builds while using the ACL controls when they are available.
38 template <typename CpuInfo>
39 auto SetSveAllowed(CpuInfo& cpuInfo, bool isEnabled, int) -> decltype(cpuInfo.set_sve_allowed(isEnabled), void())
40 {
41  cpuInfo.set_sve_allowed(isEnabled);
42 }
43 
44 template <typename CpuInfo>
45 void SetSveAllowed(CpuInfo&, bool, long)
46 {}
47 
48 template <typename CpuInfo>
49 auto SetSmeAllowed(CpuInfo& cpuInfo, bool isEnabled, int) -> decltype(cpuInfo.set_sme_allowed(isEnabled), void())
50 {
51  cpuInfo.set_sme_allowed(isEnabled);
52 }
53 
54 template <typename CpuInfo>
55 void SetSmeAllowed(CpuInfo&, bool, long)
56 {}
57 
58 } // namespace anonymous
59 
60 namespace armnn
61 {
62 
64  : m_IsFastMathEnabled(false), m_NumberOfThreads(0), m_IsSveEnabled(true), m_IsSmeEnabled(true)
65 {
66  if (!modelOptions.empty())
67  {
68  ParseOptions(modelOptions, "CpuAcc", [&](std::string name, const BackendOptions::Var& value)
69  {
70  if (name == "FastMathEnabled")
71  {
72  m_IsFastMathEnabled = ParseBool(value, m_IsFastMathEnabled);
73  }
74  if (name == "NumberOfThreads")
75  {
76  m_NumberOfThreads = ParseUnsignedInt(value, m_NumberOfThreads);
77  }
78  if (name == "SmeEnabled")
79  {
80  m_IsSmeEnabled = ParseBool(value, m_IsSmeEnabled);
81  }
82  if (name == "SveEnabled")
83  {
84  m_IsSveEnabled = ParseBool(value, m_IsSveEnabled);
85  }
86  });
87  }
88 }
89 
91 {
92  return m_IsFastMathEnabled;
93 }
94 
96 {
97  return m_NumberOfThreads;
98 }
99 
101 {
102  arm_compute::CPUInfo& cpuInfo = arm_compute::CPUInfo::get();
103  SetSveAllowed(cpuInfo, m_IsSveEnabled, 0);
104  SetSmeAllowed(cpuInfo, m_IsSmeEnabled, 0);
105 }
106 
107 } // namespace armnn
Very basic type safe variant.
unsigned int AsUnsignedInt() const
bool AsBool() const
Value getters.
bool IsBool() const
Type getters.
NeonBackendModelContext(const ModelOptions &modelOptions)
Copyright (c) 2021 ARM Limited and Contributors.
std::vector< BackendOptions > ModelOptions
void ParseOptions(const std::vector< BackendOptions > &options, BackendId backend, F f)