interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-feats.h

177 lines · 5253 bytes Raw Blame History
1#pragma once
2
3#if defined(__aarch64__) || defined(_M_ARM64)
4
5#if defined(__linux__)
6#include <sys/auxv.h>
7#include <sys/prctl.h>
8
9#if !defined(HWCAP2_SVE2)
10#define HWCAP2_SVE2 (1ULL << 1)
11#endif
12
13#if !defined(HWCAP_FPHP)
14#define HWCAP_FPHP (1 << 9)
15#endif
16
17#if !defined(HWCAP_ASIMDHP)
18#define HWCAP_ASIMDHP (1 << 10)
19#endif
20
21#if !defined(HWCAP2_I8MM)
22#define HWCAP2_I8MM (1ULL << 13)
23#endif
24
25#if !defined(HWCAP2_SVEI8MM)
26#define HWCAP2_SVEI8MM (1ULL << 9)
27#endif
28
29#if !defined(HWCAP_ASIMDDP)
30#define HWCAP_ASIMDDP (1 << 20)
31#endif
32
33#if !defined(HWCAP_SVE)
34#define HWCAP_SVE (1 << 22)
35#endif
36
37#if !defined(HWCAP2_SME)
38#define HWCAP2_SME (1ULL << 23)
39#endif
40
41#if !defined(HWCAP2_SME2)
42#define HWCAP2_SME2 (1ULL << 37)
43#endif
44
45#if !defined(PR_SVE_GET_VL)
46#define PR_SVE_GET_VL 51
47#endif
48
49#if !defined(PR_SVE_VL_LEN_MASK)
50#define PR_SVE_VL_LEN_MASK 0xffff
51#endif
52
53#elif defined(__APPLE__)
54#include <sys/sysctl.h>
55#elif defined(_WIN32)
56#include <windows.h>
57
58#if !defined(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE)
59#define PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE 43
60#endif
61
62#if !defined(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE)
63#define PF_ARM_SVE_INSTRUCTIONS_AVAILABLE 46
64#endif
65
66#if !defined(PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE)
67#define PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE 47
68#endif
69
70#if !defined(PF_ARM_SVE_I8MM_INSTRUCTIONS_AVAILABLE)
71#define PF_ARM_SVE_I8MM_INSTRUCTIONS_AVAILABLE 57
72#endif
73
74#if !defined(PF_ARM_V82_I8MM_INSTRUCTIONS_AVAILABLE)
75#define PF_ARM_V82_I8MM_INSTRUCTIONS_AVAILABLE 66
76#endif
77
78#if !defined(PF_ARM_V82_FP16_INSTRUCTIONS_AVAILABLE)
79#define PF_ARM_V82_FP16_INSTRUCTIONS_AVAILABLE 67
80#endif
81
82#if !defined(PF_ARM_SME_INSTRUCTIONS_AVAILABLE)
83#define PF_ARM_SME_INSTRUCTIONS_AVAILABLE 70
84#endif
85
86#if !defined(PF_ARM_SME2_INSTRUCTIONS_AVAILABLE)
87#define PF_ARM_SME2_INSTRUCTIONS_AVAILABLE 71
88#endif
89
90#endif
91
92typedef struct ggml_feats_arch64_runtime {
93 bool has_dotprod;
94 bool has_fp16;
95 bool has_sve;
96 bool has_sve2;
97 bool has_i8mm;
98 bool has_svei8mm;
99 bool has_sme;
100 bool has_sme2;
101 int sve_cnt;
102} ggml_feats_arch64_runtime_t;
103
104static inline ggml_feats_arch64_runtime_t ggml_feats_get_arch64_runtime(void) {
105 ggml_feats_arch64_runtime_t runtime_feat = {};
106
107#if defined(__linux__)
108 const unsigned long hwcap = getauxval(AT_HWCAP);
109 const unsigned long hwcap2 = getauxval(AT_HWCAP2);
110
111 runtime_feat.has_dotprod = !!(hwcap & HWCAP_ASIMDDP);
112 runtime_feat.has_fp16 = !!(hwcap & HWCAP_FPHP) && !!(hwcap & HWCAP_ASIMDHP);;
113 runtime_feat.has_sve = !!(hwcap & HWCAP_SVE);
114 runtime_feat.has_sve2 = !!(hwcap2 & HWCAP2_SVE2);
115 runtime_feat.has_i8mm = !!(hwcap2 & HWCAP2_I8MM);
116 runtime_feat.has_svei8mm = !!(hwcap2 & HWCAP2_SVEI8MM);
117 runtime_feat.has_sme = !!(hwcap2 & HWCAP2_SME);
118 runtime_feat.has_sme2 = !!(hwcap2 & HWCAP2_SME2);
119
120 if (runtime_feat.has_sve) {
121 const int vl = prctl(PR_SVE_GET_VL);
122 if (vl >= 0) {
123 runtime_feat.sve_cnt = vl & PR_SVE_VL_LEN_MASK;
124 }
125 }
126#elif defined(__APPLE__)
127 int oldp = 0;
128 size_t size = sizeof(oldp);
129
130 if (sysctlbyname("hw.optional.arm.FEAT_DotProd", &oldp, &size, nullptr, 0) == 0) {
131 runtime_feat.has_dotprod = static_cast<bool>(oldp);
132 }
133
134 if (sysctlbyname("hw.optional.arm.FEAT_FP16", &oldp, &size, nullptr, 0) == 0) {
135 runtime_feat.has_fp16 = static_cast<bool>(oldp);
136 }
137
138 if (sysctlbyname("hw.optional.arm.FEAT_SVE", &oldp, &size, nullptr, 0) == 0) {
139 runtime_feat.has_sve = static_cast<bool>(oldp);
140 }
141
142 if (sysctlbyname("hw.optional.arm.FEAT_SVE2", &oldp, &size, nullptr, 0) == 0) {
143 runtime_feat.has_sve2 = static_cast<bool>(oldp);
144 }
145
146 if (sysctlbyname("hw.optional.arm.FEAT_I8MM", &oldp, &size, nullptr, 0) == 0) {
147 runtime_feat.has_i8mm = static_cast<bool>(oldp);
148 }
149
150 if (sysctlbyname("hw.optional.arm.FEAT_SME", &oldp, &size, nullptr, 0) == 0) {
151 runtime_feat.has_sme = static_cast<bool>(oldp);
152 }
153
154 if (sysctlbyname("hw.optional.arm.FEAT_SME2", &oldp, &size, nullptr, 0) == 0) {
155 runtime_feat.has_sme2 = static_cast<bool>(oldp);
156 }
157
158 // Apple does not support userspace non-streaming SVE; keep SVE vector length unknown.
159 runtime_feat.sve_cnt = 0;
160#elif defined (_WIN32)
161 runtime_feat.has_dotprod = IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE) != 0;
162 runtime_feat.has_fp16 = IsProcessorFeaturePresent(PF_ARM_V82_FP16_INSTRUCTIONS_AVAILABLE) != 0;
163 runtime_feat.has_sve = IsProcessorFeaturePresent(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE) != 0;
164 runtime_feat.has_sve2 = IsProcessorFeaturePresent(PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE) != 0;
165 runtime_feat.has_i8mm = IsProcessorFeaturePresent(PF_ARM_V82_I8MM_INSTRUCTIONS_AVAILABLE) != 0;
166 runtime_feat.has_svei8mm = IsProcessorFeaturePresent(PF_ARM_SVE_I8MM_INSTRUCTIONS_AVAILABLE) != 0;
167 runtime_feat.has_sme = IsProcessorFeaturePresent(PF_ARM_SME_INSTRUCTIONS_AVAILABLE) != 0;
168 runtime_feat.has_sme2 = IsProcessorFeaturePresent(PF_ARM_SME2_INSTRUCTIONS_AVAILABLE) != 0;
169
170 // Windows exposes SVE feature presence, but not the runtime SVE vector length here.
171 runtime_feat.sve_cnt = 0;
172#endif
173
174 return runtime_feat;
175}
176
177#endif // defined(__aarch64__) || defined(_M_ARM64)