|
| 1 | +// Copyright (C) 2018-2025 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#include "openvino/op/causal_conv1d.hpp" |
| 6 | + |
| 7 | +#include <algorithm> |
| 8 | +#include <array> |
| 9 | +#include <initializer_list> |
| 10 | + |
| 11 | +#include "dimension_util.hpp" |
| 12 | +#include "itt.hpp" |
| 13 | +#include "openvino/core/validation_util.hpp" |
| 14 | +#include "openvino/core/attribute_visitor.hpp" |
| 15 | + |
| 16 | +namespace { |
| 17 | + |
| 18 | +inline void input_check(const ov::Node* node, |
| 19 | + size_t idx, |
| 20 | + const std::string_view input_name, |
| 21 | + std::initializer_list<ov::Rank>&& allowed_ranks, |
| 22 | + const std::vector<ov::element::Type>& allowed_types) { |
| 23 | + using namespace ov; |
| 24 | + using namespace ov::util; |
| 25 | + using namespace ov::element; |
| 26 | + |
| 27 | + const auto& rank = node->get_input_partial_shape(idx).rank(); |
| 28 | + const auto& tp = node->get_input_element_type(idx); |
| 29 | + |
| 30 | + auto rank_check = [&](const Rank& r) { |
| 31 | + return r.is_dynamic() || allowed_ranks.size() == 0 || is_rank_compatible_any_of(r.get_length(), allowed_ranks); |
| 32 | + }; |
| 33 | + |
| 34 | + auto type_check = [&](const Type& t) { |
| 35 | + if (allowed_types.empty()) { |
| 36 | + return true; |
| 37 | + } |
| 38 | + auto it = std::find(allowed_types.begin(), allowed_types.end(), tp); |
| 39 | + return t.is_dynamic() || it != allowed_types.end(); |
| 40 | + }; |
| 41 | + |
| 42 | + NODE_VALIDATION_CHECK(node, |
| 43 | + rank_check(rank), |
| 44 | + "Rank of `", |
| 45 | + input_name, |
| 46 | + "` input should be in [dynamic, ", |
| 47 | + join(allowed_ranks), |
| 48 | + "] list, but it is ", |
| 49 | + rank, |
| 50 | + "."); |
| 51 | + |
| 52 | + NODE_VALIDATION_CHECK(node, |
| 53 | + type_check(tp), |
| 54 | + "Element type of `", |
| 55 | + input_name, |
| 56 | + "` input should be in [dynamic, ", |
| 57 | + join(allowed_types), |
| 58 | + "] list, but it is ", |
| 59 | + tp, |
| 60 | + "."); |
| 61 | +} |
| 62 | + |
| 63 | +} // namespace |
| 64 | + |
| 65 | +namespace ov { |
| 66 | +namespace op { |
| 67 | + |
| 68 | +CausalConv1D::CausalConv1D(const ov::OutputVector& args, const std::string& activation) |
| 69 | + : ov::op::Op(args), m_activation(activation) { |
| 70 | + constructor_validate_and_infer_types(); |
| 71 | +} |
| 72 | + |
| 73 | +void CausalConv1D::validate_and_infer_types() { |
| 74 | + OV_OP_SCOPE(CausalConv1D_validate_and_infer_types); |
| 75 | + |
| 76 | + NODE_VALIDATION_CHECK(this, |
| 77 | + get_input_size() == 3 || get_input_size() == 4, |
| 78 | + "CausalConv1D expects 3 or 4 inputs, but it has ", |
| 79 | + get_input_size()); |
| 80 | + |
| 81 | + // hidden_states: (batch, hidden_size, seq_len) |
| 82 | + input_check(this, 0, "hidden_states", {3}, {}); |
| 83 | + // conv_state: (batch, hidden_size, kernel) |
| 84 | + input_check(this, 1, "conv_state", {3}, {}); |
| 85 | + // weight: (hidden_size, kernel) |
| 86 | + input_check(this, 2, "weight", {2}, {}); |
| 87 | + |
| 88 | + if (get_input_size() == 4) { |
| 89 | + // bias: (hidden_size) |
| 90 | + input_check(this, 3, "bias", {1}, {}); |
| 91 | + } |
| 92 | + |
| 93 | + const auto hidden_ps = get_input_partial_shape(0); |
| 94 | + const auto state_ps = get_input_partial_shape(1); |
| 95 | + const auto weight_ps = get_input_partial_shape(2); |
| 96 | + |
| 97 | + if (hidden_ps.rank().is_static() && state_ps.rank().is_static()) { |
| 98 | + NODE_VALIDATION_CHECK(this, |
| 99 | + hidden_ps[1].compatible(state_ps[1]), |
| 100 | + "Hidden size of hidden_states (", |
| 101 | + hidden_ps[1], |
| 102 | + ") and conv_state (", |
| 103 | + state_ps[1], |
| 104 | + ") inputs must match."); |
| 105 | + } |
| 106 | + if (hidden_ps.rank().is_static() && weight_ps.rank().is_static()) { |
| 107 | + NODE_VALIDATION_CHECK(this, |
| 108 | + hidden_ps[1].compatible(weight_ps[0]), |
| 109 | + "Hidden size of hidden_states (", |
| 110 | + hidden_ps[1], |
| 111 | + ") and weight (", |
| 112 | + weight_ps[0], |
| 113 | + ") inputs must match."); |
| 114 | + } |
| 115 | + if (state_ps.rank().is_static() && weight_ps.rank().is_static()) { |
| 116 | + NODE_VALIDATION_CHECK(this, |
| 117 | + state_ps[2].compatible(weight_ps[1]), |
| 118 | + "Kernel length of conv_state (", |
| 119 | + state_ps[2], |
| 120 | + ") and weight (", |
| 121 | + weight_ps[1], |
| 122 | + ") inputs must match."); |
| 123 | + } |
| 124 | + if (get_input_size() == 4) { |
| 125 | + const auto bias_ps = get_input_partial_shape(3); |
| 126 | + if (bias_ps.rank().is_static() && weight_ps.rank().is_static()) { |
| 127 | + NODE_VALIDATION_CHECK(this, |
| 128 | + bias_ps[0].compatible(weight_ps[0]), |
| 129 | + "Bias length (", |
| 130 | + bias_ps[0], |
| 131 | + ") must match hidden size (", |
| 132 | + weight_ps[0], |
| 133 | + ")."); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + set_output_type(0, get_input_element_type(0), get_input_partial_shape(0)); |
| 138 | + set_output_type(1, get_input_element_type(1), get_input_partial_shape(1)); |
| 139 | +} |
| 140 | + |
| 141 | +std::shared_ptr<ov::Node> CausalConv1D::clone_with_new_inputs(const ov::OutputVector& new_args) const { |
| 142 | + return std::make_shared<CausalConv1D>(new_args, m_activation); |
| 143 | +} |
| 144 | + |
| 145 | +bool CausalConv1D::visit_attributes(ov::AttributeVisitor& visitor) { |
| 146 | + visitor.on_attribute("activation", m_activation); |
| 147 | + return true; |
| 148 | +} |
| 149 | + |
| 150 | +void CausalConv1D::set_activation(const std::string& activation) { |
| 151 | + m_activation = activation; |
| 152 | +} |
| 153 | + |
| 154 | +const std::string& CausalConv1D::get_activation() const { |
| 155 | + return m_activation; |
| 156 | +} |
| 157 | + |
| 158 | +} // namespace op |
| 159 | +} // namespace ov |
0 commit comments