sipp-sys 0.1.4

Native llama.cpp FFI layer for Sipp
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
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wunused-function"
#pragma clang diagnostic ignored "-Wunused-but-set-variable"

#include <HAP_farf.h>
#include <HAP_perf.h>
#include <qurt_memory.h>

#include <math.h>
#include <string.h>

#define GGML_COMMON_DECL_C
#include "ggml-common.h"
#include "htp-ctx.h"
#include "htp-ops.h"
#include "htp-ops.h"
#include "hvx-utils.h"
#include "htp-tensor.h"
#include "htp-fence.h"

struct htp_copy_context {
    struct htp_ops_context * octx;

    uint32_t          src0_type_size;
    uint32_t          src0_block_size;

    uint32_t          dst_type_size;
    uint32_t          dst_block_size;

    uint32_t          src0_blocks_per_row;
    uint32_t          dst_blocks_per_row;

    uint32_t          elem_start;
    uint32_t          nelem;
    uint32_t          elem_per_thread;

    uint32_t          src0_nrows_per_thread;
    uint32_t          row_start;
    uint32_t          nrows;

    struct fastdiv_values div_ne01;
    struct fastdiv_values div_ne02_ne01;

    struct fastdiv_values div_ne0;
    struct fastdiv_values div_ne1_ne0;
    struct fastdiv_values div_ne2_ne1_ne0;
    struct fastdiv_values div_ne00;
    struct fastdiv_values div_ne01_ne00;
    struct fastdiv_values div_ne02_ne01_ne00;
};

#define cpy_preamble                              \
    const struct htp_tensor *src0 = octx->src[0]; \
    const struct htp_tensor *dst  = octx->dst;    \
                                                  \
    const uint32_t ne00 = src0->ne[0];            \
    const uint32_t ne01 = src0->ne[1];            \
    const uint32_t ne02 = src0->ne[2];            \
    const uint32_t ne03 = src0->ne[3];            \
                                                  \
    const uint32_t nb00 = src0->nb[0];            \
    const uint32_t nb01 = src0->nb[1];            \
    const uint32_t nb02 = src0->nb[2];            \
    const uint32_t nb03 = src0->nb[3];            \
                                                  \
    const uint32_t  ne0 = dst->ne[0];             \
    const uint32_t  ne1 = dst->ne[1];             \
    const uint32_t  ne2 = dst->ne[2];             \
    const uint32_t  ne3 = dst->ne[3];             \
                                                  \
    const uint32_t  nb0 = dst->nb[0];             \
    const uint32_t  nb1 = dst->nb[1];             \
    const uint32_t  nb2 = dst->nb[2];             \
    const uint32_t  nb3 = dst->nb[3];

#define DEFINE_CPY_SAMESHAPE(NAME, ELEM_TYPE, ELEM_SIZE)                                       \
static void cpy_thread_##NAME##_sameshape(unsigned int nth, unsigned int ith, void * data) {   \
    struct htp_copy_context * ct = (struct htp_copy_context *) data;                           \
    struct htp_ops_context * octx = ct->octx;                                                  \
    cpy_preamble;                                                                              \
    const uint32_t dr  = ct->src0_nrows_per_thread;                                            \
    const uint32_t ir0 = ct->row_start + dr * ith;                                             \
    const uint32_t ir1 = MIN(ir0 + dr, ct->row_start + ct->nrows);                             \
    if (ir0 >= ir1) return;                                                                    \
    const bool contiguous = (nb01 == ne00 * ELEM_SIZE) && (nb1 == nb01) &&                     \
                            (nb02 == ne01 * nb01)      && (nb2 == nb02) &&                     \
                            (nb03 == ne02 * nb02)      && (nb3 == nb03);                       \
    const uint32_t ne02_ne01 = ne02 * ne01;                                                    \
    uint32_t i03 = fastdiv(ir0, &ct->div_ne02_ne01);                                           \
    uint32_t rem = ir0 - i03 * ne02_ne01;                                                      \
    uint32_t i02 = fastdiv(rem, &ct->div_ne01);                                                \
    uint32_t i01 = rem - i02 * ne01;                                                           \
    uint8_t * dst_ptr  = (uint8_t *) dst->data  + i01*nb1  + i02*nb2  + i03*nb3;               \
    uint8_t * src0_ptr = (uint8_t *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;              \
    if (contiguous) {                                                                          \
        hvx_copy_uu(dst_ptr, src0_ptr, (ir1 - ir0) * ne00, ELEM_SIZE);                         \
        return;                                                                                \
    }                                                                                          \
    for (uint32_t r = ir0; r < ir1; r++) {                                                     \
        hex_l2fetch(src0_ptr, ne00 * ELEM_SIZE, nb01, 2);                                      \
        hvx_copy_uu(dst_ptr, src0_ptr, ne00, ELEM_SIZE);                                       \
        dst_ptr  += nb1;                                                                       \
        src0_ptr += nb01;                                                                      \
        if (++i01 == ne01) {                                                                   \
            i01 = 0;                                                                           \
            if (++i02 == ne02) {                                                               \
                i02 = 0;                                                                       \
                i03++;                                                                         \
            }                                                                                  \
            dst_ptr  = (uint8_t *) dst->data  + i02*nb2  + i03*nb3;                            \
            src0_ptr = (uint8_t *) src0->data + i02*nb02 + i03*nb03;                           \
        }                                                                                      \
    }                                                                                          \
}

DEFINE_CPY_SAMESHAPE(f32,  float, 4)
DEFINE_CPY_SAMESHAPE(f16, __fp16, 2)

#define DEFINE_CPY_RESHAPE(NAME, ELEM_TYPE, ELEM_SIZE)                                               \
static void cpy_thread_##NAME##_reshape(unsigned int nth, unsigned int ith, void * data) {           \
    struct htp_copy_context * ct = (struct htp_copy_context *) data;                                 \
    struct htp_ops_context * octx = ct->octx;                                                        \
    cpy_preamble;                                                                                    \
    const uint32_t th_nelem = ct->elem_per_thread;                                                   \
    const uint32_t th_start = ct->elem_start + ith * th_nelem;                                       \
    const uint32_t th_end   = MIN(th_start + th_nelem, ct->elem_start + ct->nelem);                  \
    if (th_start >= th_end) return;                                                                  \
                                                                                                     \
    if (htp_tensor_is_contiguous(src0, ELEM_SIZE) && htp_tensor_is_contiguous(dst, ELEM_SIZE)) {     \
        hvx_copy_uu((uint8_t *) dst->data + (size_t) th_start * ELEM_SIZE,                           \
                    (const uint8_t *) src0->data + (size_t) th_start * ELEM_SIZE,                    \
                    th_end - th_start, ELEM_SIZE);                                                   \
        return;                                                                                      \
    }                                                                                                \
                                                                                                     \
    const uint32_t ne01_ne00      = ne01 * ne00;                                                     \
    const uint32_t ne02_ne01_ne00 = ne02 * ne01_ne00;                                                \
    const uint32_t ne1_ne0        = ne1 * ne0;                                                       \
    const uint32_t ne2_ne1_ne0    = ne2 * ne1_ne0;                                                   \
                                                                                                     \
    uint32_t e = th_start;                                                                           \
    uint32_t i13 = fastdiv(e, &ct->div_ne2_ne1_ne0);                                                 \
    uint32_t rem = e - i13 * ne2_ne1_ne0;                                                            \
    uint32_t i12 = fastdiv(rem, &ct->div_ne1_ne0);                                                   \
    uint32_t rem2 = rem - i12 * ne1_ne0;                                                             \
    uint32_t i11 = fastdiv(rem2, &ct->div_ne0);                                                      \
    uint32_t i10 = rem2 - i11 * ne0;                                                                 \
                                                                                                     \
    uint32_t i03 = fastdiv(e, &ct->div_ne02_ne01_ne00);                                              \
    uint32_t rem_s = e - i03 * ne02_ne01_ne00;                                                       \
    uint32_t i02 = fastdiv(rem_s, &ct->div_ne01_ne00);                                               \
    uint32_t rem2_s = rem_s - i02 * ne01_ne00;                                                       \
    uint32_t i01 = fastdiv(rem2_s, &ct->div_ne00);                                                   \
    uint32_t i00 = rem2_s - i01 * ne00;                                                              \
                                                                                                     \
    char * dst_ptr        = (char *)       dst->data  + i10*nb0  + i11*nb1  + i12*nb2  + i13*nb3;    \
    const char * src0_ptr = (const char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03;   \
                                                                                                     \
    const bool rows_contig = (nb00 == ELEM_SIZE) && (nb0 == ELEM_SIZE);                              \
                                                                                                     \
    while (e < th_end) {                                                                             \
        uint32_t run = 1;                                                                            \
        if (rows_contig) {                                                                           \
            run = MIN(MIN(ne00 - i00, ne0 - i10), th_end - e);                                       \
            hvx_copy_uu((uint8_t *) dst_ptr, (const uint8_t *) src0_ptr, run, ELEM_SIZE);            \
        } else {                                                                                     \
            *((ELEM_TYPE *) dst_ptr) = *((const ELEM_TYPE *) src0_ptr);                              \
        }                                                                                            \
        e += run;                                                                                    \
                                                                                                     \
        dst_ptr += run * nb0;                                                                        \
        i10     += run;                                                                              \
        if (i10 == ne0) {                                                                            \
            i10 = 0;                                                                                 \
            if (++i11 == ne1) {                                                                      \
                i11 = 0;                                                                             \
                if (++i12 == ne2) {                                                                  \
                    i12 = 0;                                                                         \
                    i13++;                                                                           \
                }                                                                                    \
            }                                                                                        \
            dst_ptr = (char *) dst->data + i11*nb1 + i12*nb2 + i13*nb3;                              \
        }                                                                                            \
                                                                                                     \
        src0_ptr += run * nb00;                                                                      \
        i00      += run;                                                                             \
        if (i00 == ne00) {                                                                           \
            i00 = 0;                                                                                 \
            if (++i01 == ne01) {                                                                     \
                i01 = 0;                                                                             \
                if (++i02 == ne02) {                                                                 \
                    i02 = 0;                                                                         \
                    i03++;                                                                           \
                }                                                                                    \
            }                                                                                        \
            src0_ptr = (const char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;                   \
        }                                                                                            \
    }                                                                                                \
}

DEFINE_CPY_RESHAPE(f32,  float, 4)
DEFINE_CPY_RESHAPE(f16, __fp16, 2)

static void cpy_thread_f16_f32_sameshape(unsigned int nth, unsigned int ith, void * data) {
    struct htp_copy_context * ct = (struct htp_copy_context *) data;
    struct htp_ops_context * octx = ct->octx;
    cpy_preamble;

    const uint32_t dr  = ct->src0_nrows_per_thread;
    const uint32_t ir0 = ct->row_start + dr * ith;
    const uint32_t ir1 = MIN(ir0 + dr, ct->row_start + ct->nrows);
    if (ir0 >= ir1) return;

    const uint32_t ne02_ne01 = ne02 * ne01;
    uint32_t i03 = fastdiv(ir0, &ct->div_ne02_ne01);
    uint32_t rem = ir0 - i03 * ne02_ne01;
    uint32_t i02 = fastdiv(rem, &ct->div_ne01);
    uint32_t i01 = rem - i02 * ne01;

    uint8_t* dst_ptr  = (uint8_t*) dst->data  + i01*nb1  + i02*nb2  + i03*nb3;
    uint8_t* src0_ptr = (uint8_t*) src0->data + i01*nb01 + i02*nb02 + i03*nb03;

    for (uint32_t r = ir0; r < ir1; r++) {
        hex_l2fetch(src0_ptr, ne00 * sizeof(float), nb01, 2);
        hvx_copy_f16_f32_uu(dst_ptr, src0_ptr, ne00);
        dst_ptr  += nb1;
        src0_ptr += nb01;
        if (++i01 == ne01) {
            i01 = 0;
            if (++i02 == ne02) {
                i02 = 0;
                i03++;
            }
            dst_ptr  = (uint8_t*) dst->data  + i02*nb2  + i03*nb3;
            src0_ptr = (uint8_t*) src0->data + i02*nb02 + i03*nb03;
        }
    }
}

static void cpy_thread_f32_f16_sameshape(unsigned int nth, unsigned int ith, void * data) {
    struct htp_copy_context * ct = (struct htp_copy_context *) data;
    struct htp_ops_context * octx = ct->octx;
    cpy_preamble;

    const uint32_t dr  = ct->src0_nrows_per_thread;
    const uint32_t ir0 = ct->row_start + dr * ith;
    const uint32_t ir1 = MIN(ir0 + dr, ct->row_start + ct->nrows);
    if (ir0 >= ir1) return;

    const uint32_t ne02_ne01 = ne02 * ne01;
    uint32_t i03 = fastdiv(ir0, &ct->div_ne02_ne01);
    uint32_t rem = ir0 - i03 * ne02_ne01;
    uint32_t i02 = fastdiv(rem, &ct->div_ne01);
    uint32_t i01 = rem - i02 * ne01;

    uint8_t* dst_ptr  = (uint8_t*) dst->data  + i01*nb1  + i02*nb2  + i03*nb3;
    uint8_t* src0_ptr = (uint8_t*) src0->data + i01*nb01 + i02*nb02 + i03*nb03;

    for (uint32_t r = ir0; r < ir1; r++) {
        hex_l2fetch(src0_ptr, ne00 * sizeof(__fp16), nb01, 2);
        hvx_copy_f32_f16_uu(dst_ptr, src0_ptr, ne00);
        dst_ptr  += nb1;
        src0_ptr += nb01;
        if (++i01 == ne01) {
            i01 = 0;
            if (++i02 == ne02) {
                i02 = 0;
                i03++;
            }
            dst_ptr  = (uint8_t*) dst->data  + i02*nb2  + i03*nb3;
            src0_ptr = (uint8_t*) src0->data + i02*nb02 + i03*nb03;
        }
    }
}

static inline void cpy_dma_sametype_sameshape(
    struct htp_ops_context * octx,
    const struct htp_tensor * dst,
    const struct htp_tensor * src0,
    uint32_t elem_size,
    uint32_t ne00, uint32_t ne01, uint32_t ne02, uint32_t ne03,
    uint32_t nb01, uint32_t nb02, uint32_t nb03,
    uint32_t  nb1, uint32_t  nb2, uint32_t nb3
) {
    const bool contiguous_outer =
        (ne02 == 1 || (nb02 == ne01 * nb01 && nb2 == ne01 * nb1)) &&
        (ne03 == 1 || (nb03 == ne02 * nb02 && nb3 == ne02 * nb2));

    dma_queue * q = octx->ctx->dma[0];

    if (contiguous_outer) {
        if (!dma_queue_push(q, dma_make_ptr((void *) dst->data, (const void *) src0->data), nb1, nb01, ne00 * elem_size, ne01 * ne02 * ne03)) {
            dma_queue_flush(q);
            dma_queue_push(q, dma_make_ptr((void *) dst->data, (const void *) src0->data), nb1, nb01, ne00 * elem_size, ne01 * ne02 * ne03);
        }
        dma_queue_flush(q);
        return;
    }

    for (uint32_t i03 = 0; i03 < ne03; i03++) {
        for (uint32_t i02 = 0; i02 < ne02; i02++) {
            uint8_t * dst_ptr  = (uint8_t *) dst->data  + i02 * nb2  + i03 * nb3;
            uint8_t * src0_ptr = (uint8_t *) src0->data + i02 * nb02 + i03 * nb03;

            if (!dma_queue_push(q, dma_make_ptr(dst_ptr, src0_ptr), nb1, nb01, ne00 * elem_size, ne01)) {
                dma_queue_flush(q);
                dma_queue_push(q, dma_make_ptr(dst_ptr, src0_ptr), nb1, nb01, ne00 * elem_size, ne01);
            }
        }
    }

    dma_queue_flush(q);
}

static inline void cpy_dma_sametype_reshape_contig(
    struct htp_ops_context * octx,
    const struct htp_tensor * dst,
    const struct htp_tensor * src0,
    uint32_t total_bytes
) {
    dma_queue * q = octx->ctx->dma[0];
    dma_queue_push(q, dma_make_ptr((void *) dst->data, (const void *) src0->data),
                   total_bytes, total_bytes, total_bytes, /*nrows=*/ 1);
    dma_queue_pop(q);
}

static int exec_cpy(struct htp_ops_context * octx, bool * use_dma) {
    cpy_preamble;
    *use_dma = false;

    struct htp_copy_context ct;
    ct.octx = octx;

    switch (src0->type) {
    case HTP_TYPE_F32: ct.src0_type_size = 4; ct.src0_block_size = 1; ct.src0_blocks_per_row = ne00 / 1; break;
    case HTP_TYPE_F16: ct.src0_type_size = 2; ct.src0_block_size = 1; ct.src0_blocks_per_row = ne00 / 1; break;
    default:
        return HTP_STATUS_NO_SUPPORT;
    }

    switch (dst->type) {
    case HTP_TYPE_F32: ct.dst_type_size = 4; ct.dst_block_size = 1; ct.dst_blocks_per_row = ne0 / 1; break;
    case HTP_TYPE_F16: ct.dst_type_size = 2; ct.dst_block_size = 1; ct.dst_blocks_per_row = ne0 / 1; break;
    default:
        return HTP_STATUS_NO_SUPPORT;
    }

    if (octx->flags & HTP_OPFLAGS_SKIP_COMPUTE) {
        return HTP_STATUS_OK;
    }

    const bool sametype   = (src0->type == dst->type);
    const bool transposed = (nb00 > nb01) || (nb0 > nb1) ||
                            (nb00 != ct.src0_type_size) || (nb0 != ct.dst_type_size) ||
                            (nb01 < ne00 * ct.src0_type_size) || (nb1 < ne0 * ct.dst_type_size);
    const bool sameshape  = !transposed && (ne00 == ne0 && ne01 == ne1 && ne02 == ne2 && ne03 == ne3);

    const uint32_t n_threads = octx->n_threads;

    const bool src_is_contiguous = htp_tensor_is_contiguous(src0, ct.src0_type_size);
    const bool dst_is_contiguous = htp_tensor_is_contiguous(dst, ct.dst_type_size);

    if (sameshape) {
        const uint32_t total_rows = ne01 * ne02 * ne03;
        const uint32_t row_size   = ne00 * ct.dst_type_size;

        ct.div_ne01      = init_fastdiv_values(ne01);
        ct.div_ne02_ne01 = init_fastdiv_values(ne02 * ne01);

        uint32_t row_start = 0;
        uint32_t nrows     = total_rows;

        if (octx->ctx->mdev.count > 1) {
            const uint32_t rows_per_chunk = (row_size > 0) ? (HEX_L2_LINE_SIZE / hex_gcd_u32(row_size, HEX_L2_LINE_SIZE)) : 1;
            const bool can_split = htp_tensor_mdev_data_aligned(dst) && dst_is_contiguous;
            const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_rows, can_split ? rows_per_chunk : 0, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
            row_start = range.start;
            nrows     = range.count;
        }

        if (nrows == 0) {
            return HTP_STATUS_OK;
        }

        ct.row_start = row_start;
        ct.nrows     = nrows;
        ct.src0_nrows_per_thread = fastdiv(nrows + n_threads - 1, &octx->n_threads_div);

        if (sametype && octx->ctx->mdev.count <= 1) {
            *use_dma = true;
            cpy_dma_sametype_sameshape(octx, dst, src0, ct.src0_type_size, ne00, ne01, ne02, ne03, nb01, nb02, nb03, nb1, nb2, nb3);
        } else {
            work_queue_func_t copy_fun = NULL;
            if (sametype) {
                copy_fun = (src0->type == HTP_TYPE_F32) ? cpy_thread_f32_sameshape : cpy_thread_f16_sameshape;
            } else if (dst->type == HTP_TYPE_F16 && src0->type == HTP_TYPE_F32) {
                copy_fun = cpy_thread_f16_f32_sameshape;
            } else if (dst->type == HTP_TYPE_F32 && src0->type == HTP_TYPE_F16) {
                copy_fun = cpy_thread_f32_f16_sameshape;
            } else {
                return HTP_STATUS_NO_SUPPORT;
            }
            work_queue_run(octx->ctx->work_queue, copy_fun, &ct, n_threads);
        }
    } else if (sametype) {
        const uint32_t total_elems = ne0 * ne1 * ne2 * ne3;
        const uint32_t elems_per_line = (ct.dst_type_size == 4) ? 32 : 64;

        if (octx->ctx->mdev.count <= 1 && dst_is_contiguous && src_is_contiguous) {
            *use_dma = true;
            cpy_dma_sametype_reshape_contig(octx, dst, src0, total_elems * ct.dst_type_size);
            return HTP_STATUS_OK;
        }

        ct.div_ne0            = init_fastdiv_values(ne0);
        ct.div_ne1_ne0        = init_fastdiv_values(ne1 * ne0);
        ct.div_ne2_ne1_ne0    = init_fastdiv_values(ne2 * ne1 * ne0);
        ct.div_ne00           = init_fastdiv_values(ne00);
        ct.div_ne01_ne00      = init_fastdiv_values(ne01 * ne00);
        ct.div_ne02_ne01_ne00 = init_fastdiv_values(ne02 * ne01 * ne00);

        uint32_t elem_start = 0;
        uint32_t nelem      = total_elems;

        if (octx->ctx->mdev.count > 1) {
            const bool can_split = htp_tensor_mdev_data_aligned(dst) && dst_is_contiguous;
            const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_elems, can_split ? elems_per_line : 0, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
            elem_start = range.start;
            nelem      = range.count;
        }

        if (nelem == 0) {
            return HTP_STATUS_OK;
        }

        ct.elem_start      = elem_start;
        ct.nelem           = nelem;
        ct.elem_per_thread = fastdiv(nelem + n_threads - 1, &octx->n_threads_div);

        work_queue_func_t copy_fun = (src0->type == HTP_TYPE_F32) ? cpy_thread_f32_reshape : cpy_thread_f16_reshape;
        work_queue_run(octx->ctx->work_queue, copy_fun, &ct, n_threads);
    } else {
        return HTP_STATUS_NO_SUPPORT;
    }

    return HTP_STATUS_OK;
}

int op_cpy(struct htp_ops_context * octx) {
    bool use_dma = false;
    int status = exec_cpy(octx, &use_dma);

    htp_ops_context_set_status(octx, status);

    if (octx->op == HTP_OP_CPY_FENCE) {
        if (!use_dma) {
            htp_flush_dirty_ranges(octx->ctx);
        }

        htp_mdev_group_barrier(octx);

        if (octx->ctx->mdev.idx == 0) {
            const struct htp_tensor * sync = octx->src[1];
            const uint32_t seq = (uint32_t) octx->op_params[0];
            atomic_uint * sync_fence = (atomic_uint *) (uintptr_t) sync->data;
            htp_fence_write(sync_fence, seq, octx->status);

            FARF(HIGH, "ggml-hex: sync-release : fence %p seq 0x%x status %d\n", sync_fence, seq, octx->status);
        }
    }

    return octx->status;
}