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/op/div.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 / op / div.cpp` 138 lines ยท 5458 bytes Raw Blame History 1 #include "../node_context.h" 2 #include "../op_table.h" 3 #include "../utils.h" 4 #include "ggml.h" 5 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 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 & numerator, 27 const ov::Output & denominator, 28 const NodeContext & context) { 29 if (context.get_input_size() != 2) { 30 return false; 31 } 32 33 const auto * unary_op = reinterpret_cast(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(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 repeat_input_to_match(const NodeContext & context, 47 const ov::Output & input, 48 const ov::Output & 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(input_shape.rank().get_length()); 59 std::vector 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(input, repeats_node); 82 } 83 84 auto input_shape_node = std::make_shared(input, ov::element::i64); 85 auto target_shape_node = std::make_shared(target, ov::element::i64); 86 auto repeats_node = std::make_shared(target_shape_node, input_shape_node); 87 return std::make_shared(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 res = std::make_shared(input_1); 100 if (res.get_element_type() != context.get_output_type()) { 101 res = std::make_shared(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(input_0, ov::element::f32); 114 input_1 = std::make_shared(input_1, ov::element::f32); 115 } 116 117 ov::Output res = std::make_shared(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(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