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 / ssm_conv.cpp
| 1 | #include "../node_context.h" |
| 2 | #include "../op_table.h" |
| 3 | #include "../utils.h" |
| 4 | |
| 5 | #include <openvino/op/constant.hpp> |
| 6 | #include <openvino/op/group_conv.hpp> |
| 7 | #include <openvino/op/reshape.hpp> |
| 8 | #include <openvino/op/squeeze.hpp> |
| 9 | #include <openvino/op/transpose.hpp> |
| 10 | #include <openvino/op/unsqueeze.hpp> |
| 11 | |
| 12 | namespace ov { |
| 13 | namespace frontend { |
| 14 | namespace ggml { |
| 15 | namespace op { |
| 16 | |
| 17 | OutputVector translate_ssm_conv(const NodeContext & context) { |
| 18 | num_inputs_check(context, 2, 2); |
| 19 | |
| 20 | auto sx = context.get_input(0); // conv state + input: OV shape [1, n_s, d_inner, ncs] |
| 21 | auto c = context.get_input(1); // conv1d weight: OV shape [1, 1, d_inner, d_conv] |
| 22 | |
| 23 | auto sx_shape = context.get_input_shape(0).to_shape(); // [1, n_s, d_inner, ncs] |
| 24 | auto c_shape = context.get_input_shape(1).to_shape(); // [1, 1, d_inner, d_conv] |
| 25 | |
| 26 | // int64_t n_s = sx_shape[1]; |
| 27 | int64_t d_inner = sx_shape[2]; |
| 28 | // int64_t ncs = sx_shape[3]; // d_conv - 1 + n_t |
| 29 | int64_t d_conv = c_shape[3]; |
| 30 | // int64_t n_t = ncs - d_conv + 1; |
| 31 | |
| 32 | // Reshape sx from [1, n_s, d_inner, ncs] to [n_s, d_inner, ncs] for 1D GroupConvolution |
| 33 | auto sx_reshaped = |
| 34 | std::make_shared<ov::op::v0::Squeeze>(sx, ov::op::v0::Constant::create(ov::element::i64, {1}, {0})); |
| 35 | |
| 36 | // Reshape c from [1, 1, d_inner, d_conv] to [d_inner, 1, 1, d_conv] |
| 37 | // GroupConvolution filter: [groups, out_channels/groups, in_channels/groups, kernel_size] |
| 38 | auto c_new_shape = ov::op::v0::Constant::create(ov::element::i64, {4}, std::vector<int64_t>{d_inner, 1, 1, d_conv}); |
| 39 | auto c_reshaped = std::make_shared<ov::op::v1::Reshape>(c, c_new_shape, false); |
| 40 | |
| 41 | // Depthwise 1D convolution: groups=d_inner, stride=1, no padding, no dilation |
| 42 | // Input: [n_s, d_inner, ncs], Filter: [d_inner, 1, 1, d_conv] |
| 43 | // Output: [n_s, d_inner, n_t] |
| 44 | auto conv = std::make_shared<ov::op::v1::GroupConvolution>( |
| 45 | sx_reshaped, c_reshaped, ov::Strides{1}, ov::CoordinateDiff{0}, ov::CoordinateDiff{0}, ov::Strides{1}); |
| 46 | |
| 47 | // Transpose from [n_s, d_inner, n_t] to [n_s, n_t, d_inner] |
| 48 | auto perm = ov::op::v0::Constant::create(ov::element::i64, {3}, std::vector<int64_t>{0, 2, 1}); |
| 49 | auto transposed = std::make_shared<ov::op::v1::Transpose>(conv, perm); |
| 50 | |
| 51 | // Reshape to output shape [1, n_s, n_t, d_inner] |
| 52 | auto res = |
| 53 | std::make_shared<ov::op::v0::Unsqueeze>(transposed, ov::op::v0::Constant::create(ov::element::i64, {1}, {0})); |
| 54 | |
| 55 | return rename_outputs_with_suffix({res}, context.get_name()); |
| 56 | } |
| 57 | |
| 58 | } // namespace op |
| 59 | } // namespace ggml |
| 60 | } // namespace frontend |
| 61 | } // namespace ov |