Repository navigation
Expand file tree
/
Copy pathactivation_modules.cpp
More file actions
366 lines (307 loc) · 11 KB
/
Copy pathactivation_modules.cpp
File metadata and controls
366 lines (307 loc) · 11 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
#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 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 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::ModuleSchema kSnake1dSchema = {
"Snake1d",
"nn.activation",
kSnakeInputs,
2,
kActivationOutputs,
1,
"Applies Snake activation over channel-time tensors using per-channel alpha.",
};
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;
}
} // 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;
}
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 & 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;
}
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);
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;
}
} // namespace engine::modules