ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-hexagon / htp / upscale-ops.h
| 1 | #ifndef HTP_UPSCALE_OPS_H |
| 2 | #define HTP_UPSCALE_OPS_H |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | #include <stddef.h> |
| 6 | #include <stdbool.h> |
| 7 | #include <math.h> |
| 8 | |
| 9 | #include "hex-common.h" |
| 10 | #include "hex-fastdiv.h" |
| 11 | #include "htp-vtcm.h" |
| 12 | |
| 13 | enum htp_upscale_mode { |
| 14 | HTP_UPSCALE_MODE_NEAREST = 0, |
| 15 | HTP_UPSCALE_MODE_BILINEAR = 1, |
| 16 | HTP_UPSCALE_MODE_BICUBIC = 2, |
| 17 | }; |
| 18 | |
| 19 | enum htp_upscale_kernel_type { |
| 20 | HTP_UPSCALE_KERNEL_NEAREST = 0, |
| 21 | HTP_UPSCALE_KERNEL_NEAREST_2X = 1, |
| 22 | HTP_UPSCALE_KERNEL_NEAREST_4X = 2, |
| 23 | HTP_UPSCALE_KERNEL_BILINEAR = 3, |
| 24 | HTP_UPSCALE_KERNEL_BILINEAR_2X = 4, |
| 25 | HTP_UPSCALE_KERNEL_BILINEAR_AA = 5, |
| 26 | HTP_UPSCALE_KERNEL_BICUBIC = 6, |
| 27 | }; |
| 28 | |
| 29 | struct htp_upscale_kernel_params { |
| 30 | uint8_t kernel_type; |
| 31 | uint8_t mode; |
| 32 | uint8_t align_corners; |
| 33 | uint8_t antialias; |
| 34 | |
| 35 | float inv_sf0; |
| 36 | float inv_sf1; |
| 37 | float inv_sf2; |
| 38 | float inv_sf3; |
| 39 | float pixel_offset; |
| 40 | |
| 41 | float filter_radius0; |
| 42 | float filter_radius1; |
| 43 | float invscale0; |
| 44 | float invscale1; |
| 45 | |
| 46 | uint32_t n_threads; |
| 47 | uint32_t chunks_per_group; |
| 48 | |
| 49 | uint32_t dst_nrows_per_chunk; |
| 50 | uint32_t chunks_per_slice; |
| 51 | uint32_t total_chunks; |
| 52 | uint32_t vtcm_size; |
| 53 | |
| 54 | struct fastdiv_values div_chunks_per_slice; |
| 55 | struct fastdiv_values div_dst_z; |
| 56 | }; |
| 57 | |
| 58 | #if defined(__cplusplus) |
| 59 | static_assert(sizeof(struct htp_upscale_kernel_params) <= 128, "htp_upscale_kernel_params is too large"); |
| 60 | #else |
| 61 | _Static_assert(sizeof(struct htp_upscale_kernel_params) <= 128, "htp_upscale_kernel_params is too large"); |
| 62 | #endif |
| 63 | |
| 64 | struct __attribute__((aligned(128))) upscale_bilinear_x_chunk { |
| 65 | int32_t off0[32]; |
| 66 | int32_t off1[32]; |
| 67 | float w0[32]; |
| 68 | float dx[32]; |
| 69 | }; |
| 70 | |
| 71 | struct upscale_bilinear_y { |
| 72 | uint32_t y0; |
| 73 | uint32_t y1; |
| 74 | float wy0; |
| 75 | float wy1; |
| 76 | }; |
| 77 | |
| 78 | struct upscale_bicubic_x { |
| 79 | int32_t x[4]; |
| 80 | float w[4]; |
| 81 | }; |
| 82 | |
| 83 | struct upscale_bicubic_y { |
| 84 | int32_t y[4]; |
| 85 | float w[4]; |
| 86 | }; |
| 87 | |
| 88 | struct upscale_antialias_x { |
| 89 | int32_t x_min; |
| 90 | int32_t x_max; |
| 91 | float xf; |
| 92 | }; |
| 93 | |
| 94 | struct upscale_antialias_y { |
| 95 | int32_t y_min; |
| 96 | int32_t y_max; |
| 97 | float yf; |
| 98 | }; |
| 99 | |
| 100 | struct htp_upscale_vtcm_layout { |
| 101 | size_t total_bytes; |
| 102 | size_t off_src; |
| 103 | size_t off_dst; |
| 104 | size_t off_r; |
| 105 | size_t off_gather; |
| 106 | size_t src_bytes_per_thread; |
| 107 | size_t dst_bytes_per_thread; |
| 108 | size_t r_bytes_per_thread; |
| 109 | size_t gather_bytes_per_thread; |
| 110 | size_t src_spad_half_size; |
| 111 | size_t dst_spad_half_size; |
| 112 | uint32_t max_src_chunk_rows; |
| 113 | uint32_t dst_chunk_rows; |
| 114 | }; |
| 115 | |
| 116 | static inline void htp_upscale_vtcm_layout_build( |
| 117 | struct htp_upscale_vtcm_layout * L, |
| 118 | uint32_t src_x, |
| 119 | uint32_t src_y, |
| 120 | uint32_t dst_x, |
| 121 | uint32_t dst_y, |
| 122 | uint32_t dst_chunk_rows, |
| 123 | float inv_sf1, |
| 124 | float filter_radius1, |
| 125 | uint32_t n_threads |
| 126 | ) { |
| 127 | uint32_t max_src_rows; |
| 128 | if (dst_chunk_rows >= dst_y) { |
| 129 | max_src_rows = src_y; |
| 130 | } else { |
| 131 | const float est = (float) dst_chunk_rows * inv_sf1 + 2.0f * filter_radius1; |
| 132 | max_src_rows = (uint32_t) ceilf(est) + 4; |
| 133 | if (max_src_rows > src_y) { |
| 134 | max_src_rows = src_y; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | const uint32_t src_chunk_bytes = src_x * max_src_rows * sizeof(float); |
| 139 | const uint32_t dst_chunk_bytes = dst_x * dst_chunk_rows * sizeof(float); |
| 140 | |
| 141 | L->max_src_chunk_rows = max_src_rows; |
| 142 | L->dst_chunk_rows = dst_chunk_rows; |
| 143 | L->src_spad_half_size = hex_round_up(src_chunk_bytes + 256, 128); |
| 144 | L->dst_spad_half_size = hex_round_up(dst_chunk_bytes, 128); |
| 145 | L->src_bytes_per_thread = 2 * L->src_spad_half_size; |
| 146 | L->dst_bytes_per_thread = 2 * L->dst_spad_half_size; |
| 147 | L->r_bytes_per_thread = hex_round_up(src_x * sizeof(float), 128); |
| 148 | L->gather_bytes_per_thread = 256; |
| 149 | |
| 150 | size_t off = 0; |
| 151 | VTCM_LAYOUT_ALLOC(off, off_src, L->src_bytes_per_thread * n_threads); |
| 152 | VTCM_LAYOUT_ALLOC(off, off_dst, L->dst_bytes_per_thread * n_threads); |
| 153 | VTCM_LAYOUT_ALLOC(off, off_r, L->r_bytes_per_thread * n_threads); |
| 154 | VTCM_LAYOUT_ALLOC(off, off_gather, L->gather_bytes_per_thread * n_threads); |
| 155 | L->total_bytes = off; |
| 156 | } |
| 157 | |
| 158 | static inline bool htp_upscale_solve_layout( |
| 159 | struct htp_upscale_vtcm_layout * layout, |
| 160 | uint32_t src_x, |
| 161 | uint32_t src_y, |
| 162 | uint32_t dst_x, |
| 163 | uint32_t dst_y, |
| 164 | float inv_sf1, |
| 165 | float filter_radius1, |
| 166 | uint32_t n_threads, |
| 167 | size_t vtcm_budget, |
| 168 | uint32_t * out_dst_chunk_rows |
| 169 | ) { |
| 170 | // 1. Try full plane |
| 171 | uint32_t chunk_rows = dst_y; |
| 172 | htp_upscale_vtcm_layout_build(layout, src_x, src_y, dst_x, dst_y, chunk_rows, inv_sf1, filter_radius1, n_threads); |
| 173 | if (layout->total_bytes <= vtcm_budget) { |
| 174 | *out_dst_chunk_rows = chunk_rows; |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | // 2. Try smaller chunk bands: 64, 32, 16, 8, 4, 2, 1 |
| 179 | chunk_rows = 64; |
| 180 | while (chunk_rows > 1) { |
| 181 | if (chunk_rows < dst_y) { |
| 182 | htp_upscale_vtcm_layout_build(layout, src_x, src_y, dst_x, dst_y, chunk_rows, inv_sf1, filter_radius1, n_threads); |
| 183 | if (layout->total_bytes <= vtcm_budget) { |
| 184 | *out_dst_chunk_rows = chunk_rows; |
| 185 | return true; |
| 186 | } |
| 187 | } |
| 188 | chunk_rows /= 2; |
| 189 | } |
| 190 | |
| 191 | // 3. Fallback to 1 row |
| 192 | chunk_rows = 1; |
| 193 | htp_upscale_vtcm_layout_build(layout, src_x, src_y, dst_x, dst_y, chunk_rows, inv_sf1, filter_radius1, n_threads); |
| 194 | if (layout->total_bytes <= vtcm_budget) { |
| 195 | *out_dst_chunk_rows = chunk_rows; |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | int op_upscale(struct htp_ops_context * octx); |
| 203 | |
| 204 | #endif // HTP_UPSCALE_OPS_H |