|
| 1 | +// Copyright (C) 2018-2024 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +#include "internal_operation.hpp" |
| 10 | +#include "openvino/op/constant.hpp" |
| 11 | + |
| 12 | +namespace ov { |
| 13 | +namespace frontend { |
| 14 | +namespace tensorflow { |
| 15 | + |
| 16 | +// Internal operation for TensorList that represents a initial state of tensor list container |
| 17 | +class TensorList : public InternalOperation { |
| 18 | +public: |
| 19 | + OPENVINO_OP("TensorList", "ov::frontend::tensorflow", InternalOperation); |
| 20 | + |
| 21 | + TensorList(const ov::Output<ov::Node>& num_elements, |
| 22 | + const ov::Rank& element_rank, |
| 23 | + const element::Type& element_dtype, |
| 24 | + const std::shared_ptr<DecoderBase>& decoder = std::make_shared<DecoderFake>()) |
| 25 | + : InternalOperation(decoder, OutputVector{num_elements}, 1, "TensorList"), |
| 26 | + m_num_elements(num_elements), |
| 27 | + m_element_rank(element_rank), |
| 28 | + m_element_dtype(element_dtype) { |
| 29 | + validate_and_infer_types(); |
| 30 | + } |
| 31 | + |
| 32 | + void validate_and_infer_types() override { |
| 33 | + if (m_element_rank.is_static()) { |
| 34 | + auto element_rank = m_element_rank.get_length(); |
| 35 | + auto output_shape = ov::PartialShape::dynamic(element_rank + 1); |
| 36 | + set_output_type(0, m_element_dtype, output_shape); |
| 37 | + } |
| 38 | + |
| 39 | + set_output_type(0, m_element_dtype, ov::PartialShape::dynamic()); |
| 40 | + } |
| 41 | + |
| 42 | + ov::element::Type get_element_type() const { |
| 43 | + return m_element_dtype; |
| 44 | + } |
| 45 | + |
| 46 | + std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& inputs) const override { |
| 47 | + FRONT_END_OP_CONVERSION_CHECK(inputs.size() == 1, |
| 48 | + "[TensorFlow Frontend] internal error: TensorList expects no inputs"); |
| 49 | + auto tensor_list_node = std::make_shared<TensorList>(inputs[0], m_element_rank, m_element_dtype, m_decoder); |
| 50 | + tensor_list_node->set_attrs(get_attrs()); |
| 51 | + return tensor_list_node; |
| 52 | + } |
| 53 | + |
| 54 | + ov::Rank get_element_rank() const { |
| 55 | + return m_element_rank; |
| 56 | + } |
| 57 | + |
| 58 | + void set_element_rank(const ov::Rank& element_rank) { |
| 59 | + m_element_rank = element_rank; |
| 60 | + } |
| 61 | + |
| 62 | + ov::Output<ov::Node> get_num_elements() const { |
| 63 | + return m_num_elements; |
| 64 | + } |
| 65 | + |
| 66 | +private: |
| 67 | + ov::Output<ov::Node> m_num_elements; |
| 68 | + ov::Rank m_element_rank; |
| 69 | + ov::element::Type m_element_dtype; |
| 70 | +}; |
| 71 | + |
| 72 | +// Internal operation for TensorListGetItem |
| 73 | +// it gets an element (Tensor) in tensor list by index |
| 74 | +class TensorListGetItem : public InternalOperation { |
| 75 | +public: |
| 76 | + OPENVINO_OP("TensorListGetItem", "ov::frontend::tensorflow", InternalOperation); |
| 77 | + |
| 78 | + TensorListGetItem(const Output<Node>& input_handle, |
| 79 | + const Output<Node>& index, |
| 80 | + const Output<Node>& element_shape, |
| 81 | + const ov::element::Type& element_type, |
| 82 | + const std::shared_ptr<DecoderBase>& decoder = std::make_shared<DecoderFake>()) |
| 83 | + : InternalOperation(decoder, OutputVector{input_handle, index, element_shape}, 1, "TensorListGetItem"), |
| 84 | + m_element_type(element_type) { |
| 85 | + validate_and_infer_types(); |
| 86 | + } |
| 87 | + |
| 88 | + void validate_and_infer_types() override { |
| 89 | + // deduce an element (Tensor) shape |
| 90 | + ov::PartialShape comp_element_shape = ov::PartialShape::dynamic(); |
| 91 | + if (const auto& const_element_shape = |
| 92 | + ov::as_type_ptr<ov::op::v0::Constant>(input_value(2).get_node_shared_ptr())) { |
| 93 | + auto element_shape_value = const_element_shape->get_vector<int32_t>(); |
| 94 | + comp_element_shape = ov::PartialShape::dynamic(static_cast<int64_t>(element_shape_value.size())); |
| 95 | + for (size_t idx = 0; idx < element_shape_value.size(); ++idx) { |
| 96 | + comp_element_shape[idx] = (element_shape_value[idx] >= 0) |
| 97 | + ? static_cast<int64_t>(element_shape_value[idx]) |
| 98 | + : ov::Dimension::dynamic(); |
| 99 | + } |
| 100 | + } else if (input_value(0).get_partial_shape().rank().is_static()) { |
| 101 | + // the second try to deduce element shape if it is still of dynamic rank |
| 102 | + auto tensor_list_rank = input_value(0).get_partial_shape().rank().get_length(); |
| 103 | + OPENVINO_ASSERT( |
| 104 | + tensor_list_rank > 0, |
| 105 | + "[TensorFlow Frontend] internal error or inconsistent model: tensor list rank must be greater than 0"); |
| 106 | + // exclude tensor dimension (or batch) |
| 107 | + comp_element_shape = ov::PartialShape::dynamic(tensor_list_rank - 1); |
| 108 | + for (int64_t idx = 1; idx < tensor_list_rank; ++idx) { |
| 109 | + comp_element_shape[idx - 1] = input_value(0).get_partial_shape()[idx]; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // deduce an element (Tensor) type |
| 114 | + if (m_element_type.is_dynamic() && input_value(0).get_element_type().is_static()) { |
| 115 | + m_element_type = input_value(0).get_element_type(); |
| 116 | + } |
| 117 | + |
| 118 | + set_output_type(0, m_element_type, comp_element_shape); |
| 119 | + } |
| 120 | + |
| 121 | + ov::element::Type get_element_type() const { |
| 122 | + return m_element_type; |
| 123 | + } |
| 124 | + |
| 125 | + std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& inputs) const override { |
| 126 | + FRONT_END_OP_CONVERSION_CHECK(inputs.size() == 3, |
| 127 | + "[TensorFlow Frontend] internal error: TensorListGetItem expects three inputs"); |
| 128 | + auto tensor_list_get_item = |
| 129 | + std::make_shared<TensorListGetItem>(inputs[0], inputs[1], inputs[2], m_element_type, m_decoder); |
| 130 | + tensor_list_get_item->set_attrs(get_attrs()); |
| 131 | + return tensor_list_get_item; |
| 132 | + } |
| 133 | + |
| 134 | +private: |
| 135 | + ov::element::Type m_element_type; |
| 136 | +}; |
| 137 | + |
| 138 | +// Internal operation for TensorListSetItem |
| 139 | +// it inserts tensor to tensor list by index |
| 140 | +class TensorListSetItem : public InternalOperation { |
| 141 | +public: |
| 142 | + OPENVINO_OP("TensorListSetItem", "ov::frontend::tensorflow", InternalOperation); |
| 143 | + |
| 144 | + TensorListSetItem(const Output<Node>& input_handle, |
| 145 | + const Output<Node>& index, |
| 146 | + const Output<Node>& item, |
| 147 | + const std::shared_ptr<DecoderBase>& decoder = std::make_shared<DecoderFake>()) |
| 148 | + : InternalOperation(decoder, OutputVector{input_handle, index, item}, 1, "TensorListSetItem") { |
| 149 | + validate_and_infer_types(); |
| 150 | + } |
| 151 | + |
| 152 | + void validate_and_infer_types() override { |
| 153 | + // deduce a type of elements in tensor list |
| 154 | + ov::element::Type element_type = ov::element::dynamic; |
| 155 | + if (input_value(0).get_element_type().is_static()) { |
| 156 | + element_type = input_value(0).get_element_type(); |
| 157 | + } else if (input_value(2).get_element_type().is_static()) { |
| 158 | + element_type = input_value(2).get_element_type(); |
| 159 | + } |
| 160 | + |
| 161 | + // deduce a shape of tensor list [num_tensors, <tensor shape>] |
| 162 | + ov::PartialShape tensor_list_shape = ov::PartialShape::dynamic(); |
| 163 | + if (input_value(2).get_partial_shape().rank().is_static()) { |
| 164 | + auto element_rank = input_value(2).get_partial_shape().rank().get_length(); |
| 165 | + tensor_list_shape = ov::PartialShape::dynamic(element_rank + 1); |
| 166 | + for (int64_t idx = 0; idx < element_rank; ++idx) { |
| 167 | + tensor_list_shape[idx + 1] = input_value(2).get_partial_shape()[idx]; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + set_output_type(0, element_type, tensor_list_shape); |
| 172 | + } |
| 173 | + |
| 174 | + std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& inputs) const override { |
| 175 | + FRONT_END_OP_CONVERSION_CHECK(inputs.size() == 3, |
| 176 | + "[TensorFlow Frontend] internal error: TensorListSetItem expects three inputs"); |
| 177 | + auto tensor_list_set_item = std::make_shared<TensorListSetItem>(inputs[0], inputs[1], inputs[2], m_decoder); |
| 178 | + tensor_list_set_item->set_attrs(get_attrs()); |
| 179 | + return tensor_list_set_item; |
| 180 | + } |
| 181 | +}; |
| 182 | + |
| 183 | +} // namespace tensorflow |
| 184 | +} // namespace frontend |
| 185 | +} // namespace ov |
0 commit comments