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 / htp-tensor.h

199 lines · 7285 bytes Raw Blame History
1#ifndef HTP_TENSOR_H
2#define HTP_TENSOR_H
3
4#include <stdint.h>
5#include <stdbool.h>
6#include "htp-ops.h"
7#include "hex-bitmap.h"
8#include "hex-common.h"
9#include "hex-fastdiv.h"
10
11#define HTP_TENSOR_MDEV_LINE_SIZE 128
12
13// Currently HMX requires 32x32 tile formats (do not change)
14#define HTP_TENSOR_TILE_N_COLS 32
15#define HTP_TENSOR_TILE_N_ROWS 32
16
17#define HTP_TENSOR_TILE_SIZE_F16 2048
18#define HTP_TENSOR_TILE_SIZE_Q4_0 576
19#define HTP_TENSOR_TILE_SIZE_Q4_1 640
20#define HTP_TENSOR_TILE_SIZE_Q8_0 1088
21#define HTP_TENSOR_TILE_SIZE_IQ4_NL 576
22#define HTP_TENSOR_TILE_SIZE_MXFP4 544
23#define HTP_TENSOR_TILE_SIZE_Q4_K 640
24#define HTP_TENSOR_TILE_SIZE_Q5_K 768
25#define HTP_TENSOR_TILE_SIZE_Q6_K 896
26#define HTP_TENSOR_TILE_SIZE_Q3_K 512
27#define HTP_TENSOR_TILE_SIZE_Q2_K 336
28#define HTP_TENSOR_TILE_K_Q1_0 128
29#define HTP_TENSOR_TILE_SIZE_Q1_0 576
30#define HTP_TENSOR_TILE_K_Q2_0 64
31#define HTP_TENSOR_TILE_SIZE_Q2_0 576
32
33// Tiled weight tensor layouts (Q4_0, Q8_0, IQ4_NL, MXFP4, Q1_0, Q2_0, K-quants):
34// See docs/backend/snapdragon/developer.md ("Tiled Weight Formats").
35
36struct htp_tensor_mdev_range {
37 uint32_t start;
38 uint32_t count;
39};
40
41static inline bool htp_tensor_is_extended(const struct htp_tensor * t) {
42 return t && (t->data >> 32) != 0;
43}
44
45static inline bool htp_tensor_is_contiguous(const struct htp_tensor * t, uint32_t type_size) {
46 uint32_t next_nb = type_size;
47 if (t->ne[0] != 1 && t->nb[0] != next_nb) {
48 return false;
49 }
50 next_nb *= t->ne[0];
51 for (int i = 1; i < HTP_OP_MAX_DIMS; i++) {
52 if (t->ne[i] != 1 && t->nb[i] != next_nb) {
53 return false;
54 }
55 next_nb *= t->ne[i];
56 }
57 return true;
58}
59
60static inline bool htp_tensor_is_permuted(const struct htp_tensor * t) {
61 return t->nb[0] > t->nb[1] || t->nb[1] > t->nb[2] || t->nb[2] > t->nb[3];
62}
63
64static inline bool htp_tensor_mdev_data_aligned(const struct htp_tensor * t) {
65 return ((uintptr_t) t->data & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0;
66}
67
68static inline bool htp_tensor_can_row_partition(const struct htp_tensor * t, uint32_t elem_size) {
69 if (!htp_tensor_mdev_data_aligned(t)) {
70 return false;
71 }
72 if (t->ne[0] != 1 && t->nb[0] != elem_size) {
73 return false;
74 }
75 if (htp_tensor_is_permuted(t)) {
76 return false;
77 }
78 if (t->ne[1] > 1 && (t->nb[1] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) != 0) return false;
79 if (t->ne[2] > 1 && (t->nb[2] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) != 0) return false;
80 if (t->ne[3] > 1 && (t->nb[3] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) != 0) return false;
81 return true;
82}
83
84static inline bool htp_tensor_mdev_rows_per_chunk(const struct htp_tensor * t, uint32_t elem_size, uint32_t row_size, uint32_t * rows_per_chunk) {
85 *rows_per_chunk = 0;
86
87 if (!htp_tensor_mdev_data_aligned(t)) {
88 return false;
89 }
90 if (t->ne[0] != 1 && t->nb[0] != elem_size) {
91 return false;
92 }
93 if (htp_tensor_is_permuted(t)) {
94 return false;
95 }
96 if (t->ne[1] > 1 && (t->nb[1] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0 &&
97 (t->ne[2] <= 1 || (t->nb[2] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0) &&
98 (t->ne[3] <= 1 || (t->nb[3] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0)) {
99 *rows_per_chunk = 1;
100 return true;
101 }
102 if (t->nb[1] == row_size &&
103 (t->ne[2] <= 1 || t->nb[2] == t->nb[1] * t->ne[1]) &&
104 (t->ne[3] <= 1 || t->nb[3] == t->nb[2] * t->ne[2])) {
105 *rows_per_chunk = (row_size > 0) ? (HTP_TENSOR_MDEV_LINE_SIZE / hex_gcd_u32(row_size, HTP_TENSOR_MDEV_LINE_SIZE)) : 1;
106 return true;
107 }
108 return false;
109}
110
111static inline struct htp_tensor_mdev_range htp_tensor_mdev_partition(uint32_t total_units, uint32_t units_per_chunk, uint32_t mdev_idx, uint32_t mdev_count, const struct fastdiv_values * mdev_count_div) {
112 struct htp_tensor_mdev_range range = { 0, total_units };
113
114 if (mdev_count <= 1) {
115 return range;
116 }
117
118 if (units_per_chunk == 0) {
119 range.start = (mdev_idx == 0) ? 0 : total_units;
120 range.count = (mdev_idx == 0) ? total_units : 0;
121 return range;
122 }
123
124 const uint32_t total_chunks = total_units / units_per_chunk;
125 if (total_chunks < mdev_count) {
126 range.start = (mdev_idx == 0) ? 0 : total_units;
127 range.count = (mdev_idx == 0) ? total_units : 0;
128 return range;
129 }
130
131 const uint32_t chunks_per_mdev = fastdiv(total_chunks + mdev_count - 1, mdev_count_div);
132 range.start = MIN(mdev_idx * chunks_per_mdev * units_per_chunk, total_units);
133 if (mdev_idx == mdev_count - 1) {
134 range.count = total_units - range.start;
135 } else {
136 range.count = MIN(chunks_per_mdev * units_per_chunk, total_units - range.start);
137 }
138 return range;
139}
140
141static inline uint32_t htp_tensor_get_tile_size(int type) {
142 switch (type) {
143 case HTP_TYPE_Q4_0: return HTP_TENSOR_TILE_SIZE_Q4_0;
144 case HTP_TYPE_IQ4_NL: return HTP_TENSOR_TILE_SIZE_IQ4_NL;
145 case HTP_TYPE_Q4_1: return HTP_TENSOR_TILE_SIZE_Q4_1;
146 case HTP_TYPE_Q4_K: return HTP_TENSOR_TILE_SIZE_Q4_K;
147 case HTP_TYPE_Q8_0: return HTP_TENSOR_TILE_SIZE_Q8_0;
148 case HTP_TYPE_Q5_K: return HTP_TENSOR_TILE_SIZE_Q5_K;
149 case HTP_TYPE_Q6_K: return HTP_TENSOR_TILE_SIZE_Q6_K;
150 case HTP_TYPE_Q3_K: return HTP_TENSOR_TILE_SIZE_Q3_K;
151 case HTP_TYPE_Q2_K: return HTP_TENSOR_TILE_SIZE_Q2_K;
152 case HTP_TYPE_Q1_0: return HTP_TENSOR_TILE_SIZE_Q1_0;
153 case HTP_TYPE_Q2_0: return HTP_TENSOR_TILE_SIZE_Q2_0;
154 case HTP_TYPE_MXFP4: return HTP_TENSOR_TILE_SIZE_MXFP4;
155 case HTP_TYPE_F16: return HTP_TENSOR_TILE_SIZE_F16;
156 default: return 0;
157 }
158}
159
160static inline uint32_t htp_tensor_get_tile_k(int type) {
161 switch (type) {
162 case HTP_TYPE_Q1_0: return HTP_TENSOR_TILE_K_Q1_0;
163 case HTP_TYPE_Q2_0: return HTP_TENSOR_TILE_K_Q2_0;
164 default: return 32;
165 }
166}
167
168static inline uint32_t htp_tensor_get_tile_padded_size(int type) {
169 return (htp_tensor_get_tile_size(type) + 127u) & ~127u;
170}
171
172static inline bool htp_tensor_is_tiled(const struct htp_tensor * t) {
173 return t && (t->flags & HTP_TENSOR_TILED) != 0;
174}
175
176static inline uint32_t htp_tensor_get_row_size(int type, uint32_t ne00) {
177 switch (type) {
178 case HTP_TYPE_F32: return ne00 * 4;
179 case HTP_TYPE_F16:
180 case HTP_TYPE_BF16: return ne00 * 2;
181 case HTP_TYPE_Q8_0: return (ne00 / 32) * 34;
182 case HTP_TYPE_Q4_0:
183 case HTP_TYPE_IQ4_NL: return (ne00 / 32) * 18;
184 case HTP_TYPE_Q4_1: return (ne00 / 32) * 20;
185 case HTP_TYPE_MXFP4: return (ne00 / 32) * 17;
186 case HTP_TYPE_Q2_K: return (ne00 / 256) * 84;
187 case HTP_TYPE_Q2_0: return (ne00 / 64) * 18;
188 case HTP_TYPE_Q3_K: return (ne00 / 256) * 110;
189 case HTP_TYPE_Q4_K: return (ne00 / 256) * 144;
190 case HTP_TYPE_Q5_K: return (ne00 / 256) * 176;
191 case HTP_TYPE_Q6_K: return (ne00 / 256) * 210;
192 case HTP_TYPE_Q1_0: return (ne00 / HTP_TENSOR_TILE_K_Q1_0) * 18;
193 case HTP_TYPE_I32: return ne00 * 4;
194 case HTP_TYPE_I64: return ne00 * 8;
195 default: return 0;
196 }
197}
198
199#endif // HTP_TENSOR_H