transcribe-cpp-sys 0.5.2

Native FFI bindings for transcribe.cpp, a C/C++ speech-to-text library built on ggml
Documentation
#pragma once

#include "node_context.h"

#include <memory>
#include <openvino/core/node.hpp>
#include <openvino/op/convert.hpp>
#include <openvino/op/shape_of.hpp>
#include <openvino/op/slice.hpp>
#include <utility>

namespace ov {
namespace frontend {
namespace ggml {

void num_inputs_check(const NodeContext & context, size_t min_inputs, size_t max_inputs);

std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<ov::op::v3::ShapeOf> & shape,
                                         const std::vector<int> & dims);
std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<ov::Node> & node, const std::vector<int> & dims);

OutputVector rename_outputs_with_suffix(const OutputVector & outputs, const std::string & suffix);

std::pair<ov::Output<Node>, ov::Output<Node>> make_sin_cos(int32_t * rope_params,
                                                           std::shared_ptr<ov::Node> inp_pos,
                                                           std::shared_ptr<ov::Node> rope_freqs_weight = nullptr,
                                                           int mode = 0,
                                                           bool stateful = false,
                                                           int64_t head_dim = 0);

// If `weights` is a dequantization chain emitted by make_int4_weights/make_int8_weights, rebuild
// it with `indices` gathered out of the leaf Constants, so a row lookup reads the packed rows and
// dequantizes only those. Returns nullptr if the subgraph is not such a chain, or is too small to
// be worth the rewrite. Used for tied embeddings, where the same weight also feeds the lm_head
// matmul: gathering the dequantized table would give that chain a second consumer and force it to
// be materialized in full.
std::shared_ptr<ov::Node> gather_compressed_rows(const ov::Output<ov::Node> & weights,
                                                 const ov::Output<ov::Node> & indices);

ov::Output<ov::Node> process_view_input(const NodeContext & context, int input_index, int slice_len = 0, int axis = -1);

ov::Output<ov::Node> process_view_input_new(const NodeContext & context, int input_index);

bool is_recurrent_cache_reset_case(const NodeContext & context);

OutputVector translate_recurrent_cache_reset(const NodeContext & context);

// A KQ mask input. Stateful execution reads the mask parameters through the masks rebuilt from the positions (see
// add_sliced_mask_stateful); any other input, e.g. the mask rows gemma-4 gathers for its KV-shared layers, as is.
ov::Output<ov::Node> get_kq_mask_input(const NodeContext & context, size_t input_index);

// A stateless KV cache in the [1, ctx, n_heads_kv, head_size] layout (GgmlOvDecoder::is_kvcache_4d), as opposed to
// [1, n_seq, ctx, n_heads_kv * head_size]
inline bool is_kvcache_4d_shape(const ov::PartialShape & shape) {
    return shape.rank().is_static() && shape.rank().get_length() == 4 && shape[1].is_dynamic() &&
           shape[2].is_static() && shape[3].is_static();
}

// rt_info key on the Reshape that restores the heads after a grouped-query MUL_MAT (mulmat): the number of A rows
// stacked per B matrix
constexpr const char * GGML_OV_GROUPED_ROWS = "ggml_ov_grouped_rows";

// rt_info key on the stateful V cache read with `-fa off` (permute): V is [.., kv, head_size], not transposed
constexpr const char * GGML_OV_V_TOKEN_MAJOR = "ggml_ov_v_token_major";

// rt_info key on the packed SSM_SCAN output Concat [1, 1, 1, y | state]; whole-part VIEWs select one Concat input
constexpr const char * GGML_OV_SSM_SCAN_PACKED = "ggml_ov_ssm_scan_packed";

namespace op {
template <typename T> OutputVector translate_1to1_match_2_inputs(const NodeContext & context) {
    num_inputs_check(context, 2, 2);
    auto input_0 = process_view_input_new(context, 0);
    auto input_1 = process_view_input_new(context, 1);

    auto output_type = context.get_output_type();
    if (input_0.get_element_type() != input_1.get_element_type()) {
        if (input_0.get_element_type() != ov::element::f32) {
            input_0 = std::make_shared<ov::op::v0::Convert>(input_0, ov::element::f32);
        }
        if (input_1.get_element_type() != ov::element::f32) {
            input_1 = std::make_shared<ov::op::v0::Convert>(input_1, ov::element::f32);
        }
    }

    ov::Output<ov::Node> res = std::make_shared<T>(input_0, input_1);
    if (res.get_element_type() != output_type) {
        res = std::make_shared<ov::op::v0::Convert>(res, output_type);
    }
    return rename_outputs_with_suffix({res}, context.get_name());
}

template <typename T> OutputVector translate_1to1_match_1_input(const NodeContext & context) {
    num_inputs_check(context, 1, 1);
    auto input = process_view_input_new(context, 0);
    auto res = std::make_shared<T>(input);
    return rename_outputs_with_suffix({res}, context.get_name());
}
}  // namespace op

}  // namespace ggml
}  // namespace frontend
}  // namespace ov