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 / node_context.h
| 1 | #pragma once |
| 2 | |
| 3 | #include "decoder.h" |
| 4 | |
| 5 | #include <cstdint> |
| 6 | #include <openvino/frontend/node_context.hpp> |
| 7 | #include <string> |
| 8 | |
| 9 | namespace ov { |
| 10 | namespace frontend { |
| 11 | namespace ggml { |
| 12 | |
| 13 | class TranslateSession; |
| 14 | |
| 15 | typedef std::map<std::string, Output<Node>> TensorMap; |
| 16 | |
| 17 | class NodeContext : public frontend::NodeContext { |
| 18 | public: |
| 19 | NodeContext(const std::shared_ptr<GgmlDecoder> & decoder, |
| 20 | std::shared_ptr<TensorMap> & tensor_map, |
| 21 | int node_idx, |
| 22 | TranslateSession * translate_session = nullptr) : |
| 23 | ov::frontend::NodeContext(decoder->get_op_type(node_idx)), |
| 24 | m_decoder(decoder), |
| 25 | m_tensor_map(tensor_map), |
| 26 | m_node_idx(node_idx), |
| 27 | m_translate_session(translate_session) { |
| 28 | m_input_names = decoder->get_input_names(m_node_idx); |
| 29 | m_output_names = decoder->get_output_names(m_node_idx); |
| 30 | } |
| 31 | |
| 32 | TranslateSession * get_translate_session() const { return m_translate_session; } |
| 33 | |
| 34 | const std::vector<std::string> & get_input_names() const { return m_input_names; } |
| 35 | |
| 36 | const std::vector<std::string> & get_output_names() const { return m_output_names; } |
| 37 | |
| 38 | size_t get_input_size() const override { return m_decoder->get_input_size(m_node_idx); } |
| 39 | |
| 40 | ov::element::Type get_input_type(size_t index) const { |
| 41 | return m_decoder->get_input_type(m_node_idx, m_input_names[index]); |
| 42 | } |
| 43 | |
| 44 | PartialShape get_input_shape(size_t input_index) const { |
| 45 | return m_decoder->get_input_shape(m_node_idx, m_input_names[input_index]); |
| 46 | } |
| 47 | |
| 48 | std::vector<size_t> get_input_stride(size_t index) const { |
| 49 | return m_decoder->get_input_stride(m_node_idx, m_input_names[index]); |
| 50 | } |
| 51 | |
| 52 | std::string get_output_name() const { return m_output_names[0]; } |
| 53 | |
| 54 | PartialShape get_output_shape() const { return m_decoder->get_output_shape(m_node_idx); } |
| 55 | |
| 56 | int32_t * get_input_op_params(size_t index) const { |
| 57 | return m_decoder->get_input_op_params(m_node_idx, m_input_names[index]); |
| 58 | } |
| 59 | |
| 60 | size_t get_view_input_size(size_t index) const { |
| 61 | return m_decoder->get_view_input_size(m_node_idx, m_input_names[index]); |
| 62 | } |
| 63 | |
| 64 | size_t get_view_input_offset(size_t index, size_t view_index) const { |
| 65 | return m_decoder->get_view_input_offset(m_node_idx, m_input_names[index], view_index); |
| 66 | } |
| 67 | |
| 68 | size_t get_view_input_src_offset(size_t index, size_t view_index) const { |
| 69 | return m_decoder->get_view_input_src_offset(m_node_idx, m_input_names[index], view_index); |
| 70 | } |
| 71 | |
| 72 | std::vector<size_t> get_view_input_stride(size_t index, size_t view_index) const { |
| 73 | return m_decoder->get_view_input_stride(m_node_idx, m_input_names[index], view_index); |
| 74 | } |
| 75 | |
| 76 | std::vector<size_t> get_view_input_src_stride(size_t index, size_t view_index) const { |
| 77 | return m_decoder->get_view_input_src_stride(m_node_idx, m_input_names[index], view_index); |
| 78 | } |
| 79 | |
| 80 | ov::Shape get_view_input_ggml_shape(size_t index, size_t view_index) const { |
| 81 | return m_decoder->get_view_input_ggml_shape(m_node_idx, m_input_names[index], view_index); |
| 82 | } |
| 83 | |
| 84 | ov::Shape get_view_input_src_ggml_shape(size_t index, size_t view_index) const { |
| 85 | return m_decoder->get_view_input_src_ggml_shape(m_node_idx, m_input_names[index], view_index); |
| 86 | } |
| 87 | |
| 88 | ov::PartialShape get_view_input_ov_shape(size_t index, size_t view_index) const { |
| 89 | return m_decoder->get_view_input_ov_shape(m_node_idx, m_input_names[index], view_index); |
| 90 | } |
| 91 | |
| 92 | ov::PartialShape get_view_input_src_ov_shape(size_t index, size_t view_index) const { |
| 93 | return m_decoder->get_view_input_src_ov_shape(m_node_idx, m_input_names[index], view_index); |
| 94 | } |
| 95 | |
| 96 | std::string get_view_input_name(size_t index, size_t view_index) const { |
| 97 | return m_decoder->get_view_input_name(m_node_idx, m_input_names[index], view_index); |
| 98 | } |
| 99 | |
| 100 | std::string get_view_input_src_name(size_t index, size_t view_index) const { |
| 101 | return m_decoder->get_view_input_src_name(m_node_idx, m_input_names[index], view_index); |
| 102 | } |
| 103 | |
| 104 | int32_t get_op_dynamic_dim() const { return m_decoder->get_op_dynamic_dim(m_node_idx); } |
| 105 | |
| 106 | int32_t * get_output_op_params() const { return m_decoder->get_output_op_params(m_node_idx); } |
| 107 | |
| 108 | size_t get_output_op_offset() const { return m_decoder->get_output_op_offset(m_node_idx); } |
| 109 | |
| 110 | ov::element::Type get_output_type() const { return m_decoder->get_output_type(m_node_idx); } |
| 111 | |
| 112 | std::vector<size_t> get_output_stride() const { return m_decoder->get_output_stride(m_node_idx); } |
| 113 | |
| 114 | Output<Node> get_input(int idx) const override { |
| 115 | // Check if this input is a VIEW |
| 116 | size_t view_input_size = m_decoder->get_view_input_size(m_node_idx, m_input_names[idx]); |
| 117 | if (view_input_size > 0) { |
| 118 | // This is a VIEW input, get the base tensor name (last element in the chain) |
| 119 | std::string base_name = |
| 120 | m_decoder->get_view_input_src_name(m_node_idx, m_input_names[idx], view_input_size - 1); |
| 121 | // Check if the VIEW has been resolved (translate_view produced a Slice) |
| 122 | auto view_it = m_tensor_map->find(m_input_names[idx]); |
| 123 | if (!base_name.empty() && view_it != m_tensor_map->end()) { |
| 124 | auto base_it = m_tensor_map->find(base_name); |
| 125 | // A multi-output translator can publish a VIEW directly without materializing |
| 126 | // its packed parent (GatedDeltaNet attention/state). In that case the VIEW is the |
| 127 | // authoritative value. The node comparison retains the existing resolved-VIEW path. |
| 128 | if (base_it == m_tensor_map->end() || |
| 129 | view_it->second.get_node_shared_ptr() != base_it->second.get_node_shared_ptr()) { |
| 130 | return view_it->second; |
| 131 | } |
| 132 | return base_it->second; |
| 133 | } |
| 134 | if (!base_name.empty()) { |
| 135 | return m_tensor_map->at(base_name); |
| 136 | } |
| 137 | } |
| 138 | // Not a VIEW or failed to get base name, use the original logic |
| 139 | return m_tensor_map->at(m_input_names[idx]); |
| 140 | } |
| 141 | |
| 142 | Output<Node> get_input(const std::string & name) const override { |
| 143 | if (m_tensor_map->find(name) == m_tensor_map->end()) { |
| 144 | throw std::runtime_error("'" + name + "' not found in tensor map."); |
| 145 | } |
| 146 | return m_tensor_map->at(name); |
| 147 | } |
| 148 | |
| 149 | bool has_input(const std::string & name) const { return m_tensor_map->find(name) != m_tensor_map->end(); } |
| 150 | |
| 151 | void put_shared(const std::string & name, const Output<Node> & value) const { |
| 152 | m_tensor_map->insert({name, value}); |
| 153 | } |
| 154 | |
| 155 | const std::string & get_name() const override { return m_decoder->get_op_name(m_node_idx); } |
| 156 | |
| 157 | ov::Any get_attribute_as_any(const std::string & name) const override { return m_decoder->get_attribute(name); } |
| 158 | |
| 159 | int get_op_case() const { return m_decoder->get_op_case(m_node_idx); } |
| 160 | |
| 161 | bool is_static() const { return m_decoder->is_static(); } |
| 162 | |
| 163 | bool is_stateful() const { return m_decoder->is_stateful(); } |
| 164 | |
| 165 | int get_ssm_state_size() const { return m_decoder->get_ssm_state_size(); } |
| 166 | |
| 167 | private: |
| 168 | std::shared_ptr<GgmlDecoder> m_decoder; |
| 169 | std::shared_ptr<TensorMap> & m_tensor_map; |
| 170 | int m_node_idx; |
| 171 | TranslateSession * m_translate_session; |
| 172 | std::vector<std::string> m_input_names; |
| 173 | std::vector<std::string> m_output_names; |
| 174 | }; |
| 175 | |
| 176 | using CreatorFunction = std::function<ov::OutputVector(const ov::frontend::ggml::NodeContext &)>; |
| 177 | |
| 178 | } // namespace ggml |
| 179 | } // namespace frontend |
| 180 | } // namespace ov |