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 / pad.cpp
| 1 | #include "../op_table.h" |
| 2 | #include "../utils.h" |
| 3 | |
| 4 | #include <array> |
| 5 | #include <cstdint> |
| 6 | #include <openvino/op/constant.hpp> |
| 7 | #include <openvino/op/gather.hpp> |
| 8 | #include <openvino/op/pad.hpp> |
| 9 | #include <openvino/op/reshape.hpp> |
| 10 | #include <openvino/op/shape_of.hpp> |
| 11 | #include <utility> |
| 12 | #include <vector> |
| 13 | |
| 14 | namespace ov { |
| 15 | namespace frontend { |
| 16 | namespace ggml { |
| 17 | namespace op { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | ov::Output<ov::Node> translate_circular_pad(ov::Output<ov::Node> input, |
| 22 | const std::array<int32_t, 8> & pads, |
| 23 | const ov::Shape & input_shape) { |
| 24 | ov::Output<ov::Node> result = std::move(input); |
| 25 | |
| 26 | const std::array<int32_t, 4> pads_begin = {pads[6], pads[4], pads[2], pads[0]}; |
| 27 | const std::array<int32_t, 4> pads_end = {pads[7], pads[5], pads[3], pads[1]}; |
| 28 | |
| 29 | for (size_t axis = 0; axis < input_shape.size(); ++axis) { |
| 30 | const int64_t input_dim = static_cast<int64_t>(input_shape[axis]); |
| 31 | const int64_t pad_begin = pads_begin[axis]; |
| 32 | const int64_t pad_end = pads_end[axis]; |
| 33 | |
| 34 | if (pad_begin == 0 && pad_end == 0) { |
| 35 | continue; |
| 36 | } |
| 37 | |
| 38 | FRONT_END_CHECK_IMPLEMENTED(input_dim > 0, "Circular PAD requires static non-zero input dimensions"); |
| 39 | |
| 40 | std::vector<int64_t> indices(static_cast<size_t>(input_dim + pad_begin + pad_end)); |
| 41 | for (int64_t index = 0; index < static_cast<int64_t>(indices.size()); ++index) { |
| 42 | int64_t wrapped = (index - pad_begin) % input_dim; |
| 43 | if (wrapped < 0) { |
| 44 | wrapped += input_dim; |
| 45 | } |
| 46 | indices[static_cast<size_t>(index)] = wrapped; |
| 47 | } |
| 48 | |
| 49 | auto gather_indices = ov::op::v0::Constant::create(ov::element::i64, {indices.size()}, indices); |
| 50 | auto gather_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {axis}); |
| 51 | result = std::make_shared<ov::op::v8::Gather>(result, gather_indices, gather_axis); |
| 52 | } |
| 53 | |
| 54 | return result; |
| 55 | } |
| 56 | |
| 57 | } // namespace |
| 58 | |
| 59 | OutputVector translate_pad(const NodeContext & context) { |
| 60 | num_inputs_check(context, 1, 1); |
| 61 | |
| 62 | auto input = process_view_input_new(context, 0); |
| 63 | if (context.get_input_shape(0) == context.get_output_shape()) { |
| 64 | return rename_outputs_with_suffix({input}, context.get_name()); |
| 65 | } |
| 66 | |
| 67 | const int32_t * op_params = context.get_output_op_params(); |
| 68 | FRONT_END_CHECK_IMPLEMENTED(op_params != nullptr, "PAD requires output op params"); |
| 69 | |
| 70 | const std::array<int32_t, 8> pads = {op_params[0], op_params[1], op_params[2], op_params[3], |
| 71 | op_params[4], op_params[5], op_params[6], op_params[7]}; |
| 72 | const bool circular = op_params[8] != 0; |
| 73 | |
| 74 | if (circular) { |
| 75 | auto res = translate_circular_pad(input, pads, context.get_input_shape(0).to_shape()); |
| 76 | return rename_outputs_with_suffix({res}, context.get_name()); |
| 77 | } |
| 78 | |
| 79 | const std::vector<int64_t> pads_begin = {pads[6], pads[4], pads[2], pads[0]}; |
| 80 | const std::vector<int64_t> pads_end = {pads[7], pads[5], pads[3], pads[1]}; |
| 81 | |
| 82 | auto pads_begin_node = ov::op::v0::Constant::create(ov::element::i64, {pads_begin.size()}, pads_begin); |
| 83 | auto pads_end_node = ov::op::v0::Constant::create(ov::element::i64, {pads_end.size()}, pads_end); |
| 84 | auto pad_value = ov::op::v0::Constant::create(context.get_input_type(0), ov::Shape{}, {0}); |
| 85 | auto res = |
| 86 | std::make_shared<ov::op::v1::Pad>(input, pads_begin_node, pads_end_node, pad_value, ov::op::PadMode::CONSTANT); |
| 87 | |
| 88 | return rename_outputs_with_suffix({res}, context.get_name()); |
| 89 | } |
| 90 | |
| 91 | } // namespace op |
| 92 | } // namespace ggml |
| 93 | } // namespace frontend |
| 94 | } // namespace ov |