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

94 lines · 2709 bytes Raw Blame History
1#ifndef HTP_SOFTMAX_OPS_H
2#define HTP_SOFTMAX_OPS_H
3
4#include <stdint.h>
5#include <stddef.h>
6#include <stdbool.h>
7#include <math.h>
8#include "hex-fastdiv.h"
9#include "hex-common.h"
10#include "htp-vtcm.h"
11
12enum htp_softmax_kernel_type {
13 HTP_SOFTMAX_KERNEL_NOMASK = 0,
14 HTP_SOFTMAX_KERNEL_MASK_F32,
15 HTP_SOFTMAX_KERNEL_MASK_F16,
16 HTP_SOFTMAX_KERNEL_COUNT,
17};
18
19struct htp_softmax_kernel_params {
20 uint8_t kernel_type;
21 uint32_t n_threads;
22 uint32_t vtcm_size;
23
24 uint32_t n_head;
25 uint32_t n_head_log2;
26 uint32_t use_src1;
27 uint32_t use_f16;
28
29 float scale;
30 float max_bias;
31 float m0;
32 float m1;
33
34 struct fastdiv_values div_ne01;
35 struct fastdiv_values div_ne02;
36 struct fastdiv_values div_ne12;
37 struct fastdiv_values div_ne13;
38};
39
40#if defined(__cplusplus)
41static_assert(sizeof(struct htp_softmax_kernel_params) <= 128, "htp_softmax_kernel_params is too large for kernel_params blob");
42#else
43_Static_assert(sizeof(struct htp_softmax_kernel_params) <= 128, "htp_softmax_kernel_params is too large for kernel_params blob");
44#endif
45
46struct htp_softmax_vtcm_layout {
47 size_t total_bytes;
48 size_t off_src0;
49 size_t off_dst;
50 size_t off_src1;
51
52 size_t src0_bytes_per_thread;
53 size_t dst_bytes_per_thread;
54 size_t src1_bytes_per_thread;
55
56 size_t src0_spad_half_size;
57 size_t dst_spad_half_size;
58 size_t src1_spad_half_size;
59};
60
61static inline void htp_softmax_vtcm_layout_build(
62 struct htp_softmax_vtcm_layout * L,
63 uint32_t ne00,
64 uint32_t ne10,
65 bool use_src1,
66 bool use_f16,
67 uint32_t n_threads
68) {
69 size_t src0_row_size = ne00 * sizeof(float);
70 size_t dst_row_size = ne00 * sizeof(float);
71 size_t src1_row_size = use_src1 ? (ne10 * (use_f16 ? 2 : 4)) : 0;
72
73 size_t src0_row_size_aligned = hex_round_up(src0_row_size, 128);
74 size_t dst_row_size_aligned = hex_round_up(dst_row_size, 128);
75 size_t src1_row_size_aligned = use_src1 ? hex_round_up(src1_row_size, 128) : 0;
76
77 L->src0_spad_half_size = src0_row_size_aligned;
78 L->dst_spad_half_size = dst_row_size_aligned;
79 L->src1_spad_half_size = src1_row_size_aligned;
80
81 // Double buffering: 2 half-buffers per thread
82 L->src0_bytes_per_thread = src0_row_size_aligned * 2;
83 L->dst_bytes_per_thread = dst_row_size_aligned * 2;
84 L->src1_bytes_per_thread = src1_row_size_aligned * 2;
85
86 size_t off = 0;
87 VTCM_LAYOUT_ALLOC(off, off_src0, L->src0_bytes_per_thread * n_threads);
88 VTCM_LAYOUT_ALLOC(off, off_dst, L->dst_bytes_per_thread * n_threads);
89 VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_src1, L->src1_bytes_per_thread * n_threads, use_src1);
90
91 L->total_bytes = off;
92}
93
94#endif // HTP_SOFTMAX_OPS_H