forked from 0xShug0/audio.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivation_modules.cpp
More file actions
755 lines (654 loc) · 25.7 KB
/
Copy pathactivation_modules.cpp
File metadata and controls
755 lines (654 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
#include "engine/framework/modules/activation_modules.h"
#include <stdexcept>
namespace engine::modules {
namespace {
const core::ModulePortSpec kActivationInputs[] = {
{"input", core::PortKind::Activation, false},
};
const core::ModulePortSpec kActivationOutputs[] = {
{"output", core::PortKind::Activation, false},
};
const core::ModuleSchema kReluSchema = {
"ReLU",
"nn.activation",
kActivationInputs,
1,
kActivationOutputs,
1,
"Applies rectified linear activation elementwise.",
};
const core::ModuleSchema kLeakyReluSchema = {
"LeakyReLU",
"nn.activation",
kActivationInputs,
1,
kActivationOutputs,
1,
"Applies leaky rectified linear activation elementwise.",
};
const core::ModuleSchema kSigmoidSchema = {
"Sigmoid",
"nn.activation",
kActivationInputs,
1,
kActivationOutputs,
1,
"Applies sigmoid activation elementwise.",
};
const core::ModuleSchema kTanhSchema = {
"Tanh",
"nn.activation",
kActivationInputs,
1,
kActivationOutputs,
1,
"Applies tanh activation elementwise.",
};
const core::ModuleSchema kSqrtSchema = {
"Sqrt",
"nn.activation",
kActivationInputs,
1,
kActivationOutputs,
1,
"Applies square root elementwise.",
};
const core::ModuleSchema kGeluSchema = {
"GELU",
"nn.activation",
kActivationInputs,
1,
kActivationOutputs,
1,
"Applies GELU activation elementwise.",
};
const core::ModuleSchema kSiluSchema = {
"SiLU",
"nn.activation",
kActivationInputs,
1,
kActivationOutputs,
1,
"Applies SiLU activation elementwise.",
};
const core::ModuleSchema kSeluSchema = {
"SELU",
"nn.activation",
kActivationInputs,
1,
kActivationOutputs,
1,
"Applies SELU activation elementwise.",
};
const core::ModuleSchema kSwooshLSchema = {
"SwooshL",
"nn.activation",
kActivationInputs,
1,
kActivationOutputs,
1,
"Applies Zipformer SwooshL activation elementwise.",
};
const core::ModuleSchema kSwooshRSchema = {
"SwooshR",
"nn.activation",
kActivationInputs,
1,
kActivationOutputs,
1,
"Applies Zipformer SwooshR activation elementwise.",
};
const core::ModuleSchema kEluSchema = {
"ELU",
"nn.activation",
kActivationInputs,
1,
kActivationOutputs,
1,
"Applies ELU activation elementwise.",
};
const core::ModuleSchema kSoftmaxSchema = {
"Softmax",
"nn.activation",
kActivationInputs,
1,
kActivationOutputs,
1,
"Applies softmax over the last physical dimension.",
};
const core::ModuleSchema kGLUSchema = {
"GLU",
"nn.activation",
kActivationInputs,
1,
kActivationOutputs,
1,
"Splits the last dimension in half and applies sigmoid gating.",
};
const core::ModulePortSpec kSnakeInputs[] = {
{"input", core::PortKind::Activation, false},
{"alpha", core::PortKind::Parameter, false},
};
const core::ModulePortSpec kSnakeBetaInputs[] = {
{"input", core::PortKind::Activation, false},
{"alpha", core::PortKind::Parameter, false},
{"beta", core::PortKind::Parameter, false},
};
const core::ModulePortSpec kAliasFreeActivationInputs[] = {
{"input", core::PortKind::Activation, false},
{"alpha", core::PortKind::Parameter, false},
{"inv_beta", core::PortKind::Parameter, false},
{"up_filter_even", core::PortKind::Parameter, false},
{"up_filter_odd", core::PortKind::Parameter, false},
{"down_filter", core::PortKind::Parameter, false},
};
const core::ModuleSchema kSnake1dSchema = {
"Snake1d",
"nn.activation",
kSnakeInputs,
2,
kActivationOutputs,
1,
"Applies Snake activation over channel-time tensors using per-channel alpha.",
};
const core::ModuleSchema kSnakeBeta1dSchema = {
"SnakeBeta1d",
"nn.activation",
kSnakeBetaInputs,
3,
kActivationOutputs,
1,
"Applies SnakeBeta activation over channel-time tensors using per-channel alpha and beta.",
};
const core::ModuleSchema kAliasFreeActivationSchema = {
"AliasFreeActivation",
"nn.activation",
kAliasFreeActivationInputs,
6,
kActivationOutputs,
1,
"Applies filtered upsample, nonlinear activation, and filtered downsample to channel-time tensors.",
};
template <typename Fn>
core::TensorValue build_unary(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
Fn fn) {
if (ctx.ggml == nullptr) {
throw std::runtime_error("ModuleBuildContext.ggml is null");
}
core::validate_rank_between(input, 1, core::kMaxTensorRank, "input");
const auto contiguous = core::ensure_backend_addressable_layout(ctx, input);
return core::wrap_tensor(fn(ctx.ggml, contiguous.tensor), input.shape, GGML_TYPE_F32);
}
core::TensorShape make_snake_alpha_shape(const core::TensorShape & input, int64_t hidden_size) {
core::TensorShape shape = {};
shape.rank = input.rank;
for (size_t i = 0; i < shape.rank; ++i) {
shape.dims[i] = 1;
}
shape.dims[shape.rank - 2] = hidden_size;
return shape;
}
core::TensorValue ensure_f32(
core::ModuleBuildContext & ctx,
const core::TensorValue & value) {
if (value.type == GGML_TYPE_F32) {
return value;
}
return core::wrap_tensor(ggml_cast(ctx.ggml, value.tensor, GGML_TYPE_F32), value.shape, GGML_TYPE_F32);
}
bool same_shape(const core::TensorShape & lhs, const core::TensorShape & rhs) {
if (lhs.rank != rhs.rank) {
return false;
}
for (size_t i = 0; i < lhs.rank; ++i) {
if (lhs.dims[i] != rhs.dims[i]) {
return false;
}
}
return true;
}
ggml_tensor * repeat_frame(ggml_context * ctx, ggml_tensor * x, int64_t frame, int64_t count) {
ggml_tensor * src = ggml_view_2d(ctx, x, 1, x->ne[1], x->nb[1], static_cast<size_t>(frame) * x->nb[0]);
return ggml_repeat(ctx, src, ggml_new_tensor_2d(ctx, GGML_TYPE_F32, count, x->ne[1]));
}
ggml_tensor * replicate_pad_left_ct(ggml_context * ctx, ggml_tensor * x, int64_t count) {
if (count <= 0) {
return x;
}
return ggml_concat(ctx, repeat_frame(ctx, x, 0, count), x, 0);
}
ggml_tensor * depthwise_conv_transpose_causal_ct(
core::ModuleBuildContext & ctx,
ggml_tensor * x,
const core::TensorValue & even_filter,
const core::TensorValue & odd_filter,
int64_t phase_kernel,
int64_t upsample_ratio) {
const int64_t frames = x->ne[0];
const int64_t channels = x->ne[1];
ggml_tensor * input4 = ggml_reshape_4d(
ctx.ggml,
core::has_backend_addressable_layout(x) ? x : ggml_cont(ctx.ggml, x),
frames,
1,
channels,
1);
auto convolve_phase = [&](const core::TensorValue & phase_filter) {
ggml_tensor * raw = ggml_conv_2d_dw_direct(
ctx.ggml,
phase_filter.tensor,
input4,
1,
1,
static_cast<int>(phase_kernel - 1),
0,
1,
1);
raw = core::has_backend_addressable_layout(raw) ? raw : ggml_cont(ctx.ggml, raw);
auto * reshaped = ggml_reshape_2d(ctx.ggml, raw, raw->ne[0], raw->ne[2]);
return ggml_view_2d(ctx.ggml, reshaped, frames, reshaped->ne[1], reshaped->nb[1], 0);
};
ggml_tensor * even = convolve_phase(even_filter);
ggml_tensor * odd = convolve_phase(odd_filter);
ggml_tensor * even3 = ggml_reshape_3d(
ctx.ggml,
core::has_backend_addressable_layout(even) ? even : ggml_cont(ctx.ggml, even),
1,
frames,
channels);
ggml_tensor * odd3 = ggml_reshape_3d(
ctx.ggml,
core::has_backend_addressable_layout(odd) ? odd : ggml_cont(ctx.ggml, odd),
1,
frames,
channels);
return ggml_reshape_2d(ctx.ggml, ggml_concat(ctx.ggml, even3, odd3, 0), frames * upsample_ratio, channels);
}
ggml_tensor * depthwise_conv_causal_ct(
core::ModuleBuildContext & ctx,
ggml_tensor * x,
const core::TensorValue & filter,
int64_t kernel_size,
int64_t stride) {
ggml_tensor * padded = replicate_pad_left_ct(ctx.ggml, x, kernel_size - 1);
ggml_tensor * input4 = ggml_reshape_4d(
ctx.ggml,
core::has_backend_addressable_layout(padded) ? padded : ggml_cont(ctx.ggml, padded),
padded->ne[0],
1,
padded->ne[1],
1);
ggml_tensor * out = ggml_conv_2d_dw_direct(ctx.ggml, filter.tensor, input4, static_cast<int>(stride), 1, 0, 0, 1, 1);
out = core::has_backend_addressable_layout(out) ? out : ggml_cont(ctx.ggml, out);
return ggml_reshape_2d(ctx.ggml, out, out->ne[0], out->ne[2]);
}
core::TensorValue build_swoosh(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
float offset,
float constant) {
if (ctx.ggml == nullptr) {
throw std::runtime_error("ModuleBuildContext.ggml is null");
}
core::validate_rank_between(input, 1, core::kMaxTensorRank, "input");
const auto contiguous = core::ensure_backend_addressable_layout(ctx, input);
auto shifted = core::wrap_tensor(
ggml_scale_bias(ctx.ggml, contiguous.tensor, 1.0F, -offset),
input.shape,
GGML_TYPE_F32);
auto activated = core::wrap_tensor(ggml_softplus(ctx.ggml, shifted.tensor), input.shape, GGML_TYPE_F32);
auto residual = core::wrap_tensor(ggml_scale(ctx.ggml, contiguous.tensor, -0.08F), input.shape, GGML_TYPE_F32);
auto summed = core::wrap_tensor(ggml_add(ctx.ggml, activated.tensor, residual.tensor), input.shape, GGML_TYPE_F32);
return core::wrap_tensor(ggml_scale_bias(ctx.ggml, summed.tensor, 1.0F, constant), input.shape, GGML_TYPE_F32);
}
} // namespace
const core::ModuleSchema & ReluModule::schema() const noexcept {
return static_schema();
}
core::TensorValue ReluModule::build(core::ModuleBuildContext & ctx, const core::TensorValue & input) const {
return build_unary(ctx, input, ggml_relu);
}
const core::ModuleSchema & ReluModule::static_schema() noexcept {
return kReluSchema;
}
LeakyReluModule::LeakyReluModule(LeakyReluConfig config) : config_(config) {
}
const LeakyReluConfig & LeakyReluModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & LeakyReluModule::schema() const noexcept {
return static_schema();
}
core::TensorValue LeakyReluModule::build(core::ModuleBuildContext & ctx, const core::TensorValue & input) const {
if (ctx.ggml == nullptr) {
throw std::runtime_error("ModuleBuildContext.ggml is null");
}
core::validate_rank_between(input, 1, core::kMaxTensorRank, "input");
const auto contiguous = core::ensure_backend_addressable_layout(ctx, input);
return core::wrap_tensor(
ggml_leaky_relu(ctx.ggml, contiguous.tensor, config_.negative_slope, false),
input.shape,
GGML_TYPE_F32);
}
const core::ModuleSchema & LeakyReluModule::static_schema() noexcept {
return kLeakyReluSchema;
}
const core::ModuleSchema & SigmoidModule::schema() const noexcept {
return static_schema();
}
core::TensorValue SigmoidModule::build(core::ModuleBuildContext & ctx, const core::TensorValue & input) const {
return build_unary(ctx, input, ggml_sigmoid);
}
const core::ModuleSchema & SigmoidModule::static_schema() noexcept {
return kSigmoidSchema;
}
const core::ModuleSchema & TanhModule::schema() const noexcept {
return static_schema();
}
core::TensorValue TanhModule::build(core::ModuleBuildContext & ctx, const core::TensorValue & input) const {
return build_unary(ctx, input, ggml_tanh);
}
const core::ModuleSchema & TanhModule::static_schema() noexcept {
return kTanhSchema;
}
const core::ModuleSchema & SqrtModule::schema() const noexcept {
return static_schema();
}
core::TensorValue SqrtModule::build(core::ModuleBuildContext & ctx, const core::TensorValue & input) const {
return build_unary(ctx, input, ggml_sqrt);
}
const core::ModuleSchema & SqrtModule::static_schema() noexcept {
return kSqrtSchema;
}
GeluModule::GeluModule(GeluConfig config) : config_(config) {
}
const GeluConfig & GeluModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & GeluModule::schema() const noexcept {
return static_schema();
}
core::TensorValue GeluModule::build(core::ModuleBuildContext & ctx, const core::TensorValue & input) const {
switch (config_.approximation) {
case GeluApproximation::ExactErf:
return build_unary(ctx, input, ggml_gelu_erf);
case GeluApproximation::Tanh:
return build_unary(ctx, input, ggml_gelu);
case GeluApproximation::Quick:
return build_unary(ctx, input, ggml_gelu_quick);
default:
throw std::runtime_error("Unsupported GELU approximation mode");
}
}
const core::ModuleSchema & GeluModule::static_schema() noexcept {
return kGeluSchema;
}
const core::ModuleSchema & SiluModule::schema() const noexcept {
return static_schema();
}
core::TensorValue SiluModule::build(core::ModuleBuildContext & ctx, const core::TensorValue & input) const {
return build_unary(ctx, input, ggml_silu);
}
const core::ModuleSchema & SiluModule::static_schema() noexcept {
return kSiluSchema;
}
const core::ModuleSchema & SeluModule::schema() const noexcept {
return static_schema();
}
core::TensorValue SeluModule::build(core::ModuleBuildContext & ctx, const core::TensorValue & input) const {
if (ctx.ggml == nullptr) {
throw std::runtime_error("ModuleBuildContext.ggml is null");
}
core::validate_rank_between(input, 1, core::kMaxTensorRank, "input");
constexpr float kAlpha = 1.6732632423543772848170429916717F;
constexpr float kScale = 1.0507009873554804934193349852946F;
const auto contiguous = core::ensure_backend_addressable_layout(ctx, input);
auto positive = ggml_relu(ctx.ggml, contiguous.tensor);
auto negative = ggml_scale(ctx.ggml, ggml_relu(ctx.ggml, ggml_scale(ctx.ggml, contiguous.tensor, -1.0F)), -1.0F);
negative = ggml_scale(ctx.ggml, ggml_expm1(ctx.ggml, negative), kAlpha);
return core::wrap_tensor(
ggml_scale(ctx.ggml, ggml_add(ctx.ggml, positive, negative), kScale),
input.shape,
GGML_TYPE_F32);
}
const core::ModuleSchema & SeluModule::static_schema() noexcept {
return kSeluSchema;
}
const core::ModuleSchema & SwooshLModule::schema() const noexcept {
return static_schema();
}
core::TensorValue SwooshLModule::build(core::ModuleBuildContext & ctx, const core::TensorValue & input) const {
return build_swoosh(ctx, input, 4.0F, -0.035F);
}
const core::ModuleSchema & SwooshLModule::static_schema() noexcept {
return kSwooshLSchema;
}
const core::ModuleSchema & SwooshRModule::schema() const noexcept {
return static_schema();
}
core::TensorValue SwooshRModule::build(core::ModuleBuildContext & ctx, const core::TensorValue & input) const {
return build_swoosh(ctx, input, 1.0F, -0.313261687F);
}
const core::ModuleSchema & SwooshRModule::static_schema() noexcept {
return kSwooshRSchema;
}
const core::ModuleSchema & EluModule::schema() const noexcept {
return static_schema();
}
core::TensorValue EluModule::build(core::ModuleBuildContext & ctx, const core::TensorValue & input) const {
return build_unary(ctx, input, ggml_elu);
}
const core::ModuleSchema & EluModule::static_schema() noexcept {
return kEluSchema;
}
const core::ModuleSchema & SoftmaxModule::schema() const noexcept {
return static_schema();
}
core::TensorValue SoftmaxModule::build(core::ModuleBuildContext & ctx, const core::TensorValue & input) const {
return build_unary(ctx, input, ggml_soft_max);
}
const core::ModuleSchema & SoftmaxModule::static_schema() noexcept {
return kSoftmaxSchema;
}
GLUModule::GLUModule(GLUConfig config) : config_(config) {}
const core::ModuleSchema & GLUModule::schema() const noexcept {
return static_schema();
}
core::TensorValue GLUModule::build(core::ModuleBuildContext & ctx, const core::TensorValue & input) const {
if (ctx.ggml == nullptr) {
throw std::runtime_error("ModuleBuildContext.ggml is null");
}
core::validate_rank_between(input, 1, core::kMaxTensorRank, "input");
if (input.shape.last_dim() % 2 != 0) {
throw std::runtime_error("GLU input last dimension must be even");
}
const auto contiguous = core::ensure_backend_addressable_layout(ctx, input);
const auto flat = core::reshape_tensor(
ctx,
contiguous,
core::TensorShape::from_dims({contiguous.shape.num_elements() / contiguous.shape.last_dim(), contiguous.shape.last_dim()}));
const int64_t hidden = flat.shape.last_dim() / 2;
auto lhs = core::wrap_tensor(
ggml_view_2d(ctx.ggml, flat.tensor, hidden, flat.shape.dims[0], flat.tensor->nb[1], 0),
core::TensorShape::from_dims({flat.shape.dims[0], hidden}),
GGML_TYPE_F32);
auto rhs = core::wrap_tensor(
ggml_view_2d(ctx.ggml, flat.tensor, hidden, flat.shape.dims[0], flat.tensor->nb[1], hidden * sizeof(float)),
core::TensorShape::from_dims({flat.shape.dims[0], hidden}),
GGML_TYPE_F32);
if (config_.contiguous_gate) {
rhs = core::wrap_tensor(ggml_cont(ctx.ggml, rhs.tensor), rhs.shape, GGML_TYPE_F32);
}
rhs = core::wrap_tensor(ggml_sigmoid(ctx.ggml, rhs.tensor), rhs.shape, GGML_TYPE_F32);
auto output = core::wrap_tensor(ggml_mul(ctx.ggml, lhs.tensor, rhs.tensor), lhs.shape, GGML_TYPE_F32);
auto output_shape = input.shape;
output_shape.dims[output_shape.rank - 1] = hidden;
return core::reshape_tensor(ctx, output, output_shape);
}
const core::ModuleSchema & GLUModule::static_schema() noexcept {
return kGLUSchema;
}
Snake1dModule::Snake1dModule(Snake1dConfig config) : config_(config) {
if (config_.hidden_size <= 0) {
throw std::runtime_error("Snake1dConfig.hidden_size must be positive");
}
}
const Snake1dConfig & Snake1dModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & Snake1dModule::schema() const noexcept {
return static_schema();
}
core::TensorValue Snake1dModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
const Snake1dWeights & weights) const {
if (ctx.ggml == nullptr) {
throw std::runtime_error("ModuleBuildContext.ggml is null");
}
core::validate_rank_between(input, 2, core::kMaxTensorRank, "input");
if (input.shape.dims[input.shape.rank - 2] != config_.hidden_size) {
throw std::runtime_error("Snake1d input hidden dimension mismatch");
}
const auto contiguous = core::ensure_backend_addressable_layout(ctx, input);
const auto input_f32 = ensure_f32(ctx, contiguous);
core::TensorValue alpha_broadcast = {};
if (same_shape(weights.alpha.shape, input.shape)) {
alpha_broadcast = ensure_f32(ctx, weights.alpha);
} else {
core::validate_shape(weights.alpha, core::TensorShape::from_dims({config_.hidden_size}), "alpha");
const auto alpha_shape = make_snake_alpha_shape(input.shape, config_.hidden_size);
alpha_broadcast = core::reshape_tensor(ctx, ensure_f32(ctx, weights.alpha), alpha_shape);
}
const auto ax = core::wrap_tensor(ggml_mul(ctx.ggml, input_f32.tensor, alpha_broadcast.tensor), input_f32.shape, GGML_TYPE_F32);
const auto s = core::wrap_tensor(ggml_sin(ctx.ggml, ax.tensor), input_f32.shape, GGML_TYPE_F32);
const auto s2 = core::wrap_tensor(ggml_mul(ctx.ggml, s.tensor, s.tensor), input_f32.shape, GGML_TYPE_F32);
const auto frac = core::wrap_tensor(ggml_div(ctx.ggml, s2.tensor, alpha_broadcast.tensor), input_f32.shape, GGML_TYPE_F32);
return core::wrap_tensor(ggml_add(ctx.ggml, input_f32.tensor, frac.tensor), input_f32.shape, GGML_TYPE_F32);
}
const core::ModuleSchema & Snake1dModule::static_schema() noexcept {
return kSnake1dSchema;
}
SnakeBeta1dModule::SnakeBeta1dModule(SnakeBeta1dConfig config) : config_(config) {
if (config_.hidden_size <= 0) {
throw std::runtime_error("SnakeBeta1dConfig.hidden_size must be positive");
}
}
const SnakeBeta1dConfig & SnakeBeta1dModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & SnakeBeta1dModule::schema() const noexcept {
return static_schema();
}
core::TensorValue SnakeBeta1dModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
const SnakeBeta1dWeights & weights) const {
if (ctx.ggml == nullptr) {
throw std::runtime_error("ModuleBuildContext.ggml is null");
}
core::validate_rank_between(input, 2, core::kMaxTensorRank, "input");
if (input.shape.dims[input.shape.rank - 2] != config_.hidden_size) {
throw std::runtime_error("SnakeBeta1d input hidden dimension mismatch");
}
core::validate_shape(weights.alpha, core::TensorShape::from_dims({config_.hidden_size}), "alpha");
core::validate_shape(weights.beta, core::TensorShape::from_dims({config_.hidden_size}), "beta");
const auto contiguous = core::ensure_backend_addressable_layout(ctx, input);
const auto input_f32 = ensure_f32(ctx, contiguous);
const auto channel_shape = make_snake_alpha_shape(input.shape, config_.hidden_size);
auto alpha = core::reshape_tensor(ctx, ensure_f32(ctx, weights.alpha), channel_shape);
auto beta = core::reshape_tensor(ctx, ensure_f32(ctx, weights.beta), channel_shape);
if (config_.logscale) {
alpha = core::wrap_tensor(ggml_exp(ctx.ggml, alpha.tensor), alpha.shape, GGML_TYPE_F32);
beta = core::wrap_tensor(ggml_exp(ctx.ggml, beta.tensor), beta.shape, GGML_TYPE_F32);
}
const auto ax = core::wrap_tensor(ggml_mul(ctx.ggml, input_f32.tensor, alpha.tensor), input_f32.shape, GGML_TYPE_F32);
const auto s = core::wrap_tensor(ggml_sin(ctx.ggml, ax.tensor), input_f32.shape, GGML_TYPE_F32);
const auto s2 = core::wrap_tensor(ggml_mul(ctx.ggml, s.tensor, s.tensor), input_f32.shape, GGML_TYPE_F32);
const auto denom = core::wrap_tensor(ggml_scale_bias(ctx.ggml, beta.tensor, 1.0F, 1.0e-9F), beta.shape, GGML_TYPE_F32);
const auto periodic = core::wrap_tensor(ggml_div(ctx.ggml, s2.tensor, denom.tensor), input_f32.shape, GGML_TYPE_F32);
return core::wrap_tensor(ggml_add(ctx.ggml, input_f32.tensor, periodic.tensor), input_f32.shape, GGML_TYPE_F32);
}
const core::ModuleSchema & SnakeBeta1dModule::static_schema() noexcept {
return kSnakeBeta1dSchema;
}
AliasFreeActivationModule::AliasFreeActivationModule(AliasFreeActivationConfig config) : config_(config) {
if (config_.channels <= 0) {
throw std::runtime_error("AliasFreeActivationConfig.channels must be positive");
}
if (config_.kernel_size <= 0) {
throw std::runtime_error("AliasFreeActivationConfig.kernel_size must be positive");
}
if (config_.upsample_ratio <= 0 || config_.kernel_size % config_.upsample_ratio != 0) {
throw std::runtime_error("AliasFreeActivationConfig.upsample_ratio must divide kernel_size");
}
}
const AliasFreeActivationConfig & AliasFreeActivationModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & AliasFreeActivationModule::schema() const noexcept {
return static_schema();
}
core::TensorValue AliasFreeActivationModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
const AliasFreeActivationWeights & weights) const {
if (ctx.ggml == nullptr) {
throw std::runtime_error("ModuleBuildContext.ggml is null");
}
core::validate_rank_between(input, 3, 3, "input");
if (input.shape.dims[1] != config_.channels) {
throw std::runtime_error("AliasFreeActivation input channel mismatch");
}
const int64_t phase_kernel = config_.kernel_size / config_.upsample_ratio;
core::validate_shape(weights.alpha, core::TensorShape::from_dims({config_.channels}), "alpha");
core::validate_shape(weights.inv_beta, core::TensorShape::from_dims({config_.channels}), "inv_beta");
core::validate_shape(weights.up_filter_even, core::TensorShape::from_dims({config_.channels, 1, 1, phase_kernel}), "up_filter_even");
core::validate_shape(weights.up_filter_odd, core::TensorShape::from_dims({config_.channels, 1, 1, phase_kernel}), "up_filter_odd");
core::validate_shape(weights.down_filter, core::TensorShape::from_dims({config_.channels, 1, 1, config_.kernel_size}), "down_filter");
auto input_ct = ggml_reshape_2d(
ctx.ggml,
core::ensure_backend_addressable_layout(ctx, input).tensor,
input.shape.dims[2],
input.shape.dims[1]);
ggml_tensor * up = depthwise_conv_transpose_causal_ct(
ctx,
input_ct,
weights.up_filter_even,
weights.up_filter_odd,
phase_kernel,
config_.upsample_ratio);
up = ggml_scale(ctx.ggml, up, static_cast<float>(config_.upsample_ratio));
ggml_tensor * alpha = ggml_reshape_2d(ctx.ggml, weights.alpha.tensor, 1, config_.channels);
ggml_tensor * inv_beta = ggml_reshape_2d(ctx.ggml, weights.inv_beta.tensor, 1, config_.channels);
ggml_tensor * periodic = nullptr;
switch (config_.kind) {
case AliasFreeActivationKind::SnakeBeta:
periodic = ggml_sqr(ctx.ggml, ggml_sin(ctx.ggml, ggml_mul(ctx.ggml, up, alpha)));
break;
default:
throw std::runtime_error("Unsupported alias-free activation kind");
}
ggml_tensor * activated = ggml_add(ctx.ggml, up, ggml_mul(ctx.ggml, periodic, inv_beta));
ggml_tensor * output_ct = depthwise_conv_causal_ct(
ctx,
activated,
weights.down_filter,
config_.kernel_size,
config_.upsample_ratio);
return core::wrap_tensor(
ggml_reshape_3d(
ctx.ggml,
core::has_backend_addressable_layout(output_ct) ? output_ct : ggml_cont(ctx.ggml, output_ct),
output_ct->ne[0],
output_ct->ne[1],
1),
core::TensorShape::from_dims({1, output_ct->ne[1], output_ct->ne[0]}),
GGML_TYPE_F32);
}
const core::ModuleSchema & AliasFreeActivationModule::static_schema() noexcept {
return kAliasFreeActivationSchema;
}
} // namespace engine::modules