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 / gather-rows.h

189 lines · 5686 bytes Raw Blame History
1#ifndef HTP_GATHER_ROWS_H
2#define HTP_GATHER_ROWS_H
3
4#include "hex-common.h"
5#include "htp-vtcm.h"
6#include <stdint.h>
7#include <stdbool.h>
8
9#ifndef GGML_MAX_DIMS
10#define GGML_MAX_DIMS 4
11#endif
12
13struct htp_gather_rows_params {
14 uint32_t na;
15 uint32_t nb;
16 uint32_t ne;
17 uint32_t rg;
18 uint32_t vg;
19 uint32_t nr_max;
20 uint32_t v1;
21 uint32_t span1;
22 uint32_t region_size;
23 uint32_t slice;
24 uint32_t total_rows;
25 uint32_t rows_per_thread;
26 uint8_t b_dense;
27 uint8_t pad[3];
28};
29
30struct htp_gather_rows_vtcm_layout {
31 size_t off_tab;
32 size_t off_inc;
33 size_t off_cur;
34 size_t off_region0;
35 size_t off_out0;
36 size_t off_region1;
37 size_t off_out1;
38 size_t total_per_thread;
39};
40
41static inline void htp_gather_rows_vtcm_layout_build(
42 struct htp_gather_rows_vtcm_layout * L,
43 uint32_t vg,
44 uint32_t region_size,
45 uint32_t nr_max,
46 uint32_t ne,
47 uint32_t elem_size) {
48
49 const size_t vec_bytes = (size_t) vg * 128;
50 const size_t region_aligned = hex_round_up(region_size, 128);
51 const size_t out_aligned = hex_round_up(region_aligned + (size_t) nr_max * ne * elem_size, 128) - region_aligned;
52
53 size_t off = 0;
54 VTCM_LAYOUT_ALLOC(off, off_tab, vec_bytes);
55 VTCM_LAYOUT_ALLOC(off, off_inc, vec_bytes);
56 VTCM_LAYOUT_ALLOC(off, off_cur, vec_bytes);
57 VTCM_LAYOUT_ALLOC(off, off_region0, region_aligned);
58 VTCM_LAYOUT_ALLOC(off, off_out0, out_aligned);
59 VTCM_LAYOUT_ALLOC(off, off_region1, region_aligned);
60 VTCM_LAYOUT_ALLOC(off, off_out1, out_aligned);
61 L->total_per_thread = off;
62}
63
64static inline bool htp_gather_rows_solve_layout(
65 struct htp_gather_rows_params * kparams,
66 uint32_t na, uint32_t a_nb1,
67 uint32_t nb, uint32_t b_nb1, bool b_dense,
68 uint32_t elem_size,
69 uint32_t total_rows,
70 uint32_t n_threads,
71 uint32_t mdev_count,
72 uint32_t vtcm_size) {
73
74 const uint32_t max_ne0 = 128 / elem_size;
75 const uint32_t ne = na + nb;
76 if (ne == 0 || ne > max_ne0) {
77 return false;
78 }
79
80 if (elem_size != 4 && elem_size != 2) {
81 return false;
82 }
83
84 const uint32_t v_lanes = 128 / elem_size;
85 const uint32_t g = hex_gcd_u32(ne, v_lanes);
86 const uint32_t rg = v_lanes / g;
87 const uint32_t vg = ne / g;
88
89 const uint32_t slice = (vtcm_size / n_threads) & ~127u;
90 const uint32_t total_chunks = total_rows / rg;
91 const uint32_t chunks_per_dev = (total_chunks + mdev_count - 1) / mdev_count;
92 const uint32_t max_rows_per_dev = (mdev_count > 1 && total_chunks >= mdev_count)
93 ? (chunks_per_dev * rg + (total_rows % rg))
94 : total_rows;
95 const uint32_t rows_per_thread = hex_round_up((max_rows_per_dev + n_threads - 1) / n_threads, rg);
96
97 const uint32_t fixed = 3 * vg * 128;
98 const uint32_t per_row = a_nb1 + (nb > 0 ? (b_dense ? b_nb1 : nb * b_nb1) : 0) + ne * elem_size;
99 if (slice <= fixed + 2 * per_row * rg) {
100 return false;
101 }
102
103 const uint32_t avail_per_buf = (slice - fixed) / 2;
104 uint32_t nr_max = MIN(avail_per_buf / per_row, rows_per_thread);
105 if (elem_size == 2) {
106 const uint32_t per_row_region = a_nb1 + (nb > 0 ? (b_dense ? b_nb1 : nb * b_nb1) : 0);
107 if (per_row_region > 0) {
108 nr_max = MIN(nr_max, (32768u - 256u) / per_row_region);
109 }
110 }
111 nr_max = (nr_max / rg) * rg;
112
113 uint32_t v1 = 0;
114 uint32_t span1 = 0;
115 uint32_t region_size = 0;
116
117 while (nr_max > 0) {
118 v1 = hex_round_up((nr_max - 1) * a_nb1 + na * elem_size, 128);
119 if (nb > 0) {
120 if (b_dense) {
121 span1 = elem_size;
122 region_size = v1 + (nr_max - 1) * b_nb1 + nb * elem_size;
123 } else {
124 span1 = (nr_max - 1) * b_nb1 + elem_size;
125 region_size = v1 + nb * span1;
126 }
127 } else {
128 span1 = 0;
129 region_size = v1;
130 }
131
132 struct htp_gather_rows_vtcm_layout layout;
133 htp_gather_rows_vtcm_layout_build(&layout, vg, region_size, nr_max, ne, elem_size);
134
135 if ((elem_size != 2 || region_size <= 32768) && layout.total_per_thread <= slice) {
136 break;
137 }
138
139 nr_max -= rg;
140 }
141
142 if (nr_max == 0) {
143 return false;
144 }
145
146 kparams->na = na;
147 kparams->nb = nb;
148 kparams->ne = ne;
149 kparams->rg = rg;
150 kparams->vg = vg;
151 kparams->nr_max = nr_max;
152 kparams->v1 = v1;
153 kparams->span1 = span1;
154 kparams->region_size = region_size;
155 kparams->slice = slice;
156 kparams->total_rows = total_rows;
157 kparams->rows_per_thread = rows_per_thread;
158 kparams->b_dense = b_dense ? 1 : 0;
159 kparams->pad[0] = 0;
160 kparams->pad[1] = 0;
161 kparams->pad[2] = 0;
162 return true;
163}
164
165#ifdef __cplusplus
166struct ggml_tensor;
167static inline bool htp_gather_rows_tensor_outer_contiguous(const struct ggml_tensor * t) {
168 size_t next_nb = ((const size_t *) t->nb)[1] * ((const int64_t *) t->ne)[1];
169 for (int i = 2; i < GGML_MAX_DIMS; i++) {
170 if (((const int64_t *) t->ne)[i] != 1 && ((const size_t *) t->nb)[i] != next_nb) {
171 return false;
172 }
173 next_nb *= ((const int64_t *) t->ne)[i];
174 }
175 return true;
176}
177#else
178struct htp_ops_context;
179struct htp_tensor;
180
181int htp_gather_rows(
182 struct htp_ops_context * octx,
183 const struct htp_tensor * a,
184 const struct htp_tensor * b,
185 const struct htp_gather_rows_params * kparams,
186 uint32_t n_threads);
187#endif
188
189#endif // HTP_GATHER_ROWS_H