1 /**
2 * Copyright 2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #ifdef ENABLE_ARM
17 #include "src/cpu_info.h"
18 #include <set>
19 #include <fstream>
20 #include "src/common/log_adapter.h"
21 #include "nnacl/nnacl_utils.h"
22 #if defined(__ANDROID__) || defined(MS_COMPILE_OHOS)
23 #include <sys/auxv.h>
24 #include <asm/hwcap.h>
25 #endif
26 #ifdef MS_COMPILE_IOS
27 #include <mach/mach.h>
28 #include <mach/machine.h>
29 #include <mach/thread_act.h>
30 #include <sys/sysctl.h>
31 #include <sys/types.h>
32 #include "TargetConditionals.h"
33 #ifndef CPUFAMILY_ARM_HURRICANE
34 #define CPUFAMILY_ARM_HURRICANE 0x67ceee93
35 #endif
36 #ifndef CPUFAMILY_ARM_MONSOON_MISTRAL
37 #define CPUFAMILY_ARM_MONSOON_MISTRAL 0xe81e7ef6
38 #endif
39 #ifndef CPUFAMILY_ARM_VORTEX_TEMPEST
40 #define CPUFAMILY_ARM_VORTEX_TEMPEST 0x07d34b9f
41 #endif
42 #ifndef CPUFAMILY_ARM_LIGHTNING_THUNDER
43 #define CPUFAMILY_ARM_LIGHTNING_THUNDER 0x462504d2
44 #endif
45 #ifndef CPUFAMILY_ARM_FIRESTORM_ICESTORM
46 #define CPUFAMILY_ARM_FIRESTORM_ICESTORM 0x1b588bb3
47 #endif
48 #endif
49 namespace mindspore::lite {
50 #if defined(__ANDROID__) || defined(MS_COMPILE_OHOS)
MidrSetPart(uint32_t part)51 uint32_t CpuInfo::MidrSetPart(uint32_t part) {
52 return ((part << ARM_CPU_PART_OFFSET) & ARM_CPU_PART_MASK) | (midr_ & ~ARM_CPU_PART_MASK);
53 }
54
MidrSetImplementer(uint32_t implementer)55 uint32_t CpuInfo::MidrSetImplementer(uint32_t implementer) {
56 return ((implementer << ARM_CPU_IMPLEMENTER_OFFSET) & ARM_CPU_IMPLEMENTER_MASK) | (midr_ & ~ARM_CPU_IMPLEMENTER_MASK);
57 }
58
StringToDigit(const std::string & str)59 uint32_t CpuInfo::StringToDigit(const std::string &str) {
60 // hex string to digit
61 // verify hex prefix '0' and 'x'
62 if (str[0] != '0' || str[1] != 'x') {
63 return 0;
64 }
65 auto str_length = str.length();
66 uint32_t str_digit = 0;
67 for (unsigned int i = 2; i < str_length; ++i) {
68 auto tmp_char = str[i];
69 uint32_t digit;
70 if (tmp_char >= '0' && tmp_char <= '9') {
71 digit = tmp_char - '0';
72 } else if ((uint32_t)(tmp_char - 'A') < 6) {
73 digit = 10 + (tmp_char - 'A');
74 } else if ((uint32_t)(tmp_char - 'a') < 6) {
75 digit = 10 + (tmp_char - 'a');
76 } else {
77 return 0;
78 }
79 str_digit = (str_digit << 4) + digit;
80 }
81 return str_digit;
82 }
83
ParseArmCpuPart(const std::string & cpu_part)84 uint32_t CpuInfo::ParseArmCpuPart(const std::string &cpu_part) {
85 // cpu_part string length is in [3, 5]
86 auto cpu_part_length = cpu_part.length();
87 if (cpu_part_length < 3 || cpu_part_length > 5) {
88 return 0;
89 }
90 return StringToDigit(cpu_part);
91 }
92
ParseArmCpuImplementer(const std::string & str)93 uint32_t CpuInfo::ParseArmCpuImplementer(const std::string &str) {
94 auto str_length = str.length();
95 switch (str_length) {
96 case 3:
97 case 4:
98 break;
99 default:
100 return 0;
101 }
102 return StringToDigit(str);
103 }
104
GetArmProcCpuInfo(AndroidCpuInfo * android_cpu_info)105 void CpuInfo::GetArmProcCpuInfo(AndroidCpuInfo *android_cpu_info) {
106 // only get cpu part, implementer and hardware
107 std::ifstream infile("/proc/cpuinfo", std::ios::in);
108 std::string line;
109 while (getline(infile, line)) {
110 for (unsigned int i = 0; i < line.length(); ++i) {
111 if (line[i] == ':') {
112 std::string prefix = line.substr(0, i);
113 prefix.erase(0, prefix.find_first_not_of(' '));
114 prefix.erase(prefix.find_last_not_of('\t') + 1);
115 std::string suffix = line.substr(i + 2);
116 if (prefix == "CPU implementer" && android_cpu_info->cpu_implementer == 0) {
117 android_cpu_info->cpu_implementer = ParseArmCpuImplementer(suffix);
118 } else if (prefix == "CPU part" && android_cpu_info->cpu_part == 0) {
119 android_cpu_info->cpu_part = ParseArmCpuPart(suffix);
120 } else if (prefix == "Hardware" && android_cpu_info->hardware.empty()) {
121 android_cpu_info->hardware = suffix;
122 }
123 }
124 }
125 }
126 infile.close();
127 }
128 #endif
129
ArmIsSupportFp16()130 bool CpuInfo::ArmIsSupportFp16() {
131 #ifdef MS_COMPILE_IOS
132 unsigned int value = 0;
133 size_t len = sizeof(value);
134 sysctlbyname("hw.cpufamily", &value, &len, NULL, 0);
135 if (value == CPUFAMILY_ARM_MONSOON_MISTRAL || value == CPUFAMILY_ARM_VORTEX_TEMPEST ||
136 value == CPUFAMILY_ARM_LIGHTNING_THUNDER || value == CPUFAMILY_ARM_FIRESTORM_ICESTORM) {
137 return true;
138 }
139 return false;
140 #else
141 #if defined(__ANDROID__) || defined(MS_COMPILE_OHOS)
142 #ifdef ENABLE_ARM32
143 GetArmProcCpuInfo(&android_cpu_info_);
144 midr_ = MidrSetPart(android_cpu_info_.cpu_part);
145 midr_ = MidrSetImplementer(android_cpu_info_.cpu_implementer);
146 midr_ = (ARM_CPU_IMPLEMENTER_MASK | ARM_CPU_PART_MASK) & midr_;
147 std::set<uint32_t> cpu_list_support_fp16 = {
148 UINT32_C(0x4800D400), // Cortex-A76 in HiSilicon Cpu
149 UINT32_C(0x4100D050), // Arm Cortex-A55
150 UINT32_C(0x4100D060), // Arm Cortex-A65
151 UINT32_C(0x4100D0B0), // Arm Cortex-A76
152 UINT32_C(0x4100D0E0), // Arm Cortex-A76-AE
153 UINT32_C(0x4100D0D0), // Arm Cortex-A77
154 UINT32_C(0x4100D0C0), // Neoverse-N1 Cpu
155 UINT32_C(0x53000030), // Exynos-M4 Cpu
156 UINT32_C(0x53000040), // Exynos-M5 Cpu
157 UINT32_C(0x51008050), // Cortex-A55 in Kryo-485-Silver
158 UINT32_C(0x51008040), // Cortex-A76 in Kryo-485-Gold
159 UINT32_C(0x51008030), // Cortex-A55 in Kryo-385-Silver
160 UINT32_C(0x51008020) // Cortex-A75 in Kryo-385-Gold
161 };
162 if (cpu_list_support_fp16.find(midr_) != cpu_list_support_fp16.end()) {
163 fp16_flag_ = true;
164 }
165 #ifdef Debug
166 if (!fp16_flag_) {
167 MS_LOG(DEBUG) << "cpu midr_:" << midr_ << "is not support fp16!";
168 }
169 #endif
170 #elif defined(ENABLE_ARM64)
171 int hwcap_type = 16;
172 uint32_t hwcap = getHwCap(hwcap_type);
173 if (hwcap & HWCAP_FPHP) {
174 MS_LOG(DEBUG) << "Hw cap support FP16, hwcap: 0x" << hwcap;
175 fp16_flag_ = true;
176 } else {
177 MS_LOG(DEBUG) << "Hw cap NOT support FP16, hwcap: 0x" << hwcap;
178 }
179 #endif
180 #endif
181 return fp16_flag_;
182 #endif
183 }
184 } // namespace mindspore::lite
185 #endif
186