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 / diag.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/eye.hpp> |
| 7 | #include <openvino/op/multiply.hpp> |
| 8 | |
| 9 | namespace ov { |
| 10 | namespace frontend { |
| 11 | namespace ggml { |
| 12 | namespace op { |
| 13 | |
| 14 | // GGML DIAG takes a 1D vector (ne0, 1, ne2, ne3) and produces a diagonal matrix |
| 15 | // of shape (ne0, ne0, ne2, ne3). |
| 16 | // In OV layout (ggml [ne0, ne1, ne2, ne3] → OV [ne3, ne2, ne1, ne0]): |
| 17 | // input: [ne3, ne2, 1, ne0] |
| 18 | // output: [ne3, ne2, ne0, ne0] |
| 19 | // The diagonal: output[..., i, j] = input[..., 0, j] if i == j, else 0. |
| 20 | OutputVector translate_diag(const NodeContext & context) { |
| 21 | num_inputs_check(context, 1, 1); |
| 22 | |
| 23 | auto x = process_view_input_new(context, 0); // OV shape: [ne3, ne2, 1, ne0] |
| 24 | |
| 25 | auto n = get_dimensions(x.get_node_shared_ptr(), {3}); |
| 26 | auto zero_diag = ov::op::v0::Constant::create(ov::element::i64, {}, {0}); |
| 27 | |
| 28 | auto eye = std::make_shared<ov::op::v9::Eye>(n, n, zero_diag, x.get_element_type()); |
| 29 | auto res = std::make_shared<ov::op::v1::Multiply>(x, eye); |
| 30 | |
| 31 | return rename_outputs_with_suffix({res}, context.get_name()); |
| 32 | } |
| 33 | |
| 34 | } // namespace op |
| 35 | } // namespace ggml |
| 36 | } // namespace frontend |
| 37 | } // namespace ov |