char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Georgi Gerganovggml : fix arm builds, unused var (#26991)1f368f354

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