transcribe-cpp-sys 0.5.0

Native FFI bindings for transcribe.cpp, a C/C++ speech-to-text library built on ggml
Documentation
#include "../node_context.h"
#include "../op_table.h"
#include "../utils.h"
#include "ggml-openvino/ggml-openvino-extra.h"

#include <cassert>
#include <cstdint>
#include <memory>
#include <numeric>
#include <openvino/core/node.hpp>
#include <openvino/core/node_output.hpp>
#include <openvino/core/type.hpp>
#include <openvino/frontend/exception.hpp>
#include <openvino/op/add.hpp>
#include <openvino/op/broadcast.hpp>
#include <openvino/op/concat.hpp>
#include <openvino/op/constant.hpp>
#include <openvino/op/convert.hpp>
#include <openvino/op/divide.hpp>
#include <openvino/op/gather.hpp>
#include <openvino/op/mod.hpp>
#include <openvino/op/multiply.hpp>
#include <openvino/op/range.hpp>
#include <openvino/op/reshape.hpp>
#include <openvino/op/scatter_elements_update.hpp>
#include <openvino/op/scatter_update.hpp>
#include <openvino/op/shape_of.hpp>
#include <openvino/op/slice.hpp>
#include <openvino/op/squeeze.hpp>
#include <openvino/op/subtract.hpp>
#include <openvino/op/transpose.hpp>
#include <openvino/op/unsqueeze.hpp>
#include <vector>

namespace ov {
namespace frontend {
namespace ggml {
namespace op {

static OutputVector translate_conv_snapshots(const NodeContext & context) {
    using namespace ov::op;
    FRONT_END_OP_CONVERSION_CHECK(!context.is_static(), "Convolution snapshots require dynamic execution");
    auto constant = [](std::initializer_list<size_t> shape, std::initializer_list<int64_t> values) {
        return v0::Constant::create(element::i64, Shape(shape), std::vector<int64_t>(values));
    };
    const auto history = context.get_input_shape(0)[3].get_length();
    const auto features = history * context.get_input_shape(0)[1].get_length();
    const auto name = context.get_name();
    auto count = context.get_input("rs_window_count_" + name);
    auto seqs = context.get_input("rs_seq_count_" + name);
    auto zero = constant({}, {0});
    auto one = constant({}, {1});
    auto range = [&](const Output<Node> & end) {
        return std::make_shared<v4::Range>(zero, std::make_shared<v0::Squeeze>(end), one, element::i64);
    };

    // VIEW passes through conv_x; gather overlapping windows in [window, seq, channel, history] order.
    // Return the full cache to preserve the in-place alias chain.
    auto begin = context.get_input("rs_src_begin_" + name);
    auto positions = range(std::make_shared<v1::Multiply>(count, constant({}, {history})));
    auto columns = std::make_shared<v1::Add>(begin,
        std::make_shared<v1::Add>(std::make_shared<v1::Divide>(positions, constant({}, {history})),
                                 std::make_shared<v1::Mod>(positions, constant({}, {history}))));
    auto column_matrix = std::make_shared<v1::Reshape>(columns, constant({2}, {-1, history}), false);
    auto windows = std::make_shared<v8::Gather>(context.get_input(0), column_matrix, constant({}, {3}));
    auto ordered = std::make_shared<v1::Transpose>(
        std::make_shared<v0::Squeeze>(windows, constant({1}, {0})), constant({4}, {2, 0, 1, 3}));
    Output<Node> updates = std::make_shared<v1::Reshape>(ordered, constant({4}, {1, 1, -1, features}), false);
    updates = std::make_shared<v0::Convert>(updates, context.get_output_type());

    auto row = range(std::make_shared<v1::Multiply>(count, seqs));
    auto plane = std::make_shared<v1::Subtract>(std::make_shared<v1::Subtract>(count, one),
                                               std::make_shared<v1::Divide>(row, seqs));
    auto slots = std::make_shared<v1::Add>(context.get_input("rs_slot_begin_" + name),
        std::make_shared<v1::Add>(std::make_shared<v1::Mod>(row, seqs),
                                 std::make_shared<v1::Multiply>(plane,
                                     context.get_input("rs_snapshot_stride_" + name))));
    auto base = context.get_input(2);
    const auto full_features = base.get_partial_shape()[3].get_length();
    const auto feature_begin = (context.get_view_input_offset(2, 0) / context.get_output_type().size()) % full_features;
    if (features != full_features) {
        auto old_rows = std::make_shared<v8::Gather>(base, slots, constant({}, {2}));
        auto head = std::make_shared<v8::Slice>(old_rows, constant({1}, {0}),
            constant({1}, {int64_t(feature_begin)}), constant({1}, {1}), constant({1}, {3}));
        auto tail = std::make_shared<v8::Slice>(old_rows, constant({1}, {int64_t(feature_begin) + features}),
            constant({1}, {INT64_MAX}), constant({1}, {1}), constant({1}, {3}));
        updates = std::make_shared<v0::Concat>(OutputVector{head, updates, tail}, 3);
    }
    auto result = std::make_shared<v3::ScatterUpdate>(base, slots, updates, constant({}, {2}));
    return rename_outputs_with_suffix({result}, name);
}

// SET_ROWS into a column range of a plain 2-D buffer (see GgmlOvDecoder::is_column_set_rows). The result is the
// whole buffer: the indexed rows get the new columns, the other columns and rows keep their values, so the next
// write into another column range of the same buffer reads this one.
static OutputVector translate_column_set_rows(const NodeContext & context) {
    using namespace ov::op;
    auto i64_const = [](const std::vector<int64_t> & values) {
        return v0::Constant::create(ov::element::i64, {values.size()}, values);
    };

    const auto view_shape = context.get_view_input_ggml_shape(2, 0);  // [1, 1, rows, width]
    FRONT_END_OP_CONVERSION_CHECK(view_shape.size() == 4, "SET_ROWS column write needs a 4-D destination view");
    const int64_t width = (int64_t) view_shape[3];

    auto base = context.get_input(2);
    const auto base_shape = base.get_partial_shape();
    FRONT_END_OP_CONVERSION_CHECK(base_shape.rank().is_static() && base_shape[base_shape.size() - 1].is_static(),
                                  "SET_ROWS column write needs a static buffer row size");
    const int64_t row_size = base_shape[base_shape.size() - 1].get_length();
    const size_t elem_size = context.get_output_type().size();
    const int64_t col_begin = (int64_t) (context.get_view_input_offset(2, 0) / elem_size) % row_size;
    const int64_t col_end = col_begin + width;
    FRONT_END_OP_CONVERSION_CHECK(col_end <= row_size, "SET_ROWS column range is out of bounds");

    Output<Node> data = std::make_shared<v1::Reshape>(process_view_input_new(context, 0), i64_const({1, 1, -1, width}),
                                                      false);
    if (data.get_element_type() != context.get_output_type()) {
        data = std::make_shared<v0::Convert>(data, context.get_output_type());
    }
    auto row_ids = process_view_input_new(context, 1);
    Output<Node> rows = std::make_shared<v1::Reshape>(row_ids, i64_const({-1}), false);
    if (rows.get_element_type() != ov::element::i64) {
        rows = std::make_shared<v0::Convert>(rows, ov::element::i64);
    }

    auto axis = v0::Constant::create(ov::element::i64, {}, {2});
    Output<Node> buffer = std::make_shared<v1::Reshape>(base, i64_const({1, 1, -1, row_size}), false);
    Output<Node> base_shape_of = std::make_shared<v3::ShapeOf>(base, ov::element::i64);
    Output<Node> existing;

    // Every write copies the whole buffer, so fold this one into the previous column write when both use the same
    // rows (DFlash writes every target layer at the same batch rows): splice into its rows and scatter once
    auto rows_source = [](Output<Node> r) {
        if (ov::as_type_ptr<v0::Convert>(r.get_node_shared_ptr())) {
            r = r.get_node()->input_value(0);
        }
        return ov::as_type_ptr<v1::Reshape>(r.get_node_shared_ptr()) ? r.get_node()->input_value(0) : Output<Node>();
    };
    auto prev_res = ov::as_type_ptr<v1::Reshape>(base.get_node_shared_ptr());
    auto prev = prev_res ? ov::as_type_ptr<v3::ScatterUpdate>(prev_res->get_input_node_shared_ptr(0)) : nullptr;
    if (prev && rows_source(prev->input_value(1)) == row_ids &&
        prev->input_value(2).get_partial_shape().rank().is_static() &&
        prev->input_value(2).get_partial_shape()[3] == row_size) {
        buffer = prev->input_value(0);
        rows = prev->input_value(1);
        existing = prev->input_value(2);
        base_shape_of = prev_res->input_value(1);
    }

    Output<Node> updates = data;
    if (width < row_size) {
        if (!existing.get_node_shared_ptr()) {
            existing = std::make_shared<v8::Gather>(buffer, rows, axis);
        }
        auto col_axis = i64_const({3});
        OutputVector parts;
        if (col_begin > 0) {
            parts.push_back(std::make_shared<v8::Slice>(existing, i64_const({0}), i64_const({col_begin}),
                                                        i64_const({1}), col_axis));
        }
        parts.push_back(data);
        if (col_end < row_size) {
            parts.push_back(std::make_shared<v8::Slice>(existing, i64_const({col_end}), i64_const({row_size}),
                                                        i64_const({1}), col_axis));
        }
        updates = std::make_shared<v0::Concat>(parts, 3);
    }
    auto updated = std::make_shared<v3::ScatterUpdate>(buffer, rows, updates, axis);
    auto res = std::make_shared<v1::Reshape>(updated, base_shape_of, false);
    return rename_outputs_with_suffix({res}, context.get_name());
}

OutputVector translate_set_rows(const NodeContext & context) {
    num_inputs_check(context, 3, 3);

    if (context.get_op_case() == 3) {
        return translate_conv_snapshots(context);
    }
    if (context.get_op_case() == 4) {
        return translate_column_set_rows(context);
    }

    auto data = process_view_input_new(context, 0);
    auto indices = process_view_input_new(context, 1);

    data = std::make_shared<ov::op::v0::Convert>(data, context.get_output_type());

    const auto indices_shape = context.get_input_shape(1);
    const bool multidim_indices = indices_shape.rank().is_static() &&
                                  indices_shape.rank().get_length() == 4 &&
                                  ((indices_shape[1].is_static() && indices_shape[1].get_length() > 1) ||
                                   (indices_shape[2].is_static() && indices_shape[2].get_length() > 1));

    // On NPU ScatterUpdate is not in-place: it copies the whole destination to write
    // one KV row, which dominates decode once ctx grows. ScatterElementsUpdate is
    // measurably cheaper for the same single-row write.
    const bool use_elements_update =
        !multidim_indices && !context.is_stateful() && ggml_openvino_getenv_int("GGML_OPENVINO_KV_SCATTER_ELEMENTS");

    auto axes = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {2});

    const bool is_cache_write = context.get_op_case() != 2;

    if (is_cache_write && context.is_static() && !context.is_stateful() && !multidim_indices &&
        ggml_openvino_npu_kv_head_major_enabled()) {
        // Head-major cache [1, heads, rows, head_size]: write the new rows along axis 2 without reshaping the cache.
        ov::Output<Node> cache = context.get_input(2);
        while (auto reshape = ov::as_type_ptr<ov::op::v1::Reshape>(cache.get_node_shared_ptr())) {
            cache = reshape->input_value(0);
        }
        const auto cache_shape = cache.get_partial_shape();
        if (cache_shape.rank().is_static() && cache_shape.rank().get_length() == 4 && cache_shape[1].is_static() &&
            cache_shape[3].is_static() && cache_shape[1].get_length() > 1) {
            const int64_t heads = cache_shape[1].get_length();
            // Transposed V cache is [1, heads, head_size, rows]: rows go along axis 3.
            const bool v_transposed = cache.get_node()->get_friendly_name().rfind("cache_v_l", 0) == 0;
            const int64_t head_size = (v_transposed ? cache_shape[2] : cache_shape[3]).get_length();
            auto ind_squeezed = std::make_shared<ov::op::v0::Squeeze>(
                indices, ov::op::v0::Constant::create(ov::element::i64, {3}, {0, 1, 2}));
            auto rows = std::make_shared<ov::op::v1::Reshape>(
                data,
                ov::op::v0::Constant::create(ov::element::i64, {4}, {(int64_t) 1, (int64_t) -1, heads, head_size}),
                false);
            auto rows_bhsd = std::make_shared<ov::op::v1::Transpose>(
                rows, ov::op::v0::Constant::create(ov::element::i64, {4},
                                                   v_transposed ? std::vector<int64_t>{0, 2, 3, 1} :
                                                                  std::vector<int64_t>{0, 2, 1, 3}));
            if (v_transposed && context.is_prefill() && ggml_openvino_npu_hfa_prefill_enabled()) {
                // On NPU a chunk-sized scatter along the innermost axis of [1, heads, head_size, rows] is very slow:
                // the HFA prefill outputs the chunk's V^T rows [1, heads, head_size, n] instead and the host copies
                // them into the cache (as GenAI does for its prefill KV).
                return rename_outputs_with_suffix({rows_bhsd}, context.get_name());
            }
            Output<Node> res = std::make_shared<ov::op::v3::ScatterUpdate>(
                cache, ind_squeezed, rows_bhsd,
                v_transposed ? ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {3}) : axes);
            return rename_outputs_with_suffix({res}, context.get_name());
        }
    }

    auto dst = process_view_input_new(context, 2);
    Output<Node> res;
    if (is_cache_write && context.is_stateful()) {
        int concat_axis = 1;
        if (context.get_op_case() == 1) {
            // `-fa off` writes V transposed through a [1, n] reshape of the cache. The state keeps V token-major
            // like K, and the new values are already in token order, so append them to the state itself
            dst = dst.get_node()->input_value(0);
        }
        FRONT_END_OP_CONVERSION_CHECK(dst.get_partial_shape()[2].is_static() && dst.get_partial_shape()[3].is_static(),
                                      "Unsupported KV cache layout for stateful execution");
        int64_t dim2 = dst.get_partial_shape()[2].get_length();
        int64_t dim3 = dst.get_partial_shape()[3].get_length();
        data = std::make_shared<ov::op::v1::Reshape>(
            data, ov::op::v0::Constant::create(ov::element::i64, {4}, {(int64_t) 1, (int64_t) -1, dim2, dim3}), false);
        res = std::make_shared<ov::op::v0::Concat>(OutputVector{dst, data}, concat_axis);
    } else if (is_cache_write && is_kvcache_4d_shape(dst.get_partial_shape())) {
        // cache [1, ctx, n_heads_kv, head_size]: write whole tokens on axis 1. This is the pattern of the GPU
        // plugin's StatelessKVFusion (1-D axis, read with VariadicSplit, see permute), which writes the new rows in
        // place instead of copying the whole cache first
        FRONT_END_OP_CONVERSION_CHECK(!multidim_indices, "SET_ROWS into a 4-D KV cache needs one index per token");
        const int64_t n_heads_kv = dst.get_partial_shape()[2].get_length();
        const int64_t head_size = dst.get_partial_shape()[3].get_length();
        auto data_tokens = std::make_shared<ov::op::v1::Reshape>(
            data,
            ov::op::v0::Constant::create(ov::element::i64, {4}, {(int64_t) 1, (int64_t) -1, n_heads_kv, head_size}),
            false);
        auto ind_squeezed = std::make_shared<ov::op::v0::Squeeze>(
            indices, ov::op::v0::Constant::create(ov::element::i64, {3}, {0, 1, 2}));
        res = std::make_shared<ov::op::v3::ScatterUpdate>(dst, ind_squeezed, data_tokens,
                                                          ov::op::v0::Constant::create(ov::element::i64, {1}, {1}));
        return rename_outputs_with_suffix({res}, context.get_name());
    } else if (multidim_indices || !is_cache_write) {
        // updates [.., n_rows, row_size] with one index per row; stateful models may drop the leading 1 of the
        // updates but not of the destination
        const auto dst_rank = dst.get_partial_shape().rank();
        const auto data_rank = data.get_partial_shape().rank();
        FRONT_END_OP_CONVERSION_CHECK(dst_rank.is_static() && data_rank.is_static() &&
                                          data_rank.get_length() <= dst_rank.get_length(),
                                      "SET_ROWS with per-row indices: updates ", data.get_partial_shape(),
                                      " do not fit the destination ", dst.get_partial_shape());
        if (data_rank.get_length() < dst_rank.get_length()) {
            std::vector<int64_t> lead_axes(dst_rank.get_length() - data_rank.get_length());
            std::iota(lead_axes.begin(), lead_axes.end(), 0);
            data = std::make_shared<ov::op::v0::Unsqueeze>(
                data, ov::op::v0::Constant::create(ov::element::i64, {lead_axes.size()}, lead_axes));
        }
        auto updates_shape = std::make_shared<ov::op::v3::ShapeOf>(data, ov::element::i64);

        auto indices_rank3 = std::make_shared<ov::op::v0::Squeeze>(
            indices, ov::op::v0::Constant::create(ov::element::i64, {1}, {0}));
        auto one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1});
        std::vector<int> row_dims(dst_rank.get_length() - 1);
        std::iota(row_dims.begin(), row_dims.end(), 0);
        auto indices_rows_shape =
            std::make_shared<ov::op::v0::Concat>(OutputVector{get_dimensions(updates_shape, row_dims), one}, 0);
        auto indices_rows = std::make_shared<ov::op::v1::Reshape>(indices_rank3, indices_rows_shape, false);
        auto broadcasted_indices = std::make_shared<ov::op::v3::Broadcast>(indices_rows, updates_shape);
        auto rows_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {-2});

        res = std::make_shared<ov::op::v3::ScatterElementsUpdate>(dst, broadcasted_indices, data, rows_axis);
    } else {
        auto row_size = context.get_input_shape(2)[3].get_length();
        auto ind_squeezed = std::make_shared<ov::op::v0::Squeeze>(
            indices, ov::op::v0::Constant::create(ov::element::i64, {3}, {0, 1, 2}));
        // skip the reshape when the rows already have that shape: it costs a GPU primitive per cache write
        const auto & data_shape = data.get_partial_shape();
        const bool data_is_rows = data_shape.rank().is_static() && data_shape.rank().get_length() == 4 &&
                                  data_shape[0] == 1 && data_shape[1] == 1 && data_shape[3] == row_size;
        ov::Output<ov::Node> data_reshaped = data;
        if (!data_is_rows) {
            data_reshaped = std::make_shared<ov::op::v1::Reshape>(
                data,
                ov::op::v0::Constant::create(ov::element::i64, {4},
                                             {(int64_t) 1, (int64_t) 1, (int64_t) -1, (int64_t) row_size}),
                false);
        }
        if (use_elements_update) {
            auto updates_shape = std::make_shared<ov::op::v3::ShapeOf>(data_reshaped, ov::element::i64);
            auto ind_rank4 = std::make_shared<ov::op::v1::Reshape>(
                ind_squeezed, ov::op::v0::Constant::create(ov::element::i64, {4}, {1, 1, -1, 1}), false);
            auto broadcasted_indices = std::make_shared<ov::op::v3::Broadcast>(ind_rank4, updates_shape);
            res = std::make_shared<ov::op::v3::ScatterElementsUpdate>(dst, broadcasted_indices, data_reshaped, axes);
        } else {
            res = std::make_shared<ov::op::v3::ScatterUpdate>(dst, ind_squeezed, data_reshaped, axes);
        }
    }

    auto dst_reshape = ov::as_type_ptr<ov::op::v1::Reshape>(dst.get_node_shared_ptr());
    if (!multidim_indices && is_cache_write && dst_reshape) {
        // Fix the case of multiple sequences, reshape back to original shape [1, n_seq, ctx_per_seq, emb]
        // ctx_per_seq is not fixed due to llama-bench compatibility
        auto dst_shape_partial = dst_reshape->get_input_partial_shape(0);
        std::vector<int64_t> dst_shape = {dst_shape_partial[0].get_length(), dst_shape_partial[1].get_length(),
                                          dst_shape_partial[2].is_static() ? dst_shape_partial[2].get_length() : -1,
                                          dst_shape_partial[3].get_length()};
        res = std::make_shared<ov::op::v1::Reshape>(res, ov::op::v0::Constant::create(ov::element::i64, {4}, dst_shape),
                                                    false);
    }
    return rename_outputs_with_suffix({res}, context.get_name());
}

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