#pragma once
#include "ggml-backend.h"
#include "ggml.h"
#include "transcribe-backend.h"
#include "transcribe-mel.h"
#include "transcribe-model.h"
#include "transcribe-session.h"
#include "transcribe-tokenizer.h"
#include "weights.h"
#include <cstdint>
#include <optional>
#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::cohere {
void apply_family_invariants(transcribe_model & model);
struct CohereKvCache {
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) {
ggml_backend_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;
n_batch = 1;
cross_populated = false;
}
};
bool kv_cache_init(CohereKvCache & cache,
ggml_backend_t backend,
int n_ctx,
int T_enc,
int n_state,
int n_layer,
ggml_type kv_type);
bool kv_cache_init_batched(CohereKvCache & cache,
ggml_backend_t backend,
int n_ctx,
int T_enc,
int n_state,
int n_layer,
int n_batch,
ggml_type kv_type);
struct CohereModel final : public transcribe_model {
Tokenizer tok;
CohereHParams hparams;
CohereWeights weights;
ggml_context * ctx_meta = nullptr;
transcribe::BackendPlan plan;
ggml_backend_buffer_t backend_buffer = nullptr;
ggml_context * bn_fused_ctx = nullptr;
ggml_backend_buffer_t bn_fused_buffer = nullptr;
ggml_context * conv_pw_f32_ctx = nullptr;
ggml_backend_buffer_t conv_pw_f32_buffer = nullptr;
std::optional<transcribe::MelFrontend> mel;
CohereModel() = default;
~CohereModel() override;
const transcribe::Tokenizer * tokenizer() const override { return &tok; }
};
struct CohereSession final : public transcribe_session {
ggml_context * compute_ctx = nullptr;
ggml_backend_sched_t sched = nullptr;
ggml_tensor * encoder_out = nullptr;
CohereKvCache kv_cache;
std::vector<float> mel_buf;
std::vector<float> pos_buf;
std::vector<float> pos_div_term;
std::vector<float> enc_host;
bool encoder_use_flash = true;
bool decoder_use_flash = true;
CohereSession() = default;
~CohereSession() override;
};
}