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/upscale.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 / upscale.cpp` 181 lines ยท 8212 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 15 namespace ov { 16 namespace frontend { 17 namespace ggml { 18 namespace op { 19 20 OutputVector translate_upscale(const NodeContext & context) { 21 num_inputs_check(context, 1, 1); 22 23 using Interpolate = ov::op::v4::Interpolate; 24 auto input = process_view_input_new(context, 0); 25 26 const auto input_shape = context.get_input_shape(0).to_shape(); 27 const auto output_shape = context.get_output_shape().to_shape(); 28 29 if (input_shape == output_shape) { 30 return rename_outputs_with_suffix({input}, context.get_name()); 31 } 32 33 ov::Output res = input; 34 35 // Resample batch / channel dimensions (ne[3] and ne[2], corresponding to OV axes 0 and 1) 36 // using nearest-neighbor index mapping: i0_d = floor(i_d * in_d / out_d). 37 for (size_t axis = 0; axis < 2; ++axis) { 38 const size_t in_dim = input_shape[axis]; 39 const size_t out_dim = output_shape[axis]; 40 if (in_dim != out_dim) { 41 std::vector indices(out_dim); 42 for (size_t i = 0; i < out_dim; ++i) { 43 indices[i] = static_cast((i * in_dim) / out_dim); 44 } 45 auto indices_node = ov::op::v0::Constant::create(ov::element::i64, {indices.size()}, indices); 46 auto axis_node = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {axis}); 47 res = std::make_shared(res, indices_node, axis_node); 48 } 49 } 50 51 // Spatial interpolation for ne[1] and ne[0] (OV axes 2 and 3) 52 if (input_shape[2] != output_shape[2] || input_shape[3] != output_shape[3]) { 53 const int32_t * op_params = context.get_output_op_params(); 54 const int32_t mode_flags = op_params != nullptr ? op_params[0] : 0; 55 56 const int op_case = context.get_op_case(); 57 const bool align_corners = (mode_flags & GGML_SCALE_FLAG_ALIGN_CORNERS) != 0; 58 const bool antialias = (mode_flags & GGML_SCALE_FLAG_ANTIALIAS) != 0; 59 60 if (op_case == 2 && antialias) { // GGML_SCALE_MODE_BILINEAR with antialias 61 const size_t H_in = input_shape[2]; 62 const size_t W_in = input_shape[3]; 63 const size_t H_out = output_shape[2]; 64 const size_t W_out = output_shape[3]; 65 66 const float pixel_offset = 0.5f; 67 68 // Height projection: [H_out, H_in] 69 std::vector Wy_data(H_out * H_in, 0.0f); 70 const float sf1 = static_cast(H_out) / H_in; 71 const float support1 = std::max(1.0f, 1.0f / sf1); 72 const float invscale1 = 1.0f / support1; 73 74 for (size_t i1 = 0; i1 < H_out; ++i1) { 75 const float y = (static_cast(i1) + pixel_offset) / sf1; 76 const auto y_start = static_cast(y - support1 + pixel_offset); 77 const size_t y_min = y_start > 0 ? static_cast(y_start) : 0; 78 const auto y_end = static_cast(y + support1 + pixel_offset); 79 const size_t y_max = y_end > 0 ? std::min(static_cast(y_end), H_in) : 0; 80 81 float total_weight = 0.0f; 82 for (size_t sy = y_min; sy < y_max; ++sy) { 83 float diff = std::abs((static_cast(sy) - y + pixel_offset) * invscale1); 84 float weight = std::max(1.0f - diff, 0.0f); 85 Wy_data[i1 * H_in + sy] = weight; 86 total_weight += weight; 87 } 88 if (total_weight > 0.0f) { 89 for (size_t sy = y_min; sy < y_max; ++sy) { 90 Wy_data[i1 * H_in + sy] /= total_weight; 91 } 92 } 93 } 94 95 // Width projection: [W_in, W_out] 96 std::vector Wx_data(W_in * W_out, 0.0f); 97 const float sf0 = static_cast(W_out) / W_in; 98 const float support0 = std::max(1.0f, 1.0f / sf0); 99 const float invscale0 = 1.0f / support0; 100 101 for (size_t i0 = 0; i0 < W_out; ++i0) { 102 const float x = (static_cast(i0) + pixel_offset) / sf0; 103 const auto x_start = static_cast(x - support0 + pixel_offset); 104 const size_t x_min = x_start > 0 ? static_cast(x_start) : 0; 105 const auto x_end = static_cast(x + support0 + pixel_offset); 106 const size_t x_max = x_end > 0 ? std::min(static_cast(x_end), W_in) : 0; 107 108 float total_weight = 0.0f; 109 for (size_t sx = x_min; sx < x_max; ++sx) { 110 float diff = std::abs((static_cast(sx) - x + pixel_offset) * invscale0); 111 float weight = std::max(1.0f - diff, 0.0f); 112 Wx_data[sx * W_out + i0] = weight; 113 total_weight += weight; 114 } 115 if (total_weight > 0.0f) { 116 for (size_t sx = x_min; sx < x_max; ++sx) { 117 Wx_data[sx * W_out + i0] /= total_weight; 118 } 119 } 120 } 121 122 auto Wy_node = ov::op::v0::Constant::create(ov::element::f32, {H_out, H_in}, Wy_data); 123 auto Wx_node = ov::op::v0::Constant::create(ov::element::f32, {W_in, W_out}, Wx_data); 124 125 auto res_y = std::make_shared(Wy_node, res); 126 res = std::make_shared(res_y, Wx_node); 127 } else { 128 Interpolate::InterpolateAttrs attrs; 129 attrs.shape_calculation_mode = Interpolate::ShapeCalcMode::SIZES; 130 attrs.antialias = antialias; 131 attrs.pads_begin = {0, 0, 0, 0}; 132 attrs.pads_end = {0, 0, 0, 0}; 133 134 switch (op_case) { 135 case 1: // GGML_SCALE_MODE_NEAREST 136 attrs.mode = Interpolate::InterpolateMode::NEAREST; 137 attrs.nearest_mode = Interpolate::NearestMode::FLOOR; 138 attrs.coordinate_transformation_mode = Interpolate::CoordinateTransformMode::ASYMMETRIC; 139 break; 140 case 2: // GGML_SCALE_MODE_BILINEAR 141 attrs.mode = Interpolate::InterpolateMode::LINEAR; 142 attrs.coordinate_transformation_mode = align_corners 143 ? Interpolate::CoordinateTransformMode::ALIGN_CORNERS 144 : Interpolate::CoordinateTransformMode::HALF_PIXEL; 145 break; 146 case 3: // GGML_SCALE_MODE_BICUBIC 147 attrs.mode = Interpolate::InterpolateMode::CUBIC; 148 attrs.cube_coeff = -0.75; 149 attrs.coordinate_transformation_mode = align_corners 150 ? Interpolate::CoordinateTransformMode::ALIGN_CORNERS 151 : Interpolate::CoordinateTransformMode::HALF_PIXEL; 152 break; 153 default: 154 FRONT_END_OP_CONVERSION_CHECK(false, "Unsupported upscale op_case: ", op_case); 155 } 156 157 std::vector target_shape_vec = { 158 static_cast(output_shape[2]), 159 static_cast(output_shape[3]), 160 }; 161 std::vector scales_vec = { 162 static_cast(output_shape[2]) / static_cast(input_shape[2]), 163 static_cast(output_shape[3]) / static_cast(input_shape[3]), 164 }; 165 166 auto target_shape_node = ov::op::v0::Constant::create(ov::element::i64, {2}, target_shape_vec); 167 auto scales_node = ov::op::v0::Constant::create(ov::element::f32, {2}, scales_vec); 168 auto axes_node = ov::op::v0::Constant::create(ov::element::i64, {2}, {2, 3}); 169 170 res = std::make_shared( 171 res, target_shape_node, scales_node, axes_node, attrs); 172 } 173 } 174 175 return rename_outputs_with_suffix({res}, context.get_name()); 176 } 177 178 } // namespace op 179 } // namespace ggml 180 } // namespace frontend 181 } // namespace ov