#pragma once
#include "transcribe.h"
#include "ggml.h"
#include "ggml-backend.h"
#include <cstdint>
#include <string>
#include <vector>
namespace transcribe::parakeet {
struct ParakeetModel;
struct ParakeetHParams;
struct HostLstmLayer {
std::vector<float> Wx; std::vector<float> Wh; std::vector<float> b;
ggml_tensor * g_Wx = nullptr;
ggml_tensor * g_Wh = nullptr;
ggml_tensor * g_b = nullptr;
};
struct HostPredictor {
int pred_hidden = 0;
int pred_vocab = 0; std::vector<float> embed_w; std::vector<HostLstmLayer> lstm;
ggml_context * lstm_w_ctx = nullptr;
ggml_backend_t lstm_w_backend = nullptr; ggml_backend_buffer_t lstm_w_buf = nullptr;
bool lstm_ready = false;
HostPredictor() = default;
~HostPredictor();
HostPredictor(const HostPredictor &) = delete;
HostPredictor & operator=(const HostPredictor &) = delete;
HostPredictor(HostPredictor &&) = delete;
HostPredictor & operator=(HostPredictor &&) = delete;
};
struct HostJoint {
int d_enc = 0; int pred_hidden = 0;
int joint_h = 0;
int joint_n = 0; std::string activation;
std::vector<float> enc_w; std::vector<float> enc_b; std::vector<float> pred_w; std::vector<float> pred_b; std::vector<float> out_b;
ggml_context * w_ctx = nullptr;
ggml_backend_t w_backend = nullptr; ggml_backend_buffer_t w_buf = nullptr;
ggml_tensor * g_enc_w = nullptr; ggml_tensor * g_enc_b = nullptr; ggml_tensor * g_pred_w = nullptr; ggml_tensor * g_pred_b = nullptr; ggml_tensor * gw_w = nullptr; ggml_tensor * gw_b = nullptr; bool w_ready = false;
HostJoint() = default;
~HostJoint();
HostJoint(const HostJoint &) = delete;
HostJoint & operator=(const HostJoint &) = delete;
HostJoint(HostJoint &&) = delete;
HostJoint & operator=(HostJoint &&) = delete;
};
struct HostCtcHead {
int n_classes = 0; int blank_id = 0; int d_enc = 0;
std::vector<float> weight; std::vector<float> bias; };
enum class HostHeadKind { TDT, RNNT, CTC };
struct HostDecoderWeights {
HostHeadKind head_kind = HostHeadKind::TDT;
HostPredictor predictor; HostJoint joint; HostCtcHead ctc_head; std::vector<int32_t> tdt_durations; int tdt_max_symbols = 0;
int blank_id = 0; int n_vocab = 0; };
transcribe_status build_host_decoder_weights(const ParakeetModel & model,
HostDecoderWeights & out);
struct LstmState {
std::vector<std::vector<float>> h;
std::vector<std::vector<float>> c;
void reset(int n_layers, int pred_hidden);
};
struct TdtToken {
int id = 0;
float p = 0.0f; int step_at_emit = 0;
int duration_frames = 0;
};
transcribe_status decode_tdt_greedy(const HostDecoderWeights & w,
const float * enc_out,
int T_enc,
int d_enc,
int n_threads,
std::vector<TdtToken> & out_tokens);
transcribe_status decode_rnnt_greedy(const HostDecoderWeights & w,
const float * enc_out,
int T_enc,
int d_enc,
int n_threads,
std::vector<TdtToken> & out_tokens);
transcribe_status decode_rnnt_greedy_streaming(
const HostDecoderWeights & w,
const float * enc_out,
int T_enc_new,
int d_enc,
LstmState & state_io,
int & last_token_io,
int frame_offset,
int n_threads,
std::vector<TdtToken> & out_tokens);
transcribe_status decode_ctc_greedy(const HostDecoderWeights & w,
const float * enc_out,
int T_enc,
int d_enc,
int n_threads,
std::vector<TdtToken> & out_tokens);
}