char-fork/llama.cpp

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

Kevin Pougetggml-virtgpu: improve the reliability of the code (#19846)ffaafde16

char/aliceai-t5-moe
3.1 KiB102 linesraw
1#include "backend-dispatched.h"
2#include "backend-virgl-apir.h"
3#include "ggml-backend-impl.h"
4#include "ggml-backend.h"
5#include "ggml-impl.h"
6#include "shared/apir_backend.h"
7
8#include <cstdint>
9
10static uint32_t validate_graph_operation(size_t cgraph_size, uint32_t shmem_res_id, const char * operation) {
11    if (cgraph_size == 0) {
12        GGML_LOG_ERROR(GGML_VIRTGPU_BCK "%s: Zero-size computation graph\n", operation);
13        return 1;
14    }
15
16    // place-holder: validate that the size of shmem_res_id is <= cgraph_size
17    // need to add another method in the Virgl->APIR callback interface
18    GGML_UNUSED(shmem_res_id);
19
20    return 0;  // Valid
21}
22
23uint32_t backend_backend_graph_compute(apir_encoder * enc, apir_decoder * dec, virgl_apir_context * ctx) {
24    GGML_UNUSED(ctx);
25
26    static bool async_backend_initialized = false;
27    static bool async_backend;
28
29    if (!async_backend_initialized) {
30        ggml_backend_dev_props props;
31
32        dev->iface.get_props(dev, &props);
33        async_backend             = props.caps.async;
34        async_backend_initialized = true;
35    }
36
37    uint32_t shmem_res_id;
38    apir_decode_virtgpu_shmem_res_id(dec, &shmem_res_id);
39
40    const void * shmem_data = ctx->iface->get_shmem_ptr(ctx->ctx_id, shmem_res_id);
41    if (!shmem_data) {
42        GGML_LOG_ERROR(GGML_VIRTGPU_BCK "%s: Couldn't get the shmem addr from virgl\n", __func__);
43        apir_decoder_set_fatal(dec);
44        return 1;
45    }
46    size_t cgraph_size;
47    apir_decode_size_t(dec, &cgraph_size);
48
49    if (validate_graph_operation(cgraph_size, shmem_res_id, __func__) != 0) {
50        apir_decoder_set_fatal(dec);
51        return 1;
52    }
53
54    apir_decoder secondary_dec = apir_new_decoder((const char *) shmem_data, cgraph_size);
55
56    ggml_cgraph * cgraph = apir_decode_ggml_cgraph(&secondary_dec, cgraph_size);
57
58    if (!cgraph || apir_decoder_get_fatal(&secondary_dec)) {
59        GGML_LOG_ERROR(GGML_VIRTGPU_BCK "%s: Failed to deserialize computation graph\n", __func__);
60        return 1;
61    }
62
63    if (cgraph->n_nodes < 0 || cgraph->n_leafs < 0) {
64        GGML_LOG_ERROR(GGML_VIRTGPU_BCK "%s: Invalid negative node/leaf count: nodes=%d leafs=%d\n", __func__,
65                       cgraph->n_nodes, cgraph->n_leafs);
66        return 1;
67    }
68
69    ggml_status status;
70#if APIR_BACKEND_CHECK_SUPPORTS_OP == 1
71    for (int idx = 0; idx < cgraph->n_nodes; idx++) {
72        ggml_tensor * op = ggml_graph_node(cgraph, idx);
73        if (dev->iface.supports_op(dev, op)) {
74            continue;
75        }
76        GGML_LOG_ERROR(GGML_VIRTGPU_BCK "%s: Graph node %d (%s) not supported by the backend\n", __func__, idx,
77                       ggml_op_desc(op));
78
79        status = GGML_STATUS_ABORTED;
80        apir_encode_ggml_status(enc, &status);
81
82        return 0;
83    }
84#endif
85
86    // Check if backend is properly initialized
87    if (!bck) {
88        GGML_LOG_ERROR(GGML_VIRTGPU_BCK "%s: Backend not initialized (bck is null)\n", __func__);
89
90        return 1;
91    }
92
93    status = bck->iface.graph_compute(bck, cgraph);
94
95    if (async_backend && bck->iface.synchronize) {
96        bck->iface.synchronize(bck);
97    }
98
99    apir_encode_ggml_status(enc, &status);
100
101    return 0;
102}