#pragma once
#include "ggml-backend.h"
#include "ggml.h"
#include "transcribe-backend.h"
#include "transcribe-load-common.h"
#include "transcribe-model.h"
#include "transcribe-session.h"
#include "transcribe-tokenizer.h"
#include "weights.h"
#include <cstdint>
#include <string>
#include <vector>
struct ggml_context;
struct ggml_tensor;
struct ggml_backend;
struct ggml_backend_buffer;
struct ggml_backend_sched;
typedef struct ggml_backend * ggml_backend_t;
typedef struct ggml_backend_buffer * ggml_backend_buffer_t;
typedef struct ggml_backend_sched * ggml_backend_sched_t;
namespace transcribe::moonshine {
void apply_family_invariants(transcribe_model & model);
struct MoonshineKvCache {
ggml_tensor * self_k = nullptr;
ggml_tensor * self_v = nullptr;
ggml_tensor * cross_k = nullptr;
ggml_tensor * cross_v = nullptr;
ggml_context * ctx = nullptr;
ggml_backend_buffer_t buffer = nullptr;
int n_ctx = 0; int n = 0; int head = 0; int T_enc = 0; int n_batch = 1;
bool cross_populated = false;
void free() {
if (buffer != nullptr) {
safe_buffer_free(buffer);
buffer = nullptr;
}
if (ctx != nullptr) {
ggml_free(ctx);
ctx = nullptr;
}
self_k = nullptr;
self_v = nullptr;
cross_k = nullptr;
cross_v = nullptr;
n = 0;
head = 0;
T_enc = 0;
n_batch = 1;
cross_populated = false;
}
};
bool kv_cache_init(MoonshineKvCache & cache,
ggml_backend_t backend,
int n_ctx,
int T_enc,
int d_model,
int n_layer,
ggml_type kv_type);
bool kv_cache_init_batched(MoonshineKvCache & cache,
ggml_backend_t backend,
int n_ctx,
int T_enc,
int d_model,
int n_layer,
int n_batch,
ggml_type kv_type);
struct MoonshineModel final : public transcribe_model {
Tokenizer tok;
MoonshineHParams hparams;
MoonshineWeights weights;
ggml_context * ctx_meta = nullptr;
transcribe::BackendPlan plan;
transcribe::load_common::WeightStorage weight_storage;
MoonshineModel() = default;
~MoonshineModel() override;
const transcribe::Tokenizer * tokenizer() const override { return &tok; }
};
struct MoonshineSession final : public transcribe_session {
std::vector<float> enc_host;
int enc_T = 0;
MoonshineKvCache kv_cache;
bool encoder_use_flash = true;
bool decoder_use_flash = true;
MoonshineSession() = default;
~MoonshineSession() override;
};
}