Repository navigation
Expand file tree
/
Copy pathconv_modules.cpp
More file actions
617 lines (564 loc) · 24.1 KB
/
Copy pathconv_modules.cpp
File metadata and controls
617 lines (564 loc) · 24.1 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
#include "engine/framework/modules/conv_modules.h"
#include "tensor_layout_utils.h"
#include "engine/framework/core/backend.h"
#include "engine/framework/modules/structural_modules.h"
#include <stdexcept>
#include <string>
namespace engine::modules {
namespace {
const core::ModulePortSpec kConvInputs[] = {
{"input", core::PortKind::Activation, false},
{"weight", core::PortKind::Parameter, false},
{"bias", core::PortKind::Parameter, true},
};
const core::ModulePortSpec kSingleOutput[] = {
{"output", core::PortKind::Activation, false},
};
const core::ModuleSchema kConv1dSchema = {
"Conv1d",
"nn.conv",
kConvInputs,
3,
kSingleOutput,
1,
"Applies a 1D convolution to channel-first inputs [batch, channels, frames].",
};
const core::ModuleSchema kConv2dSchema = {
"Conv2d",
"nn.conv",
kConvInputs,
3,
kSingleOutput,
1,
"Applies a 2D convolution to channel-first inputs [batch, channels, height, width].",
};
const core::ModuleSchema kCausalConv2dSchema = {
"CausalConv2d",
"nn.conv",
kConvInputs,
3,
kSingleOutput,
1,
"Applies explicit asymmetric 2D padding followed by Conv2d.",
};
const core::ModuleSchema kDepthwiseConv2dSchema = {
"DepthwiseConv2d",
"nn.conv",
kConvInputs,
3,
kSingleOutput,
1,
"Applies a depthwise 2D convolution to channel-first inputs [batch, channels, height, width].",
};
const core::ModuleSchema kConvTranspose1dSchema = {
"ConvTranspose1d",
"nn.conv",
kConvInputs,
3,
kSingleOutput,
1,
"Applies a 1D transposed convolution to channel-first inputs [batch, channels, frames].",
};
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);
}
core::TensorValue regular_conv_weight(
core::ModuleBuildContext & ctx,
const core::TensorValue & weight,
const char * module_name) {
const auto contiguous = tensor_layout::ensure_contiguous_layout_if_needed(ctx, weight);
if (contiguous.type == GGML_TYPE_F32 || contiguous.type == GGML_TYPE_F16) {
return contiguous;
}
if (contiguous.type == GGML_TYPE_BF16) {
return core::wrap_tensor(ggml_cast(ctx.ggml, contiguous.tensor, GGML_TYPE_F16), contiguous.shape, GGML_TYPE_F16);
}
if (ggml_is_quantized(contiguous.type)) {
return core::wrap_tensor(ggml_cast(ctx.ggml, contiguous.tensor, GGML_TYPE_F32), contiguous.shape, GGML_TYPE_F32);
}
throw std::runtime_error(
std::string(module_name) + " does not support weight type with the current ggml conv path: " +
ggml_type_name(contiguous.type));
}
core::TensorValue conv_transpose1d_weight(
core::ModuleBuildContext & ctx,
const core::TensorValue & weight) {
const auto contiguous = tensor_layout::ensure_contiguous_layout_if_needed(ctx, weight);
if (contiguous.type == GGML_TYPE_F32) {
return contiguous;
}
if (contiguous.type == GGML_TYPE_F16 && core::uses_host_graph_plan(ctx.backend_type)) {
return contiguous;
}
if (contiguous.type == GGML_TYPE_BF16 && core::uses_host_graph_plan(ctx.backend_type)) {
return core::wrap_tensor(ggml_cast(ctx.ggml, contiguous.tensor, GGML_TYPE_F16), contiguous.shape, GGML_TYPE_F16);
}
if (contiguous.type == GGML_TYPE_F16 || contiguous.type == GGML_TYPE_BF16) {
return core::wrap_tensor(ggml_cast(ctx.ggml, contiguous.tensor, GGML_TYPE_F32), contiguous.shape, GGML_TYPE_F32);
}
if (ggml_is_quantized(contiguous.type)) {
return core::wrap_tensor(ggml_cast(ctx.ggml, contiguous.tensor, GGML_TYPE_F32), contiguous.shape, GGML_TYPE_F32);
}
throw std::runtime_error(
std::string("ConvTranspose1dModule does not support weight type with the current ggml conv-transpose path: ") +
ggml_type_name(contiguous.type));
}
core::TensorValue depthwise_conv2d_weight(
core::ModuleBuildContext & ctx,
const core::TensorValue & weight) {
const auto contiguous = tensor_layout::ensure_contiguous_layout_if_needed(ctx, weight);
if (contiguous.type == GGML_TYPE_F32) {
return contiguous;
}
if (contiguous.type == GGML_TYPE_F16 || contiguous.type == GGML_TYPE_BF16 || ggml_is_quantized(contiguous.type)) {
return core::wrap_tensor(ggml_cast(ctx.ggml, contiguous.tensor, GGML_TYPE_F32), contiguous.shape, GGML_TYPE_F32);
}
throw std::runtime_error(
std::string("DepthwiseConv2dModule does not support weight type with the current ggml depthwise conv path: ") +
ggml_type_name(contiguous.type));
}
int64_t conv1d_output_frames(const Conv1dConfig & config, int64_t input_frames) {
return (input_frames + 2 * config.padding - config.dilation * (config.kernel_size - 1) - 1) / config.stride + 1;
}
int64_t conv2d_output_dim(int64_t input, int kernel, int stride, int padding, int dilation) {
return (input + 2 * padding - dilation * (kernel - 1) - 1) / stride + 1;
}
int64_t conv_transpose1d_output_frames(const ConvTranspose1dConfig & config, int64_t input_frames) {
return (input_frames - 1) * config.stride - 2 * config.padding + config.dilation * (config.kernel_size - 1) + 1;
}
core::TensorValue add_bias_if_needed(
core::ModuleBuildContext & ctx,
const core::TensorValue & output,
int64_t out_channels,
const std::optional<core::TensorValue> & bias) {
if (!bias.has_value()) {
return output;
}
core::validate_shape(*bias, core::TensorShape::from_dims({out_channels}), "bias");
const auto output_contiguous = tensor_layout::ensure_contiguous_layout_if_needed(ctx, output);
const auto bias_view = core::reshape_tensor(ctx, *bias, core::TensorShape::from_dims({1, out_channels, 1}));
const auto bias_expanded = core::wrap_tensor(ggml_repeat(ctx.ggml, bias_view.tensor, output_contiguous.tensor), output.shape, GGML_TYPE_F32);
return core::wrap_tensor(ggml_add(ctx.ggml, output_contiguous.tensor, bias_expanded.tensor), output.shape, GGML_TYPE_F32);
}
core::TensorValue add_4d_channel_bias_if_needed(
core::ModuleBuildContext & ctx,
const core::TensorValue & output,
int64_t channels,
const std::optional<core::TensorValue> & bias) {
if (!bias.has_value()) {
return output;
}
core::validate_shape(*bias, core::TensorShape::from_dims({channels}), "bias");
const auto output_contiguous = tensor_layout::ensure_contiguous_layout_if_needed(ctx, output);
const auto bias_view = core::reshape_tensor(ctx, *bias, core::TensorShape::from_dims({1, channels, 1, 1}));
const auto bias_expanded =
core::wrap_tensor(ggml_repeat(ctx.ggml, bias_view.tensor, output_contiguous.tensor), output.shape, GGML_TYPE_F32);
return core::wrap_tensor(ggml_add(ctx.ggml, output_contiguous.tensor, bias_expanded.tensor), output.shape, GGML_TYPE_F32);
}
core::TensorValue build_conv_transpose1d_cuda_col2im_path(
core::ModuleBuildContext & ctx,
const ConvTranspose1dConfig & config,
const core::TensorValue & input,
const ConvTranspose1dWeights & weights,
const core::TensorShape & output_shape) {
if (!is_conv_transpose1d_col2im_fast_path_eligible(ctx, config)) {
throw std::runtime_error("ConvTranspose1dModule CUDA col2im path was requested for an ineligible config");
}
const auto input_contiguous = core::ensure_backend_addressable_layout(ctx, input);
auto weight_contiguous = tensor_layout::ensure_contiguous_layout_if_needed(ctx, weights.weight);
if (weight_contiguous.type != GGML_TYPE_F32) {
weight_contiguous = core::wrap_tensor(ggml_cast(ctx.ggml, weight_contiguous.tensor, GGML_TYPE_F32), weight_contiguous.shape, GGML_TYPE_F32);
}
auto * weight_perm = ggml_reshape_2d(
ctx.ggml,
ggml_cont(ctx.ggml, ggml_permute(ctx.ggml, weight_contiguous.tensor, 1, 2, 0, 3)),
config.in_channels,
config.kernel_size * config.out_channels);
ggml_tensor * bias_matrix = nullptr;
if (config.use_bias) {
if (!weights.bias.has_value()) {
throw std::runtime_error("ConvTranspose1dModule col2im fast path requires bias when use_bias is true");
}
core::validate_shape(*weights.bias, core::TensorShape::from_dims({config.out_channels}), "bias");
bias_matrix = ggml_reshape_2d(ctx.ggml, weights.bias->tensor, 1, config.out_channels);
}
core::TensorValue output;
for (int64_t batch_index = 0; batch_index < input.shape.dims[0]; ++batch_index) {
auto * batch_input = ggml_view_2d(
ctx.ggml,
input_contiguous.tensor,
input_contiguous.tensor->ne[0],
input_contiguous.tensor->ne[1],
input_contiguous.tensor->nb[1],
static_cast<size_t>(batch_index) * input_contiguous.tensor->nb[2]);
auto * transposed_input = ggml_cont(ctx.ggml, ggml_transpose(ctx.ggml, batch_input));
auto * columns = ggml_mul_mat(ctx.ggml, weight_perm, transposed_input);
auto * batch_output = ggml_col2im_1d(
ctx.ggml,
columns,
config.stride,
static_cast<int>(config.out_channels),
config.padding);
if (bias_matrix != nullptr) {
batch_output = ggml_add(ctx.ggml, batch_output, bias_matrix);
}
auto batch_value = core::wrap_tensor(
ggml_reshape_3d(ctx.ggml, batch_output, batch_output->ne[0], batch_output->ne[1], 1),
core::TensorShape::from_dims({1, config.out_channels, batch_output->ne[0]}),
GGML_TYPE_F32);
if (batch_value.shape.dims[2] != output_shape.dims[2]) {
throw std::runtime_error("ConvTranspose1dModule col2im fast path produced unexpected frame count");
}
output = output.valid() ? ConcatModule({0}).build(ctx, output, batch_value) : batch_value;
}
return output;
}
core::TensorValue view_batch_matrix(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
int64_t batch_index,
int64_t channels,
int64_t frames) {
auto * view = ggml_view_2d(
ctx.ggml,
input.tensor,
frames,
channels,
input.tensor->nb[1],
static_cast<size_t>(batch_index) * input.tensor->nb[2]);
return core::wrap_tensor(view, core::TensorShape::from_dims({channels, frames}), input.type);
}
} // namespace
bool is_conv_transpose1d_col2im_fast_path_eligible(
const core::ModuleBuildContext & ctx,
const ConvTranspose1dConfig & config) noexcept {
return ctx.backend_type == core::BackendType::Cuda && config.dilation == 1;
}
Conv1dModule::Conv1dModule(Conv1dConfig config) : config_(config) {
if (config_.in_channels <= 0 || config_.out_channels <= 0 || config_.kernel_size <= 0) {
throw std::runtime_error("Conv1dConfig dimensions must be positive");
}
if (config_.stride <= 0 || config_.dilation <= 0) {
throw std::runtime_error("Conv1d stride and dilation must be positive");
}
}
const Conv1dConfig & Conv1dModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & Conv1dModule::schema() const noexcept {
return static_schema();
}
core::TensorValue Conv1dModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
const Conv1dWeights & weights) const {
if (ctx.ggml == nullptr) {
throw std::runtime_error("ModuleBuildContext.ggml is null");
}
core::validate_rank_between(input, 3, 3, "input");
core::validate_shape(
input,
core::TensorShape::from_dims({input.shape.dims[0], config_.in_channels, input.shape.dims[2]}),
"input");
core::validate_shape(
weights.weight,
core::TensorShape::from_dims({config_.out_channels, config_.in_channels, config_.kernel_size}),
"weight");
const auto output_shape = core::TensorShape::from_dims(
{input.shape.dims[0], config_.out_channels, conv1d_output_frames(config_, input.shape.dims[2])});
const auto input_contiguous = ensure_f32(ctx, tensor_layout::ensure_contiguous_layout_if_needed(ctx, input));
const auto weight_contiguous = regular_conv_weight(ctx, weights.weight, "Conv1dModule");
core::TensorValue output;
if (input.shape.dims[0] == 1) {
output = core::wrap_tensor(
ggml_conv_1d(
ctx.ggml,
weight_contiguous.tensor,
input_contiguous.tensor,
config_.stride,
config_.padding,
config_.dilation),
output_shape,
GGML_TYPE_F32);
} else {
for (int64_t batch_index = 0; batch_index < input.shape.dims[0]; ++batch_index) {
const auto matrix_input = view_batch_matrix(
ctx,
input_contiguous,
batch_index,
config_.in_channels,
input.shape.dims[2]);
auto batch_output = core::wrap_tensor(
ggml_conv_1d(
ctx.ggml,
weight_contiguous.tensor,
matrix_input.tensor,
config_.stride,
config_.padding,
config_.dilation),
core::TensorShape::from_dims({1, config_.out_channels, output_shape.dims[2]}),
GGML_TYPE_F32);
output = output.valid() ? ConcatModule({0}).build(ctx, output, batch_output) : batch_output;
}
}
if (config_.use_bias) {
output = add_bias_if_needed(ctx, output, config_.out_channels, weights.bias);
}
return output;
}
const core::ModuleSchema & Conv1dModule::static_schema() noexcept {
return kConv1dSchema;
}
Conv2dModule::Conv2dModule(Conv2dConfig config) : config_(config) {
if (config_.in_channels <= 0 || config_.out_channels <= 0 || config_.kernel_height <= 0 || config_.kernel_width <= 0) {
throw std::runtime_error("Conv2dConfig dimensions must be positive");
}
if (config_.stride_height <= 0 || config_.stride_width <= 0 || config_.dilation_height <= 0 || config_.dilation_width <= 0) {
throw std::runtime_error("Conv2d stride and dilation must be positive");
}
}
const Conv2dConfig & Conv2dModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & Conv2dModule::schema() const noexcept {
return static_schema();
}
core::TensorValue Conv2dModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
const Conv2dWeights & weights) const {
if (ctx.ggml == nullptr) {
throw std::runtime_error("ModuleBuildContext.ggml is null");
}
core::validate_rank_between(input, 4, 4, "input");
core::validate_shape(
input,
core::TensorShape::from_dims({input.shape.dims[0], config_.in_channels, input.shape.dims[2], input.shape.dims[3]}),
"input");
core::validate_shape(
weights.weight,
core::TensorShape::from_dims({config_.out_channels, config_.in_channels, config_.kernel_height, config_.kernel_width}),
"weight");
const auto output_shape = core::TensorShape::from_dims({
input.shape.dims[0],
config_.out_channels,
conv2d_output_dim(
input.shape.dims[2],
static_cast<int>(config_.kernel_height),
config_.stride_height,
config_.padding_height,
config_.dilation_height),
conv2d_output_dim(
input.shape.dims[3],
static_cast<int>(config_.kernel_width),
config_.stride_width,
config_.padding_width,
config_.dilation_width),
});
const auto input_contiguous = ensure_f32(ctx, tensor_layout::ensure_contiguous_layout_if_needed(ctx, input));
const auto weight_contiguous = regular_conv_weight(ctx, weights.weight, "Conv2dModule");
auto output = core::wrap_tensor(
ggml_conv_2d(
ctx.ggml,
weight_contiguous.tensor,
input_contiguous.tensor,
config_.stride_width,
config_.stride_height,
config_.padding_width,
config_.padding_height,
config_.dilation_width,
config_.dilation_height),
output_shape,
GGML_TYPE_F32);
if (config_.use_bias) {
if (!weights.bias.has_value()) {
throw std::runtime_error("bias is required when Conv2dConfig.use_bias is true");
}
const auto output_contiguous = tensor_layout::ensure_contiguous_layout_if_needed(ctx, output);
core::validate_shape(*weights.bias, core::TensorShape::from_dims({config_.out_channels}), "bias");
const auto bias_view = core::reshape_tensor(ctx, *weights.bias, core::TensorShape::from_dims({1, config_.out_channels, 1, 1}));
const auto bias_expanded =
core::wrap_tensor(ggml_repeat(ctx.ggml, bias_view.tensor, output_contiguous.tensor), output.shape, GGML_TYPE_F32);
output = core::wrap_tensor(ggml_add(ctx.ggml, output_contiguous.tensor, bias_expanded.tensor), output.shape, GGML_TYPE_F32);
}
return output;
}
const core::ModuleSchema & Conv2dModule::static_schema() noexcept {
return kConv2dSchema;
}
CausalConv2dModule::CausalConv2dModule(CausalConv2dConfig config) : config_(config) {
if (config_.pad_left < 0 || config_.pad_right < 0 || config_.pad_top < 0 || config_.pad_bottom < 0) {
throw std::runtime_error("CausalConv2d padding must be non-negative");
}
if (config_.conv.padding_height != 0 || config_.conv.padding_width != 0) {
throw std::runtime_error("CausalConv2d uses explicit Pad2d padding; Conv2dConfig padding must be zero");
}
}
const CausalConv2dConfig & CausalConv2dModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & CausalConv2dModule::schema() const noexcept {
return static_schema();
}
core::TensorValue CausalConv2dModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
const Conv2dWeights & weights) const {
auto padded = Pad2dModule({
config_.pad_left,
config_.pad_right,
config_.pad_top,
config_.pad_bottom,
}).build(ctx, input);
return Conv2dModule(config_.conv).build(ctx, padded, weights);
}
const core::ModuleSchema & CausalConv2dModule::static_schema() noexcept {
return kCausalConv2dSchema;
}
DepthwiseConv2dModule::DepthwiseConv2dModule(DepthwiseConv2dConfig config) : config_(config) {
if (config_.channels <= 0 || config_.kernel_height <= 0 || config_.kernel_width <= 0) {
throw std::runtime_error("DepthwiseConv2dConfig dimensions must be positive");
}
if (config_.stride_height <= 0 || config_.stride_width <= 0 || config_.dilation_height <= 0 || config_.dilation_width <= 0) {
throw std::runtime_error("DepthwiseConv2d stride and dilation must be positive");
}
}
const DepthwiseConv2dConfig & DepthwiseConv2dModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & DepthwiseConv2dModule::schema() const noexcept {
return static_schema();
}
core::TensorValue DepthwiseConv2dModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
const Conv2dWeights & weights) const {
if (ctx.ggml == nullptr) {
throw std::runtime_error("ModuleBuildContext.ggml is null");
}
core::validate_rank_between(input, 4, 4, "input");
core::validate_shape(
input,
core::TensorShape::from_dims({input.shape.dims[0], config_.channels, input.shape.dims[2], input.shape.dims[3]}),
"input");
core::validate_shape(
weights.weight,
core::TensorShape::from_dims({config_.channels, 1, config_.kernel_height, config_.kernel_width}),
"weight");
const auto output_shape = core::TensorShape::from_dims({
input.shape.dims[0],
config_.channels,
conv2d_output_dim(
input.shape.dims[2],
static_cast<int>(config_.kernel_height),
config_.stride_height,
config_.padding_height,
config_.dilation_height),
conv2d_output_dim(
input.shape.dims[3],
static_cast<int>(config_.kernel_width),
config_.stride_width,
config_.padding_width,
config_.dilation_width),
});
const auto input_contiguous = ensure_f32(ctx, tensor_layout::ensure_contiguous_layout_if_needed(ctx, input));
const auto weight_contiguous = depthwise_conv2d_weight(ctx, weights.weight);
auto output = core::wrap_tensor(
ggml_conv_2d_dw_direct(
ctx.ggml,
weight_contiguous.tensor,
input_contiguous.tensor,
config_.stride_width,
config_.stride_height,
config_.padding_width,
config_.padding_height,
config_.dilation_width,
config_.dilation_height),
output_shape,
GGML_TYPE_F32);
if (config_.use_bias) {
if (!weights.bias.has_value()) {
throw std::runtime_error("bias is required when DepthwiseConv2dConfig.use_bias is true");
}
output = add_4d_channel_bias_if_needed(ctx, output, config_.channels, weights.bias);
}
return output;
}
const core::ModuleSchema & DepthwiseConv2dModule::static_schema() noexcept {
return kDepthwiseConv2dSchema;
}
ConvTranspose1dModule::ConvTranspose1dModule(ConvTranspose1dConfig config) : config_(config) {
if (config_.in_channels <= 0 || config_.out_channels <= 0 || config_.kernel_size <= 0) {
throw std::runtime_error("ConvTranspose1dConfig dimensions must be positive");
}
if (config_.stride <= 0 || config_.dilation <= 0) {
throw std::runtime_error("ConvTranspose1d stride and dilation must be positive");
}
}
const ConvTranspose1dConfig & ConvTranspose1dModule::config() const noexcept {
return config_;
}
const core::ModuleSchema & ConvTranspose1dModule::schema() const noexcept {
return static_schema();
}
core::TensorValue ConvTranspose1dModule::build(
core::ModuleBuildContext & ctx,
const core::TensorValue & input,
const ConvTranspose1dWeights & weights) const {
if (ctx.ggml == nullptr) {
throw std::runtime_error("ModuleBuildContext.ggml is null");
}
core::validate_rank_between(input, 3, 3, "input");
core::validate_shape(
input,
core::TensorShape::from_dims({input.shape.dims[0], config_.in_channels, input.shape.dims[2]}),
"input");
core::validate_shape(
weights.weight,
core::TensorShape::from_dims({config_.in_channels, config_.out_channels, config_.kernel_size}),
"weight");
const auto output_shape = core::TensorShape::from_dims(
{input.shape.dims[0], config_.out_channels, conv_transpose1d_output_frames(config_, input.shape.dims[2])});
if (is_conv_transpose1d_col2im_fast_path_eligible(ctx, config_)) {
return build_conv_transpose1d_cuda_col2im_path(ctx, config_, input, weights, output_shape);
}
const auto input_contiguous = ensure_f32(ctx, tensor_layout::ensure_contiguous_layout_if_needed(ctx, input));
const auto weight_contiguous = conv_transpose1d_weight(ctx, weights.weight);
core::TensorValue output;
for (int64_t batch_index = 0; batch_index < input.shape.dims[0]; ++batch_index) {
const auto matrix_input = view_batch_matrix(
ctx,
input_contiguous,
batch_index,
config_.in_channels,
input.shape.dims[2]);
auto batch_output = core::wrap_tensor(
ggml_conv_transpose_1d(
ctx.ggml,
weight_contiguous.tensor,
matrix_input.tensor,
config_.stride,
config_.padding,
config_.dilation),
core::TensorShape::from_dims({1, config_.out_channels, output_shape.dims[2]}),
GGML_TYPE_F32);
if (!output.valid()) {
output = batch_output;
} else {
output = ConcatModule({0}).build(ctx, output, batch_output);
}
}
if (config_.use_bias) {
output = add_bias_if_needed(ctx, output, config_.out_channels, weights.bias);
}
return output;
}
const core::ModuleSchema & ConvTranspose1dModule::static_schema() noexcept {
return kConvTranspose1dSchema;
}
} // namespace engine::modules