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
5.9 KiB113 linesraw
1#include "../node_context.h"
2#include "../op_table.h"
3#include "../utils.h"
4
5#include <cstdint>
6#include <memory>
7#include <openvino/core/node.hpp>
8#include <openvino/core/node_output.hpp>
9#include <openvino/frontend/exception.hpp>
10#include <openvino/op/concat.hpp>
11#include <openvino/op/constant.hpp>
12#include <openvino/op/reshape.hpp>
13#include <vector>
14
15namespace ov {
16namespace frontend {
17namespace ggml {
18namespace op {
19
20OutputVector translate_reshape(const NodeContext & context) {
21    num_inputs_check(context, 1, 1);
22    if (context.get_input(0).get_partial_shape().is_static() &&
23        context.get_input_shape(0) == context.get_output_shape()) {
24        return {context.get_input(0)};
25    }
26
27    int op_case = context.get_op_case();
28
29    auto output_shape = context.get_output_shape().to_shape();
30    std::shared_ptr<ov::Node> new_shape_node;
31    if (op_case == 0) {
32        new_shape_node = ov::op::v0::Constant::create(ov::element::i64, {4}, context.get_output_shape().to_shape());
33    } else if (op_case == 1) {
34        if (context.is_stateful()) {
35            new_shape_node = ov::op::v0::Constant::create(
36                ov::element::i64, {3}, std::vector<int64_t>{-1, (int64_t) output_shape[2], (int64_t) output_shape[3]});
37        } else {
38            new_shape_node = ov::op::v0::Constant::create(
39                ov::element::i64, {4},
40                std::vector<int64_t>{(int64_t) output_shape[0], -1, (int64_t) output_shape[2],
41                                     (int64_t) output_shape[3]});
42        }
43    } else if (op_case == 2) {
44        new_shape_node = ov::op::v0::Constant::create(
45            ov::element::i64, {4},
46            std::vector<int64_t>{(int64_t) output_shape[0], (int64_t) output_shape[1], -1, (int64_t) output_shape[3]});
47
48    } else if (op_case == 3) {
49        //  -  14: [     1,  1024,     1,     1] RESHAPE              Vcur-0 (reshaped) (reshaped)
50        //         [   512,     2,     1,     1]            0: RESHAPE     Vcur-0 (reshaped)
51        //  -  15: [     1, 524288,     1,     1] RESHAPE              cache_v_l0 (reshaped)
52        //         [   512,  1024,     1,     1]            0: NONE        cache_v_l0
53        //  -  16: [     1, 524288,     1,     1] SET_ROWS             cache_v_l0 (reshaped) (view)
54        //         [     1,  1024,     1,     1]            0: RESHAPE     Vcur-0 (reshaped) (reshaped)
55        //         [  1024,     1,     1,     1]            1: NONE        leaf_11
56        //         [     1, 524288,     1,     1]            2: RESHAPE     cache_v_l0 (reshaped)
57        new_shape_node = ov::op::v0::Constant::create(
58            ov::element::i64, {4}, std::vector<int64_t>{(int64_t) output_shape[0], (int64_t) output_shape[1], -1, 1});
59
60    } else if (op_case == 4) {
61        return {context.get_input(0).get_node_shared_ptr()->input_value(0)};
62
63    } else if (op_case == 5) {
64        if (context.is_stateful()) {
65            std::vector<int64_t> shape_vec = {1, -1, (int64_t) context.get_output_shape().to_shape()[3]};
66            new_shape_node = ov::op::v0::Constant::create(ov::element::i64, {3}, shape_vec);
67        } else {
68            std::vector<int64_t> shape_vec = {1, 1, -1, (int64_t) context.get_output_shape().to_shape()[3]};
69            new_shape_node = ov::op::v0::Constant::create(ov::element::i64, {4}, shape_vec);
70        }
71
72        // // Alternative
73        // auto token_len = context.get_input("token_len");
74        // auto emb_size =
75        //     ov::op::v0::Constant::create(ov::element::i64, {1}, {(int64_t) context.get_output_shape().to_shape()[3]});
76        // auto one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1});
77        // new_shape_node = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{one, one, token_len, emb_size}, 0);
78    } else if (op_case == 6) {
79        // 14: [  6144,     1,     2,     1] RESHAPE              linear_attn_qkv_mixed-0
80        //     [  6144,     2,     1,     1]            0: MUL_MAT     node_13
81        // reshape to [1, n_slot_active_len, -1, 6144]
82        if (context.has_input("s_copy_active_slot_len")) {
83            auto n_slot_active_len = context.get_input("s_copy_active_slot_len");
84            auto emb_size = ov::op::v0::Constant::create(ov::element::i64, {1},
85                                                         {(int64_t) context.get_output_shape().to_shape()[3]});
86            auto one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1});
87            auto neg_one = ov::op::v0::Constant::create(ov::element::i64, {1}, {-1});
88            new_shape_node =
89                std::make_shared<ov::op::v0::Concat>(ov::OutputVector{one, n_slot_active_len, neg_one, emb_size}, 0);
90        } else {
91            new_shape_node = ov::op::v0::Constant::create(ov::element::i64, {4}, context.get_output_shape().to_shape());
92        }
93    } else if (op_case == 7) {
94        // 57: [  2048,     2,     1,     1] RESHAPE              linear_attn_out-0 (reshaped)
95        //     [  2048,     1,     2,     1]            0: MUL_MAT     linear_attn_out-0
96        std::vector<int64_t> shape_vec = {1, 1, -1, (int64_t) context.get_output_shape().to_shape()[3]};
97        new_shape_node = ov::op::v0::Constant::create(ov::element::i64, {4}, shape_vec);
98    } else if (op_case == 8) {
99        // 106: [   128,   128,    16,     2] RESHAPE              state_predelta-1
100        //      [ 262144,     2,     1,     1]            0: GET_ROWS    node_86
101        auto output_shape = context.get_output_shape().to_shape();
102        std::vector<int64_t> shape_vec = {-1, (int64_t) output_shape[1], (int64_t) output_shape[2],
103                                          (int64_t) output_shape[3]};
104        new_shape_node = ov::op::v0::Constant::create(ov::element::i64, {4}, shape_vec);
105    }
106    auto res = std::make_shared<ov::op::v1::Reshape>(context.get_input(0), new_shape_node, false);
107    return rename_outputs_with_suffix({res}, context.get_name());
108}
109
110}  // namespace op
111}  // namespace ggml
112}  // namespace frontend
113}  // namespace ov