ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-openvino / openvino / op / view.cpp
| 1 | #include "../op_table.h" |
| 2 | #include "../utils.h" |
| 3 | |
| 4 | #include <openvino/op/concat.hpp> |
| 5 | #include <openvino/op/constant.hpp> |
| 6 | #include <openvino/op/gather.hpp> |
| 7 | #include <openvino/op/reshape.hpp> |
| 8 | #include <openvino/op/shape_of.hpp> |
| 9 | #include <openvino/op/slice.hpp> |
| 10 | |
| 11 | namespace ov { |
| 12 | namespace frontend { |
| 13 | namespace ggml { |
| 14 | namespace op { |
| 15 | |
| 16 | OutputVector translate_view(const NodeContext & context) { |
| 17 | num_inputs_check(context, 1, 1); |
| 18 | |
| 19 | if (context.get_op_case() == 1) { |
| 20 | // Static-mode identity pass-through for VIEWs over a GATED_DELTA_NET combined output or |
| 21 | // the conv_input CONCAT; the consuming op (CPY/RMS_NORM) does its own runtime-correct |
| 22 | // slicing on the full tensor (see ggml-decoder.cpp compute_op_case, GGML_OP_VIEW). |
| 23 | return {context.get_input(0)}; |
| 24 | } |
| 25 | |
| 26 | if (!context.is_static()) { |
| 27 | // On the stateless/non-static path VIEW is normally a no-op (consumers re-slice). |
| 28 | // EXCEPTION: the MoE expert aggregation slices each expert plane out of |
| 29 | // ffn_moe_weighted [n_embd, n_expert_used, n_tokens] with ggml_view_2d and then |
| 30 | // sums the planes with a chain of ADDs (llama-graph.cpp). Those ADDs read this |
| 31 | // VIEW node directly from the tensor map and do NOT re-slice, so a no-op here |
| 32 | // makes every plane the full tensor and the expert sum collapses. Materialize the |
| 33 | // single-expert slice here. Gated by name (ffn_moe_weighted...view) so it can't |
| 34 | // affect any other view. |
| 35 | const std::string & vname = context.get_name(); |
| 36 | if (vname.find("ffn_moe_weighted") != std::string::npos) { |
| 37 | auto src_ps = context.get_input_shape(0); |
| 38 | auto dst_ps = context.get_output_shape(); |
| 39 | if (src_ps.rank().is_static() && dst_ps.rank().is_static() && src_ps.rank() == dst_ps.rank() && |
| 40 | src_ps.is_static() && dst_ps.is_static()) { |
| 41 | auto sst = context.get_input_stride(0); |
| 42 | auto dst = context.get_output_stride(); |
| 43 | size_t voff = context.get_output_op_offset(); |
| 44 | auto ss = src_ps.to_shape(); |
| 45 | auto dd = dst_ps.to_shape(); |
| 46 | const size_t nd = ss.size(); |
| 47 | // Stateful models drop the leading size-1 batch dim, so the real OV tensor can |
| 48 | // be one rank lower than the ggml shape metadata above. Axis indices derived |
| 49 | // from that metadata must be shifted down by the difference before they are |
| 50 | // used as OV axes. Bail out if the dims we would drop are not all 1. |
| 51 | const auto in_rank = context.get_input(0).get_partial_shape().rank(); |
| 52 | const int axis_shift = |
| 53 | in_rank.is_static() ? (int) nd - (int) in_rank.get_length() : 0; |
| 54 | bool shift_ok = axis_shift >= 0 && (size_t) axis_shift < nd; |
| 55 | for (int a = 0; a < axis_shift && shift_ok; ++a) { |
| 56 | shift_ok = (ss[a] == 1 && dd[a] == 1); |
| 57 | } |
| 58 | if (shift_ok && sst.size() == nd && dst.size() == nd) { |
| 59 | // Map each dst axis of size>1 to a src axis with equal (size,stride); |
| 60 | // the unmatched src axis of size>1 is the indexed expert axis. |
| 61 | // dst_to_src[d] records which src axis each dst axis came from, so we can |
| 62 | // later pull the dynamic (token) dim from the right source axis at runtime. |
| 63 | std::vector<bool> used(nd, false); |
| 64 | std::vector<int> dst_to_src(nd, -1); |
| 65 | bool ok = true; |
| 66 | for (size_t d = 0; d < nd; ++d) { |
| 67 | if (dd[d] == 1) { |
| 68 | continue; |
| 69 | } |
| 70 | int found = -1; |
| 71 | for (size_t s = 0; s < nd; ++s) { |
| 72 | if (!used[s] && ss[s] == dd[d] && sst[s] == dst[d]) { |
| 73 | found = (int) s; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | if (found < 0) { |
| 78 | ok = false; |
| 79 | break; |
| 80 | } |
| 81 | used[found] = true; |
| 82 | dst_to_src[d] = found; |
| 83 | } |
| 84 | int dropped = -1; |
| 85 | if (ok) { |
| 86 | for (size_t s = 0; s < nd; ++s) { |
| 87 | if (!used[s] && ss[s] > 1) { |
| 88 | if (dropped >= 0) { |
| 89 | ok = false; |
| 90 | break; |
| 91 | } |
| 92 | dropped = (int) s; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | if (ok && dropped >= 0) { |
| 97 | const size_t dstr = sst[dropped]; |
| 98 | const int64_t dsz = (int64_t) ss[dropped]; |
| 99 | if (dstr > 0 && voff % dstr == 0) { |
| 100 | const int64_t sel = (int64_t) (voff / dstr); |
| 101 | if (sel >= 0 && sel < dsz) { |
| 102 | ov::Output<ov::Node> sl = std::make_shared<ov::op::v8::Slice>( |
| 103 | context.get_input(0), |
| 104 | ov::op::v0::Constant::create(ov::element::i64, {1}, {sel}), |
| 105 | ov::op::v0::Constant::create(ov::element::i64, {1}, {sel + 1}), |
| 106 | ov::op::v0::Constant::create(ov::element::i64, {1}, {1}), |
| 107 | ov::op::v0::Constant::create(ov::element::i64, {1}, |
| 108 | {dropped - axis_shift})); |
| 109 | // Build the reshape target from the (concrete) dst shape, but |
| 110 | // keep the dynamic token axis dynamic instead of freezing it |
| 111 | // to the captured n_tokens. Without this the constant dst |
| 112 | // shape bakes in the prefill token count and the static value |
| 113 | // flows downstream, turning every later decoder layer static |
| 114 | // (the GPU in-place-concat KV-cache bug). The token axis is |
| 115 | // PERMUTED between the sliced input and the dst (e.g. input |
| 116 | // [1,tok,expert,emb] -> dst [1,1,tok,emb]), so special_zero |
| 117 | // (which copies the same-position dim) is not enough: pull the |
| 118 | // dynamic dim from the correct SOURCE axis via ShapeOf+Gather |
| 119 | // and place it at the dst token position. |
| 120 | const int32_t dyn = context.get_op_dynamic_dim(); // output ggml axis, -1 if none |
| 121 | // still in ggml metadata axis space; get_shape() reverses ggml order |
| 122 | int dst_ov_axis = (dyn != -1) ? ((int) nd - 1 - (int) dyn) : -1; |
| 123 | int src_ov_axis = (dst_ov_axis >= axis_shift && dst_ov_axis < (int) nd) |
| 124 | ? dst_to_src[dst_ov_axis] |
| 125 | : -1; |
| 126 | if (dst_ov_axis >= 0 && src_ov_axis >= axis_shift) { |
| 127 | // target = concat of per-axis scalars; the token axis is a |
| 128 | // runtime Gather of the slice's shape, the rest are constants. |
| 129 | auto sl_shape = std::make_shared<ov::op::v3::ShapeOf>(sl, ov::element::i64); |
| 130 | auto tok_dim = std::make_shared<ov::op::v8::Gather>( |
| 131 | sl_shape, |
| 132 | ov::op::v0::Constant::create(ov::element::i64, {1}, |
| 133 | {src_ov_axis - axis_shift}), |
| 134 | ov::op::v0::Constant::create(ov::element::i64, {}, {0})); |
| 135 | ov::OutputVector parts; |
| 136 | for (int a = axis_shift; a < (int) nd; ++a) { |
| 137 | if (a == dst_ov_axis) { |
| 138 | parts.push_back(tok_dim); |
| 139 | } else { |
| 140 | parts.push_back(ov::op::v0::Constant::create( |
| 141 | ov::element::i64, {1}, {(int64_t) dd[a]})); |
| 142 | } |
| 143 | } |
| 144 | auto dc = std::make_shared<ov::op::v0::Concat>(parts, 0); |
| 145 | auto rs = std::make_shared<ov::op::v1::Reshape>(sl, dc, false); |
| 146 | return rename_outputs_with_suffix({rs}, context.get_name()); |
| 147 | } |
| 148 | std::vector<int64_t> dd_ov(dd.begin() + axis_shift, dd.end()); |
| 149 | auto dc = ov::op::v0::Constant::create(ov::element::i64, {dd_ov.size()}, dd_ov); |
| 150 | auto rs = std::make_shared<ov::op::v1::Reshape>(sl, dc, false); |
| 151 | return rename_outputs_with_suffix({rs}, context.get_name()); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | return {context.get_input(0)}; |
| 159 | } |
| 160 | |
| 161 | auto input = context.get_input(0); |
| 162 | auto src_shape = context.get_input_shape(0); |
| 163 | auto dst_shape = context.get_output_shape(); |
| 164 | |
| 165 | if (src_shape.rank().is_dynamic() || dst_shape.rank().is_dynamic()) { |
| 166 | return {input}; |
| 167 | } |
| 168 | |
| 169 | int64_t src_elems = 1; |
| 170 | int64_t dst_elems = 1; |
| 171 | for (int64_t i = 0; i < src_shape.rank().get_length(); ++i) { |
| 172 | if (src_shape[i].is_dynamic()) { |
| 173 | return {input}; |
| 174 | } |
| 175 | src_elems *= src_shape[i].get_length(); |
| 176 | } |
| 177 | for (int64_t i = 0; i < dst_shape.rank().get_length(); ++i) { |
| 178 | if (dst_shape[i].is_dynamic()) { |
| 179 | return {input}; |
| 180 | } |
| 181 | dst_elems *= dst_shape[i].get_length(); |
| 182 | } |
| 183 | |
| 184 | if (dst_elems >= src_elems) { |
| 185 | return {input}; |
| 186 | } |
| 187 | |
| 188 | auto src_stride = context.get_input_stride(0); |
| 189 | auto dst_stride = context.get_output_stride(); |
| 190 | size_t view_offset = context.get_output_op_offset(); |
| 191 | |
| 192 | bool same_stride = (src_stride.size() == dst_stride.size()); |
| 193 | if (same_stride) { |
| 194 | for (size_t i = 0; i < src_stride.size(); ++i) { |
| 195 | if (src_stride[i] != dst_stride[i]) { |
| 196 | same_stride = false; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (!same_stride) { |
| 203 | return {input}; |
| 204 | } |
| 205 | |
| 206 | auto src_ov_shape = src_shape.to_shape(); |
| 207 | auto dst_ov_shape = dst_shape.to_shape(); |
| 208 | size_t ndims = src_ov_shape.size(); |
| 209 | if (dst_ov_shape.size() != ndims) { |
| 210 | return {input}; |
| 211 | } |
| 212 | |
| 213 | std::vector<int> diff_dims; |
| 214 | for (size_t i = 0; i < ndims; ++i) { |
| 215 | if (src_ov_shape[i] != dst_ov_shape[i]) { |
| 216 | diff_dims.push_back(static_cast<int>(i)); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (diff_dims.size() != 1) { |
| 221 | return {input}; |
| 222 | } |
| 223 | |
| 224 | int slice_dim = diff_dims[0]; |
| 225 | int64_t dim_size = static_cast<int64_t>(src_ov_shape[slice_dim]); |
| 226 | |
| 227 | size_t ov_stride_for_dim = 1; |
| 228 | for (size_t i = slice_dim + 1; i < ndims; ++i) { |
| 229 | ov_stride_for_dim *= src_ov_shape[i]; |
| 230 | } |
| 231 | size_t elem_size = src_stride.back(); |
| 232 | if (elem_size == 0) { |
| 233 | elem_size = 1; |
| 234 | } |
| 235 | |
| 236 | int64_t begin_val = 0; |
| 237 | if (ov_stride_for_dim > 0 && elem_size > 0) { |
| 238 | begin_val = static_cast<int64_t>((view_offset / elem_size) / ov_stride_for_dim); |
| 239 | } |
| 240 | int64_t end_val = begin_val + static_cast<int64_t>(dst_ov_shape[slice_dim]); |
| 241 | |
| 242 | if (begin_val < 0 || end_val > dim_size) { |
| 243 | return {input}; |
| 244 | } |
| 245 | |
| 246 | auto sliced = |
| 247 | std::make_shared<ov::op::v8::Slice>(input, ov::op::v0::Constant::create(ov::element::i64, {1}, {begin_val}), |
| 248 | ov::op::v0::Constant::create(ov::element::i64, {1}, {end_val}), |
| 249 | ov::op::v0::Constant::create(ov::element::i64, {1}, {1}), |
| 250 | ov::op::v0::Constant::create(ov::element::i64, {1}, {slice_dim})); |
| 251 | |
| 252 | sliced->set_friendly_name(context.get_output_name()); |
| 253 | return {sliced->output(0)}; |
| 254 | } |
| 255 | |
| 256 | } // namespace op |
| 257 | } // namespace ggml |
| 258 | } // namespace frontend |
| 259 | } // namespace ov |