| 584 | } |
| 585 | |
| 586 | core::TensorValue global_encoder( |
| 587 | core::ModuleBuildContext & ctx, |
| 588 | const core::TensorValue & input_btc, |
| 589 | const MioCodecGlobalEncoderWeights & weights) { |
| 590 | auto x = engine::modules::TransposeModule({{0, 2, 1, 3}, input_btc.shape.rank}).build(ctx, input_btc); |
| 591 | x = engine::modules::Conv1dModule(weights.embedding.config).build(ctx, x, weights.embedding.weights); |
| 592 | x = engine::modules::TransposeModule({{0, 2, 1, 3}, x.shape.rank}).build(ctx, x); |
| 593 | x = engine::modules::LayerNormModule({384, 1.0e-6F, true, true}).build(ctx, x, weights.embedding_norm); |
| 594 | x = engine::modules::TransposeModule({{0, 2, 1, 3}, x.shape.rank}).build(ctx, x); |
| 595 | for (const auto & block : weights.blocks) { |
| 596 | x = convnext_block(ctx, x, block); |
| 597 | } |
| 598 | auto features_btc = engine::modules::TransposeModule({{0, 2, 1, 3}, x.shape.rank}).build(ctx, x); |
| 599 | features_btc = engine::modules::LayerNormModule({384, 1.0e-6F, true, true}).build(ctx, features_btc, weights.final_norm); |
| 600 | auto features_bct = engine::modules::TransposeModule({{0, 2, 1, 3}, features_btc.shape.rank}).build(ctx, features_btc); |
| 601 | auto alpha = engine::modules::Conv1dModule(weights.attention_conv1.config).build(ctx, features_bct, weights.attention_conv1.weights); |
| 602 | alpha = core::wrap_tensor(ggml_tanh(ctx.ggml, alpha.tensor), alpha.shape, GGML_TYPE_F32); |
| 603 | alpha = engine::modules::Conv1dModule(weights.attention_conv2.config).build(ctx, alpha, weights.attention_conv2.weights); |
| 604 | alpha = core::wrap_tensor(ggml_soft_max(ctx.ggml, core::ensure_backend_addressable_layout(ctx, alpha).tensor), alpha.shape, GGML_TYPE_F32); |
| 605 | auto weighted = core::wrap_tensor(ggml_mul(ctx.ggml, alpha.tensor, features_bct.tensor), features_bct.shape, GGML_TYPE_F32); |
| 606 | auto mean = engine::modules::ReduceSumModule({2}).build(ctx, weighted); |
| 607 | auto features_contiguous = core::ensure_backend_addressable_layout(ctx, features_bct); |
| 608 | auto squared = core::wrap_tensor(ggml_sqr(ctx.ggml, features_contiguous.tensor), features_contiguous.shape, GGML_TYPE_F32); |
| 609 | auto second = engine::modules::ReduceSumModule({2}).build(ctx, core::wrap_tensor(ggml_mul(ctx.ggml, alpha.tensor, squared.tensor), squared.shape, GGML_TYPE_F32)); |
| 610 | auto mean_sq = core::wrap_tensor(ggml_sqr(ctx.ggml, mean.tensor), mean.shape, GGML_TYPE_F32); |
| 611 | auto var = core::wrap_tensor(ggml_sub(ctx.ggml, second.tensor, mean_sq.tensor), mean.shape, GGML_TYPE_F32); |
| 612 | var = core::wrap_tensor(ggml_clamp(ctx.ggml, var.tensor, 1.0e-4F, 1.0e4F), var.shape, GGML_TYPE_F32); |
| 613 | auto std = core::wrap_tensor(ggml_sqrt(ctx.ggml, var.tensor), var.shape, GGML_TYPE_F32); |
| 614 | auto stats = engine::modules::ConcatModule({1}).build(ctx, mean, std); |
| 615 | stats = core::reshape_tensor(ctx, core::ensure_backend_addressable_layout(ctx, stats), core::TensorShape::from_dims({stats.shape.dims[0], 768})); |
| 616 | auto out = engine::modules::LinearModule({768, 128, weights.pooling_projection.bias.has_value()}).build( |
| 617 | ctx, |
| 618 | stats, |
| 619 | weights.pooling_projection); |
| 620 | return engine::modules::LayerNormModule({128, 1.0e-5F, true, true}).build(ctx, out, weights.pooling_norm); |
| 621 | } |
| 622 | |
| 623 | core::TensorValue dynamic_linear_interpolate_bct( |
| 624 | core::ModuleBuildContext & ctx, |
no test coverage detected