#pragma once
#include "transcribe.h"
#include <cstdint>
#include <string>
#include <vector>
struct gguf_context;
struct ggml_context;
struct ggml_tensor;
namespace transcribe::granite_nar {
struct GraniteNarHParams {
int32_t enc_n_layers = 0; int32_t enc_hidden = 0; int32_t enc_n_heads = 0; int32_t enc_head_dim = 0; int32_t enc_input_dim = 0; int32_t enc_output_dim = 0; int32_t enc_bpe_output_dim = 0; int32_t enc_bpe_pool_window = 0; int32_t enc_bpe_blank_id = 0;
int32_t enc_self_cond_layer = 0; int32_t enc_feedforward_mult = 0; int32_t enc_conv_kernel_size = 0; int32_t enc_conv_expansion = 0; int32_t enc_max_pos_emb = 0; int32_t enc_context_size = 0;
std::vector<int32_t> enc_layer_indices;
int32_t prj_n_layers = 0; int32_t prj_hidden = 0; int32_t prj_mlp_ratio = 0; int32_t prj_n_heads = 0; int32_t prj_encoder_dim = 0; int32_t prj_num_encoder_layers = 0; int32_t prj_block_size = 0; int32_t prj_downsample_rate = 0; int32_t prj_llm_dim = 0; float prj_layernorm_eps = 0.0f;
bool prj_attn_bias = true;
bool prj_mlp_bias = true;
bool scale_projected_embeddings = true;
int32_t dec_n_layers = 0; int32_t dec_hidden = 0; int32_t dec_intermediate = 0; int32_t dec_n_heads = 0; int32_t dec_n_kv_heads = 0; int32_t dec_head_dim = 0; std::string dec_hidden_act;
float dec_rms_norm_eps = 0.0f;
float dec_rope_theta = 0.0f;
int32_t dec_max_pos_emb = 0;
bool dec_tie_word_embeddings = true;
int32_t dec_vocab_size = 0;
float dec_embedding_multiplier = 0.0f; float dec_logits_scaling = 0.0f; float dec_attention_multiplier = 0.0f; float dec_residual_multiplier = 0.0f;
int32_t dec_bos_id = 0;
int32_t dec_eos_id = 0;
int32_t dec_pad_id = 0;
std::string fe_type; int32_t fe_sample_rate = 0;
int32_t fe_num_mels = 0;
int32_t fe_n_fft = 0;
int32_t fe_win_length = 0;
int32_t fe_hop_length = 0;
std::string fe_window;
std::string fe_normalize;
std::string fe_pad_mode;
std::string fe_mel_norm;
std::vector<std::string> ctc_chars;
};
transcribe_status read_granite_nar_hparams(const gguf_context * gguf,
GraniteNarHParams & hp);
struct GraniteNarEncTop {
ggml_tensor * input_linear_w = nullptr;
ggml_tensor * input_linear_b = nullptr;
ggml_tensor * ctc_proj_w = nullptr; ggml_tensor * ctc_proj_b = nullptr;
ggml_tensor * ctc_bypass_w = nullptr; ggml_tensor * ctc_bypass_b = nullptr;
ggml_tensor * ctc_bpe_w = nullptr; ggml_tensor * ctc_bpe_b = nullptr;
};
struct GraniteNarEncBlock {
ggml_tensor * norm_ff1_w = nullptr;
ggml_tensor * norm_ff1_b = nullptr;
ggml_tensor * ff1_up_w = nullptr;
ggml_tensor * ff1_up_b = nullptr;
ggml_tensor * ff1_down_w = nullptr;
ggml_tensor * ff1_down_b = nullptr;
ggml_tensor * norm_attn_w = nullptr;
ggml_tensor * norm_attn_b = nullptr;
ggml_tensor * attn_q_w = nullptr;
ggml_tensor * attn_kv_w = nullptr;
ggml_tensor * attn_out_w = nullptr;
ggml_tensor * attn_out_b = nullptr;
ggml_tensor * attn_rel_pos_emb = nullptr;
ggml_tensor * norm_conv_w = nullptr;
ggml_tensor * norm_conv_b = nullptr;
ggml_tensor * conv_pointwise1_w = nullptr;
ggml_tensor * conv_pointwise1_b = nullptr;
ggml_tensor * conv_depthwise_w = nullptr;
ggml_tensor * conv_bn_w = nullptr;
ggml_tensor * conv_bn_b = nullptr;
ggml_tensor * conv_bn_mean = nullptr;
ggml_tensor * conv_bn_var = nullptr;
ggml_tensor * conv_pointwise2_w = nullptr;
ggml_tensor * conv_pointwise2_b = nullptr;
ggml_tensor * conv_bn_fused_scale = nullptr;
ggml_tensor * conv_bn_fused_bias = nullptr;
ggml_tensor * norm_ff2_w = nullptr;
ggml_tensor * norm_ff2_b = nullptr;
ggml_tensor * ff2_up_w = nullptr;
ggml_tensor * ff2_up_b = nullptr;
ggml_tensor * ff2_down_w = nullptr;
ggml_tensor * ff2_down_b = nullptr;
ggml_tensor * norm_post_w = nullptr;
ggml_tensor * norm_post_b = nullptr;
};
struct GraniteNarProjTop {
std::vector<ggml_tensor *> layer_norms_w;
std::vector<ggml_tensor *> layer_norms_b;
ggml_tensor * layer_projector_w = nullptr; ggml_tensor * layer_projector_b = nullptr;
ggml_tensor * out_norm_w = nullptr; ggml_tensor * out_norm_b = nullptr;
ggml_tensor * out_linear_w = nullptr; ggml_tensor * out_linear_b = nullptr;
ggml_tensor * query = nullptr; ggml_tensor * window_positions = nullptr; };
struct GraniteNarProjBlock {
ggml_tensor * norm_attn_w = nullptr;
ggml_tensor * norm_attn_b = nullptr;
ggml_tensor * cross_attn_q_w = nullptr;
ggml_tensor * cross_attn_q_b = nullptr;
ggml_tensor * cross_attn_k_w = nullptr;
ggml_tensor * cross_attn_k_b = nullptr;
ggml_tensor * cross_attn_v_w = nullptr;
ggml_tensor * cross_attn_v_b = nullptr;
ggml_tensor * cross_attn_o_w = nullptr;
ggml_tensor * cross_attn_o_b = nullptr;
ggml_tensor * norm_ffn_w = nullptr;
ggml_tensor * norm_ffn_b = nullptr;
ggml_tensor * ffn_fc1_w = nullptr;
ggml_tensor * ffn_fc1_b = nullptr;
ggml_tensor * ffn_fc2_w = nullptr;
ggml_tensor * ffn_fc2_b = nullptr;
};
struct GraniteNarDecEmbed {
ggml_tensor * token_w = nullptr; };
struct GraniteNarDecBlock {
ggml_tensor * norm_attn_w = nullptr;
ggml_tensor * norm_ffn_w = nullptr;
ggml_tensor * attn_q_w = nullptr;
ggml_tensor * attn_k_w = nullptr;
ggml_tensor * attn_v_w = nullptr;
ggml_tensor * attn_o_w = nullptr;
ggml_tensor * ffn_gate_w = nullptr;
ggml_tensor * ffn_up_w = nullptr;
ggml_tensor * ffn_down_w = nullptr;
};
struct GraniteNarDecFinal {
ggml_tensor * norm_w = nullptr;
};
struct GraniteNarWeights {
GraniteNarEncTop enc_top;
std::vector<GraniteNarEncBlock> enc_blocks;
GraniteNarProjTop proj_top;
std::vector<GraniteNarProjBlock> proj_blocks;
GraniteNarDecEmbed dec_embed;
std::vector<GraniteNarDecBlock> dec_blocks;
GraniteNarDecFinal dec_final;
};
transcribe_status build_granite_nar_weights(ggml_context * ctx_meta,
const GraniteNarHParams & hp,
GraniteNarWeights & weights);
}