18#if defined(__x86_64__) || defined(__i386__)
25constexpr std::uint32_t kEndOfCentralDirectory = 0x06054b50;
26constexpr std::uint32_t kZip64Locator = 0x07064b50;
27constexpr std::uint32_t kZip64EndOfCentralDirectory = 0x06064b50;
28constexpr std::uint32_t kCentralEntry = 0x02014b50;
29constexpr std::uint32_t kLocalHeader = 0x04034b50;
31constexpr std::uint64_t kMetadataLimit = 1 << 20;
33std::uint64_t little(
const std::string& bytes, std::size_t at,
int width)
35 if (at +
static_cast<std::size_t
>(width) > bytes.size())
36 throw std::out_of_range(
"truncated zip record");
37 std::uint64_t value = 0;
38 for (
int b = width - 1; b >= 0; --b)
39 value = (value << 8) |
static_cast<unsigned char>(
40 bytes[
at +
static_cast<std::size_t
>(b)]);
44std::string readAt(std::ifstream& file, std::uint64_t offset,
47 std::string bytes(
static_cast<std::size_t
>(count),
'\0');
49 file.seekg(
static_cast<std::streamoff
>(offset));
50 file.read(bytes.data(),
static_cast<std::streamsize
>(count));
51 bytes.resize(
static_cast<std::size_t
>(file.gcount()));
55bool endsWith(
const std::string& text,
const std::string& suffix)
57 return text.size() >= suffix.size() &&
58 text.compare(text.size() - suffix.size(), suffix.size(), suffix) ==
63std::string jsonString(
const std::string& json,
const std::string& key)
65 const std::string quoted =
"\"" + key +
"\"";
66 std::size_t
at = json.find(quoted);
67 if (at == std::string::npos)
return {};
68 at = json.find(
':', at + quoted.size());
69 if (at == std::string::npos)
return {};
70 at = json.find(
'"', at + 1);
71 if (at == std::string::npos)
return {};
72 const std::size_t end = json.find(
'"', at + 1);
73 if (end == std::string::npos)
return {};
74 return json.substr(at + 1, end - at - 1);
78std::vector<std::string> metadataEntries(
const std::string& path)
80 std::ifstream file(path, std::ios::binary);
81 if (!file)
throw std::runtime_error(
"cannot open model package " + path);
82 file.seekg(0, std::ios::end);
83 const auto size =
static_cast<std::uint64_t
>(file.tellg());
84 std::vector<std::string> entries;
85 if (size < 22)
return entries;
87 const std::uint64_t tail_size = std::min<std::uint64_t>(size, 22 + 65535);
88 const std::string tail = readAt(file, size - tail_size, tail_size);
89 std::size_t end = std::string::npos;
90 for (std::size_t at = tail.size() - 22 + 1; at-- > 0;)
91 if (little(tail, at, 4) == kEndOfCentralDirectory) {
95 if (end == std::string::npos)
return entries;
96 std::uint64_t count = little(tail, end + 10, 2);
97 std::uint64_t directory_size = little(tail, end + 12, 4);
98 std::uint64_t directory = little(tail, end + 16, 4);
99 if ((count == 0xffff || directory == 0xffffffff ||
100 directory_size == 0xffffffff) &&
101 end >= 20 && little(tail, end - 20, 4) == kZip64Locator) {
102 const std::uint64_t record = little(tail, end - 20 + 8, 8);
103 const std::string zip64 = readAt(file, record, 56);
104 if (zip64.size() == 56 &&
105 little(zip64, 0, 4) == kZip64EndOfCentralDirectory) {
106 count = little(zip64, 32, 8);
107 directory_size = little(zip64, 40, 8);
108 directory = little(zip64, 48, 8);
111 if (directory + directory_size > size)
return entries;
112 const std::string listing = readAt(file, directory, directory_size);
114 for (std::uint64_t entry = 0; entry < count; ++entry) {
115 if (at + 46 > listing.size() || little(listing, at, 4) != kCentralEntry)
117 const auto method = little(listing, at + 10, 2);
118 std::uint64_t stored = little(listing, at + 20, 4);
119 std::uint64_t original = little(listing, at + 24, 4);
120 const auto name_length =
121 static_cast<std::size_t
>(little(listing, at + 28, 2));
122 const auto extra_length =
123 static_cast<std::size_t
>(little(listing, at + 30, 2));
124 const auto comment_length =
125 static_cast<std::size_t
>(little(listing, at + 32, 2));
126 std::uint64_t local = little(listing, at + 42, 4);
127 const std::string name = listing.substr(at + 46, name_length);
129 for (std::size_t extra = at + 46 + name_length;
130 extra + 4 <=
at + 46 + name_length + extra_length;) {
131 const auto id = little(listing, extra, 2);
133 static_cast<std::size_t
>(little(listing, extra + 2, 2));
135 std::size_t field = extra + 4;
136 if (original == 0xffffffff) {
137 original = little(listing, field, 8);
140 if (stored == 0xffffffff) {
141 stored = little(listing, field, 8);
144 if (local == 0xffffffff) local = little(listing, field, 8);
148 at += 46 + name_length + extra_length + comment_length;
149 if (!endsWith(name,
"_metadata.json") || method != 0 ||
150 stored != original || stored > kMetadataLimit)
152 const std::string header = readAt(file, local, 30);
153 if (header.size() < 30 || little(header, 0, 4) != kLocalHeader)
155 const std::uint64_t data =
156 local + 30 + little(header, 26, 2) + little(header, 28, 2);
157 entries.push_back(readAt(file, data, stored));
162#if defined(__x86_64__) || defined(__i386__)
163std::uint64_t extendedControlRegister() noexcept
165 std::uint32_t low = 0, high = 0;
166 __asm__
volatile(
"xgetbv" :
"=a"(low),
"=d"(high) :
"c"(0));
167 return (
static_cast<std::uint64_t
>(high) << 32) | low;
176#if defined(__x86_64__) || defined(__i386__)
177 unsigned a = 0, b = 0, c = 0, d = 0;
178 if (!__get_cpuid(1, &a, &b, &c, &d))
return host;
179 const bool osxsave = c & (1u << 27);
180 const bool avx = c & (1u << 28);
181 const bool fma = c & (1u << 12);
182 const std::uint64_t xcr0 = osxsave ? extendedControlRegister() : 0;
185 const bool ymm = (xcr0 & 0x6) == 0x6;
186 const bool zmm = ymm && (xcr0 & 0xe0) == 0xe0;
187 const bool tiles = (xcr0 & (3ull << 17)) == (3ull << 17);
188 if (__get_cpuid_max(0,
nullptr) < 7)
return host;
190 __cpuid_count(7, 0, leaf7a, b, c, d);
191 host.
fma = ymm && avx &&
fma;
192 host.
avx2 = ymm && avx && (b & (1u << 5));
193 host.
avx512f = zmm && (b & (1u << 16));
194 host.
avx512dq = zmm && (b & (1u << 17));
195 host.
avx512bw = zmm && (b & (1u << 30));
196 host.
avx512vl = zmm && (b & (1u << 31));
198 host.
amx_bf16 = tiles && (d & (1u << 22));
199 host.
amx_tile = tiles && (d & (1u << 24));
200 host.
amx_int8 = tiles && (d & (1u << 25));
202 __cpuid_count(7, 1, a, b, c, d);
212 std::vector<std::string> entries;
214 entries = metadataEntries(path);
215 }
catch (
const std::out_of_range&) {
220 for (
const std::string& json: entries) {
222 if (target.
device.empty())
223 target.
device = jsonString(json,
"AOTI_DEVICE_KEY");
224 std::istringstream words(jsonString(json,
"AOTI_CPU_ISA"));
225 for (std::string word; words >> word;) {
226 std::transform(word.begin(), word.end(), word.begin(),
227 [](
unsigned char ch) {
228 return static_cast<char>(std::toupper(ch));
230 if (std::find(target.
cpu_isa.begin(), target.
cpu_isa.end(), word) ==
232 target.
cpu_isa.push_back(word);
241 std::vector<std::string> missing;
242 for (
const std::string& token: target.
cpu_isa) {
244 if (token ==
"DEFAULT" || token ==
"NONE")
246 else if (token ==
"AVX2")
247 present = host.
avx2 && host.
fma;
248 else if (token ==
"AVX512")
251 else if (token ==
"AVX512_VNNI")
253 else if (token ==
"AVX512_BF16")
255 else if (token ==
"AMX" || token ==
"AMX_TILE")
257 else if (token ==
"AMX_BF16")
259 else if (token ==
"AMX_INT8")
262 missing.push_back(
"unknown:" + token);
265 if (!present) missing.push_back(token);
274 std::error_code ignored;
275 if (!std::filesystem::is_regular_file(path, ignored))
return;
278 std::string lacking, unknown;
279 for (
const std::string& token:
missingIsa(target, host)) {
280 if (token.rfind(
"unknown:", 0) == 0)
281 unknown += (unknown.empty() ?
"" :
" ") + token.substr(8);
283 lacking += (lacking.empty() ?
"" :
" ") + token;
285 if (!unknown.empty())
286 std::cerr <<
"[model] " << path <<
" names CPU extensions this check "
287 <<
"does not know (" << unknown
288 <<
"); loading it unchecked\n";
289 if (lacking.empty())
return;
291 for (
const std::string& token: target.
cpu_isa)
292 isa += (isa.empty() ?
"" :
" ") + token;
293 std::string available;
294 const std::pair<const char*, bool> extensions[] = {
295 {
"AVX2", host.
avx2 && host.
fma},
303 for (
const auto& [name, present]: extensions)
305 available += (available.empty() ?
"" :
" ") + std::string(name);
306 if (available.empty())
307 available =
"none of the vector extensions above SSE";
308 throw std::runtime_error(
309 "model package " + path +
" requires CPU extensions " + isa +
310 "; this host provides " + available +
" and lacks " + lacking +
311 ", so the package would stop with an illegal instruction. AOTInductor "
312 "packages are compiled per host family: run on a host with " +
313 lacking +
" or pass a package exported on this host family");
Host check for the instruction set an AOTInductor package needs.
Eigen::Vector3d at(const FieldMatrixd &mat, Index_t i)
Potential wrappers and the batched oracle interface.
AotiPackageTarget readAotiPackageTarget(const std::string &path)
std::vector< std::string > missingIsa(const AotiPackageTarget &target, const HostIsa &host)
void requireAotiPackageRunsOnHost(const std::string &path)
void requireAotiPackageRunsOn(const std::string &path, const HostIsa &host)
requireAotiPackageRunsOnHost against an explicit host description.
The fields of an AOTInductor package that name the hardware it targets.
std::string device
AOTI_DEVICE_KEY, e.g. "cpu" or "cuda".
bool metadata_found
any *_metadata.json entry read
std::vector< std::string > cpu_isa
AOTI_CPU_ISA tokens, upper case.
static HostIsa detect() noexcept
CPUID and XGETBV of the calling host; all false off x86-64.