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/glu_swiglu.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 / glu_swiglu.cpp` 131 lines ยท 5537 bytes Raw Blame History 1 #include "../node_context.h" 2 #include "../op_table.h" 3 #include "../utils.h" 4 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 17 namespace ov { 18 namespace frontend { 19 namespace ggml { 20 namespace op { 21 22 static std::pair, ov::Output> get_glu_inputs(const NodeContext & context) { 23 num_inputs_check(context, 1, 2); 24 25 ov::Output src0; 26 ov::Output src1; 27 if (context.get_input_size() == 2) { 28 // Inputs may be VIEW slices of a combined gate_up tensor (MoE experts): 29 // resolve them so each half has its real sliced shape, not the base tensor. 30 src0 = process_view_input_new(context, 0); 31 src1 = process_view_input_new(context, 1); 32 } else { 33 // GGML splits along ne[0] (OV last axis) using floor division: nc = ne[0] / 2. 34 // Both halves are nc elements; if the dimension is odd, the last element is dropped. 35 // Use Slice instead of Split to handle odd dimensions correctly. 36 // Resolve a VIEW input (e.g. non-contiguous slice) to its real shape first. 37 auto combined = process_view_input_new(context, 0); 38 auto combined_shape = combined.get_partial_shape(); 39 int64_t last_dim_val = combined_shape[combined_shape.rank().get_length() - 1].get_length(); 40 int64_t nc = last_dim_val / 2; 41 42 auto axis = ov::op::v0::Constant::create(ov::element::i64, {1}, {-1}); 43 auto step = ov::op::v0::Constant::create(ov::element::i64, {1}, {1}); 44 auto start0 = ov::op::v0::Constant::create(ov::element::i64, {1}, {0}); 45 auto stop0 = ov::op::v0::Constant::create(ov::element::i64, {1}, {nc}); 46 auto start1 = ov::op::v0::Constant::create(ov::element::i64, {1}, {nc}); 47 auto stop1 = ov::op::v0::Constant::create(ov::element::i64, {1}, {2 * nc}); 48 49 src0 = std::make_shared(combined, start0, stop0, step, axis); 50 src1 = std::make_shared(combined, start1, stop1, step, axis); 51 } 52 53 int32_t * params = context.get_output_op_params(); 54 const int32_t swapped = params[1]; 55 if (swapped) { 56 std::swap(src0, src1); 57 } 58 59 return {src0, src1}; 60 } 61 62 OutputVector translate_glu_swiglu(const NodeContext & context) { 63 auto [src0, src1] = get_glu_inputs(context); 64 65 auto silu = std::make_shared(src0); 66 auto res = std::make_shared(silu, src1); 67 68 return rename_outputs_with_suffix({res}, context.get_name()); 69 } 70 71 OutputVector translate_glu_swiglu_oai(const NodeContext & context) { 72 auto [src0, src1] = get_glu_inputs(context); 73 74 const int32_t * params = context.get_output_op_params(); 75 const float alpha = reinterpret_cast(params)[2]; 76 const float limit = reinterpret_cast(params)[3]; 77 78 // Compute in f32: alpha_const/one are f32, and Swish/Add need matching input types. 79 auto output_type = context.get_output_type(); 80 if (src0.get_element_type() != ov::element::f32) { 81 src0 = std::make_shared(src0, ov::element::f32); 82 } 83 if (src1.get_element_type() != ov::element::f32) { 84 src1 = std::make_shared(src1, ov::element::f32); 85 } 86 87 auto gate = std::make_shared(src0, -std::numeric_limits::infinity(), limit); 88 auto alpha_const = ov::op::v0::Constant::create(ov::element::f32, {}, {alpha}); 89 auto out_glu = std::make_shared(gate, alpha_const); 90 91 auto up = std::make_shared(src1, -limit, limit); 92 auto one = ov::op::v0::Constant::create(ov::element::f32, {}, {1.0f}); 93 auto up_plus_one = std::make_shared(up, one); 94 ov::Output res = std::make_shared(out_glu, up_plus_one); 95 if (res.get_element_type() != output_type) { 96 res = std::make_shared(res, output_type); 97 } 98 99 return rename_outputs_with_suffix({res}, context.get_name()); 100 } 101 102 OutputVector translate_glu_swiglu_clamp(const NodeContext & context) { 103 auto [src0, src1] = get_glu_inputs(context); 104 105 const int32_t * params = context.get_output_op_params(); 106 const float limit = reinterpret_cast(params)[3]; 107 108 // Compute in f32: f16 Swish/Clamp rounding drifts past the 1e-7 test tolerance. 109 auto output_type = context.get_output_type(); 110 if (src0.get_element_type() != ov::element::f32) { 111 src0 = std::make_shared(src0, ov::element::f32); 112 } 113 if (src1.get_element_type() != ov::element::f32) { 114 src1 = std::make_shared(src1, ov::element::f32); 115 } 116 117 auto gate = std::make_shared(src0, -std::numeric_limits::infinity(), limit); 118 auto silu = std::make_shared(gate); 119 auto up = std::make_shared(src1, -limit, limit); 120 ov::Output res = std::make_shared(silu, up); 121 if (res.get_element_type() != output_type) { 122 res = std::make_shared(res, output_type); 123 } 124 125 return rename_outputs_with_suffix({res}, context.get_name()); 126 } 127 128 } // namespace op 129 } // namespace ggml 130 } // namespace frontend 131 } // namespace ov