interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-hexagon / htp / cpy-ops.h

220 lines · 7397 bytes Raw Blame History
1#ifndef HTP_CPY_OPS_H
2#define HTP_CPY_OPS_H
3
4#include "hex-common.h"
5#include "hex-fastdiv.h"
6#include "htp-vtcm.h"
7#include "gather-rows.h"
8#include <stdint.h>
9
10enum htp_copy_kernel_type {
11 HTP_COPY_KERNEL_UNSUPPORTED = 0,
12 HTP_COPY_KERNEL_1D_CONTIG = 1,
13 HTP_COPY_KERNEL_SAMESHAPE_SAMETYPE = 2,
14 HTP_COPY_KERNEL_SAMESHAPE_CONVERT = 3,
15 HTP_COPY_KERNEL_RESHAPE = 4,
16 HTP_COPY_KERNEL_SCALAR = 5,
17 HTP_COPY_KERNEL_GATHER_ROWS = 6,
18 HTP_COPY_KERNEL_TRANSPOSE = 7,
19};
20
21struct htp_copy_convert_params {
22 uint32_t blk_rows; // rows moved per DMA descriptor
23 struct fastdiv_values div_ne01;
24 struct fastdiv_values div_ne02_ne01;
25};
26
27struct htp_copy_reshape_params {
28 struct fastdiv_values div_ne0;
29 struct fastdiv_values div_ne1_ne0;
30 struct fastdiv_values div_ne2_ne1_ne0;
31 struct fastdiv_values div_ne00;
32 struct fastdiv_values div_ne01_ne00;
33 struct fastdiv_values div_ne02_ne01_ne00;
34};
35
36// Batched 2D transpose of elem_size elements (dim 0 dense in dst, dim 1 dense in src)
37struct htp_copy_transpose_params {
38 uint32_t ne[3]; // dims 0..2
39 uint32_t src_nb[3]; // src strides for dims 0, 2, 3
40 uint32_t dst_nb[3]; // dst strides for dims 1, 2, 3
41 struct fastdiv_values div_nkb;
42 struct fastdiv_values div_n0b;
43 struct fastdiv_values div_ne2;
44 uint32_t blk0; // dim-0 elements per tile
45 uint32_t blkk; // dim-1 elements per tile
46 uint32_t n0b; // tiles along dim 0
47 uint32_t nkb; // tiles along dim 1
48 uint32_t in_stride; // VTCM row pitch of staged src tile
49 uint32_t out_stride; // VTCM row pitch of transposed tile
50 uint32_t in_buf_size;
51 uint32_t out_buf_size;
52 uint32_t total_tiles;
53 uint8_t elem_size;
54 uint8_t packed; // gather straight to dst order
55 uint8_t log2_m; // log2(gather lanes per element)
56 uint8_t log2_r; // log2(dim-0 elements per vector)
57};
58
59struct htp_copy_kernel_params {
60 uint8_t kernel_type;
61 uint8_t src0_type_size;
62 uint8_t dst_type_size;
63 uint8_t n_threads;
64
65 uint32_t total_elems;
66 uint32_t total_rows;
67 uint32_t vtcm_size;
68
69 union {
70 struct htp_copy_convert_params convert;
71 struct htp_copy_reshape_params reshape;
72 struct htp_gather_rows_params gather;
73 struct htp_copy_transpose_params transpose;
74 } u;
75};
76
77// Size of one VTCM staging buffer: big enough that a DMA descriptor moves many short
78// rows at once. The copy kernels keep four per thread (two in flight each way), about
79// 1 MB of VTCM with 8 threads.
80#define HTP_COPY_VTCM_BLK_BYTES (32 * 1024)
81
82struct htp_copy_convert_vtcm_layout {
83 size_t total_bytes;
84 size_t off_src0;
85 size_t off_dst;
86 uint32_t src0_row_stride;
87 uint32_t dst_row_stride;
88 uint32_t blk_rows;
89 uint32_t src0_buf_size;
90 uint32_t dst_buf_size;
91 uint32_t spad0_size_per_thread;
92 uint32_t spad1_size_per_thread;
93};
94
95static inline void htp_copy_convert_vtcm_layout_build(
96 struct htp_copy_convert_vtcm_layout * L,
97 uint32_t ne00,
98 uint32_t src_type_size,
99 uint32_t dst_type_size,
100 uint32_t blk_rows,
101 uint32_t n_threads) {
102
103 const uint32_t src0_row_stride = hex_round_up(ne00 * src_type_size, 256);
104 const uint32_t dst_row_stride = hex_round_up(ne00 * dst_type_size, 256);
105
106 L->src0_row_stride = src0_row_stride;
107 L->dst_row_stride = dst_row_stride;
108 L->blk_rows = blk_rows;
109 L->src0_buf_size = blk_rows * src0_row_stride;
110 L->dst_buf_size = blk_rows * dst_row_stride;
111 L->spad0_size_per_thread = 2 * L->src0_buf_size;
112 L->spad1_size_per_thread = 2 * L->dst_buf_size;
113
114 size_t off = 0;
115 VTCM_LAYOUT_ALLOC(off, off_src0, (size_t) n_threads * L->spad0_size_per_thread);
116 VTCM_LAYOUT_ALLOC(off, off_dst, (size_t) n_threads * L->spad1_size_per_thread);
117 L->total_bytes = off;
118}
119
120// ne01 bounds a block: rows of one block share the dim-1 stride.
121static inline void htp_copy_convert_solve_layout(
122 struct htp_copy_convert_vtcm_layout * layout,
123 uint32_t ne00,
124 uint32_t ne01,
125 uint32_t src_type_size,
126 uint32_t dst_type_size,
127 uint32_t n_threads) {
128
129 const uint32_t src0_row_stride = hex_round_up(ne00 * src_type_size, 256);
130 const uint32_t dst_row_stride = hex_round_up(ne00 * dst_type_size, 256);
131
132 const uint32_t max_row = src0_row_stride > dst_row_stride ? src0_row_stride : dst_row_stride;
133 uint32_t blk_rows = HTP_COPY_VTCM_BLK_BYTES / max_row;
134 if (blk_rows > ne01) {
135 blk_rows = ne01;
136 }
137 if (blk_rows == 0) {
138 blk_rows = 1;
139 }
140
141 htp_copy_convert_vtcm_layout_build(layout, ne00, src_type_size, dst_type_size, blk_rows, n_threads);
142}
143
144#define HTP_COPY_TRANSPOSE_BLK_BYTES (64 * 1024)
145
146static inline bool htp_copy_transpose_layout_build(struct htp_copy_transpose_params * tp, uint32_t blk_bytes) {
147 const uint32_t ne0 = tp->ne[0];
148 const uint32_t nek = tp->ne[1];
149 const uint32_t es = tp->elem_size;
150 const uint32_t epv = 128 / es;
151
152 uint32_t blk0;
153 uint32_t blkk;
154 if (tp->packed) {
155 // Packed: vector holds epv / ne0 whole dst rows
156 blk0 = ne0;
157 blkk = blk_bytes / (ne0 * es);
158 if (es == 2 && blkk > 16384 / ne0 - 64) {
159 blkk = 16384 / ne0 - 64;
160 }
161 if (blkk > nek) {
162 blkk = nek;
163 }
164 if (blkk == 0) {
165 return false;
166 }
167 } else if (nek <= epv) {
168 // Stage short contiguous dim and expand dim 0
169 blkk = nek;
170 blk0 = blk_bytes / (nek * es);
171 blk0 -= blk0 % epv;
172 if (blk0 > hex_round_up(ne0, epv)) {
173 blk0 = hex_round_up(ne0, epv);
174 }
175 if (blk0 < epv) {
176 return false;
177 }
178 } else {
179 blk0 = hex_round_up(ne0, epv);
180 if (blk0 > 256) {
181 blk0 = 256;
182 }
183 blkk = blk_bytes / (blk0 * es);
184 blkk -= blkk % epv;
185 // Cap f16 blkk to 256 for 16-bit gather offsets
186 const uint32_t max_blkk = (es == 2 ? 512 : 4096) / es;
187 if (blkk > max_blkk) {
188 blkk = max_blkk;
189 }
190 if (blkk > hex_round_up(nek, epv)) {
191 blkk = hex_round_up(nek, epv);
192 }
193 if (blkk < epv) {
194 return false;
195 }
196 }
197
198 tp->blk0 = blk0;
199 tp->blkk = blkk;
200 tp->n0b = (ne0 + blk0 - 1) / blk0;
201 tp->nkb = (nek + blkk - 1) / blkk;
202 // Pad row pitch by 1 element to avoid VTCM bank conflicts
203 const uint32_t pad = (es == 2) ? 2 : 4;
204 tp->in_stride = blkk * es + pad;
205 tp->out_stride = blk0 * es;
206 // Extra vector for packed gather overhang
207 tp->in_buf_size = hex_round_up(blk0 * tp->in_stride + (tp->packed ? 128 : 0), 128);
208 tp->out_buf_size = hex_round_up(blkk * tp->out_stride, 128);
209 tp->log2_m = es == 2 ? 0 : __builtin_ctz(es) - 2;
210 tp->log2_r = __builtin_ctz(tp->packed ? ne0 : epv);
211 return true;
212}
213
214#if defined(__cplusplus)
215static_assert(sizeof(struct htp_copy_kernel_params) <= 128, "htp_copy_kernel_params is too large for kernel_params blob");
216#else
217_Static_assert(sizeof(struct htp_copy_kernel_params) <= 128, "htp_copy_kernel_params is too large for kernel_params blob");
218#endif
219
220#endif // HTP_CPY_OPS_H