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/pass/align_eltwise_ranks.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 / pass / align_eltwise_ranks.cpp` 96 lines ยท 3539 bytes Raw Blame History 1 #include "align_eltwise_ranks.h" 2 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 13 namespace ov { 14 namespace frontend { 15 namespace ggml { 16 namespace pass { 17 18 namespace { 19 20 // True for an RMS-norm output, x * (1 / sqrt(mean(x^2) + eps)) as translate_rms_norm builds it, optionally 21 // scaled by the norm weight. 22 bool is_rms_norm_output(const ov::Output & value, int depth = 1) { 23 const auto * node = value.get_node(); 24 if (!ov::is_type(node)) { 25 return false; 26 } 27 for (const auto & input : node->input_values()) { 28 const auto * src = input.get_node(); 29 if (ov::is_type(src) && ov::is_type(src->get_input_node_ptr(1))) { 30 return true; 31 } 32 if (depth > 0 && is_rms_norm_output(input, depth - 1)) { 33 return true; 34 } 35 } 36 return false; 37 } 38 39 } // namespace 40 41 AlignEltwiseOperandRanks::AlignEltwiseOperandRanks() { 42 auto eltwise_m = ov::pass::pattern::wrap_type(); 43 44 const auto callback = [this](ov::pass::pattern::Matcher & m) { 45 auto node = m.get_match_root(); 46 if (node->get_input_size() != 2) { 47 return false; 48 } 49 50 auto lhs = node->input_value(0); 51 auto rhs = node->input_value(1); 52 53 // A tensor-vs-Constant mismatch is the norm's own eps / 1-over-sqrt arithmetic. That 54 // folds into the `rms` primitive itself instead of becoming a fused eltwise post-op, 55 // so it is not affected and is left alone. 56 if (ov::is_type(lhs.get_node()) || 57 ov::is_type(rhs.get_node())) { 58 return false; 59 } 60 61 const auto lhs_rank = lhs.get_partial_shape().rank(); 62 const auto rhs_rank = rhs.get_partial_shape().rank(); 63 if (lhs_rank.is_dynamic() || rhs_rank.is_dynamic() || lhs_rank == rhs_rank) { 64 return false; 65 } 66 67 const size_t shorter_idx = lhs_rank.get_length() < rhs_rank.get_length() ? 0 : 1; 68 const auto & shorter = shorter_idx == 0 ? lhs : rhs; 69 70 // The defect is with the norm output as the higher-rank operand. When the norm output is the 71 // lower-rank one (gemma-3 adds it to a rank-4 residual), unsqueezing it puts the Unsqueeze between 72 // `rms` and its post-op, and the GPU plugin then computes the layer wrongly. 73 if (is_rms_norm_output(shorter)) { 74 return false; 75 } 76 const int64_t diff = std::abs(lhs_rank.get_length() - rhs_rank.get_length()); 77 78 std::vector axes(static_cast(diff)); 79 std::iota(axes.begin(), axes.end(), 0); 80 auto unsqueeze = std::make_shared( 81 shorter, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{ axes.size() }, axes)); 82 83 node->input(shorter_idx).replace_source_output(unsqueeze->output(0)); 84 register_new_node(unsqueeze); 85 return true; 86 }; 87 88 register_matcher( 89 std::make_shared(eltwise_m, "ov::frontend::ggml::pass::AlignEltwiseOperandRanks"), 90 callback); 91 } 92 93 } // namespace pass 94 } // namespace ggml 95 } // namespace frontend 96 } // namespace ov