Repository navigation
Expand file tree
/
Copy pathbackend.cpp
More file actions
768 lines (695 loc) · 28.9 KB
/
Copy pathbackend.cpp
File metadata and controls
768 lines (695 loc) · 28.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#include "engine/framework/core/backend.h"
#include "engine/framework/core/execution_context.h"
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <ostream>
#include <stdexcept>
#include <string>
namespace engine::core {
void ensure_backends_loaded() {
if (ggml_backend_reg_count() == 0) {
ggml_backend_load_all();
}
}
namespace {
// A backend is identified by the name of the ggml registry that owns it. The device type
// (GPU/IGPU/ACCEL) deliberately plays no part in that: Metal reports GPU rather than ACCEL,
// Vulkan reports IGPU on integrated GPUs, and those values are free to change upstream.
//
// The CUDA registry name depends on how ggml was built - GGML_CUDA_NAME is "MUSA" under
// MUSA - so every alias has to be accepted. HIP builds share the ggml CUDA backend but
// register as "ROCm" and get their own BackendType::Hip. The names are spelled out here
// instead of pulled from ggml-cuda.h/ggml-vulkan.h because those headers resolve against the
// backend's own build flags, which are not visible from this translation unit.
struct BackendRegNames {
BackendType type;
const char * names[3]; // unused entries are nullptr
};
constexpr BackendRegNames k_backend_reg_names[] = {
{BackendType::Cuda, {"CUDA", "MUSA", nullptr}},
{BackendType::Hip, {"ROCm", nullptr, nullptr}},
{BackendType::Vulkan, {"Vulkan", nullptr, nullptr}},
{BackendType::Metal, {"MTL", nullptr, nullptr}},
};
bool reg_name_matches(BackendType type, const char * reg_name) {
if (reg_name == nullptr) {
return false;
}
for (const BackendRegNames & entry : k_backend_reg_names) {
if (entry.type != type) {
continue;
}
for (const char * name : entry.names) {
if (name != nullptr && std::strcmp(reg_name, name) == 0) {
return true;
}
}
return false;
}
return false;
}
bool backend_handle_matches(ggml_backend_t backend, BackendType type) {
if (backend == nullptr) return false;
ggml_backend_dev_t device = ggml_backend_get_device(backend);
if (device == nullptr) return false;
ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(device);
return reg != nullptr && reg_name_matches(type, ggml_backend_reg_name(reg));
}
bool is_cuda_backend_handle(ggml_backend_t backend) {
return backend_handle_matches(backend, BackendType::Cuda);
}
bool is_hip_backend_handle(ggml_backend_t backend) {
return backend_handle_matches(backend, BackendType::Hip);
}
bool is_vulkan_backend_handle(ggml_backend_t backend) {
return backend_handle_matches(backend, BackendType::Vulkan);
}
bool is_metal_backend_handle(ggml_backend_t backend) {
return backend_handle_matches(backend, BackendType::Metal);
}
ggml_backend_reg_t find_reg_by_backend_type(BackendType type) {
for (size_t i = 0; i < ggml_backend_reg_count(); ++i) {
ggml_backend_reg_t reg = ggml_backend_reg_get(i);
if (reg != nullptr && reg_name_matches(type, ggml_backend_reg_name(reg))) {
return reg;
}
}
return nullptr;
}
// Device indices are relative to the owning registry, matching how ggml itself numbers the
// devices of a backend.
ggml_backend_dev_t find_device_by_backend_type(BackendType type, int device_index) {
if (device_index < 0) {
return nullptr;
}
ggml_backend_reg_t reg = find_reg_by_backend_type(type);
if (reg == nullptr) {
return nullptr;
}
if (static_cast<size_t>(device_index) >= ggml_backend_reg_dev_count(reg)) {
return nullptr;
}
return ggml_backend_reg_dev_get(reg, static_cast<size_t>(device_index));
}
const char * backend_dev_type_label(enum ggml_backend_dev_type type) {
switch (type) {
case GGML_BACKEND_DEVICE_TYPE_CPU: return "CPU";
case GGML_BACKEND_DEVICE_TYPE_GPU: return "GPU";
case GGML_BACKEND_DEVICE_TYPE_IGPU: return "IGPU";
case GGML_BACKEND_DEVICE_TYPE_ACCEL: return "ACCEL";
case GGML_BACKEND_DEVICE_TYPE_META: return "META";
}
return "UNKNOWN";
}
std::string describe_available_devices() {
std::string description;
for (const auto & device : list_backend_devices()) {
if (!description.empty()) {
description += ", ";
}
description += device.backend + ":" + std::to_string(device.index);
if (!device.name.empty()) {
description += " \"" + device.name + "\"";
}
description += " [" + device.type + "]";
}
return description.empty() ? "none" : description;
}
std::string describe_missing_device(BackendType type, const char * label, int device_index) {
ggml_backend_reg_t reg = find_reg_by_backend_type(type);
std::string message = std::string(label) + " backend requested but ";
if (reg == nullptr) {
message += "it is not registered in this build";
} else {
message += "registry '" + std::string(ggml_backend_reg_name(reg)) +
"' has no device " + std::to_string(device_index);
}
return message + " (available: " + describe_available_devices() + ")";
}
ggml_backend_t init_device_backend(BackendType type, const char * label, const BackendConfig & config) {
if (config.device < 0) {
throw std::runtime_error(
std::string(label) + " backend requested with negative device index");
}
ggml_backend_dev_t device = find_device_by_backend_type(type, config.device);
if (device == nullptr) {
throw std::runtime_error(describe_missing_device(type, label, config.device));
}
ggml_backend_t backend = ggml_backend_dev_init(device, nullptr);
if (backend == nullptr) {
throw std::runtime_error(
"Failed to initialize " + std::string(label) + " backend on device " +
std::to_string(config.device));
}
return backend;
}
#ifndef NDEBUG
bool backend_graph_validation_enabled() {
static const bool enabled = [] {
const char * value = std::getenv("ENGINE_VALIDATE_BACKEND_GRAPH");
return value != nullptr && std::strcmp(value, "1") == 0;
}();
return enabled;
}
#endif
} // namespace
std::vector<BackendDeviceInfo> list_backend_devices() {
ensure_backends_loaded();
std::vector<BackendDeviceInfo> devices;
for (size_t i = 0; i < ggml_backend_reg_count(); ++i) {
ggml_backend_reg_t reg = ggml_backend_reg_get(i);
if (reg == nullptr) {
continue;
}
const char * reg_name = ggml_backend_reg_name(reg);
for (size_t j = 0; j < ggml_backend_reg_dev_count(reg); ++j) {
ggml_backend_dev_t dev = ggml_backend_reg_dev_get(reg, j);
if (dev == nullptr) {
continue;
}
BackendDeviceInfo info;
info.backend = reg_name != nullptr ? reg_name : "<unnamed>";
info.index = static_cast<int>(j);
// Prefer the description: ggml's device name is a generic handle
// ("CUDA0", "ROCm0"), while the description carries the hardware
// name ("NVIDIA GeForce RTX 2080 Ti") that tells identical cards apart.
const char * dev_description = ggml_backend_dev_description(dev);
const char * dev_name = ggml_backend_dev_name(dev);
if (dev_description != nullptr && dev_description[0] != '\0') {
info.name = dev_description;
} else if (dev_name != nullptr) {
info.name = dev_name;
}
info.type = backend_dev_type_label(ggml_backend_dev_type(dev));
devices.push_back(std::move(info));
}
}
return devices;
}
void print_backend_devices(std::ostream & out) {
const auto devices = list_backend_devices();
out << "available_devices=" << devices.size() << "\n";
for (const auto & device : devices) {
out << device.backend << ":" << device.index;
if (!device.name.empty()) {
out << " \"" << device.name << "\"";
}
out << " [" << device.type << "]\n";
}
out << "select with: --backend <cuda|hip|vulkan|metal|cpu> --device <index>\n";
}
ggml_backend_t init_backend(const BackendConfig & config) {
ensure_backends_loaded();
switch (config.type) {
case BackendType::Cpu: {
ggml_backend_t backend = ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr);
if (backend == nullptr) {
throw std::runtime_error("Failed to initialize CPU backend");
}
return backend;
}
case BackendType::Hip:
return init_device_backend(BackendType::Hip, "HIP", config);
case BackendType::Cuda:
return init_device_backend(BackendType::Cuda, "CUDA", config);
case BackendType::Vulkan:
return init_device_backend(BackendType::Vulkan, "Vulkan", config);
case BackendType::Metal:
return init_device_backend(BackendType::Metal, "Metal", config);
case BackendType::BestAvailable: {
ggml_backend_t backend = ggml_backend_init_best();
if (backend == nullptr) {
throw std::runtime_error("Failed to initialize best backend");
}
return backend;
}
default:
throw std::runtime_error("Unsupported backend type");
}
}
void set_backend_threads(ggml_backend_t backend, int threads) {
if (backend == nullptr) {
return;
}
ggml_backend_dev_t device = ggml_backend_get_device(backend);
if (device == nullptr || ggml_backend_dev_type(device) != GGML_BACKEND_DEVICE_TYPE_CPU) {
return;
}
// Use generic proc-address lookup for thread-setting (works with GGML_BACKEND_DL)
ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(device);
void * fn = ggml_backend_reg_get_proc_address(reg, "ggml_backend_set_n_threads");
if (fn != nullptr) {
((ggml_backend_set_n_threads_t)fn)(backend, threads);
}
}
bool is_host_backend(ggml_backend_t backend) {
if (backend == nullptr) {
return false;
}
ggml_backend_dev_t device = ggml_backend_get_device(backend);
return device != nullptr && ggml_backend_dev_type(device) == GGML_BACKEND_DEVICE_TYPE_CPU;
}
BackendType backend_type(ggml_backend_t backend) {
if (is_host_backend(backend)) {
return BackendType::Cpu;
}
if (is_hip_backend_handle(backend)) {
return BackendType::Hip;
}
if (is_cuda_backend_handle(backend)) {
return BackendType::Cuda;
}
if (is_vulkan_backend_handle(backend)) {
return BackendType::Vulkan;
}
if (is_metal_backend_handle(backend)) {
return BackendType::Metal;
}
return BackendType::BestAvailable;
}
bool uses_host_graph_plan(BackendType type) {
return type == BackendType::Cpu;
}
bool uses_host_graph_plan(ggml_backend_t backend) {
return is_host_backend(backend);
}
bool requested_backend_uses_host_graph_plan(const BackendConfig & config) {
return uses_host_graph_plan(config.type);
}
static void cuda_clear_graph(ggml_backend_t backend, ggml_cgraph * graph) {
if (backend == nullptr || graph == nullptr) return;
ggml_backend_dev_t device = ggml_backend_get_device(backend);
if (device == nullptr) return;
auto fn = (void (*)(ggml_backend_t, const ggml_cgraph *))
ggml_backend_reg_get_proc_address(
ggml_backend_dev_backend_reg(device),
"ggml_backend_cuda_clear_graph");
if (fn != nullptr) fn(backend, graph);
}
static void cuda_trim_pools(ggml_backend_t backend) {
if (backend == nullptr) return;
ggml_backend_dev_t device = ggml_backend_get_device(backend);
if (device == nullptr) return;
auto fn = (void (*)(ggml_backend_t))
ggml_backend_reg_get_proc_address(
ggml_backend_dev_backend_reg(device),
"ggml_backend_cuda_trim_pools");
if (fn != nullptr) fn(backend);
}
void trim_backend_pools(ggml_backend_t backend) {
if (is_cuda_backend_handle(backend) || is_hip_backend_handle(backend)) cuda_trim_pools(backend);
}
void set_backend_stream_priority(ggml_backend_t backend, int priority) {
if (backend == nullptr) return;
if (!is_cuda_backend_handle(backend) && !is_hip_backend_handle(backend)) return;
ggml_backend_dev_t device = ggml_backend_get_device(backend);
if (device == nullptr) return;
auto fn = (void (*)(ggml_backend_t, int))
ggml_backend_reg_get_proc_address(
ggml_backend_dev_backend_reg(device),
"ggml_backend_cuda_set_stream_priority");
if (fn != nullptr) fn(backend, priority);
}
void * backend_cuda_stream(ggml_backend_t backend) {
if (backend == nullptr) return nullptr;
if (!is_cuda_backend_handle(backend) && !is_hip_backend_handle(backend)) return nullptr;
ggml_backend_dev_t device = ggml_backend_get_device(backend);
if (device == nullptr) {
throw std::runtime_error("CUDA backend stream lookup failed: backend has no device");
}
auto fn = (void * (*)(ggml_backend_t))
ggml_backend_reg_get_proc_address(
ggml_backend_dev_backend_reg(device),
"ggml_backend_cuda_get_stream");
if (fn == nullptr) {
throw std::runtime_error("CUDA backend stream lookup failed: backend does not export ggml_backend_cuda_get_stream");
}
return fn(backend);
}
// evict_cuda_graph_cache defaults to false, preserving historical behavior
// for existing call sites: before the CUDA backend exported
// ggml_backend_cuda_clear_graph the lookup resolved nothing, and families
// that rebuild same-shape graphs between requests inherit a warm CUDA-graph
// cache from that. Families that prefer bounded memory over the warm
// carry-over opt in with true.
void release_backend_graph_resources(ggml_backend_t backend, ggml_cgraph * graph, bool evict_cuda_graph_cache) {
if (!evict_cuda_graph_cache) return; // existing callsite/behavior unchanged
if (is_cuda_backend_handle(backend) || is_hip_backend_handle(backend)) cuda_clear_graph(backend, graph);
}
void release_backend_graph_resources(BackendType backend_type, ggml_backend_t backend, ggml_cgraph * graph, bool evict_cuda_graph_cache) {
if (!evict_cuda_graph_cache) return; // existing callsite/behavior unchanged
if (backend_type == BackendType::Cuda || backend_type == BackendType::Hip) cuda_clear_graph(backend, graph);
}
void validate_backend_graph_supported(ggml_backend_t backend, ggml_cgraph * graph, const char * label) {
if (backend == nullptr || graph == nullptr) {
throw std::runtime_error("Cannot validate backend graph support for null backend or graph");
}
const int nodes = ggml_graph_n_nodes(graph);
for (int i = 0; i < nodes; ++i) {
ggml_tensor * node = ggml_graph_node(graph, i);
if (node != nullptr && !ggml_backend_supports_op(backend, node)) {
const char * graph_label = label != nullptr ? label : "graph";
std::string op_name = ggml_op_name(node->op);
if (node->op == GGML_OP_UNARY) {
op_name += "/";
op_name += ggml_unary_op_name(ggml_get_unary_op(node));
}
throw std::runtime_error(
std::string(graph_label) +
" contains unsupported backend op '" +
op_name +
"' at node " +
std::to_string(i) +
" tensor '" +
(node->name[0] != '\0' ? node->name : "<unnamed>") +
"'");
}
}
}
BackendMemorySnapshot query_backend_memory(ggml_backend_t backend, int device_hint) {
BackendMemorySnapshot snapshot;
if (backend == nullptr) {
return snapshot;
}
ggml_backend_dev_t device = ggml_backend_get_device(backend);
if (device != nullptr) {
size_t free_bytes = 0;
size_t total_bytes = 0;
ggml_backend_dev_memory(device, &free_bytes, &total_bytes);
if (total_bytes > 0 && free_bytes <= total_bytes) {
snapshot.available = true;
snapshot.total_bytes = static_cast<int64_t>(total_bytes);
snapshot.free_bytes = static_cast<int64_t>(free_bytes);
snapshot.used_bytes = static_cast<int64_t>(total_bytes - free_bytes);
}
}
(void)device_hint;
return snapshot;
}
BackendMemorySnapshot query_backend_memory(const BackendConfig & config) {
BackendMemorySnapshot snapshot;
switch (config.type) {
case BackendType::Cpu:
return snapshot;
case BackendType::BestAvailable:
return snapshot;
default:
break;
}
ggml_backend_dev_t device = find_device_by_backend_type(config.type, config.device);
if (device != nullptr) {
size_t free_bytes = 0;
size_t total_bytes = 0;
ggml_backend_dev_memory(device, &free_bytes, &total_bytes);
if (total_bytes > 0 && free_bytes <= total_bytes) {
snapshot.available = true;
snapshot.total_bytes = static_cast<int64_t>(total_bytes);
snapshot.free_bytes = static_cast<int64_t>(free_bytes);
snapshot.used_bytes = static_cast<int64_t>(total_bytes - free_bytes);
}
}
return snapshot;
}
ggml_backend_graph_plan_t create_backend_graph_plan_if_host(ggml_backend_t backend, ggml_cgraph * graph) {
if (backend == nullptr || graph == nullptr || !is_host_backend(backend)) {
return nullptr;
}
return ggml_backend_graph_plan_create(backend, graph);
}
void free_backend_graph_plan(ggml_backend_t backend, ggml_backend_graph_plan_t & plan) {
if (plan != nullptr) {
ggml_backend_graph_plan_free(backend, plan);
plan = nullptr;
}
}
ggml_status compute_backend_graph(
ggml_backend_t backend,
ggml_cgraph * graph,
ggml_backend_graph_plan_t plan,
const char * label) {
if (backend == nullptr || graph == nullptr) {
return GGML_STATUS_FAILED;
}
#ifndef NDEBUG
if (plan == nullptr && backend_graph_validation_enabled()) {
validate_backend_graph_supported(backend, graph, label);
} else {
(void)label;
}
#else
(void)label;
#endif
return plan != nullptr
? ggml_backend_graph_plan_compute(backend, plan)
: ggml_backend_graph_compute(backend, graph);
}
void prepare_host_graph_plan(const ExecutionContext & execution_context, ggml_cgraph * graph, HostGraphPlan & plan) {
plan.reset();
if (!execution_context.uses_host_graph_plan()) {
return;
}
ggml_backend_t backend = execution_context.backend();
if (backend == nullptr) {
return;
}
ggml_backend_graph_plan_t new_plan = ggml_backend_graph_plan_create(backend, graph);
if (new_plan != nullptr) {
plan.plan = new_plan;
plan.backend = backend;
}
}
ggml_status compute_graph(
const ExecutionContext & execution_context,
ggml_cgraph * graph,
HostGraphPlan & plan,
const char * label) {
if (plan.active()) {
return ggml_backend_graph_plan_compute(plan.backend, plan.plan);
}
return compute_backend_graph(execution_context.backend(), graph, nullptr, label);
}
void write_tensor_f32(const TensorValue & tensor, const float * values, size_t count) {
if (tensor.type != GGML_TYPE_F32) {
throw std::runtime_error("write_tensor_f32 requires GGML_TYPE_F32 tensor");
}
if (tensor.shape.num_elements() != static_cast<int64_t>(count)) {
throw std::runtime_error(
"write_tensor_f32 value count does not match tensor shape for tensor '" +
std::string(tensor.tensor != nullptr ? tensor.tensor->name : "<null>") +
"': expected " + std::to_string(tensor.shape.num_elements()) +
", got " + std::to_string(count));
}
ggml_backend_tensor_set(tensor.tensor, values, 0, count * sizeof(float));
}
void write_tensor_f32_slice(const TensorValue & tensor, size_t element_offset, const float * values, size_t count) {
if (tensor.type != GGML_TYPE_F32) {
throw std::runtime_error("write_tensor_f32_slice requires GGML_TYPE_F32 tensor");
}
const size_t total = static_cast<size_t>(tensor.shape.num_elements());
if (element_offset > total || count > total - element_offset) {
throw std::runtime_error("write_tensor_f32_slice range exceeds tensor shape");
}
ggml_backend_tensor_set(
tensor.tensor,
values,
element_offset * sizeof(float),
count * sizeof(float));
}
void write_tensor_f32(const TensorValue & tensor, const std::vector<float> & values) {
write_tensor_f32(tensor, values.data(), values.size());
}
void write_tensor_f16(const TensorValue & tensor, const float * values, size_t count) {
if (tensor.type != GGML_TYPE_F16) {
throw std::runtime_error("write_tensor_f16 requires GGML_TYPE_F16 tensor");
}
if (tensor.shape.num_elements() != static_cast<int64_t>(count)) {
throw std::runtime_error(
"write_tensor_f16 value count does not match tensor shape for tensor '" +
std::string(tensor.tensor != nullptr ? tensor.tensor->name : "<null>") +
"': expected " + std::to_string(tensor.shape.num_elements()) +
", got " + std::to_string(count));
}
std::vector<ggml_fp16_t> fp16_values(count);
ggml_fp32_to_fp16_row(values, fp16_values.data(), static_cast<int64_t>(count));
ggml_backend_tensor_set(tensor.tensor, fp16_values.data(), 0, count * sizeof(ggml_fp16_t));
}
void write_tensor_f16(const TensorValue & tensor, const std::vector<float> & values) {
write_tensor_f16(tensor, values.data(), values.size());
}
void write_tensor_bf16(const TensorValue & tensor, const float * values, size_t count) {
if (tensor.type != GGML_TYPE_BF16) {
throw std::runtime_error("write_tensor_bf16 requires GGML_TYPE_BF16 tensor");
}
if (tensor.shape.num_elements() != static_cast<int64_t>(count)) {
throw std::runtime_error(
"write_tensor_bf16 value count does not match tensor shape for tensor '" +
std::string(tensor.tensor != nullptr ? tensor.tensor->name : "<null>") +
"': expected " + std::to_string(tensor.shape.num_elements()) +
", got " + std::to_string(count));
}
std::vector<ggml_bf16_t> bf16_values(count);
ggml_fp32_to_bf16_row(values, bf16_values.data(), static_cast<int64_t>(count));
ggml_backend_tensor_set(tensor.tensor, bf16_values.data(), 0, count * sizeof(ggml_bf16_t));
}
void write_tensor_bf16(const TensorValue & tensor, const std::vector<float> & values) {
write_tensor_bf16(tensor, values.data(), values.size());
}
void write_tensor_float(const TensorValue & tensor, const float * values, size_t count) {
switch (tensor.type) {
case GGML_TYPE_F32:
write_tensor_f32(tensor, values, count);
return;
case GGML_TYPE_F16:
write_tensor_f16(tensor, values, count);
return;
case GGML_TYPE_BF16:
write_tensor_bf16(tensor, values, count);
return;
default:
throw std::runtime_error("write_tensor_float supports only f32/f16/bf16 tensors");
}
}
void write_tensor_float(const TensorValue & tensor, const std::vector<float> & values) {
write_tensor_float(tensor, values.data(), values.size());
}
void write_tensor_bytes(const TensorValue & tensor, const std::vector<std::byte> & bytes) {
if (tensor.tensor == nullptr) {
throw std::runtime_error("write_tensor_bytes requires non-null tensor");
}
const size_t expected = static_cast<size_t>(ggml_nbytes(tensor.tensor));
if (bytes.size() != expected) {
throw std::runtime_error(
"write_tensor_bytes byte count does not match tensor '" +
std::string(tensor.tensor->name) +
"': expected " + std::to_string(expected) +
", got " + std::to_string(bytes.size()));
}
ggml_backend_tensor_set(tensor.tensor, bytes.data(), 0, bytes.size());
}
void round_f32_to_bf16_in_place(float * values, size_t count) {
if (count == 0) {
return;
}
std::vector<ggml_bf16_t> bf16_values(count);
ggml_fp32_to_bf16_row(values, bf16_values.data(), static_cast<int64_t>(count));
ggml_bf16_to_fp32_row(bf16_values.data(), values, static_cast<int64_t>(count));
}
void round_f32_to_bf16_in_place(std::vector<float> & values) {
round_f32_to_bf16_in_place(values.data(), values.size());
}
void write_tensor_i32(const TensorValue & tensor, const int32_t * values, size_t count) {
if (tensor.type != GGML_TYPE_I32) {
throw std::runtime_error("write_tensor_i32 requires GGML_TYPE_I32 tensor");
}
if (tensor.shape.num_elements() != static_cast<int64_t>(count)) {
throw std::runtime_error("write_tensor_i32 value count does not match tensor shape");
}
ggml_backend_tensor_set(tensor.tensor, values, 0, count * sizeof(int32_t));
}
void write_tensor_i32(const TensorValue & tensor, const std::vector<int32_t> & values) {
write_tensor_i32(tensor, values.data(), values.size());
}
template <typename T>
void read_tensor_typed_into(const ggml_tensor * tensor, ggml_type expected_type, std::vector<T> & values) {
if (tensor->type != expected_type) {
throw std::runtime_error("read_tensor_typed type mismatch");
}
const size_t element_count = static_cast<size_t>(ggml_nelements(tensor));
values.resize(element_count);
const size_t byte_count = static_cast<size_t>(ggml_nbytes(tensor));
if (ggml_is_contiguous(tensor) && tensor->nb[0] == sizeof(T)) {
ggml_backend_tensor_get(tensor, values.data(), 0, byte_count);
return;
}
std::vector<uint8_t> raw(byte_count);
ggml_backend_tensor_get(tensor, raw.data(), 0, raw.size());
size_t out_index = 0;
const char * base = reinterpret_cast<const char *>(raw.data());
for (int64_t i3 = 0; i3 < tensor->ne[3]; ++i3) {
for (int64_t i2 = 0; i2 < tensor->ne[2]; ++i2) {
for (int64_t i1 = 0; i1 < tensor->ne[1]; ++i1) {
for (int64_t i0 = 0; i0 < tensor->ne[0]; ++i0) {
const char * ptr = base
+ i0 * tensor->nb[0]
+ i1 * tensor->nb[1]
+ i2 * tensor->nb[2]
+ i3 * tensor->nb[3];
std::memcpy(&values[out_index++], ptr, sizeof(T));
}
}
}
}
}
template <typename T>
std::vector<T> read_tensor_typed(const ggml_tensor * tensor, ggml_type expected_type) {
std::vector<T> values;
read_tensor_typed_into(tensor, expected_type, values);
return values;
}
void read_tensor_f32_into(const ggml_tensor * tensor, std::vector<float> & values) {
read_tensor_typed_into<float>(tensor, GGML_TYPE_F32, values);
}
std::vector<float> read_tensor_f32(const ggml_tensor * tensor) {
return read_tensor_typed<float>(tensor, GGML_TYPE_F32);
}
void read_tensor_f16_into(const ggml_tensor * tensor, std::vector<float> & values) {
const auto fp16_values = read_tensor_typed<ggml_fp16_t>(tensor, GGML_TYPE_F16);
values.resize(fp16_values.size());
ggml_fp16_to_fp32_row(fp16_values.data(), values.data(), static_cast<int64_t>(values.size()));
}
std::vector<float> read_tensor_f16(const ggml_tensor * tensor) {
std::vector<float> values;
read_tensor_f16_into(tensor, values);
return values;
}
void read_tensor_bf16_into(const ggml_tensor * tensor, std::vector<float> & values) {
const auto bf16_values = read_tensor_typed<ggml_bf16_t>(tensor, GGML_TYPE_BF16);
values.resize(bf16_values.size());
ggml_bf16_to_fp32_row(bf16_values.data(), values.data(), static_cast<int64_t>(values.size()));
}
std::vector<float> read_tensor_bf16(const ggml_tensor * tensor) {
std::vector<float> values;
read_tensor_bf16_into(tensor, values);
return values;
}
void read_tensor_float_into(const ggml_tensor * tensor, std::vector<float> & values) {
switch (tensor->type) {
case GGML_TYPE_F32:
read_tensor_f32_into(tensor, values);
return;
case GGML_TYPE_F16:
read_tensor_f16_into(tensor, values);
return;
case GGML_TYPE_BF16:
read_tensor_bf16_into(tensor, values);
return;
default:
throw std::runtime_error("read_tensor_float supports only f32/f16/bf16 tensors");
}
}
std::vector<float> read_tensor_float(const ggml_tensor * tensor) {
std::vector<float> values;
read_tensor_float_into(tensor, values);
return values;
}
void read_tensor_bytes_into(const ggml_tensor * tensor, std::vector<std::byte> & bytes) {
if (tensor == nullptr) {
throw std::runtime_error("read_tensor_bytes requires non-null tensor");
}
const size_t byte_count = static_cast<size_t>(ggml_nbytes(tensor));
bytes.resize(byte_count);
ggml_backend_tensor_get(tensor, bytes.data(), 0, byte_count);
}
std::vector<std::byte> read_tensor_bytes(const ggml_tensor * tensor) {
std::vector<std::byte> bytes;
read_tensor_bytes_into(tensor, bytes);
return bytes;
}
void read_tensor_i32_into(const ggml_tensor * tensor, std::vector<int32_t> & values) {
read_tensor_typed_into<int32_t>(tensor, GGML_TYPE_I32, values);
}
std::vector<int32_t> read_tensor_i32(const ggml_tensor * tensor) {
return read_tensor_typed<int32_t>(tensor, GGML_TYPE_I32);
}
} // namespace engine::core