Repository navigation
Expand file tree
/
Copy pathoptions.cpp
More file actions
327 lines (296 loc) · 11.2 KB
/
Copy pathoptions.cpp
File metadata and controls
327 lines (296 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "engine/framework/runtime/options.h"
#include "engine/framework/assets/tensor_source.h"
#include <chrono>
#include <cmath>
#include <limits>
#include <random>
#include <stdexcept>
namespace engine::runtime {
namespace {
int parse_int_value(const std::string & value, std::string_view key) {
size_t parsed = 0;
const long long out = std::stoll(value, &parsed);
if (parsed != value.size()) {
throw std::runtime_error(std::string(key) + " must be an integer");
}
if (out < std::numeric_limits<int>::min() || out > std::numeric_limits<int>::max()) {
throw std::runtime_error(std::string(key) + " is outside int range");
}
return static_cast<int>(out);
}
float parse_float_value(const std::string & value, std::string_view key) {
size_t parsed = 0;
const float out = std::stof(value, &parsed);
if (parsed != value.size()) {
throw std::runtime_error(std::string(key) + " must be a float");
}
return out;
}
uint32_t parse_u32_value(const std::string & value, std::string_view key) {
if (!value.empty() && value.front() == '-') {
throw std::runtime_error(std::string(key) + " must be an unsigned integer");
}
size_t parsed = 0;
const unsigned long out = std::stoul(value, &parsed);
if (parsed != value.size()) {
throw std::runtime_error(std::string(key) + " must be an unsigned integer");
}
if (out > std::numeric_limits<uint32_t>::max()) {
throw std::runtime_error(std::string(key) + " is outside uint32 range");
}
return static_cast<uint32_t>(out);
}
uint64_t parse_u64_value(const std::string & value, std::string_view key) {
if (!value.empty() && value.front() == '-') {
throw std::runtime_error(std::string(key) + " must be an unsigned integer");
}
size_t parsed = 0;
const unsigned long long out = std::stoull(value, &parsed);
if (parsed != value.size()) {
throw std::runtime_error(std::string(key) + " must be an unsigned integer");
}
if (out > std::numeric_limits<uint64_t>::max()) {
throw std::runtime_error(std::string(key) + " is outside uint64 range");
}
return static_cast<uint64_t>(out);
}
int64_t parse_i64_value(const std::string & value, std::string_view key) {
size_t parsed = 0;
const int64_t out = std::stoll(value, &parsed);
if (parsed != value.size()) {
throw std::runtime_error(std::string(key) + " must be an integer");
}
return out;
}
size_t checked_mib_to_bytes(int64_t mb, std::string_view key) {
constexpr int64_t kMiB = 1024 * 1024;
if (mb > static_cast<int64_t>(std::numeric_limits<size_t>::max() / kMiB)) {
throw std::runtime_error(std::string(key) + " is too large");
}
return static_cast<size_t>(mb) * static_cast<size_t>(kMiB);
}
std::string tensor_storage_type_name(assets::TensorStorageType storage_type) {
switch (storage_type) {
case assets::TensorStorageType::Native:
return "native";
case assets::TensorStorageType::F32:
return "f32";
case assets::TensorStorageType::F16:
return "f16";
case assets::TensorStorageType::BF16:
return "bf16";
case assets::TensorStorageType::I8:
return "i8";
case assets::TensorStorageType::Q4_0:
return "q4_0";
case assets::TensorStorageType::Q4_1:
return "q4_1";
case assets::TensorStorageType::Q5_0:
return "q5_0";
case assets::TensorStorageType::Q5_1:
return "q5_1";
case assets::TensorStorageType::Q2_K:
return "q2_k";
case assets::TensorStorageType::Q3_K:
return "q3_k";
case assets::TensorStorageType::Q4_K:
return "q4_k";
case assets::TensorStorageType::Q5_K:
return "q5_k";
case assets::TensorStorageType::Q6_K:
return "q6_k";
case assets::TensorStorageType::Q8_0:
return "q8_0";
}
throw std::runtime_error("unknown tensor storage type");
}
std::string join_tensor_storage_types(std::initializer_list<assets::TensorStorageType> values) {
std::string out;
size_t index = 0;
for (const auto value : values) {
if (index != 0) {
out += index + 1 == values.size() ? ", and " : ", ";
}
out += tensor_storage_type_name(value);
++index;
}
return out;
}
bool contains_tensor_storage_type(
std::initializer_list<assets::TensorStorageType> values,
assets::TensorStorageType needle) {
for (const auto value : values) {
if (value == needle) {
return true;
}
}
return false;
}
} // namespace
std::optional<OptionMatch> find_option_match(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
std::optional<OptionMatch> match;
for (const std::string_view key : keys) {
const auto it = options.find(std::string(key));
if (it != options.end() && !it->second.empty()) {
if (match.has_value()) {
if (match->value != it->second) {
throw std::runtime_error(
"conflicting option values for " + match->key + " and " + std::string(key));
}
continue;
}
match = OptionMatch{std::string(key), it->second};
}
}
return match;
}
std::optional<std::string> find_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
const auto match = find_option_match(options, keys);
return match.has_value() ? std::optional<std::string>(match->value) : std::nullopt;
}
bool parse_bool_option(const std::string & value, std::string_view key) {
if (value == "true" || value == "1" || value == "yes" || value == "on") {
return true;
}
if (value == "false" || value == "0" || value == "no" || value == "off") {
return false;
}
throw std::runtime_error(std::string(key) + " must be true/false, yes/no, on/off, or 1/0");
}
std::optional<int> parse_int_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
if (const auto match = find_option_match(options, keys)) {
return parse_int_value(match->value, match->key);
}
return std::nullopt;
}
std::optional<float> parse_float_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
if (const auto match = find_option_match(options, keys)) {
return parse_float_value(match->value, match->key);
}
return std::nullopt;
}
std::optional<float> parse_finite_float_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
if (const auto match = find_option_match(options, keys)) {
const float value = parse_float_value(match->value, match->key);
if (!std::isfinite(value)) {
throw std::runtime_error(match->key + " must be a finite float");
}
return value;
}
return std::nullopt;
}
std::optional<float> parse_positive_finite_float_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
if (const auto match = find_option_match(options, keys)) {
const float value = parse_float_value(match->value, match->key);
if (!std::isfinite(value)) {
throw std::runtime_error(match->key + " must be a finite float");
}
if (value <= 0.0F) {
throw std::runtime_error(match->key + " must be positive");
}
return value;
}
return std::nullopt;
}
std::optional<uint32_t> parse_u32_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
if (const auto match = find_option_match(options, keys)) {
return parse_u32_value(match->value, match->key);
}
return std::nullopt;
}
std::optional<uint64_t> parse_u64_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
if (const auto match = find_option_match(options, keys)) {
return parse_u64_value(match->value, match->key);
}
return std::nullopt;
}
std::optional<int64_t> parse_i64_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
if (const auto match = find_option_match(options, keys)) {
return parse_i64_value(match->value, match->key);
}
return std::nullopt;
}
int64_t parse_positive_i64_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys,
int64_t fallback) {
if (const auto match = find_option_match(options, keys)) {
const int64_t value = parse_i64_value(match->value, match->key);
if (value <= 0) {
throw std::runtime_error(match->key + " must be positive");
}
return value;
}
return fallback;
}
size_t parse_size_mb_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys,
size_t fallback_bytes) {
if (const auto match = find_option_match(options, keys)) {
const int64_t mb = parse_i64_value(match->value, match->key);
if (mb <= 0) {
throw std::runtime_error(match->key + " must be positive");
}
return checked_mib_to_bytes(mb, match->key);
}
return fallback_bytes;
}
assets::TensorStorageType parse_tensor_storage_option(
const std::unordered_map<std::string, std::string> & options,
std::string_view key,
assets::TensorStorageType fallback,
std::initializer_list<assets::TensorStorageType> allowed) {
if (allowed.size() == 0) {
throw std::runtime_error("tensor storage option allowlist must not be empty");
}
const auto match = find_option_match(options, {key});
if (!match.has_value()) {
return fallback;
}
const auto storage_type = assets::parse_tensor_storage_type(match->value);
if (contains_tensor_storage_type(allowed, storage_type)) {
return storage_type;
}
throw std::runtime_error(match->key + " supports only " + join_tensor_storage_types(allowed));
}
assets::TensorStorageType parse_tensor_storage_option(
const std::unordered_map<std::string, std::string> & options,
std::string_view key,
std::string_view fallback_key,
assets::TensorStorageType fallback,
std::initializer_list<assets::TensorStorageType> allowed) {
if (find_option_match(options, {key}).has_value()) {
return parse_tensor_storage_option(options, key, fallback, allowed);
}
return parse_tensor_storage_option(options, fallback_key, fallback, allowed);
}
uint64_t random_u64_seed() {
std::random_device random_device;
const uint64_t hi = static_cast<uint64_t>(random_device()) << 32U;
const uint64_t lo = static_cast<uint64_t>(random_device());
const auto now = std::chrono::high_resolution_clock::now().time_since_epoch().count();
return hi ^ lo ^ static_cast<uint64_t>(now);
}
uint32_t random_u32_seed() {
return static_cast<uint32_t>(random_u64_seed());
}
} // namespace engine::runtime