Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-openvino/openvino/op/get_rows.cpp # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-openvino / openvino / op / get_rows.cpp` 178 lines ยท 10159 bytes Raw Blame History 1 #include "../node_context.h" 2 #include "../op_table.h" 3 #include "../utils.h" 4 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 18 19 namespace ov { 20 namespace frontend { 21 namespace ggml { 22 namespace op { 23 24 OutputVector translate_get_rows(const NodeContext & context) { 25 num_inputs_check(context, 2, 2); 26 27 Output res; 28 Output data; 29 int64_t row_offset = 0; 30 if (context.get_view_input_size(0) > 0 && context.get_input(0).get_partial_shape().rank() == 2) { 31 // A row range view over a 2D weight Constant folds into the gather indices, 32 // which keeps the dequantization subgraph intact for the plugins. 33 const auto view_shape = context.get_view_input_ggml_shape(0, 0); 34 const auto view_stride = context.get_view_input_stride(0, 0); 35 const size_t view_offset = context.get_view_input_offset(0, 0); 36 const size_t row_bytes = view_stride[2]; 37 data = context.get_input(0); 38 FRONT_END_OP_CONVERSION_CHECK(row_bytes > 0 && view_offset % row_bytes == 0 && 39 data.get_partial_shape()[1].compatible(view_shape[3]), 40 "GET_ROWS: view over a weight must be a row range"); 41 row_offset = static_cast(view_offset / row_bytes); 42 } else { 43 data = process_view_input_new(context, 0); 44 } 45 46 auto op_case = context.get_op_case(); 47 if (op_case == 3 || op_case == 4) { 48 return {data}; 49 } 50 51 ov::Output indices; 52 if ((op_case == 1 || op_case == 2) && context.has_input("s_copy_active_slot_len")) { 53 // Recurrent state reorder (inp->s_copy): slice the active (op_case 1) or extra (op_case 2) 54 // segment from the s_copy index list at runtime, instead of baking the static view offset, 55 // so the cached IR works for any number of active sequences. 56 auto s_copy = context.get_input(1); 57 auto len = context.get_input("s_copy_active_slot_len"); 58 auto step = ov::op::v0::Constant::create(ov::element::i64, {1}, {1}); 59 auto axis = ov::op::v0::Constant::create(ov::element::i64, {1}, {3}); 60 if (op_case == 1) { 61 auto begin = ov::op::v0::Constant::create(ov::element::i64, {1}, {0}); 62 indices = std::make_shared(s_copy, begin, len, step, axis); 63 } else { 64 auto end = ov::op::v0::Constant::create(ov::element::i64, {1}, {INT_MAX}); 65 indices = std::make_shared(s_copy, len, end, step, axis); 66 } 67 } else { 68 indices = process_view_input_new(context, 1); 69 } 70 71 // data[1,b,x,y] ind[1,1,b,x'] test-backend-ops case 72 // data[x,y] ind[1,1,1,x'] normal case 73 // Squeeze the leading dims down to [b,x']. Stateful models drop one rank, so a hardcoded 74 // {0,1} would also strip the batch dim whenever b == 1 (every decode step). 75 const auto indices_rank = indices.get_partial_shape().rank(); 76 FRONT_END_OP_CONVERSION_CHECK(indices_rank.is_static(), "Expected static rank for GET_ROWS indices"); 77 std::vector indices_squeeze_axes; 78 for (int64_t i = 0; i + 2 < indices_rank.get_length(); ++i) { 79 indices_squeeze_axes.push_back(i); 80 } 81 if (!indices_squeeze_axes.empty()) { 82 indices = std::make_shared( 83 indices, ov::op::v0::Constant::create(ov::element::i64, {indices_squeeze_axes.size()}, 84 indices_squeeze_axes)); 85 } 86 if (row_offset != 0) { 87 indices = std::make_shared( 88 indices, ov::op::v0::Constant::create(indices.get_element_type(), {}, {row_offset})); 89 } 90 if (data.get_partial_shape().rank() == 4) { 91 if (!(data.get_partial_shape()[1].is_dynamic()) && data.get_partial_shape()[1].get_length() == 1) { 92 // Work-around for a bug in ov cpu plugin for test-backend-ops 93 data = std::make_shared(data, 94 ov::op::v0::Constant::create(ov::element::i64, {2}, {0, 1})); 95 auto axis = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{}, {0}); 96 res = std::make_shared(data, indices, axis); 97 } else { 98 auto axis = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{}, {1}); 99 data = 100 std::make_shared(data, ov::op::v0::Constant::create(ov::element::i64, {1}, {0})); 101 // data: [batch, rows, ...], indices: [batch, n] - this is a batched gather 102 // (batch_dims=1) along the rows axis. The data and indices batch dims are 103 // logically equal (both == n_tokens) but reach this node through independent 104 // reshapes, so the GPU plugin's gather shape inference cannot prove 105 // data.shape[0] == indices.shape[0] and rejects the node. We must tie both 106 // batch dims to the SAME value, and crucially that value must stay DYNAMIC. 107 const auto data_ps = data.get_partial_shape(); 108 const auto idx_ps = indices.get_partial_shape(); 109 const bool data_batch_static = data_ps.rank().is_static() && data_ps[0].is_static(); 110 const bool idx_batch_dynamic = idx_ps.rank().is_dynamic() || idx_ps[0].is_dynamic(); 111 112 if (data_batch_static && idx_batch_dynamic) { 113 // MoE per-expert-scale path: `data` is a statically-tiled REPEAT 114 // (ggml_repeat_4d(scale, 1, n_expert, n_tokens, 1)) whose batch dim is a 115 // compile-time-constant n_tokens, and every batch slice is IDENTICAL (it was 116 // tiled from a single [1, n_expert, 1] scale). `indices` (selected_experts) 117 // carries the genuinely dynamic token dim. Broadcasting indices up to the 118 // static data batch (the naive fix) would freeze the token dim to the 119 // captured prefill length, and that static value then flows through the 120 // gather into the residual stream, making every following decoder layer 121 // static -> triggers the GPU in-place-concat KV-cache corruption (only 122 // layer 0 stays dynamic). A static->dynamic Broadcast cannot expand, so 123 // instead collapse the redundant data batch to 1 and broadcast 1->dynamic to 124 // match the indices batch. Mathematically identical (the slices are equal), 125 // and the whole graph stays dynamic. 126 auto zero = ov::op::v0::Constant::create(ov::element::i64, {1}, {0}); 127 auto one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1}); 128 auto axis0 = ov::op::v0::Constant::create(ov::element::i64, {1}, {0}); 129 auto data_b1 = std::make_shared(data, zero, one, one, axis0); // [1, rows, ...] 130 131 auto idx_shape = std::make_shared(indices, ov::element::i64); 132 auto idx_batch = get_dimensions(idx_shape, {0}); // [batch] (dynamic) 133 auto data_b1_shape = std::make_shared(data_b1, ov::element::i64); 134 const auto rank = data_ps.rank().get_length(); 135 std::vector rest_axes; 136 for (int a = 1; a < rank; ++a) { 137 rest_axes.push_back(a); 138 } 139 auto data_rest = get_dimensions(data_b1_shape, rest_axes); // [rows, ...] 140 auto data_target = std::make_shared(ov::OutputVector{idx_batch, data_rest}, 0); 141 data = 142 std::make_shared(data_b1, data_target, ov::op::BroadcastType::BIDIRECTIONAL); 143 res = std::make_shared(data, indices, axis, 1); 144 } else { 145 // General case: tie the indices batch to the data batch (the data batch is 146 // already dynamic, e.g. the routing-weights gather whose data comes from the 147 // activations). Broadcast indices to [data_batch, indices_n]. 148 auto data_shape = std::make_shared(data, ov::element::i64); 149 auto data_batch = get_dimensions(data_shape, {0}); // [batch] 150 auto idx_shape = std::make_shared(indices, ov::element::i64); 151 auto idx_n = get_dimensions(idx_shape, {1}); // [n] 152 auto idx_target = std::make_shared(ov::OutputVector{data_batch, idx_n}, 0); 153 indices = std::make_shared(indices, idx_target, 154 ov::op::BroadcastType::BIDIRECTIONAL); 155 res = std::make_shared(data, indices, axis, 1); 156 } 157 } 158 } else if (context.is_stateful() && data.get_partial_shape().rank() == 3) { 159 auto axis = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{}, {1}); 160 res = std::make_shared(data, indices, axis, 1); 161 } else { 162 auto axis = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{}, {0}); 163 res = std::make_shared(data, indices, axis); 164 } 165 166 if (res.get_element_type() != context.get_output_type()) { 167 res = std::make_shared(res, context.get_output_type()); 168 } 169 if (!(context.is_stateful())) { 170 res = std::make_shared(res, ov::op::v0::Constant::create(ov::element::i64, {1}, {0})); 171 } 172 return rename_outputs_with_suffix({res}, context.get_name()); 173 } 174 175 } // namespace op 176 } // namespace ggml 177 } // namespace frontend 178 } // namespace ov