|
| 1 | +// Copyright (C) 2018-2026 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include "openvino/op/paged_selective_ssm.hpp" |
| 8 | +#include "utils.hpp" |
| 9 | + |
| 10 | +namespace ov::op::internal { |
| 11 | + |
| 12 | +template <class T, class TRShape = result_shape_t<T>> |
| 13 | +std::vector<TRShape> shape_infer(const PagedSelectiveSSM* op, const std::vector<T>& input_shapes) { |
| 14 | + NODE_VALIDATION_CHECK(op, input_shapes.size() == 11); |
| 15 | + |
| 16 | + NODE_SHAPE_INFER_CHECK(op, input_shapes, input_shapes[0].rank().compatible(1)); |
| 17 | + NODE_SHAPE_INFER_CHECK(op, input_shapes, input_shapes[1].rank().compatible(2)); |
| 18 | + NODE_SHAPE_INFER_CHECK(op, input_shapes, input_shapes[2].rank().compatible(3)); |
| 19 | + NODE_SHAPE_INFER_CHECK(op, input_shapes, input_shapes[3].rank().compatible(3)); |
| 20 | + NODE_SHAPE_INFER_CHECK(op, input_shapes, input_shapes[4].rank().compatible(3)); |
| 21 | + NODE_SHAPE_INFER_CHECK(op, input_shapes, input_shapes[5].rank().compatible(4)); |
| 22 | + for (size_t i = 6; i < 11; ++i) { |
| 23 | + NODE_SHAPE_INFER_CHECK(op, input_shapes, input_shapes[i].rank().compatible(1)); |
| 24 | + } |
| 25 | + |
| 26 | + const auto& A_ps = input_shapes[0]; |
| 27 | + const auto& dt_ps = input_shapes[1]; |
| 28 | + const auto& B_ps = input_shapes[2]; |
| 29 | + const auto& x_ps = input_shapes[3]; |
| 30 | + const auto& C_ps = input_shapes[4]; |
| 31 | + const auto& state_ps = input_shapes[5]; |
| 32 | + const auto& subsequence_begins_ps = input_shapes[6]; |
| 33 | + const auto& la_block_indices_ps = input_shapes[7]; |
| 34 | + const auto& la_block_indices_begins_ps = input_shapes[8]; |
| 35 | + const auto& num_processed_tokens_ps = input_shapes[9]; |
| 36 | + const auto& cache_interval_ps = input_shapes[10]; |
| 37 | + |
| 38 | + const auto A_rank_is_static = A_ps.rank().is_static(); |
| 39 | + const auto dt_rank_is_static = dt_ps.rank().is_static(); |
| 40 | + const auto B_rank_is_static = B_ps.rank().is_static(); |
| 41 | + const auto x_rank_is_static = x_ps.rank().is_static(); |
| 42 | + const auto C_rank_is_static = C_ps.rank().is_static(); |
| 43 | + const auto state_rank_is_static = state_ps.rank().is_static(); |
| 44 | + const auto subsequence_begins_rank_is_static = subsequence_begins_ps.rank().is_static(); |
| 45 | + const auto la_block_indices_rank_is_static = la_block_indices_ps.rank().is_static(); |
| 46 | + const auto la_block_indices_begins_rank_is_static = la_block_indices_begins_ps.rank().is_static(); |
| 47 | + const auto num_processed_tokens_rank_is_static = num_processed_tokens_ps.rank().is_static(); |
| 48 | + const auto cache_interval_rank_is_static = cache_interval_ps.rank().is_static(); |
| 49 | + |
| 50 | + Dimension token_dim, num_heads_dim, head_dim_dim, state_size_dim; |
| 51 | + |
| 52 | + bool token_ok = true; |
| 53 | + if (x_rank_is_static) |
| 54 | + token_ok &= Dimension::merge(token_dim, token_dim, x_ps[0]); |
| 55 | + if (dt_rank_is_static) |
| 56 | + token_ok &= Dimension::merge(token_dim, token_dim, dt_ps[0]); |
| 57 | + if (B_rank_is_static) |
| 58 | + token_ok &= Dimension::merge(token_dim, token_dim, B_ps[0]); |
| 59 | + if (C_rank_is_static) |
| 60 | + token_ok &= Dimension::merge(token_dim, token_dim, C_ps[0]); |
| 61 | + NODE_SHAPE_INFER_CHECK(op, |
| 62 | + input_shapes, |
| 63 | + token_ok, |
| 64 | + "The token dimension of `dt`, `B`, `x` and `C` should be the same."); |
| 65 | + |
| 66 | + bool num_heads_ok = true; |
| 67 | + if (x_rank_is_static) |
| 68 | + num_heads_ok &= Dimension::merge(num_heads_dim, num_heads_dim, x_ps[1]); |
| 69 | + if (A_rank_is_static) |
| 70 | + num_heads_ok &= Dimension::merge(num_heads_dim, num_heads_dim, A_ps[0]); |
| 71 | + if (dt_rank_is_static) |
| 72 | + num_heads_ok &= Dimension::merge(num_heads_dim, num_heads_dim, dt_ps[1]); |
| 73 | + if (state_rank_is_static) |
| 74 | + num_heads_ok &= Dimension::merge(num_heads_dim, num_heads_dim, state_ps[1]); |
| 75 | + NODE_SHAPE_INFER_CHECK(op, |
| 76 | + input_shapes, |
| 77 | + num_heads_ok, |
| 78 | + "The number of heads of `A`, `dt`, `x` and `recurrent_state_table` should be the same."); |
| 79 | + |
| 80 | + if (B_rank_is_static && C_rank_is_static) { |
| 81 | + NODE_SHAPE_INFER_CHECK(op, |
| 82 | + input_shapes, |
| 83 | + C_ps[1].compatible(B_ps[1]), |
| 84 | + "The number of groups of `B` and `C` should be the same."); |
| 85 | + } |
| 86 | + |
| 87 | + bool head_dim_ok = true; |
| 88 | + if (x_rank_is_static) |
| 89 | + head_dim_ok &= Dimension::merge(head_dim_dim, head_dim_dim, x_ps[2]); |
| 90 | + if (state_rank_is_static) |
| 91 | + head_dim_ok &= Dimension::merge(head_dim_dim, head_dim_dim, state_ps[2]); |
| 92 | + NODE_SHAPE_INFER_CHECK(op, |
| 93 | + input_shapes, |
| 94 | + head_dim_ok, |
| 95 | + "The head dimension of `x` and `recurrent_state_table` should be the same."); |
| 96 | + |
| 97 | + bool state_size_ok = true; |
| 98 | + if (state_rank_is_static) |
| 99 | + state_size_ok &= Dimension::merge(state_size_dim, state_size_dim, state_ps[3]); |
| 100 | + if (B_rank_is_static) |
| 101 | + state_size_ok &= Dimension::merge(state_size_dim, state_size_dim, B_ps[2]); |
| 102 | + if (C_rank_is_static) |
| 103 | + state_size_ok &= Dimension::merge(state_size_dim, state_size_dim, C_ps[2]); |
| 104 | + NODE_SHAPE_INFER_CHECK(op, |
| 105 | + input_shapes, |
| 106 | + state_size_ok, |
| 107 | + "The state size of `B`, `C` and `recurrent_state_table` should be the same."); |
| 108 | + |
| 109 | + if (num_heads_dim.is_static() && B_rank_is_static) { |
| 110 | + const auto& num_groups = B_ps[1]; |
| 111 | + if (num_groups.is_static()) { |
| 112 | + NODE_SHAPE_INFER_CHECK( |
| 113 | + op, |
| 114 | + input_shapes, |
| 115 | + num_groups.get_length() != 0 && num_heads_dim.get_length() % num_groups.get_length() == 0, |
| 116 | + "The number of heads should be divisible by the number of groups."); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (la_block_indices_rank_is_static && state_rank_is_static) { |
| 121 | + NODE_SHAPE_INFER_CHECK(op, |
| 122 | + input_shapes, |
| 123 | + la_block_indices_ps[0].compatible(state_ps[0]), |
| 124 | + "The number of blocks of `la_block_indices` and `recurrent_state_table` should " |
| 125 | + "be the same."); |
| 126 | + } |
| 127 | + if (subsequence_begins_rank_is_static && la_block_indices_begins_rank_is_static) { |
| 128 | + NODE_SHAPE_INFER_CHECK(op, |
| 129 | + input_shapes, |
| 130 | + subsequence_begins_ps[0].compatible(la_block_indices_begins_ps[0]), |
| 131 | + "The number of sequences of `subsequence_begins` and `la_block_indices_begins` " |
| 132 | + "should be the same."); |
| 133 | + } |
| 134 | + if (num_processed_tokens_rank_is_static && cache_interval_rank_is_static) { |
| 135 | + NODE_SHAPE_INFER_CHECK(op, |
| 136 | + input_shapes, |
| 137 | + num_processed_tokens_ps[0].compatible(cache_interval_ps[0]), |
| 138 | + "The number of sequences of `num_processed_tokens` and `cache_interval` should " |
| 139 | + "be the same."); |
| 140 | + } |
| 141 | + if (subsequence_begins_rank_is_static && num_processed_tokens_rank_is_static) { |
| 142 | + NODE_SHAPE_INFER_CHECK(op, |
| 143 | + input_shapes, |
| 144 | + (subsequence_begins_ps[0] - Dimension(1)).compatible(num_processed_tokens_ps[0]), |
| 145 | + "The number of sequences of `subsequence_begins` and `num_processed_tokens` " |
| 146 | + "should be the same."); |
| 147 | + } |
| 148 | + |
| 149 | + auto output_shapes = std::vector<TRShape>{x_ps}; |
| 150 | + if (x_rank_is_static) { |
| 151 | + output_shapes[0] = TRShape{token_dim, num_heads_dim, head_dim_dim}; |
| 152 | + } |
| 153 | + return output_shapes; |
| 154 | +} |
| 155 | + |
| 156 | +} // namespace ov::op::internal |
0 commit comments