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 / utils.h
| 1 | #pragma once |
| 2 | |
| 3 | #include "node_context.h" |
| 4 | |
| 5 | #include <memory> |
| 6 | #include <openvino/core/node.hpp> |
| 7 | #include <openvino/op/convert.hpp> |
| 8 | #include <openvino/op/shape_of.hpp> |
| 9 | #include <openvino/op/slice.hpp> |
| 10 | #include <utility> |
| 11 | |
| 12 | namespace ov { |
| 13 | namespace frontend { |
| 14 | namespace ggml { |
| 15 | |
| 16 | std::string getCurrentTime(); |
| 17 | |
| 18 | void num_inputs_check(const NodeContext & context, size_t min_inputs, size_t max_inputs); |
| 19 | |
| 20 | int non_cont_dim(std::vector<size_t> ne, std::vector<size_t> nb); |
| 21 | |
| 22 | template <typename T> std::vector<int> argsort_descend(const std::vector<T> & v) { |
| 23 | std::vector<int> idx(v.size()); |
| 24 | std::iota(idx.begin(), idx.end(), 0); |
| 25 | std::sort(idx.begin(), idx.end(), [&v](int i1, int i2) { return v[i1] > v[i2]; }); |
| 26 | return idx; |
| 27 | } |
| 28 | |
| 29 | template <typename T> std::vector<T> sorted_descend(std::vector<T> v) { |
| 30 | std::sort(v.begin(), v.end(), [](T a, T b) { return a > b; }); |
| 31 | return v; |
| 32 | } |
| 33 | |
| 34 | template <typename T> bool is_permuted(const std::vector<T> & strides) { |
| 35 | for (size_t i = 0; i < strides.size() - 1; ++i) { |
| 36 | if (strides[i] < strides[i + 1]) { |
| 37 | return true; |
| 38 | } |
| 39 | } |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | template <typename T> std::vector<T> permute(const std::vector<T> & x, const std::vector<int> & perm) { |
| 44 | std::vector<T> result; |
| 45 | result.reserve(perm.size()); |
| 46 | for (int i : perm) { |
| 47 | result.push_back(x[i]); |
| 48 | } |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<ov::op::v3::ShapeOf> & shape, |
| 53 | const std::vector<int> & dims); |
| 54 | std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<ov::Node> & node, const std::vector<int> & dims); |
| 55 | |
| 56 | OutputVector rename_outputs_with_suffix(const OutputVector & outputs, const std::string & suffix); |
| 57 | |
| 58 | std::pair<ov::Output<Node>, ov::Output<Node>> make_sin_cos(int32_t * rope_params, |
| 59 | std::shared_ptr<ov::Node> inp_pos, |
| 60 | std::shared_ptr<ov::Node> rope_freqs_weight = nullptr, |
| 61 | int mode = 0, |
| 62 | bool stateful = false, |
| 63 | int64_t head_dim = 0); |
| 64 | |
| 65 | ov::Output<ov::Node> process_view_input(const NodeContext & context, int input_index, int slice_len = 0, int axis = -1); |
| 66 | |
| 67 | ov::Output<ov::Node> process_view_input_new(const NodeContext & context, int input_index); |
| 68 | |
| 69 | namespace op { |
| 70 | template <typename T> OutputVector translate_1to1_match_2_inputs(const NodeContext & context) { |
| 71 | num_inputs_check(context, 2, 2); |
| 72 | auto input_0 = process_view_input_new(context, 0); |
| 73 | auto input_1 = process_view_input_new(context, 1); |
| 74 | |
| 75 | auto output_type = context.get_output_type(); |
| 76 | if (input_0.get_element_type() != input_1.get_element_type()) { |
| 77 | if (input_0.get_element_type() != ov::element::f32) { |
| 78 | input_0 = std::make_shared<ov::op::v0::Convert>(input_0, ov::element::f32); |
| 79 | } |
| 80 | if (input_1.get_element_type() != ov::element::f32) { |
| 81 | input_1 = std::make_shared<ov::op::v0::Convert>(input_1, ov::element::f32); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | ov::Output<ov::Node> res = std::make_shared<T>(input_0, input_1); |
| 86 | if (res.get_element_type() != output_type) { |
| 87 | res = std::make_shared<ov::op::v0::Convert>(res, output_type); |
| 88 | } |
| 89 | return rename_outputs_with_suffix({res}, context.get_name()); |
| 90 | } |
| 91 | |
| 92 | template <typename T> OutputVector translate_1to1_match_1_input(const NodeContext & context) { |
| 93 | num_inputs_check(context, 1, 1); |
| 94 | auto input = process_view_input_new(context, 0); |
| 95 | auto res = std::make_shared<T>(input); |
| 96 | return rename_outputs_with_suffix({res}, context.get_name()); |
| 97 | } |
| 98 | } // namespace op |
| 99 | |
| 100 | } // namespace ggml |
| 101 | } // namespace frontend |
| 102 | } // namespace ov |