#pragma once
#include "transcribe-backend.h"
#include "transcribe-load-common.h"
#include "transcribe-model.h"
#include "transcribe-vad.h"
#include <cstdint>
#include <vector>
struct ggml_context;
struct ggml_tensor;
struct ggml_cgraph;
struct gguf_context;
struct ggml_backend_buffer;
typedef struct ggml_backend_buffer * ggml_backend_buffer_t;
namespace transcribe {
struct Arch;
}
namespace transcribe::silero_vad {
constexpr int kNumEncoder = 4;
constexpr int kKernel = 3;
struct HParams {
int32_t sample_rate = 16000;
int32_t n_fft = 256;
int32_t hop = 128;
int32_t frame_samples = 512;
int32_t context_samples = 64;
int32_t reflect_pad = 64;
int32_t channels[kNumEncoder + 1]{}; int32_t strides[kNumEncoder]{};
int32_t hidden = 128;
int32_t n_bins = 0; int32_t n_bins_pad = 0; int32_t chunk = 0; int32_t padded = 0; int32_t t_len[kNumEncoder + 1]{}; };
transcribe_status read_hparams(const gguf_context * gguf, HParams & hp);
struct Weights {
ggml_tensor * stft_basis = nullptr; ggml_tensor * conv_w[kNumEncoder]{}; ggml_tensor * conv_b[kNumEncoder]{}; ggml_tensor * lstm_w_ih = nullptr; ggml_tensor * lstm_w_hh = nullptr; ggml_tensor * lstm_b_ih = nullptr; ggml_tensor * lstm_b_hh = nullptr; ggml_tensor * head_w = nullptr; ggml_tensor * head_b = nullptr;
ggml_tensor * stft_basis_pad = nullptr;
ggml_tensor * dense_w[kNumEncoder]{}; ggml_tensor * dense_b[kNumEncoder]{}; ggml_tensor * lstm_b = nullptr; };
transcribe_status bind_weights(ggml_context * ctx_meta, const HParams & hp, Weights & w);
struct HostDecoder {
std::vector<float> w_hh_t;
std::vector<float> head_w;
float head_b = 0.0f;
};
struct Model final : public transcribe_model {
HParams hparams;
Weights weights;
ggml_context * ctx_meta = nullptr; load_common::WeightStorage weight_storage;
ggml_context * ctx_derived = nullptr;
ggml_backend_buffer_t derived_buffer = nullptr;
BackendPlan plan;
HostDecoder host;
Model() = default;
~Model() override;
};
transcribe_status build_derived_weights(Model & m);
transcribe_status load_from_bin(const char * path,
const transcribe_model_load_params * params,
transcribe_model ** out_model);
struct DumpTrace {
std::vector<float> stft_mag; std::vector<float> enc[kNumEncoder]; std::vector<float> lstm_h; std::vector<float> lstm_c; std::vector<float> probs; int64_t n_frames = 0;
void clear();
};
struct GraphTensors {
ggml_cgraph * graph = nullptr;
ggml_tensor * windows = nullptr; ggml_tensor * stft_mag = nullptr; ggml_tensor * enc[kNumEncoder]{}; ggml_tensor * gates_in = nullptr; };
struct Session final : public transcribe_vad_session {
std::vector<float> h;
std::vector<float> c;
std::vector<float> context;
std::vector<float> chunk_buf; std::vector<float> windows; std::vector<float> gates_in; std::vector<float> gates;
GraphTensors g;
int graph_n = 0;
DumpTrace trace;
protected:
void on_scratch_released() noexcept override {
g = GraphTensors{};
graph_n = 0;
}
};
transcribe_status encode_block(Session & s,
const Model & m,
const float * windows,
int n_graph,
int n_real,
float * gates_in);
void build_windows(Session & s, const HParams & hp, const float * pcm, int n, float * out);
void decode_block(Session & s, const Model & m, const float * gates_in, int n, float * probs);
extern const Arch arch;
}