#pragma once
#include "ggml.h"
namespace transcribe::conformer {
constexpr float kLayerNormEps = 1e-5f;
struct PreEncodeView {
ggml_tensor * conv0_w = nullptr; ggml_tensor * conv0_b = nullptr; ggml_tensor * conv2_w = nullptr; ggml_tensor * conv2_b = nullptr; ggml_tensor * conv3_w = nullptr; ggml_tensor * conv3_b = nullptr; ggml_tensor * conv5_w = nullptr; ggml_tensor * conv5_b = nullptr;
ggml_tensor * conv6_w = nullptr; ggml_tensor * conv6_b = nullptr;
ggml_tensor * out_w = nullptr; ggml_tensor * out_b = nullptr; };
struct BlockView {
ggml_tensor * norm_ff1_w = nullptr; ggml_tensor * norm_ff1_b = nullptr; ggml_tensor * ff1_lin1_w = nullptr; ggml_tensor * ff1_lin1_b = nullptr; ggml_tensor * ff1_lin2_w = nullptr; ggml_tensor * ff1_lin2_b = nullptr;
ggml_tensor * norm_attn_w = nullptr; ggml_tensor * norm_attn_b = nullptr; ggml_tensor * attn_q_w = nullptr; ggml_tensor * attn_q_b = nullptr; ggml_tensor * attn_k_w = nullptr; ggml_tensor * attn_k_b = nullptr; ggml_tensor * attn_v_w = nullptr; ggml_tensor * attn_v_b = nullptr; ggml_tensor * attn_out_w = nullptr; ggml_tensor * attn_out_b = nullptr; ggml_tensor * attn_pos_w = nullptr; ggml_tensor * attn_pos_u = nullptr; ggml_tensor * attn_pos_v = nullptr;
ggml_tensor * norm_conv_w = nullptr; ggml_tensor * norm_conv_b = nullptr; ggml_tensor * conv_pw1_w = nullptr; ggml_tensor * conv_pw1_b = nullptr; ggml_tensor * conv_dw_w = nullptr; ggml_tensor * conv_dw_b = nullptr; ggml_tensor * conv_pw2_w = nullptr; ggml_tensor * conv_pw2_b = nullptr; ggml_tensor * conv_bn_fused_scale = nullptr; ggml_tensor * conv_bn_fused_bias = nullptr; ggml_tensor * conv_ln_w = nullptr; ggml_tensor * conv_ln_b = nullptr;
ggml_tensor * norm_ff2_w = nullptr;
ggml_tensor * norm_ff2_b = nullptr;
ggml_tensor * ff2_lin1_w = nullptr;
ggml_tensor * ff2_lin1_b = nullptr; ggml_tensor * ff2_lin2_w = nullptr;
ggml_tensor * ff2_lin2_b = nullptr;
ggml_tensor * norm_out_w = nullptr;
ggml_tensor * norm_out_b = nullptr;
};
struct ConvPolicy {
bool direct_pw = true;
bool direct_dw_in_block = false;
bool direct_dw_in_pre_encode = false;
bool causal_pre_encode = false;
};
bool detect_direct_pw(const char * backend);
struct BlockParams {
int d_model;
int n_head;
int conv_kernel;
ggml_type kv_type; bool use_flash; ConvPolicy policy;
int att_context_left = -1;
int att_context_right = -1;
enum class AttContextStyle { Regular, ChunkedLimited };
AttContextStyle att_context_style = AttContextStyle::Regular;
ggml_tensor * attn_chunked_mask = nullptr;
ggml_tensor * attn_pad_mask = nullptr;
int conv_context_left = -1;
int conv_context_right = -1;
ggml_tensor * conv_pad_mask = nullptr;
enum class ConvNormType { BatchNorm, LayerNorm };
ConvNormType conv_norm_type = ConvNormType::BatchNorm;
ggml_tensor * streaming_channel_in = nullptr;
ggml_tensor * streaming_channel_out = nullptr;
ggml_tensor * streaming_time_in = nullptr;
ggml_tensor * streaming_time_out = nullptr;
int streaming_T_q_new = 0;
ggml_cgraph * streaming_graph = nullptr;
};
ggml_tensor * named(ggml_tensor * t, const char * name);
ggml_tensor * layer_norm(ggml_context * ctx,
ggml_tensor * x,
ggml_tensor * gamma,
ggml_tensor * beta);
ggml_tensor * feed_forward(ggml_context * ctx,
ggml_tensor * x,
ggml_tensor * lin1_w,
ggml_tensor * lin1_b,
ggml_tensor * lin2_w,
ggml_tensor * lin2_b);
ggml_tensor * macaron_ff_residual(ggml_context * ctx,
ggml_tensor * x,
ggml_tensor * norm_w,
ggml_tensor * norm_b,
ggml_tensor * lin1_w,
ggml_tensor * lin1_b,
ggml_tensor * lin2_w,
ggml_tensor * lin2_b);
ggml_tensor * rel_shift(ggml_context * ctx, ggml_tensor * x);
ggml_tensor * conv_1d_f32(ggml_context * ctx,
ggml_tensor * kernel,
ggml_tensor * data,
int stride,
int padding,
int dilation);
ggml_tensor * conv_2d_dw_f32(ggml_context * ctx,
ggml_tensor * kernel,
ggml_tensor * data,
int s0, int s1,
int p0, int p1,
int d0, int d1);
ggml_tensor * conv_1d_dw_f32(ggml_context * ctx,
ggml_tensor * kernel,
ggml_tensor * data,
int stride,
int padding,
int dilation);
ggml_tensor * fused_batch_norm(ggml_context * ctx,
ggml_tensor * x,
ggml_tensor * scale_1d,
ggml_tensor * bias_1d);
ggml_tensor * add_conv_bias(ggml_context * ctx,
ggml_tensor * conv_out,
ggml_tensor * bias_1d);
ggml_tensor * conv_module(ggml_context * ctx,
ggml_tensor * x,
const BlockView & b,
const BlockParams & params);
ggml_tensor * rel_pos_mhsa(ggml_context * ctx,
ggml_tensor * x,
ggml_tensor * pos_emb,
const BlockView & b,
const BlockParams & params);
struct BlockObserver {
void (*on_point)(void * user,
const char * tag,
ggml_tensor * t) = nullptr;
void * user = nullptr;
};
ggml_tensor * build_conformer_block(ggml_context * ctx,
ggml_tensor * x,
ggml_tensor * pos_emb,
const BlockView & b,
const BlockParams & params,
const BlockObserver * obs = nullptr);
struct PreEncodeValidMasks {
ggml_tensor * mask_s1 = nullptr; ggml_tensor * mask_s2 = nullptr; ggml_tensor * mask_s3 = nullptr; };
ggml_tensor * build_pre_encode(ggml_context * ctx,
const PreEncodeView & pe,
ggml_tensor * mel_in,
const ConvPolicy & policy,
const char * name_prefix,
const char * error_tag,
PreEncodeValidMasks * valid_masks = nullptr);
}