Debian Codegraph @

external/whispercpp/unary-ops.cpp

raw symbols SCIP
1 #cxx . . $ `<file>/external/whispercpp/unary-ops.cpp`/include "unary-ops.h"cxx . . $ `<file>/external/whispercpp/unary-ops.h`/
2
3 static inline float op_abscxx . . $ op_abs(fbcf3211dfe1bfd1).(float x) {
4 return fabsf(x);
5 }
6
7 static inline float op_sgncxx . . $ op_sgn(fbcf3211dfe1bfd1).(float x) {
8 return (x > 0.f) ? 1.f : ((x < 0.f) ? -1.f : 0.f);
9 }
10
11 static inline float op_negcxx . . $ op_neg(fbcf3211dfe1bfd1).(float x) {
12 return -x;
13 }
14
15 static inline float op_stepcxx . . $ op_step(fbcf3211dfe1bfd1).(float x) {
16 return (x > 0.f) ? 1.f : 0.f;
17 }
18
19 static inline float op_tanhcxx . . $ op_tanh(fbcf3211dfe1bfd1).(float x) {
20 return tanhf(x);
21 }
22
23 static inline float op_elucxx . . $ op_elu(fbcf3211dfe1bfd1).(float x) {
24 return (x > 0.f) ? x : expm1f(x);
25 }
26
27 static inline float op_relucxx . . $ op_relu(fbcf3211dfe1bfd1).(float x) {
28 return (x > 0.f) ? x : 0.f;
29 }
30
31 static inline float op_sigmoidcxx . . $ op_sigmoid(fbcf3211dfe1bfd1).(float x) {
32 return 1.f / (1.f + expf(-x));
33 }
34
35 static inline float op_hardsigmoidcxx . . $ op_hardsigmoid(fbcf3211dfe1bfd1).(float x) {
36 return fminf(1.0f, fmaxf(0.0f, (x + 3.0f) / 6.0f));
37 }
38
39 static inline float op_expcxx . . $ op_exp(fbcf3211dfe1bfd1).(float x) {
40 return expf(x);
41 }
42
43 static inline float op_hardswishcxx . . $ op_hardswish(fbcf3211dfe1bfd1).(float x) {
44 return x * fminf(1.0f, fmaxf(0.0f, (x + 3.0f) / 6.0f));
45 }
46
47 static inline float op_sqrcxx . . $ op_sqr(fbcf3211dfe1bfd1).(float x) {
48 return x * x;
49 }
50
51 static inline float op_sqrtcxx . . $ op_sqrt(fbcf3211dfe1bfd1).(float x) {
52 return sqrtf(x);
53 }
54
55 static inline float op_xielucxx . . $ op_xielu(a4708ef16e45b76e).(float x, float alpha_n, float alpha_p, float beta, float eps) {
56 if (x > 0.0f) {
57 return alpha_p * x * x + beta * x;
58 } else {
59 const float min_x_eps = fminf(x, eps);
60 return (expm1f(min_x_eps) - x) * alpha_n + beta * x;
61 }
62 }
63
64 static inline float op_sincxx . . $ op_sin(fbcf3211dfe1bfd1).(float x) {
65 return sinf(x);
66 }
67
68 static inline float op_coscxx . . $ op_cos(fbcf3211dfe1bfd1).(float x) {
69 return cosf(x);
70 }
71
72 static inline float op_logcxx . . $ op_log(fbcf3211dfe1bfd1).(float x) {
73 return logf(x);
74 }
75
76 static inline float op_expm1cxx . . $ op_expm1(fbcf3211dfe1bfd1).(float x) {
77 return expf(x) - 1.0f;
78 }
79
80 static inline float op_softpluscxx . . $ op_softplus(fbcf3211dfe1bfd1).(float x) {
81 return (x > 20.0f) ? x : logf(1.0f + expf(x));
82 }
83
84 static inline float op_floorcxx . . $ op_floor(fbcf3211dfe1bfd1).(float x) {
85 return floorf(x);
86 }
87
88 static inline float op_ceilcxx . . $ op_ceil(fbcf3211dfe1bfd1).(float x) {
89 return ceilf(x);
90 }
91
92 static inline float op_roundcxx . . $ op_round(fbcf3211dfe1bfd1).(float x) {
93 return roundf(x);
94 }
95
96 static inline float op_trunccxx . . $ op_trunc(fbcf3211dfe1bfd1).(float x) {
97 return truncf(x);
98 }
99
100 template <float (*op)(float), typename src0_t, typename dst_t>
101 static inline void vec_unary_opcxx . . $ vec_unary_op(85d32506d6237439).(int64_tcxx . . $ int64_t# n, dst_t * y, const src0_t * x) {
102 constexpr auto src0_to_f32 = type_conversion_tablecxx . . $ type_conversion_table#<src0_t>::to_f32;
103 constexpr auto f32_to_dst = type_conversion_tablecxx . . $ type_conversion_table#<dst_t >::from_f32;
104
105 for (int i = 0; i < n; i++) {
106 y[i] = f32_to_dst(op(src0_to_f32(x[i])));
107 }
108 }
109
110 template <float (*op)(float), typename src0_t, typename dst_t>
111 static void apply_unary_opcxx . . $ apply_unary_op(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
112 const ggml_tensor * src0 = dst->src[0];
113
114 GGML_ASSERTcxx . . $ `external/whispercpp/ggml.h:300:9`!(ggml_is_contiguous_rows(src0) && ggml_is_contiguous_rows(dst) && ggml_are_same_shape(src0, dst));
115
116 GGML_TENSOR_UNARY_OP_LOCALScxx . . $ `external/whispercpp/ggml.h:326:9`!
117
118 GGML_ASSERTcxx . . $ `external/whispercpp/ggml.h:300:9`!( nb0 == sizeof(dst_t));
119 GGML_ASSERTcxx . . $ `external/whispercpp/ggml.h:300:9`!(nb00 == sizeof(src0_t));
120
121 const auto [ir0, ir1] = get_thread_rangecxx . . $ get_thread_range(9e153b0c10f4f4a0).(params, src0);
122
123 for (int64_tcxx . . $ int64_t# ir = ir0; ir < ir1; ++ir) {
124 const int64_tcxx . . $ int64_t# i03 = ir/(ne02*ne01);
125 const int64_tcxx . . $ int64_t# i02 = (ir - i03*ne02*ne01)/ne01;
126 const int64_tcxx . . $ int64_t# i01 = (ir - i03*ne02*ne01 - i02*ne01);
127
128 dst_t * dst_ptr = (dst_t *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
129 const src0_t * src0_ptr = (const src0_t *) ((const char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
130
131 vec_unary_opcxx . . $ vec_unary_op(85d32506d6237439).<op>(ne0, dst_ptr, src0_ptr);
132 }
133 }
134
135 // TODO: Use the 'traits' lookup table (for type conversion fns), instead of a mass of 'if' conditions with long templates
136 template <float (*op)(float)>
137 static void unary_opcxx . . $ unary_op(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
138 const ggml_tensor * src0 = dst->src[0];
139
140 /* */ if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) { // all f32
141 apply_unary_opcxx . . $ apply_unary_op(4ff5508a48b20a57).<op, float, float>(params, dst);
142 } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) { // all f16
143 apply_unary_opcxx . . $ apply_unary_op(4ff5508a48b20a57).<op, ggml_fp16_t, ggml_fp16_t>(params, dst);
144 } else if (src0->type == GGML_TYPE_BF16 && dst->type == GGML_TYPE_BF16) { // all bf16
145 apply_unary_opcxx . . $ apply_unary_op(4ff5508a48b20a57).<op, ggml_bf16_t, ggml_bf16_t>(params, dst);
146 } else if (src0->type == GGML_TYPE_BF16 && dst->type == GGML_TYPE_F32) {
147 apply_unary_opcxx . . $ apply_unary_op(4ff5508a48b20a57).<op, ggml_bf16_t, float>(params, dst);
148 } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
149 apply_unary_opcxx . . $ apply_unary_op(4ff5508a48b20a57).<op, ggml_fp16_t, float>(params, dst);
150 } else {
151 fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: unsupported types: dst: %s, src0: %s\n", __func__,
152 ggml_type_name(dst->type), ggml_type_name(src0->type));
153 GGML_ABORTcxx . . $ `external/whispercpp/ggml.h:299:9`!("fatal error");
154 }
155 }
156
157 template <float (*op)(float, ggml_tensor *)>
158 static void unary_op_paramscxx . . $ unary_op_params(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
159 const ggml_tensor * src0 = dst->src[0];
160
161 /* */ if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) { // all f32
162 apply_unary_opcxx . . $ apply_unary_op(4ff5508a48b20a57).<op, float, float>(params, dst);
163 } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) { // all f16
164 apply_unary_opcxx . . $ apply_unary_op(4ff5508a48b20a57).<op, ggml_fp16_t, ggml_fp16_t>(params, dst);
165 } else if (src0->type == GGML_TYPE_BF16 && dst->type == GGML_TYPE_BF16) { // all bf16
166 apply_unary_opcxx . . $ apply_unary_op(4ff5508a48b20a57).<op, ggml_bf16_t, ggml_bf16_t>(params, dst);
167 } else if (src0->type == GGML_TYPE_BF16 && dst->type == GGML_TYPE_F32) {
168 apply_unary_opcxx . . $ apply_unary_op(4ff5508a48b20a57).<op, ggml_bf16_t, float>(params, dst);
169 } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
170 apply_unary_opcxx . . $ apply_unary_op(4ff5508a48b20a57).<op, ggml_fp16_t, float>(params, dst);
171 } else {
172 fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: unsupported types: dst: %s, src0: %s\n", __func__,
173 ggml_type_name(dst->type), ggml_type_name(src0->type));
174 GGML_ABORTcxx . . $ `external/whispercpp/ggml.h:299:9`!("fatal error");
175 }
176 }
177
178 // Extend vec_unary_op to support functors
179 template <typename Op, typename src0_t, typename dst_t>
180 static inline void vec_unary_op_functorcxx . . $ vec_unary_op_functor(699103bcd644e828).(int64_tcxx . . $ int64_t# n, dst_t * y, const src0_t * x, Op op) {
181 constexpr auto src0_to_f32 = type_conversion_tablecxx . . $ type_conversion_table#<src0_t>::to_f32;
182 constexpr auto f32_to_dst = type_conversion_tablecxx . . $ type_conversion_table#<dst_t >::from_f32;
183
184 for (int i = 0; i < n; i++) {
185 y[i] = f32_to_dst(op(src0_to_f32(x[i])));
186 }
187 }
188
189 // Extend apply_unary_op to support functors
190 template <typename Op, typename src0_t, typename dst_t>
191 static void apply_unary_op_functorcxx . . $ apply_unary_op_functor(b9da1e62252dcc89).(const ggml_compute_params * params, ggml_tensor * dst, Op op) {
192 const ggml_tensor * src0 = dst->src[0];
193
194 GGML_ASSERTcxx . . $ `external/whispercpp/ggml.h:300:9`!(ggml_is_contiguous_1(src0) && ggml_is_contiguous_1(dst) && ggml_are_same_shape(src0, dst));
195
196 GGML_TENSOR_UNARY_OP_LOCALScxx . . $ `external/whispercpp/ggml.h:326:9`!
197
198 GGML_ASSERTcxx . . $ `external/whispercpp/ggml.h:300:9`!( nb0 == sizeof(dst_t));
199 GGML_ASSERTcxx . . $ `external/whispercpp/ggml.h:300:9`!(nb00 == sizeof(src0_t));
200
201 const auto [ir0, ir1] = get_thread_rangecxx . . $ get_thread_range(9e153b0c10f4f4a0).(params, src0);
202
203 for (int64_tcxx . . $ int64_t# ir = ir0; ir < ir1; ++ir) {
204 const int64_tcxx . . $ int64_t# i03 = ir/(ne02*ne01);
205 const int64_tcxx . . $ int64_t# i02 = (ir - i03*ne02*ne01)/ne01;
206 const int64_tcxx . . $ int64_t# i01 = (ir - i03*ne02*ne01 - i02*ne01);
207
208 dst_t * dst_ptr = (dst_t *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
209 const src0_t * src0_ptr = (const src0_t *) ((const char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
210
211 vec_unary_op_functorcxx . . $ vec_unary_op_functor(699103bcd644e828).(ne0, dst_ptr, src0_ptr, op);
212 }
213 }
214
215 // Generic dispatcher for functors
216 template <typename Op>
217 static void unary_op_functorcxx . . $ unary_op_functor(b9da1e62252dcc89).(const ggml_compute_params * params, ggml_tensor * dst, Op op) {
218 const ggml_tensor * src0 = dst->src[0];
219
220 /* */ if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) { // all f32
221 apply_unary_op_functorcxx . . $ apply_unary_op_functor(b9da1e62252dcc89).<Op, float, float>(params, dst, op);
222 } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) { // all f16
223 apply_unary_op_functorcxx . . $ apply_unary_op_functor(b9da1e62252dcc89).<Op, ggml_fp16_t, ggml_fp16_t>(params, dst, op);
224 } else if (src0->type == GGML_TYPE_BF16 && dst->type == GGML_TYPE_BF16) { // all bf16
225 apply_unary_op_functorcxx . . $ apply_unary_op_functor(b9da1e62252dcc89).<Op, ggml_bf16_t, ggml_bf16_t>(params, dst, op);
226 } else if (src0->type == GGML_TYPE_BF16 && dst->type == GGML_TYPE_F32) {
227 apply_unary_op_functorcxx . . $ apply_unary_op_functor(b9da1e62252dcc89).<Op, ggml_bf16_t, float>(params, dst, op);
228 } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
229 apply_unary_op_functorcxx . . $ apply_unary_op_functor(b9da1e62252dcc89).<Op, ggml_fp16_t, float>(params, dst, op);
230 } else {
231 fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: unsupported types: dst: %s, src0: %s\n", __func__,
232 ggml_type_name(dst->type), ggml_type_name(src0->type));
233 GGML_ABORTcxx . . $ `external/whispercpp/ggml.h:299:9`!("fatal error");
234 }
235 }
236
237 void ggml_compute_forward_abscxx . . $ ggml_compute_forward_abs(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
238 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_abscxx . . $ op_abs(fbcf3211dfe1bfd1).>(params, dst);
239 }
240
241 void ggml_compute_forward_sgncxx . . $ ggml_compute_forward_sgn(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
242 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_sgncxx . . $ op_sgn(fbcf3211dfe1bfd1).>(params, dst);
243 }
244
245 void ggml_compute_forward_negcxx . . $ ggml_compute_forward_neg(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
246 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_negcxx . . $ op_neg(fbcf3211dfe1bfd1).>(params, dst);
247 }
248
249 void ggml_compute_forward_stepcxx . . $ ggml_compute_forward_step(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
250 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_stepcxx . . $ op_step(fbcf3211dfe1bfd1).>(params, dst);
251 }
252
253 void ggml_compute_forward_tanhcxx . . $ ggml_compute_forward_tanh(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
254 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_tanhcxx . . $ op_tanh(fbcf3211dfe1bfd1).>(params, dst);
255 }
256
257 void ggml_compute_forward_elucxx . . $ ggml_compute_forward_elu(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
258 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_elucxx . . $ op_elu(fbcf3211dfe1bfd1).>(params, dst);
259 }
260
261 void ggml_compute_forward_relucxx . . $ ggml_compute_forward_relu(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
262 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_relucxx . . $ op_relu(fbcf3211dfe1bfd1).>(params, dst);
263 }
264
265 void ggml_compute_forward_sigmoidcxx . . $ ggml_compute_forward_sigmoid(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
266 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_sigmoidcxx . . $ op_sigmoid(fbcf3211dfe1bfd1).>(params, dst);
267 }
268
269 void ggml_compute_forward_hardsigmoidcxx . . $ ggml_compute_forward_hardsigmoid(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
270 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_hardsigmoidcxx . . $ op_hardsigmoid(fbcf3211dfe1bfd1).>(params, dst);
271 }
272
273 void ggml_compute_forward_expcxx . . $ ggml_compute_forward_exp(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
274 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_expcxx . . $ op_exp(fbcf3211dfe1bfd1).>(params, dst);
275 }
276
277 void ggml_compute_forward_hardswishcxx . . $ ggml_compute_forward_hardswish(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
278 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_hardswishcxx . . $ op_hardswish(fbcf3211dfe1bfd1).>(params, dst);
279 }
280
281 void ggml_compute_forward_sqrcxx . . $ ggml_compute_forward_sqr(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
282 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_sqrcxx . . $ op_sqr(fbcf3211dfe1bfd1).>(params, dst);
283 }
284
285 void ggml_compute_forward_sqrtcxx . . $ ggml_compute_forward_sqrt(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
286 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_sqrtcxx . . $ op_sqrt(fbcf3211dfe1bfd1).>(params, dst);
287 }
288
289 void ggml_compute_forward_sincxx . . $ ggml_compute_forward_sin(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
290 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_sincxx . . $ op_sin(fbcf3211dfe1bfd1).>(params, dst);
291 }
292
293 void ggml_compute_forward_coscxx . . $ ggml_compute_forward_cos(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
294 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_coscxx . . $ op_cos(fbcf3211dfe1bfd1).>(params, dst);
295 }
296
297 void ggml_compute_forward_logcxx . . $ ggml_compute_forward_log(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
298 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_logcxx . . $ op_log(fbcf3211dfe1bfd1).>(params, dst);
299 }
300
301 void ggml_compute_forward_expm1cxx . . $ ggml_compute_forward_expm1(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
302 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_expm1cxx . . $ op_expm1(fbcf3211dfe1bfd1).>(params, dst);
303 }
304
305 void ggml_compute_forward_softpluscxx . . $ ggml_compute_forward_softplus(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
306 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_softpluscxx . . $ op_softplus(fbcf3211dfe1bfd1).>(params, dst);
307 }
308
309 void ggml_compute_forward_floorcxx . . $ ggml_compute_forward_floor(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
310 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_floorcxx . . $ op_floor(fbcf3211dfe1bfd1).>(params, dst);
311 }
312
313 void ggml_compute_forward_ceilcxx . . $ ggml_compute_forward_ceil(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
314 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_ceilcxx . . $ op_ceil(fbcf3211dfe1bfd1).>(params, dst);
315 }
316
317 void ggml_compute_forward_roundcxx . . $ ggml_compute_forward_round(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
318 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_roundcxx . . $ op_round(fbcf3211dfe1bfd1).>(params, dst);
319 }
320
321 void ggml_compute_forward_trunccxx . . $ ggml_compute_forward_trunc(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
322 unary_opcxx . . $ unary_op(4ff5508a48b20a57).<op_trunccxx . . $ op_trunc(fbcf3211dfe1bfd1).>(params, dst);
323 }
324
325 void ggml_compute_forward_xielucxx . . $ ggml_compute_forward_xielu(4ff5508a48b20a57).(const ggml_compute_params * params, ggml_tensor * dst) {
326 const float alpha_n = ggml_get_op_params_f32(dst, 1);
327 const float alpha_p = ggml_get_op_params_f32(dst, 2);
328 const float beta = ggml_get_op_params_f32(dst, 3);
329 const float eps = ggml_get_op_params_f32(dst, 4);
330
331 const auto xielu_op_params = [alpha_n, alpha_p, beta, eps](float f) {
332 return op_xielucxx . . $ op_xielu(a4708ef16e45b76e).(f, alpha_n, alpha_p, beta, eps);
333 };
334
335 unary_op_functorcxx . . $ unary_op_functor(b9da1e62252dcc89).(params, dst, xielu_op_params);
336 }
337