interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

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 <numeric>
4#include <openvino/op/add.hpp>
5#include <openvino/op/constant.hpp>
6#include <openvino/op/divide.hpp>
7#include <openvino/op/multiply.hpp>
8#include <openvino/op/sqrt.hpp>
9#include <openvino/op/subtract.hpp>
10#include <openvino/op/unsqueeze.hpp>
11#include <openvino/pass/pattern/op/wrap_type.hpp>
12
13namespace ov {
14namespace frontend {
15namespace ggml {
16namespace pass {
17
18namespace {
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.
22bool is_rms_norm_output(const ov::Output<ov::Node> & value, int depth = 1) {
23 const auto * node = value.get_node();
24 if (!ov::is_type<ov::op::v1::Multiply>(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<ov::op::v1::Divide>(src) && ov::is_type<ov::op::v0::Sqrt>(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
41AlignEltwiseOperandRanks::AlignEltwiseOperandRanks() {
42 auto eltwise_m = ov::pass::pattern::wrap_type<ov::op::v1::Add, ov::op::v1::Multiply, ov::op::v1::Subtract>();
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<ov::op::v0::Constant>(lhs.get_node()) ||
57 ov::is_type<ov::op::v0::Constant>(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<int64_t> axes(static_cast<size_t>(diff));
79 std::iota(axes.begin(), axes.end(), 0);
80 auto unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(
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<ov::pass::pattern::Matcher>(eltwise_m, "ov::frontend::ggml::pass::AlignEltwiseOperandRanks"),
90 callback);
91}
92
93} // namespace pass
94} // namespace ggml
95} // namespace frontend
96} // namespace ov