|
| 1 | +#include "keep_sum_square_fp32.h" |
| 2 | + |
| 3 | +#include <map> |
| 4 | +#include <openvino/core/type/element_type.hpp> |
| 5 | +#include <openvino/core/visibility.hpp> |
| 6 | +#include <openvino/op/broadcast.hpp> |
| 7 | +#include <openvino/op/constant.hpp> |
| 8 | +#include <openvino/op/convert.hpp> |
| 9 | +#include <openvino/op/divide.hpp> |
| 10 | +#include <openvino/op/multiply.hpp> |
| 11 | +#include <openvino/op/reduce_sum.hpp> |
| 12 | +#include <openvino/op/sqrt.hpp> |
| 13 | +#include <openvino/op/tile.hpp> |
| 14 | +#include <openvino/pass/pattern/op/wrap_type.hpp> |
| 15 | +#include <unordered_set> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +#ifdef OPENVINO_STATIC_LIBRARY |
| 19 | +# define TRANSFORMATIONS_API |
| 20 | +#else |
| 21 | +# ifdef IMPLEMENT_OPENVINO_API |
| 22 | +# define TRANSFORMATIONS_API OPENVINO_CORE_EXPORTS |
| 23 | +# else |
| 24 | +# define TRANSFORMATIONS_API OPENVINO_CORE_IMPORTS |
| 25 | +# endif // IMPLEMENT_OPENVINO_API |
| 26 | +#endif // OPENVINO_STATIC_LIBRARY |
| 27 | + |
| 28 | +namespace ov { |
| 29 | +// from transformations/rt_info/disable_precision_conversion.hpp, which the OpenVINO runtime package does not ship |
| 30 | +TRANSFORMATIONS_API void disable_conversion(const std::shared_ptr<Node> & node, const element::Type & to); |
| 31 | +} // namespace ov |
| 32 | + |
| 33 | +namespace ov { |
| 34 | +namespace frontend { |
| 35 | +namespace ggml { |
| 36 | +namespace pass { |
| 37 | + |
| 38 | +namespace { |
| 39 | +// the only consumer of node's output 0, or nullptr |
| 40 | +std::shared_ptr<ov::Node> single_consumer(const std::shared_ptr<ov::Node> & node) { |
| 41 | + const auto targets = node->output(0).get_target_inputs(); |
| 42 | + return targets.size() == 1 ? targets.begin()->get_node()->shared_from_this() : nullptr; |
| 43 | +} |
| 44 | + |
| 45 | +bool is_scalar_constant(const ov::Output<ov::Node> & output) { |
| 46 | + auto constant = ov::as_type_ptr<ov::op::v0::Constant>(output.get_node_shared_ptr()); |
| 47 | + return constant && ov::shape_size(constant->get_shape()) == 1; |
| 48 | +} |
| 49 | + |
| 50 | +// Marks the island to stay in f32 and puts a no-op Convert on every edge into or out of it. |
| 51 | +// MarkSugraphsToKeepInMixedPrecision spreads the mark through elementwise/reshape/slice/concat ops in both |
| 52 | +// directions and stops at a Convert. Without the Converts it reaches most of the graph, which then runs in |
| 53 | +// f32 with a Reorder at every boundary and makes decode slow. |
| 54 | +void keep_island_in_fp32(const std::vector<std::shared_ptr<ov::Node>> & island) { |
| 55 | + std::unordered_set<const ov::Node *> members; |
| 56 | + for (const auto & node : island) { |
| 57 | + members.insert(node.get()); |
| 58 | + ov::disable_conversion(node, ov::element::f16); |
| 59 | + } |
| 60 | + |
| 61 | + std::map<ov::Output<ov::Node>, std::shared_ptr<ov::Node>> converts_in; |
| 62 | + for (const auto & node : island) { |
| 63 | + for (auto input : node->inputs()) { |
| 64 | + const auto source = input.get_source_output(); |
| 65 | + const auto * producer = source.get_node(); |
| 66 | + if (members.count(producer) || ov::is_type<ov::op::v0::Constant>(producer) || |
| 67 | + ov::is_type<ov::op::v0::Convert>(producer)) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + auto & convert = converts_in[source]; |
| 71 | + if (!convert) { |
| 72 | + convert = std::make_shared<ov::op::v0::Convert>(source, source.get_element_type()); |
| 73 | + } |
| 74 | + input.replace_source_output(convert); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + for (const auto & node : island) { |
| 79 | + for (const auto & output : node->outputs()) { |
| 80 | + std::shared_ptr<ov::Node> convert; |
| 81 | + for (auto target : output.get_target_inputs()) { |
| 82 | + const auto * consumer = target.get_node(); |
| 83 | + if (members.count(consumer) || ov::is_type<ov::op::v0::Convert>(consumer)) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + if (!convert) { |
| 87 | + convert = std::make_shared<ov::op::v0::Convert>(output, output.get_element_type()); |
| 88 | + } |
| 89 | + target.replace_source_output(convert); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +} // namespace |
| 95 | + |
| 96 | +KeepSumSquareFP32::KeepSumSquareFP32() { |
| 97 | + auto square = ov::pass::pattern::wrap_type<ov::op::v1::Multiply>(); |
| 98 | + auto pattern = ov::pass::pattern::wrap_type<ov::op::v1::ReduceSum>({square, ov::pass::pattern::any_input()}); |
| 99 | + |
| 100 | + const auto callback = [=](ov::pass::pattern::Matcher & m) { |
| 101 | + auto sum = m.get_match_root(); |
| 102 | + auto square_node = m.get_pattern_value_map().at(square).get_node_shared_ptr(); |
| 103 | + if (square_node->input_value(0) != square_node->input_value(1)) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + std::vector<std::shared_ptr<ov::Node>> island{square_node, sum}; |
| 107 | + |
| 108 | + // optional ggml_scale (e.g. 1/(n-1) in a std), then Sqrt brings the result back into f16 range |
| 109 | + auto next = single_consumer(sum); |
| 110 | + if (ov::is_type<ov::op::v1::Multiply>(next) && |
| 111 | + (is_scalar_constant(next->input_value(0)) || is_scalar_constant(next->input_value(1)))) { |
| 112 | + island.push_back(next); |
| 113 | + next = single_consumer(next); |
| 114 | + } |
| 115 | + if (ov::is_type<ov::op::v0::Sqrt>(next)) { |
| 116 | + island.push_back(next); |
| 117 | + |
| 118 | + // a * s / ||x|| (Gemma 3n altup magnitude matching): a * s overflows f16 before the Divide |
| 119 | + std::vector<std::shared_ptr<ov::Node>> norms{next}; |
| 120 | + for (size_t i = 0; i < norms.size(); ++i) { |
| 121 | + for (const auto & target : norms[i]->output(0).get_target_inputs()) { |
| 122 | + auto consumer = target.get_node()->shared_from_this(); |
| 123 | + if (ov::is_type<ov::op::v0::Tile>(consumer) || ov::is_type<ov::op::v3::Broadcast>(consumer)) { |
| 124 | + norms.push_back(consumer); |
| 125 | + } else if (ov::is_type<ov::op::v1::Divide>(consumer) && target.get_index() == 1) { |
| 126 | + island.push_back(consumer); |
| 127 | + auto numerator = consumer->get_input_node_shared_ptr(0); |
| 128 | + if (ov::is_type<ov::op::v1::Multiply>(numerator)) { |
| 129 | + island.push_back(numerator); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + keep_island_in_fp32(island); |
| 137 | + return true; |
| 138 | + }; |
| 139 | + |
| 140 | + register_matcher(std::make_shared<ov::pass::pattern::Matcher>(pattern, "ov::frontend::ggml::pass::KeepSumSquareFP32"), |
| 141 | + callback); |
| 142 | +} |
| 143 | + |
| 144 | +} // namespace pass |
| 145 | +} // namespace ggml |
| 146 | +} // namespace frontend |
| 147 | +} // namespace ov |
0 commit comments