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 / div.cpp
| 1 | #include "../node_context.h" |
| 2 | #include "../op_table.h" |
| 3 | #include "../utils.h" |
| 4 | #include "ggml.h" |
| 5 | |
| 6 | #include <memory> |
| 7 | #include <openvino/core/type.hpp> |
| 8 | #include <openvino/op/constant.hpp> |
| 9 | #include <openvino/op/convert.hpp> |
| 10 | #include <openvino/op/divide.hpp> |
| 11 | #include <openvino/op/multiply.hpp> |
| 12 | #include <openvino/op/shape_of.hpp> |
| 13 | #include <openvino/op/sigmoid.hpp> |
| 14 | #include <openvino/op/swish.hpp> |
| 15 | #include <openvino/op/tile.hpp> |
| 16 | #include <openvino/op/util/precision_sensitive_attribute.hpp> |
| 17 | #include <vector> |
| 18 | |
| 19 | namespace ov { |
| 20 | namespace frontend { |
| 21 | namespace ggml { |
| 22 | namespace op { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | bool is_silu_div_pattern(const ov::Output<ov::Node> & numerator, |
| 27 | const ov::Output<ov::Node> & denominator, |
| 28 | const NodeContext & context) { |
| 29 | if (context.get_input_size() != 2) { |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | const auto * unary_op = reinterpret_cast<const ggml_unary_op *>(context.get_input_op_params(0)); |
| 34 | if (unary_op == nullptr || *unary_op != GGML_UNARY_OP_SILU) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | const auto denom_node = denominator.get_node_shared_ptr(); |
| 39 | |
| 40 | if (auto swish = ov::as_type_ptr<ov::op::v4::Swish>(numerator.get_node_shared_ptr())) { |
| 41 | return swish->input_value(0).get_node_shared_ptr() == denom_node; |
| 42 | } |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | ov::Output<ov::Node> repeat_input_to_match(const NodeContext & context, |
| 47 | const ov::Output<ov::Node> & input, |
| 48 | const ov::Output<ov::Node> & target, |
| 49 | size_t input_index) { |
| 50 | const auto input_shape = context.get_input_shape(input_index); |
| 51 | const auto target_shape = context.get_input_shape(0); |
| 52 | |
| 53 | if (input_shape == target_shape) { |
| 54 | return input; |
| 55 | } |
| 56 | |
| 57 | if (input_shape.rank().is_static() && target_shape.rank().is_static()) { |
| 58 | const auto rank = static_cast<size_t>(input_shape.rank().get_length()); |
| 59 | std::vector<int64_t> repeats(rank, 1); |
| 60 | bool needs_repeat = false; |
| 61 | |
| 62 | for (size_t axis = 0; axis < rank; ++axis) { |
| 63 | FRONT_END_OP_CONVERSION_CHECK(input_shape[axis].is_static() && target_shape[axis].is_static(), |
| 64 | "DIV repeat requires static dimensions on both inputs"); |
| 65 | |
| 66 | const int64_t input_dim = input_shape[axis].get_length(); |
| 67 | const int64_t target_dim = target_shape[axis].get_length(); |
| 68 | |
| 69 | FRONT_END_OP_CONVERSION_CHECK(input_dim > 0 && target_dim > 0 && target_dim % input_dim == 0, |
| 70 | "DIV input shape ", input_shape, " cannot repeat to match ", target_shape); |
| 71 | |
| 72 | repeats[axis] = target_dim / input_dim; |
| 73 | needs_repeat = needs_repeat || repeats[axis] != 1; |
| 74 | } |
| 75 | |
| 76 | if (!needs_repeat) { |
| 77 | return input; |
| 78 | } |
| 79 | |
| 80 | auto repeats_node = ov::op::v0::Constant::create(ov::element::i64, {repeats.size()}, repeats); |
| 81 | return std::make_shared<ov::op::v0::Tile>(input, repeats_node); |
| 82 | } |
| 83 | |
| 84 | auto input_shape_node = std::make_shared<ov::op::v3::ShapeOf>(input, ov::element::i64); |
| 85 | auto target_shape_node = std::make_shared<ov::op::v3::ShapeOf>(target, ov::element::i64); |
| 86 | auto repeats_node = std::make_shared<ov::op::v1::Divide>(target_shape_node, input_shape_node); |
| 87 | return std::make_shared<ov::op::v0::Tile>(input, repeats_node); |
| 88 | } |
| 89 | |
| 90 | } // namespace |
| 91 | |
| 92 | OutputVector translate_div(const NodeContext & context) { |
| 93 | num_inputs_check(context, 2, 2); |
| 94 | |
| 95 | auto input_0 = process_view_input_new(context, 0); |
| 96 | auto input_1 = process_view_input_new(context, 1); |
| 97 | |
| 98 | if (is_silu_div_pattern(input_0, input_1, context)) { |
| 99 | ov::Output<ov::Node> res = std::make_shared<ov::op::v0::Sigmoid>(input_1); |
| 100 | if (res.get_element_type() != context.get_output_type()) { |
| 101 | res = std::make_shared<ov::op::v0::Convert>(res, context.get_output_type()); |
| 102 | } |
| 103 | return rename_outputs_with_suffix({res}, context.get_name()); |
| 104 | } |
| 105 | |
| 106 | input_1 = repeat_input_to_match(context, input_1, input_0, 1); |
| 107 | |
| 108 | const auto output_type = context.get_output_type(); |
| 109 | const bool use_f32_compute = input_0.get_element_type() != ov::element::f32 || |
| 110 | input_1.get_element_type() != ov::element::f32 || output_type != ov::element::f32; |
| 111 | |
| 112 | if (use_f32_compute) { |
| 113 | input_0 = std::make_shared<ov::op::v0::Convert>(input_0, ov::element::f32); |
| 114 | input_1 = std::make_shared<ov::op::v0::Convert>(input_1, ov::element::f32); |
| 115 | } |
| 116 | |
| 117 | ov::Output<ov::Node> res = std::make_shared<ov::op::v1::Divide>(input_0, input_1); |
| 118 | if (use_f32_compute) { |
| 119 | // Keep the reciprocal/divide path in FP32. Without this hint, the GPU |
| 120 | // plugin can still compress the subgraph back to FP16 and overflow on |
| 121 | // small shexp gate values (e.g. silu(x) / x in qwen2moe). |
| 122 | ov::mark_as_precision_sensitive(res.get_node_shared_ptr()->input(0)); |
| 123 | ov::mark_as_precision_sensitive(res.get_node_shared_ptr()->input(1)); |
| 124 | } |
| 125 | if (res.get_element_type() != output_type) { |
| 126 | auto output_convert = std::make_shared<ov::op::v0::Convert>(res, output_type); |
| 127 | if (use_f32_compute) { |
| 128 | ov::mark_as_precision_sensitive(output_convert->input(0)); |
| 129 | } |
| 130 | res = output_convert; |
| 131 | } |
| 132 | return rename_outputs_with_suffix({res}, context.get_name()); |
| 133 | } |
| 134 | |
| 135 | } // namespace op |
| 136 | } // namespace ggml |
| 137 | } // namespace frontend |
| 138 | } // namespace ov |