Repository navigation
Expand file tree
/
Copy pathpackage.cpp
More file actions
462 lines (424 loc) · 18.9 KB
/
Copy pathpackage.cpp
File metadata and controls
462 lines (424 loc) · 18.9 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include "engine/framework/model_spec/package.h"
#include "engine/framework/model_spec/schema.h"
#include "engine/framework/assets/tensor_source.h"
#include "engine/framework/io/filesystem.h"
#include "engine/framework/io/json.h"
#include <algorithm>
#include <cctype>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
namespace engine::model_spec {
namespace {
thread_local std::optional<std::filesystem::path> active_model_spec_override;
thread_local std::optional<std::filesystem::path> active_model_path;
thread_local bool active_embedded_spec_checked = false;
thread_local std::optional<assets::GgufEmbeddedModelSpec> active_embedded_spec;
const std::unordered_map<std::string, std::string_view> & builtin_model_specs() {
static const std::unordered_map<std::string, std::string_view> specs = {
#include "model_specs.inc"
};
return specs;
}
std::optional<std::string_view> builtin_model_spec(const std::filesystem::path & spec_path) {
if (spec_path.parent_path() != "@builtin")
return std::nullopt;
const auto it = builtin_model_specs().find(spec_path.stem().string());
if (it == builtin_model_specs().end())
return std::nullopt;
return it->second;
}
std::string model_spec_description(const std::filesystem::path & spec_path) {
if (spec_path.parent_path() == "@gguf") {
return "embedded GGUF model spec for family '" + spec_path.stem().string() + "'";
}
if (spec_path.parent_path() == "@builtin") {
return "builtin model spec for family '" + spec_path.stem().string() + "'";
}
return "model spec '" + spec_path.string() + "'";
}
bool is_gguf_file(const std::filesystem::path & path) {
if (!engine::io::is_existing_file(path))
return false;
std::string extension = path.extension().string();
std::transform(extension.begin(), extension.end(), extension.begin(),
[](unsigned char value) { return static_cast<char>(std::tolower(value)); });
return extension == ".gguf";
}
std::vector<std::string> directory_gguf_files(const std::filesystem::path & path) {
std::vector<std::string> files;
if (!engine::io::is_existing_directory(path)) {
return files;
}
for (const auto & entry : std::filesystem::directory_iterator(path)) {
const auto candidate = entry.path();
if (is_gguf_file(candidate)) {
files.push_back(candidate.filename().string());
}
}
std::sort(files.begin(), files.end());
return files;
}
std::string directory_gguf_hint(std::string_view family) {
if (!active_model_path.has_value()) {
return {};
}
const auto files = directory_gguf_files(*active_model_path);
if (files.empty()) {
return {};
}
std::string message = "model directory has no default GGUF for family '" + std::string(family) +
"': " + active_model_path->string() + "; found: ";
for (size_t i = 0; i < files.size(); ++i) {
if (i != 0) {
message += ", ";
}
message += files[i];
}
message += "; pass the GGUF file directly with --model, or rename it to model.gguf";
return message;
}
std::optional<std::filesystem::path> active_gguf_path() {
if (!active_model_path.has_value())
return std::nullopt;
const auto & path = *active_model_path;
if (is_gguf_file(path)) {
return std::filesystem::weakly_canonical(path);
}
if (engine::io::is_existing_directory(path) && engine::io::is_existing_file(path / "model.gguf")) {
return std::filesystem::weakly_canonical(path / "model.gguf");
}
return std::nullopt;
}
const std::optional<assets::GgufEmbeddedModelSpec> & embedded_model_spec() {
if (!active_embedded_spec_checked) {
active_embedded_spec_checked = true;
if (const auto gguf = active_gguf_path()) {
active_embedded_spec = assets::read_gguf_embedded_model_spec(*gguf);
}
}
return active_embedded_spec;
}
bool external_spec_matches_family(const std::filesystem::path & path, std::string_view family) {
if (!engine::io::is_existing_file(path))
return false;
try {
const auto root = engine::io::json::parse_file(path);
return engine::io::json::require_string(root, "family") == family;
} catch (const std::exception & error) {
throw std::runtime_error("invalid candidate model spec '" + path.string() +
"' while resolving family '" + std::string(family) + "': " + error.what());
}
}
std::optional<std::filesystem::path> discover_external_model_spec(std::string_view family) {
std::vector<std::filesystem::path> candidates;
if (active_model_path.has_value()) {
const auto root = engine::io::is_existing_directory(*active_model_path) ? *active_model_path
: active_model_path->parent_path();
candidates.push_back(root / "model_specs" / (std::string(family) + ".json"));
candidates.push_back(root / (std::string(family) + ".json"));
candidates.push_back(root / "model_spec.json");
candidates.push_back(root.parent_path() / "model_specs" / (std::string(family) + ".json"));
}
auto cursor = std::filesystem::current_path();
while (true) {
candidates.push_back(cursor / "model_specs" / (std::string(family) + ".json"));
const auto parent = cursor.parent_path();
if (parent == cursor || parent.empty())
break;
cursor = parent;
}
for (const auto & candidate : candidates) {
if (external_spec_matches_family(candidate, family)) {
return std::filesystem::weakly_canonical(candidate);
}
}
return std::nullopt;
}
engine::io::json::Value parse_model_spec(const std::filesystem::path & spec_path) {
engine::io::json::Value root;
if (spec_path.parent_path() == "@gguf") {
const auto & spec = embedded_model_spec();
if (!spec.has_value() || spec->family != spec_path.stem().string()) {
throw std::runtime_error("embedded GGUF model spec is not available for: " +
spec_path.stem().string());
}
root = engine::io::json::parse(spec->json);
if (engine::io::json::require_string(root, "family") != spec->family) {
throw std::runtime_error("embedded GGUF model spec family metadata does not match its JSON");
}
} else if (const auto text = builtin_model_spec(spec_path)) {
root = engine::io::json::parse(*text);
} else {
root = engine::io::json::parse_file(spec_path);
}
validate_spec(root, model_spec_description(spec_path));
return root;
}
std::filesystem::path resolve_model_root(const std::filesystem::path & model_path) {
if (engine::io::is_existing_directory(model_path)) {
return std::filesystem::weakly_canonical(model_path);
}
if (engine::io::is_existing_file(model_path)) {
return std::filesystem::weakly_canonical(model_path.parent_path());
}
throw std::runtime_error("model path does not exist: " + model_path.string());
}
std::string require_source_format(const engine::io::json::Value & source) {
return engine::io::json::require_string(source, "format");
}
using ResourceRoots = std::unordered_map<std::string, std::filesystem::path>;
ResourceRoots resolve_source_roots(const std::filesystem::path & model_root, const engine::io::json::Value & source,
const std::optional<std::filesystem::path> & standalone_gguf) {
ResourceRoots roots;
const auto & root_object = source.require("roots").as_object();
for (const auto & [id, value] : root_object) {
const auto root_value = value.as_string();
if (root_value == "$gguf" && !standalone_gguf.has_value()) {
throw std::runtime_error("model source requires a GGUF root: " + id);
}
const auto root_path = root_value == "$gguf" ? *standalone_gguf : model_root / root_value;
if (!engine::io::is_existing_directory(root_path) && !engine::io::is_existing_file(root_path)) {
throw std::runtime_error("missing model root: " + id + "=" + root_path.string());
}
roots.emplace(id, std::filesystem::weakly_canonical(root_path));
}
return roots;
}
std::filesystem::path resolve_resource_ref(const std::unordered_map<std::string, std::filesystem::path> & roots,
const std::string & ref) {
const auto split = ref.find(':');
if (split == std::string::npos || split == 0) {
throw std::runtime_error("invalid model resource reference: " + ref);
}
const auto root_id = ref.substr(0, split);
const auto relative_path = ref.substr(split + 1);
const auto root_it = roots.find(root_id);
if (root_it == roots.end()) {
throw std::runtime_error("unknown model resource root: " + root_id);
}
if (relative_path.empty()) {
return root_it->second;
}
return root_it->second / relative_path;
}
void add_resource_map(assets::ResourceBundle & bundle,
const ResourceRoots & roots,
const engine::io::json::Value * map_value) {
if (map_value == nullptr || map_value->is_null()) {
return;
}
for (const auto & [id, ref] : map_value->as_object()) {
const auto path = resolve_resource_ref(roots, ref.as_string());
if (!engine::io::is_existing_file(path)) {
throw std::runtime_error("missing model file '" + id + "': " + path.string());
}
bundle.add_file(id, path);
}
}
std::filesystem::path resolve_tensor_source_ref(const ResourceRoots & roots, const engine::io::json::Value & value,
std::string & prefix) {
if (value.is_string()) {
prefix.clear();
return resolve_resource_ref(roots, value.as_string());
}
const auto & object = value.as_object();
const auto source_it = object.find("source");
if (source_it == object.end()) {
throw std::runtime_error("model tensor source object requires source");
}
const auto prefix_it = object.find("prefix");
prefix = prefix_it == object.end() ? "" : prefix_it->second.as_string();
return resolve_resource_ref(roots, source_it->second.as_string());
}
void add_tensor_map(assets::ResourceBundle & bundle,
const ResourceRoots & roots,
const engine::io::json::Value * map_value) {
if (map_value == nullptr || map_value->is_null()) {
return;
}
for (const auto & [id, ref] : map_value->as_object()) {
std::string prefix;
const auto path = resolve_tensor_source_ref(roots, ref, prefix);
if (!engine::io::is_existing_file(path)) {
throw std::runtime_error("missing model tensor source '" + id + "': " + path.string());
}
bundle.add_tensor_source(id, path, std::move(prefix));
}
}
void add_optional_resource_map(assets::ResourceBundle & bundle, const ResourceRoots & roots,
const engine::io::json::Value * map_value) {
if (map_value == nullptr || map_value->is_null()) {
return;
}
for (const auto & [id, ref] : map_value->as_object()) {
const auto path = resolve_resource_ref(roots, ref.as_string());
if (engine::io::is_existing_file(path)) {
bundle.add_file(id, path);
}
}
}
std::vector<assets::ResourceFile> resources_from_resource_map(const ResourceRoots & roots,
const engine::io::json::Value * map_value,
bool required) {
std::vector<assets::ResourceFile> out;
if (map_value == nullptr || map_value->is_null()) {
return out;
}
const auto & map = map_value->as_object();
out.reserve(map.size());
for (const auto & [id, ref] : map) {
std::string prefix;
const auto path = resolve_tensor_source_ref(roots, ref, prefix);
if (required || engine::io::is_existing_file(path)) {
out.push_back({id, std::filesystem::weakly_canonical(path)});
}
}
return out;
}
assets::ResourceBundle load_source(const std::filesystem::path & model_root, const engine::io::json::Value & source,
const ResourceRoots & roots) {
assets::ResourceBundle bundle(model_root);
add_resource_map(bundle, roots, source.find("files"));
add_optional_resource_map(bundle, roots, source.find("optional_files"));
add_tensor_map(bundle, roots, source.find("tensors"));
return bundle;
}
std::vector<assets::ResourceFile> discover_safetensors_source_resources(const engine::io::json::Value & source,
ResourceKind kind,
const ResourceRoots & roots) {
auto resources = resources_from_resource_map(
roots, source.find(kind == ResourceKind::Files ? "files" : "tensors"), true);
if (kind == ResourceKind::Files) {
auto optional = resources_from_resource_map(roots, source.find("optional_files"), false);
resources.insert(resources.end(), optional.begin(), optional.end());
}
return resources;
}
struct SelectedSource {
std::filesystem::path model_root;
engine::io::json::Value source;
ResourceRoots roots;
std::string spec_description;
std::string source_format;
};
SelectedSource select_source(const std::filesystem::path & model_path, const std::filesystem::path & model_root,
const std::string & spec_description, const engine::io::json::Value & source) {
const auto format = require_source_format(source);
if (format == "gguf") {
const auto prepared = assets::prepare_model_directory(model_path);
auto roots = resolve_source_roots(prepared.model_root, source, prepared.standalone_gguf);
return SelectedSource{prepared.model_root, source, std::move(roots), spec_description, format};
}
if (format == "safetensors") {
auto roots = resolve_source_roots(model_root, source, std::nullopt);
return SelectedSource{model_root, source, std::move(roots), spec_description, format};
}
throw std::runtime_error("unsupported model source format: " + format);
}
SelectedSource require_selected_source(const std::filesystem::path & model_path,
const std::filesystem::path & spec_path) {
const auto model_root = resolve_model_root(model_path);
const auto spec_description = model_spec_description(spec_path);
engine::io::json::Value spec;
try {
spec = parse_model_spec(spec_path);
} catch (const std::exception & error) {
throw std::runtime_error("failed to parse " + spec_description + ": " + error.what());
}
const auto & sources = spec.require("sources").as_array();
const bool explicit_gguf_path = is_gguf_file(model_path);
const bool directory_has_gguf =
engine::io::is_existing_directory(model_path) && engine::io::is_existing_file(model_path / "model.gguf");
const bool use_gguf = explicit_gguf_path || directory_has_gguf;
for (const auto & source : sources) {
const auto format = require_source_format(source);
if ((use_gguf && format == "gguf") || (!use_gguf && format == "safetensors")) {
try {
return select_source(model_path, model_root, spec_description, source);
} catch (const std::exception & error) {
throw std::runtime_error("failed to select " + std::string(use_gguf ? "GGUF" : "safetensors") +
" source from " + spec_description + ": " + error.what());
}
}
}
throw std::runtime_error(std::string("no ") + (use_gguf ? "gguf" : "safetensors") + " model source in " +
spec_description);
}
} // namespace
ScopedSpecOverride::ScopedSpecOverride(const std::optional<std::filesystem::path> & path,
const std::filesystem::path & model_path)
: previous_(active_model_spec_override), previous_model_path_(active_model_path) {
active_model_spec_override = path;
active_model_path = model_path.empty() ? std::nullopt : std::make_optional(model_path);
active_embedded_spec_checked = false;
active_embedded_spec.reset();
}
ScopedSpecOverride::~ScopedSpecOverride() {
active_model_spec_override = std::move(previous_);
active_model_path = std::move(previous_model_path_);
active_embedded_spec_checked = false;
active_embedded_spec.reset();
}
std::filesystem::path default_spec_path(std::string_view family) {
if (active_model_spec_override.has_value()) {
auto path = *active_model_spec_override;
if (engine::io::is_existing_directory(path)) {
path /= std::string(family) + ".json";
}
if (!engine::io::is_existing_file(path)) {
throw std::runtime_error("model spec override not found: " + path.string());
}
return std::filesystem::weakly_canonical(path);
}
if (const auto & embedded = embedded_model_spec(); embedded.has_value()) {
if (embedded->family != family) {
throw std::runtime_error("GGUF embeds model spec for family '" + embedded->family + "', not '" +
std::string(family) + "'");
}
return std::filesystem::path("@gguf") / (std::string(family) + ".json");
}
if (builtin_model_specs().find(std::string(family)) != builtin_model_specs().end()) {
return std::filesystem::path("@builtin") / (std::string(family) + ".json");
}
if (const auto external = discover_external_model_spec(family)) {
return *external;
}
if (const auto hint = directory_gguf_hint(family); !hint.empty()) {
throw std::runtime_error(hint);
}
throw std::runtime_error("model spec not found for family '" + std::string(family) +
"' (provide --model-spec-override, embed it in the GGUF, enable "
"AUDIOCPP_DEPLOYMENT_BUILD, or install model_specs/" +
std::string(family) + ".json)");
}
assets::ResourceBundle load_resource_bundle(
const std::filesystem::path & model_path,
const std::filesystem::path & spec_path) {
auto selected = require_selected_source(model_path, spec_path);
try {
return load_source(selected.model_root, selected.source, selected.roots);
} catch (const std::exception & error) {
throw std::runtime_error("failed to load model resources using " + selected.spec_description +
" source '" + selected.source_format + "': " + error.what());
}
}
engine::io::json::Value load_spec(const std::filesystem::path & spec_path) {
return parse_model_spec(spec_path);
}
std::vector<assets::ResourceFile> discover_resources(
const std::filesystem::path & model_path,
const std::filesystem::path & spec_path,
ResourceKind kind) {
auto selected = require_selected_source(model_path, spec_path);
try {
return discover_safetensors_source_resources(selected.source, kind, selected.roots);
} catch (const std::exception & error) {
throw std::runtime_error("failed to discover model resources using " + selected.spec_description +
" source '" + selected.source_format + "': " + error.what());
}
}
} // namespace engine::model_spec