char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Zijun YuOpenVINO: Qwen3.5, memory optimization, and test-recurrent-state-rollback (#26952)aee56b3ab

char/aliceai-t5-moe
2.8 KiB76 linesraw
1#include "../node_context.h"
2#include "../op_table.h"
3#include "../utils.h"
4
5#include <cstdint>
6#include <openvino/frontend/exception.hpp>
7#include <openvino/op/add.hpp>
8#include <openvino/op/constant.hpp>
9#include <openvino/op/convert.hpp>
10#include <openvino/op/range.hpp>
11#include <openvino/op/reduce_prod.hpp>
12#include <openvino/op/reshape.hpp>
13#include <openvino/op/scatter_update.hpp>
14#include <openvino/op/shape_of.hpp>
15
16namespace ov {
17namespace frontend {
18namespace ggml {
19namespace op {
20
21// GGML SET writes src1 into a view of src0 and returns the updated tensor.
22OutputVector translate_set(const NodeContext & context) {
23    num_inputs_check(context, 2, 2);
24
25    auto dst = process_view_input_new(context, 0);
26    auto src = process_view_input_new(context, 1);
27
28    src = std::make_shared<ov::op::v0::Convert>(src, context.get_output_type());
29
30    const auto dst_stride = context.get_input_stride(0);
31    FRONT_END_OP_CONVERSION_CHECK(dst_stride.size() >= 4, "SET requires 4D destination strides");
32
33    const auto * op_params = reinterpret_cast<const uint32_t *>(context.get_output_op_params());
34    const size_t offset = static_cast<size_t>(op_params[3]);
35
36    const size_t elem_size = dst_stride.back();
37    FRONT_END_OP_CONVERSION_CHECK(elem_size != 0 && offset % elem_size == 0,
38                                  "SET offset must be aligned to destination element size");
39
40    const int64_t offset_elems = static_cast<int64_t>(offset / elem_size);
41
42    auto dst_flat = std::make_shared<ov::op::v1::Reshape>(
43        dst,
44        ov::op::v0::Constant::create(ov::element::i64, {1}, {-1}),
45        false);
46
47    auto src_flat = std::make_shared<ov::op::v1::Reshape>(
48        src,
49        ov::op::v0::Constant::create(ov::element::i64, {1}, {-1}),
50        false);
51
52    auto src_shape = std::make_shared<ov::op::v3::ShapeOf>(src_flat, ov::element::i64);
53    auto src_len = std::make_shared<ov::op::v1::ReduceProd>(
54        src_shape,
55        ov::op::v0::Constant::create(ov::element::i64, {1}, {0}),
56        false);
57
58    auto start = ov::op::v0::Constant::create(ov::element::i64, {}, {offset_elems});
59    auto stop = std::make_shared<ov::op::v1::Add>(start, src_len);
60    auto step = ov::op::v0::Constant::create(ov::element::i64, {}, {1});
61
62    auto indices = std::make_shared<ov::op::v4::Range>(start, stop, step, ov::element::i64);
63    auto axis = ov::op::v0::Constant::create(ov::element::i64, {}, {0});
64
65    auto updated_flat = std::make_shared<ov::op::v3::ScatterUpdate>(dst_flat, indices, src_flat, axis);
66
67    auto dst_shape = std::make_shared<ov::op::v3::ShapeOf>(dst, ov::element::i64);
68    auto res = std::make_shared<ov::op::v1::Reshape>(updated_flat, dst_shape, false);
69
70    return rename_outputs_with_suffix({res}, context.get_name());
71}
72
73}  // namespace op
74}  // namespace ggml
75}  // namespace frontend
76}  // namespace ov