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 / pass / fuse_argsort_topk.cpp
| 1 | #include "fuse_argsort_topk.h" |
| 2 | |
| 3 | #include <openvino/op/constant.hpp> |
| 4 | #include <openvino/op/slice.hpp> |
| 5 | #include <openvino/op/topk.hpp> |
| 6 | #include <openvino/pass/pattern/op/wrap_type.hpp> |
| 7 | |
| 8 | namespace ov { |
| 9 | namespace frontend { |
| 10 | namespace ggml { |
| 11 | namespace pass { |
| 12 | |
| 13 | FuseArgsortTopK::FuseArgsortTopK() { |
| 14 | // GGML argsort materializes the full ordering before a VIEW keeps the prefix. |
| 15 | // Let TopK produce only that prefix when all index consumers are such views. |
| 16 | auto pattern = ov::pass::pattern::wrap_type<ov::op::v11::TopK>(); |
| 17 | |
| 18 | const auto callback = [](ov::pass::pattern::Matcher & m) { |
| 19 | auto topk = ov::as_type_ptr<ov::op::v11::TopK>(m.get_match_root()); |
| 20 | if (!topk->output(0).get_target_inputs().empty() || topk->output(1).get_target_inputs().empty() || |
| 21 | topk->get_sort_type() != ov::op::v11::TopK::SortType::SORT_VALUES) { |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | const auto shape = topk->get_output_partial_shape(1); |
| 26 | if (shape.rank().is_dynamic()) { |
| 27 | return false; |
| 28 | } |
| 29 | const int64_t rank = shape.rank().get_length(); |
| 30 | const int64_t axis = topk->get_axis(); |
| 31 | if (shape[axis].is_dynamic()) { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | int64_t prefix = -1; |
| 36 | for (const auto & input : topk->output(1).get_target_inputs()) { |
| 37 | const auto * slice = ov::as_type<ov::op::v8::Slice>(input.get_node()); |
| 38 | if (!slice || input.get_index() != 0 || slice->get_input_size() != 5) { |
| 39 | return false; |
| 40 | } |
| 41 | int64_t values[4]; |
| 42 | for (size_t i = 0; i < 4; ++i) { |
| 43 | auto value = ov::as_type_ptr<ov::op::v0::Constant>(slice->get_input_node_shared_ptr(i + 1)); |
| 44 | if (!value || ov::shape_size(value->get_shape()) != 1) { |
| 45 | return false; |
| 46 | } |
| 47 | values[i] = value->cast_vector<int64_t>()[0]; |
| 48 | } |
| 49 | const int64_t slice_axis = values[3] < 0 ? values[3] + rank : values[3]; |
| 50 | if (values[0] != 0 || values[2] != 1 || slice_axis != axis || values[1] <= 0 || |
| 51 | values[1] >= shape[axis].get_length() || (prefix != -1 && prefix != values[1])) { |
| 52 | return false; |
| 53 | } |
| 54 | prefix = values[1]; |
| 55 | } |
| 56 | |
| 57 | // ggml_argsort_top_k sorts the full row, then exposes only its first k indices. |
| 58 | // Keep other TopK forms unchanged because their ordering can be observable. |
| 59 | topk->set_argument(1, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {prefix})); |
| 60 | topk->validate_and_infer_types(); |
| 61 | return true; |
| 62 | }; |
| 63 | |
| 64 | register_matcher(std::make_shared<ov::pass::pattern::Matcher>(pattern, "ov::frontend::ggml::pass::FuseArgsortTopK"), callback); |
| 65 | } |
| 66 | |
| 67 | } // namespace pass |
| 68 | } // namespace ggml |
| 69 | } // namespace frontend |
| 70 | } // namespace ov |