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 / conv2d-ops.h

197 lines · 5747 bytes Raw Blame History
1#ifndef HTP_CONV2D_OPS_H
2#define HTP_CONV2D_OPS_H
3
4#include <stddef.h>
5#include <stdint.h>
6
7#include "hex-common.h"
8#include "hex-fastdiv.h"
9#include "htp-ops.h"
10#include "htp-vtcm.h"
11
12enum htp_conv2d_kernel_type {
13 HTP_CONV2D_KERNEL_UNSUPPORTED = 0,
14 HTP_CONV2D_KERNEL_HMX_WINDOW, // HMX convolution with sliding-window staging
15 HTP_CONV2D_KERNEL_HMX_TILED, // tiled implicit-GEMM convolution on HMX
16};
17
18// HMX_WINDOW output staging buffers per thread
19#define HTP_CONV2D_WIN_OBUF 2
20
21// HMX scales of one row tile at the start of VTCM: 32 (scale, bias) fp16 pairs and 128 zero bytes
22#define HTP_CONV2D_HMX_SCALES_SIZE 256
23
24struct htp_conv2d_tiled_params {
25 uint32_t w_kstride;
26 uint32_t k_size;
27 uint32_t k_group;
28 uint32_t k_group_pad;
29 uint32_t n_groups;
30 uint32_t n_k_tiles;
31 uint32_t n_ow_tiles;
32 uint32_t seg_tiles;
33 uint32_t stage_row;
34 uint32_t n_stage;
35 uint32_t wk_chunk;
36 struct fastdiv_values div_ow_tiles;
37 struct fastdiv_values div_oh;
38};
39
40struct htp_conv2d_window_params {
41 uint32_t fold;
42 uint32_t n_cc;
43 uint32_t act_rt_last;
44 uint32_t sr;
45 uint32_t ihs;
46 uint32_t max_off;
47 uint32_t n_bc;
48 uint32_t seg_b;
49 uint32_t seg_t;
50 uint32_t w_cs;
51 struct fastdiv_values div_sr;
52 struct fastdiv_values div_ihs;
53 struct fastdiv_values div_kw;
54 struct fastdiv_values div_tg;
55 struct fastdiv_values div_cc;
56 struct fastdiv_values div_cs;
57};
58
59struct htp_conv2d_kernel_params {
60 uint8_t kernel_type;
61 uint8_t n_threads;
62 uint8_t cwhn;
63 uint8_t w_kmajor;
64
65 uint32_t total_rows;
66 uint32_t n_row_tiles;
67 uint32_t ct_chunk;
68 uint32_t rt_block;
69 uint32_t stage_size;
70 uint32_t thr_size;
71 uint32_t vtcm_size;
72
73 union {
74 struct htp_conv2d_tiled_params tiled;
75 struct htp_conv2d_window_params window;
76 };
77};
78
79#if defined(__cplusplus)
80static_assert(sizeof(struct htp_conv2d_kernel_params) <= 128, "htp_conv2d_kernel_params is too large for kernel_params blob");
81#else
82_Static_assert(sizeof(struct htp_conv2d_kernel_params) <= 128, "htp_conv2d_kernel_params is too large for kernel_params blob");
83#endif
84
85enum htp_conv2d_dw_kernel_type {
86 HTP_CONV2D_DW_KERNEL_UNSUPPORTED = 0,
87 HTP_CONV2D_DW_KERNEL_CWHN, // CWHN channel-contiguous layout
88 HTP_CONV2D_DW_KERNEL_WHCN_S1, // WHCN general stride-1
89 HTP_CONV2D_DW_KERNEL_WHCN_S2, // WHCN general stride-2
90 HTP_CONV2D_DW_KERNEL_WHCN_S1_K3X3, // WHCN aligned w%32==0, 3x3 s=1 p=1 d=1
91 HTP_CONV2D_DW_KERNEL_WHCN_S2_K3X3_W32, // WHCN w=32->ow=16, 3x3 s=2 p=1 d=1 (2 rows per vector)
92 HTP_CONV2D_DW_KERNEL_WHCN_S2_K3X3_W64, // WHCN w=64->ow=32, 3x3 s=2 p=1 d=1 (1 vector rolling kh)
93 HTP_CONV2D_DW_KERNEL_WHCN_S2_K3X3, // WHCN aligned w%32==0 (w>=96), 3x3 s=2 p=1 d=1
94 HTP_CONV2D_DW_KERNEL_COUNT,
95};
96
97struct htp_conv2d_dw_kernel_params {
98 uint8_t kernel_type;
99 uint8_t n_threads;
100 uint8_t cwhn;
101 uint8_t is_f16;
102
103 uint32_t total_tasks;
104 uint32_t vtcm_size;
105 uint32_t w;
106 uint32_t h;
107 uint32_t c;
108 uint32_t n;
109 uint32_t kw;
110 uint32_t kh;
111 uint32_t ow;
112 uint32_t oh;
113 int32_t s0;
114 int32_t s1;
115 int32_t p0;
116 int32_t p1;
117 int32_t d0;
118 int32_t d1;
119 uint32_t oh_chunk;
120 uint32_t n_chunks;
121 uint32_t row_src_bytes;
122 uint32_t row_dst_bytes;
123 uint32_t pad_top_stride;
124 struct fastdiv_values div_c;
125 struct fastdiv_values div_n_chunks;
126};
127
128#if defined(__cplusplus)
129static_assert(sizeof(struct htp_conv2d_dw_kernel_params) <= 128, "htp_conv2d_dw_kernel_params is too large for kernel_params blob");
130#else
131_Static_assert(sizeof(struct htp_conv2d_dw_kernel_params) <= 128, "htp_conv2d_dw_kernel_params is too large for kernel_params blob");
132#endif
133
134struct htp_conv2d_dw_vtcm_layout {
135 size_t total_bytes;
136 size_t off_w;
137 size_t off_src;
138 size_t off_dst;
139 size_t src_spad_half_size;
140 size_t dst_spad_half_size;
141 size_t src_bytes_per_thread;
142 size_t dst_bytes_per_thread;
143};
144
145static inline void htp_conv2d_dw_vtcm_layout_build(
146 struct htp_conv2d_dw_vtcm_layout * L,
147 uint32_t max_src_bytes,
148 uint32_t max_dst_bytes,
149 uint32_t w_size,
150 uint32_t n_threads
151) {
152 L->src_spad_half_size = hex_round_up(max_src_bytes + 256, 128);
153 L->dst_spad_half_size = hex_round_up(max_dst_bytes + 128, 128);
154 L->src_bytes_per_thread = 2 * L->src_spad_half_size;
155 L->dst_bytes_per_thread = 2 * L->dst_spad_half_size;
156
157 size_t off = 0;
158 VTCM_LAYOUT_ALLOC(off, off_w, hex_round_up(w_size, 128));
159 VTCM_LAYOUT_ALLOC(off, off_src, L->src_bytes_per_thread * n_threads);
160 VTCM_LAYOUT_ALLOC(off, off_dst, L->dst_bytes_per_thread * n_threads);
161 L->total_bytes = off;
162}
163
164struct htp_conv2d_vtcm_layout {
165 size_t total_bytes;
166 size_t off_scales;
167 size_t off_w;
168 size_t off_act[2];
169 size_t off_out[2];
170 size_t off_thr;
171
172 size_t act_size;
173};
174
175static inline void htp_conv2d_vtcm_layout_build(
176 struct htp_conv2d_vtcm_layout * L,
177 uint32_t rt_block,
178 size_t w_size,
179 size_t act_size,
180 size_t out_size,
181 size_t thr_size,
182 uint32_t n_threads
183) {
184 const size_t sc = hex_round_up((size_t) rt_block * HTP_CONV2D_HMX_SCALES_SIZE, 2048);
185 size_t off = 0;
186 VTCM_LAYOUT_ALLOC(off, off_scales, sc);
187 VTCM_LAYOUT_ALLOC(off, off_w, w_size);
188 VTCM_LAYOUT_ALLOC(off, off_act[0], act_size);
189 VTCM_LAYOUT_ALLOC(off, off_act[1], act_size);
190 VTCM_LAYOUT_ALLOC(off, off_out[0], out_size);
191 VTCM_LAYOUT_ALLOC(off, off_out[1], out_size);
192 VTCM_LAYOUT_ALLOC(off, off_thr, (size_t) n_threads * thr_size);
193 L->act_size = act_size;
194 L->total_bytes = off;
195}
196
197#endif // HTP_CONV2D_OPS_H