Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

Add BatchReplicatedBranches transformation for camera branch batching for ACT Model (#38575)

Deepak Soma Reddy46d2833b3d0a
diff --git a/src/common/transformations/include/transformations/utils/utils.hpp b/src/common/transformations/include/transformations/utils/utils.hpp
index ce69c1ef686..738fad30154 100644
--- a/src/common/transformations/include/transformations/utils/utils.hpp
+++ b/src/common/transformations/include/transformations/utils/utils.hpp
@@ -238,6 +238,23 @@ TRANSFORMATIONS_API void visit_path_forward(ov::Node* start_node,
TRANSFORMATIONS_API bool have_same_fake_quantize_params(const std::shared_ptr<ov::op::v0::FakeQuantize>& lhs,
const std::shared_ptr<ov::op::v0::FakeQuantize>& rhs);
+/**
+ * \brief Collects the attributes reported by node->visit_attributes() into a map that supports equality comparison.
+ *
+ * \param node The node whose attributes are collected.
+ * \return The map from the attribute name to its value.
+ * \throws ov::NotImplemented for the attributes that can not be compared, e.g. sub-models or raw buffers.
+ */
+TRANSFORMATIONS_API ov::AnyMap get_comparable_attributes(const std::shared_ptr<ov::Node>& node);
+
+/**
+ * \brief Checks that two constants have the same element type, shape and data.
+ *
+ * \return false if any of the constants is nullptr or if they differ.
+ */
+TRANSFORMATIONS_API bool constants_are_equal(const std::shared_ptr<ov::op::v0::Constant>& lhs,
+ const std::shared_ptr<ov::op::v0::Constant>& rhs);
+
/**
* \brief Traverses a shapeOf subgraph starting from the node and not including the ShapeOf nodes,
* and calls "func" for each ov::Node.
diff --git a/src/common/transformations/src/transformations/common_optimizations/shared_ops_optimization.cpp b/src/common/transformations/src/transformations/common_optimizations/shared_ops_optimization.cpp
index 585be3ceee1..dfee6af700d 100644
--- a/src/common/transformations/src/transformations/common_optimizations/shared_ops_optimization.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/shared_ops_optimization.cpp
@@ -10,6 +10,7 @@
#include "openvino/op/loop.hpp"
#include "openvino/op/shape_of.hpp"
#include "openvino/op/util/sub_graph_base.hpp"
+#include "transformations/utils/utils.hpp"
using namespace std;
using namespace ov;
@@ -17,51 +18,6 @@ using namespace ov;
namespace v0 = ov::op::v0;
namespace op_util = ov::op::util;
namespace {
-#define ACCESSOR(type) \
- void on_adapter(const std::string& name, ValueAccessor<type>& adapter) override { \
- m_attributes_map[name] = adapter.get(); \
- };
-#define ACCESSOR_V(type) ACCESSOR(type) ACCESSOR(std::vector<type>)
-
-class NodeComparingVisitor : public ov::AttributeVisitor {
-public:
- ACCESSOR(bool)
- ACCESSOR_V(std::string)
- ACCESSOR_V(int8_t)
- ACCESSOR_V(int16_t)
- ACCESSOR_V(int32_t)
- ACCESSOR_V(int64_t)
- ACCESSOR_V(uint8_t)
- ACCESSOR_V(uint16_t)
- ACCESSOR_V(uint32_t)
- ACCESSOR_V(uint64_t)
- ACCESSOR_V(float)
- ACCESSOR_V(double)
-
- void on_adapter(const std::string& name, ValueAccessor<void>& adapter) override {
- if (auto a = ov::as_type<ov::AttributeAdapter<std::vector<ov::element::Type>>>(&adapter)) {
- m_attributes_map.insert({name, a->get()});
- } else {
- OPENVINO_THROW_NOT_IMPLEMENTED("Can not compare void");
- }
- };
- void on_adapter(const std::string& name, ValueAccessor<void*>& adapter) override {
- OPENVINO_THROW_NOT_IMPLEMENTED("Can not compare void*");
- };
- void on_adapter(const std::string& name, ValueAccessor<std::shared_ptr<ov::Model>>& adapter) override {
- OPENVINO_THROW_NOT_IMPLEMENTED("Can not compare models");
- };
-
- ov::AnyMap visit_attributes(const std::shared_ptr<Node>& node) {
- m_attributes_map.clear();
- node->visit_attributes(*this);
- return m_attributes_map;
- }
-
-private:
- ov::AnyMap m_attributes_map;
-};
-
bool inputs_from_same_source_or_equal_constants(const std::shared_ptr<Node>& lhs, const std::shared_ptr<Node>& rhs) {
size_t input_size = lhs->get_input_size();
if (input_size != rhs->get_input_size())
@@ -69,15 +25,8 @@ bool inputs_from_same_source_or_equal_constants(const std::shared_ptr<Node>& lhs
for (size_t i = 0; i < input_size; ++i) {
if (op_util::input_sources_are_equal(lhs, rhs, i))
continue;
- auto lhs_constant = as_type<v0::Constant>(lhs->get_input_node_ptr(i));
- auto rhs_constant = as_type<v0::Constant>(rhs->get_input_node_ptr(i));
- if (!lhs_constant || !rhs_constant)
- return false;
- if (lhs_constant->get_element_type() != rhs_constant->get_element_type())
- return false;
- if (lhs_constant->get_shape() != rhs_constant->get_shape())
- return false;
- if (memcmp(lhs_constant->get_data_ptr(), rhs_constant->get_data_ptr(), lhs_constant->get_byte_size()) != 0)
+ if (!op_util::constants_are_equal(ov::as_type_ptr<v0::Constant>(lhs->get_input_node_shared_ptr(i)),
+ ov::as_type_ptr<v0::Constant>(rhs->get_input_node_shared_ptr(i))))
return false;
}
return true;
@@ -87,8 +36,7 @@ void collect_node_attrs(const std::shared_ptr<Node>& node,
std::unordered_map<std::shared_ptr<ov::Node>, ov::AnyMap>& node_attributes_cache) {
if (node_attributes_cache.count(node))
return;
- auto visitor = NodeComparingVisitor();
- node_attributes_cache[node] = visitor.visit_attributes(node);
+ node_attributes_cache[node] = op_util::get_comparable_attributes(node);
}
bool nodes_are_equal(const std::shared_ptr<Node>& lhs,
diff --git a/src/common/transformations/src/transformations/utils/utils.cpp b/src/common/transformations/src/transformations/utils/utils.cpp
index 43e1e77c7e2..72d69591329 100644
--- a/src/common/transformations/src/transformations/utils/utils.cpp
+++ b/src/common/transformations/src/transformations/utils/utils.cpp
@@ -6,11 +6,13 @@
#include <assert.h>
+#include <cstring>
#include <functional>
#include <memory>
#include <tuple>
#include <vector>
+#include "openvino/core/attribute_visitor.hpp"
#include "openvino/core/graph_util.hpp"
#include "openvino/core/validation_util.hpp"
#include "openvino/op/abs.hpp"
@@ -53,10 +55,70 @@ namespace v3 = ov::op::v3;
namespace v6 = ov::op::v6;
namespace v8 = ov::op::v8;
namespace op_util = ov::op::util;
+
+namespace {
+#define ACCESSOR(type) \
+ void on_adapter(const std::string& name, ov::ValueAccessor<type>& adapter) override { \
+ m_attributes_map[name] = adapter.get(); \
+ };
+#define ACCESSOR_V(type) ACCESSOR(type) ACCESSOR(std::vector<type>)
+
+// Attributes without equality operators throw ov::NotImplemented
+class NodeComparingVisitor : public ov::AttributeVisitor {
+public:
+ ACCESSOR(bool)
+ ACCESSOR_V(std::string)
+ ACCESSOR_V(int8_t)
+ ACCESSOR_V(int16_t)
+ ACCESSOR_V(int32_t)
+ ACCESSOR_V(int64_t)
+ ACCESSOR_V(uint8_t)
+ ACCESSOR_V(uint16_t)
+ ACCESSOR_V(uint32_t)
+ ACCESSOR_V(uint64_t)
+ ACCESSOR_V(float)
+ ACCESSOR_V(double)
+
+ void on_adapter(const std::string& name, ov::ValueAccessor<void>& adapter) override {
+ if (auto a = ov::as_type<ov::AttributeAdapter<std::vector<ov::element::Type>>>(&adapter)) {
+ m_attributes_map.insert({name, a->get()});
+ } else {
+ OPENVINO_THROW_NOT_IMPLEMENTED("Can not compare void");
+ }
+ };
+ void on_adapter(const std::string& name, ov::ValueAccessor<void*>& adapter) override {
+ OPENVINO_THROW_NOT_IMPLEMENTED("Can not compare void*");
+ };
+ void on_adapter(const std::string& name, ov::ValueAccessor<std::shared_ptr<ov::Model>>& adapter) override {
+ OPENVINO_THROW_NOT_IMPLEMENTED("Can not compare models");
+ };
+
+ ov::AnyMap visit_attributes(const std::shared_ptr<ov::Node>& node) {
+ m_attributes_map.clear();
+ node->visit_attributes(*this);
+ return m_attributes_map;
+ }
+
+private:
+ ov::AnyMap m_attributes_map;
+};
+#undef ACCESSOR_V
+#undef ACCESSOR
+} // namespace
+
namespace ov {
namespace op {
namespace util {
+ov::AnyMap get_comparable_attributes(const std::shared_ptr<ov::Node>& node) {
+ return NodeComparingVisitor().visit_attributes(node);
+}
+
+bool constants_are_equal(const std::shared_ptr<v0::Constant>& lhs, const std::shared_ptr<v0::Constant>& rhs) {
+ return lhs && rhs && lhs->get_element_type() == rhs->get_element_type() && lhs->get_shape() == rhs->get_shape() &&
+ std::memcmp(lhs->get_data_ptr(), rhs->get_data_ptr(), lhs->get_byte_size()) == 0;
+}
+
void visit_path(ov::Node* node,
std::unordered_set<ov::Node*>& visited,
std::function<void(ov::Node*)> func,
diff --git a/src/plugins/intel_gpu/sources.cmake b/src/plugins/intel_gpu/sources.cmake
index 68492694e67..abfd4880168 100644
--- a/src/plugins/intel_gpu/sources.cmake
+++ b/src/plugins/intel_gpu/sources.cmake
@@ -150,6 +150,7 @@ set(GPU_PLUGIN_OPS_SOURCES
)
set(GPU_PLUGIN_TRANSFORMATIONS_SOURCES
+ ${CMAKE_CURRENT_LIST_DIR}/src/plugin/transformations/batch_replicated_branches.cpp
${CMAKE_CURRENT_LIST_DIR}/src/plugin/transformations/bcast_and_pad_zp_buffers.cpp
${CMAKE_CURRENT_LIST_DIR}/src/plugin/transformations/binary_conv_to_conv.cpp
${CMAKE_CURRENT_LIST_DIR}/src/plugin/transformations/clamp_fp16_output.cpp
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/batch_replicated_branches.cpp b/src/plugins/intel_gpu/src/plugin/transformations/batch_replicated_branches.cpp
new file mode 100644
index 00000000000..4bf096c9966
--- /dev/null
+++ b/src/plugins/intel_gpu/src/plugin/transformations/batch_replicated_branches.cpp
@@ -0,0 +1,304 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "batch_replicated_branches.hpp"
+
+#include <algorithm>
+#include <cstdint>
+#include <memory>
+#include <numeric>
+#include <optional>
+#include <string>
+#include <vector>
+
+#include "openvino/core/graph_util.hpp"
+#include "openvino/core/rt_info.hpp"
+#include "openvino/core/validation_util.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/concat.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/convolution.hpp"
+#include "openvino/op/max_pool.hpp"
+#include "openvino/op/relu.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/transpose.hpp"
+#include "openvino/op/util/gather_base.hpp"
+#include "openvino/pass/pattern/matcher.hpp"
+#include "openvino/pass/pattern/op/block.hpp"
+#include "openvino/pass/pattern/op/optional.hpp"
+#include "openvino/pass/pattern/op/pattern.hpp"
+#include "openvino/pass/pattern/op/wrap_type.hpp"
+#include "ov_ops/type_relaxed.hpp"
+#include "transformations/utils/utils.hpp"
+
+namespace ov::intel_gpu {
+
+namespace {
+
+using namespace ov::pass::pattern;
+using ov::pass::pattern::op::Predicate;
+
+bool is_type_relaxed(const ov::Node* node) {
+ return dynamic_cast<const ov::op::TypeRelaxedBase*>(node) != nullptr;
+}
+
+// Branch i: Gather(data, i, axis) -> Convolution -> [MaxPool] -> [Add] -> [Relu]
+class ReplicaBranch : public ov::pass::pattern::op::Block {
+public:
+ explicit ReplicaBranch(size_t index) : Block({}, {}, "ReplicaBranch" + std::to_string(index)) {
+ // Branch nodes are cloned with [N, ...] input instead of [1, ...], so they must have batch 1
+ // and a single consumer. Gather is replaced by a new Reshape, which would drop a TypeRelaxed
+ // type override. Cloning does not keep control dependencies, and control dependents would keep
+ // the original branch alive
+ const auto branch_node = has_static_shape() && shape_matches("[1, ?, ?, ?]") && consumers_count(1) &&
+ Predicate(
+ [](const ov::Output<ov::Node>& output) {
+ const auto* node = output.get_node();
+ return !is_type_relaxed(node) && node->get_control_dependencies().empty() && node->get_control_dependents().empty();
+ },
+ "batchable_branch_node");
+ // MaxPool indices are not batched, so they must be unused
+ const auto indices_unused = Predicate(
+ [](const ov::Output<ov::Node>& output) {
+ const auto* node = output.get_node();
+ return output.get_index() == 0 && (node->get_output_size() < 2 || node->output(1).get_target_inputs().empty());
+ },
+ "max_pool_indices_unused");
+ // Gather v1 has no batch_dims attribute, so attrs_match would reject it
+ const auto no_batch_dims = Predicate(
+ [](const ov::Output<ov::Node>& output) {
+ return ov::as_type<ov::op::util::GatherBase>(output.get_node())->get_batch_dims() == 0;
+ },
+ "no_batch_dims");
+
+ auto data = any_input(has_static_shape());
+ auto axis = wrap_type<ov::op::v0::Constant>();
+ auto gather = wrap_type<ov::op::util::GatherBase>({data, wrap_type<ov::op::v0::Constant>(value_matches(std::to_string(index))), axis},
+ branch_node && no_batch_dims);
+ auto weights = wrap_type<ov::op::v0::Constant>();
+ auto weights_convert = optional<ov::op::v0::Convert>({weights});
+ auto conv = wrap_type<ov::op::v1::Convolution>({gather, weights_convert}, branch_node);
+ auto pool = optional<ov::op::v1::MaxPool, ov::op::v8::MaxPool, ov::op::v14::MaxPool>({conv}, branch_node && indices_unused);
+ // Bias broadcast to [1, C, H, W] is also valid for [N, C, H, W]
+ auto bias = wrap_type<ov::op::v0::Constant>();
+ auto add = optional<ov::op::v1::Add>({pool, bias}, branch_node, {{"auto_broadcast", "numpy"}});
+ auto relu = optional<ov::op::v0::Relu>({add}, branch_node);
+
+ m_inputs = {data};
+ m_outputs = {relu};
+ REGISTER_ANCHORS(this, data, axis, gather, weights, weights_convert, conv, pool, bias, add, relu);
+ }
+
+ // Compared by value
+ std::vector<std::optional<ov::Output<ov::Node>>> get_constants_to_compare(const PatternValueMap& pm) const {
+ return {get_anchor("axis", pm), get_anchor("weights", pm), get_anchor("bias", pm)};
+ }
+
+ // Compared by type and attributes
+ std::vector<std::optional<ov::Output<ov::Node>>> get_nodes_to_compare(const PatternValueMap& pm) const {
+ return {get_anchor("gather", pm),
+ get_anchor("weights_convert", pm),
+ get_anchor("conv", pm),
+ get_anchor("pool", pm),
+ get_anchor("add", pm),
+ get_anchor("relu", pm)};
+ }
+};
+
+struct MatchedBranch {
+ std::shared_ptr<ReplicaBranch> block;
+ PatternValueMap pm;
+
+ std::optional<ov::Output<ov::Node>> get(const std::string& name) const {
+ return block->get_anchor(name, pm);
+ }
+};
+
+// Optional nodes must be matched in both branches or in neither
+template <typename Equal>
+bool all_equal(const std::vector<std::optional<ov::Output<ov::Node>>>& lhs, const std::vector<std::optional<ov::Output<ov::Node>>>& rhs, Equal equal) {
+ for (size_t i = 0; i < lhs.size(); ++i) {
+ if (lhs[i].has_value() != rhs[i].has_value() || (lhs[i] && !equal(lhs[i]->get_node_shared_ptr(), rhs[i]->get_node_shared_ptr()))) {
+ return false;
+ }
+ }
+ return true;
+}
+
+bool branches_are_equal(const MatchedBranch& lhs, const MatchedBranch& rhs) {
+ const auto constants_are_equal = [](const std::shared_ptr<ov::Node>& lhs, const std::shared_ptr<ov::Node>& rhs) {
+ return ov::op::util::constants_are_equal(ov::as_type_ptr<ov::op::v0::Constant>(lhs), ov::as_type_ptr<ov::op::v0::Constant>(rhs));
+ };
+ // The pattern fixes the branch ops, and all of them have comparable attributes
+ const auto attributes_are_equal = [](const std::shared_ptr<ov::Node>& lhs, const std::shared_ptr<ov::Node>& rhs) {
+ return lhs->get_type_info() == rhs->get_type_info() && ov::op::util::get_comparable_attributes(lhs) == ov::op::util::get_comparable_attributes(rhs);
+ };
+ return all_equal(lhs.block->get_constants_to_compare(lhs.pm), rhs.block->get_constants_to_compare(rhs.pm), constants_are_equal) &&
+ all_equal(lhs.block->get_nodes_to_compare(lhs.pm), rhs.block->get_nodes_to_compare(rhs.pm), attributes_are_equal);
+}
+
+// Checks relations the single-branch pattern can't express: all branches gather from the same
+// tensor, the slices cover the whole replica dimension, and all branches are equal
+std::optional<std::vector<MatchedBranch>> match_branches(const ov::Output<ov::Node>& concat_output) {
+ const auto concat = ov::as_type_ptr<ov::op::v0::Concat>(concat_output.get_node_shared_ptr());
+ const auto replica_count = concat->get_input_size();
+ // A single-input Concat is valid but has nothing to batch. The axis may be negative, so it is normalized
+ // to reject axis 0: the repack below only handles a Concat on axis k > 0
+ if (replica_count < 2 || ov::util::normalize(concat->get_axis(), concat->get_output_shape(0).size()) <= 0) {
+ return std::nullopt;
+ }
+
+ std::vector<MatchedBranch> branches;
+ branches.reserve(replica_count);
+ for (size_t i = 0; i < replica_count; ++i) {
+ const auto block = std::make_shared<ReplicaBranch>(i);
+ Matcher matcher(block, "BatchReplicatedBranch");
+ if (!matcher.match(concat->input_value(i))) {
+ return std::nullopt;
+ }
+ const auto& branch = branches.emplace_back(MatchedBranch{block, matcher.get_pattern_value_map()});
+ const auto& reference = branches.front();
+ if (i == 0) {
+ const auto gather = ov::as_type_ptr<ov::op::util::GatherBase>(branch.get("gather")->get_node_shared_ptr());
+ if (gather->get_input_shape(0)[gather->get_axis()] != replica_count) {
+ return std::nullopt;
+ }
+ } else if (branch.get("data") != reference.get("data") || !branches_are_equal(reference, branch)) {
+ return std::nullopt;
+ }
+ }
+ return branches;
+}
+
+// rt_info of all replicas is merged (e.g. fused names) so query_model reports all of them
+void copy_metadata(const ov::NodeVector& replicas, const std::shared_ptr<ov::Node>& clone) {
+ const auto& reference = replicas.front();
+ clone->set_friendly_name(reference->get_friendly_name());
+ ov::copy_runtime_info(replicas, clone);
+ for (size_t i = 0; i < clone->get_input_size(); ++i) {
+ clone->input(i).get_rt_info() = reference->input(i).get_rt_info();
+ }
+ for (size_t i = 0; i < clone->get_output_size(); ++i) {
+ clone->output(i).get_rt_info() = reference->output(i).get_rt_info();
+ clone->output(i).set_names(reference->output(i).get_names());
+ }
+}
+
+bool batch_replicated_branches(const std::shared_ptr<ov::op::v0::Concat>& output_concat, const std::vector<MatchedBranch>& branches) {
+ const auto& reference = branches.front();
+ const auto replica_count = branches.size();
+ const auto replicas_of = [&branches](const std::string& name) {
+ ov::NodeVector replicas;
+ for (const auto& branch : branches) {
+ replicas.push_back(branch.get(name)->get_node_shared_ptr());
+ }
+ return replicas;
+ };
+
+ // Branch i gathers slice i of data, so the stacked slices are data with the replica axis moved to the front.
+ // Transpose is needed only when a dim before the replica axis is not 1, otherwise Reshape keeps the order
+ const auto gather = ov::as_type_ptr<ov::op::util::GatherBase>(reference.get("gather")->get_node_shared_ptr());
+ ov::Output<ov::Node> data = gather->input_value(0);
+ const auto& data_shape = data.get_shape();
+ const auto replica_axis = static_cast<size_t>(gather->get_axis());
+ ov::NodeVector batched_input_nodes;
+ if (std::any_of(data_shape.begin(), data_shape.begin() + replica_axis, [](size_t dim) {
+ return dim != 1;
+ })) {
+ std::vector<int64_t> order{static_cast<int64_t>(replica_axis)};
+ for (size_t axis = 0; axis < data_shape.size(); ++axis) {
+ if (axis != replica_axis) {
+ order.push_back(static_cast<int64_t>(axis));
+ }
+ }
+ data = std::make_shared<ov::op::v1::Transpose>(data, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{order.size()}, order));
+ data.get_node()->set_friendly_name(output_concat->get_friendly_name() + "/replica_batch_transpose");
+ batched_input_nodes.push_back(data.get_node_shared_ptr());
+ }
+ auto batched_input_shape = gather->get_output_shape(0);
+ batched_input_shape[0] = replica_count;
+ auto batched_input =
+ std::make_shared<ov::op::v1::Reshape>(data,
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{batched_input_shape.size()}, batched_input_shape),
+ false);
+ batched_input->set_friendly_name(output_concat->get_friendly_name() + "/replica_batch");
+ batched_input_nodes.push_back(batched_input);
+ ov::copy_runtime_info(replicas_of("gather"), batched_input_nodes);
+
+ ov::Output<ov::Node> reference_output = *reference.get("gather");
+ ov::Output<ov::Node> batched = batched_input;
+ for (const auto& name : {"conv", "pool", "add", "relu"}) {
+ const auto matched = reference.get(name);
+ if (!matched) {
+ continue;
+ }
+ const auto node = matched->get_node_shared_ptr();
+ auto inputs = node->input_values();
+ // Constant inputs are equal in all replicas, so the reference ones are reused as is with
+ // their markers. Add is commutative, so the data input may be on any port
+ std::replace(inputs.begin(), inputs.end(), reference_output, batched);
+ const auto clone = node->clone_with_new_inputs(inputs);
+ copy_metadata(replicas_of(name), clone);
+ reference_output = *matched;
+ batched = clone->output(0);
+ }
+
+ const auto branch_shape = output_concat->get_input_shape(0);
+ auto batched_shape = branch_shape;
+ batched_shape[0] = replica_count;
+
+ // The model is not modified yet, so skipping leaves it intact
+ if (batched.get_shape() != batched_shape) {
+ return false;
+ }
+
+ // Restore the Concat layout, e.g. for axis 3: [N, C, H, W] -> [C, H, N, W] -> [1, C, H, N * W]
+ const auto& output_shape = output_concat->get_output_shape(0);
+ const auto rank = output_shape.size();
+ const auto concat_axis = static_cast<size_t>(ov::util::normalize(output_concat->get_axis(), rank));
+ std::vector<int64_t> transpose_order(rank - 1);
+ std::iota(transpose_order.begin(), transpose_order.end(), int64_t{1});
+ transpose_order.insert(transpose_order.begin() + (concat_axis - 1), 0);
+ auto repack_transpose = std::make_shared<ov::op::v1::Transpose>(batched, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{rank}, transpose_order));
+ repack_transpose->set_friendly_name(output_concat->get_friendly_name() + "/replica_repack_transpose");
+ auto repacked_output =
+ std::make_shared<ov::op::v1::Reshape>(repack_transpose, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{rank}, output_shape), false);
+ ov::copy_runtime_info(output_concat, repack_transpose);
+ ov::replace_node_update_name(output_concat, repacked_output);
+ return true;
+}
+
+} // namespace
+
+BatchReplicatedBranches::BatchReplicatedBranches() {
+ // Cheap filter before the full branch match: every branch ends with one of these ops
+ const auto first_input_is_branch_output = Predicate(
+ [](const ov::Output<ov::Node>& output) {
+ using namespace ov::op;
+ const auto* producer = output.get_node()->get_input_node_ptr(0);
+ return ov::is_type_any_of<v1::Convolution, v1::MaxPool, v8::MaxPool, v14::MaxPool, v1::Add, v0::Relu>(producer);
+ },
+ "first_input_is_branch_output");
+ auto output_concat = wrap_type<ov::op::v0::Concat>(has_static_shape() && first_input_is_branch_output &&
+ Predicate(
+ [](const ov::Output<ov::Node>& output) {
+ return !is_type_relaxed(output.get_node());
+ },
+ "not_type_relaxed"));
+
+ auto callback = [OV_CAPTURE_CPY_AND_THIS](Matcher& m) {
+ const auto concat = ov::as_type_ptr<ov::op::v0::Concat>(m.get_match_root());
+ if (transformation_callback(concat)) {
+ return false;
+ }
+ const auto branches = match_branches(concat->output(0));
+ return branches && batch_replicated_branches(concat, *branches);
+ };
+
+ auto matcher = std::make_shared<Matcher>(output_concat, "BatchReplicatedBranches");
+ register_matcher(matcher, callback);
+}
+
+} // namespace ov::intel_gpu
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/batch_replicated_branches.hpp b/src/plugins/intel_gpu/src/plugin/transformations/batch_replicated_branches.hpp
new file mode 100644
index 00000000000..cf40e026b3c
--- /dev/null
+++ b/src/plugins/intel_gpu/src/plugin/transformations/batch_replicated_branches.hpp
@@ -0,0 +1,80 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "openvino/pass/matcher_pass.hpp"
+
+namespace ov::intel_gpu {
+
+/**
+ * @brief Runs N equal branches that process slices of one tensor as a single branch with batch N.
+ *
+ * Multi-camera models (e.g. BEV detectors) run the same backbone on every camera image and concatenate the
+ * results. Each replica is a separate batch-1 Convolution, which underuses the GPU; one batch-N Convolution
+ * computes the same values in one kernel.
+ *
+ * Before ([ ] marks optional nodes):
+ *
+ * data [.., N, ..]
+ * │ │
+ * ┌─────┴─────┐ ┌─────┴─────┐
+ * │ Gather(0) │ │Gather(N-1)│
+ * └─────┬─────┘ └─────┬─────┘
+ * ┌──────┴──────┐ ┌──────┴──────┐
+ * │ Convolution │ ... │ Convolution │
+ * └──────┬──────┘ └──────┬──────┘
+ * [MaxPool] [MaxPool]
+ * [Add] [Add]
+ * [Relu] [Relu]
+ * │ [1,C,H,W] │ [1,C,H,W]
+ * └────────────┬────────────┘
+ * ┌─────────┴─────────┐
+ * │ Concat(axis k > 0)│
+ * └───────────────────┘
+ *
+ * After:
+ *
+ * data [.., N, ..]
+ * │
+ * [Transpose] replica axis to the front
+ * │
+ * ┌─────┴─────┐
+ * │ Reshape │ [N,C,H,W]
+ * └─────┬─────┘
+ * ┌──────┴──────┐
+ * │ Convolution │ weights of branch 0
+ * └──────┬──────┘
+ * [MaxPool]
+ * [Add]
+ * [Relu]
+ * ┌─────┴─────┐
+ * │ Transpose │ N in front of axis k
+ * └─────┬─────┘
+ * ┌─────┴─────┐
+ * │ Reshape │ N merged into axis k
+ * └───────────┘
+ *
+ * The rewrite is applied when:
+ * - all shapes are static and every branch output is [1, C, H, W] with Concat on axis k > 0, so batch N is
+ * free for the replicas and the Concat layout can be restored by Transpose and Reshape;
+ * - branch i gathers (Gather v1, v7 or v8) slice i of one tensor and the N slices cover the gathered
+ * dimension, so the batched input is that tensor with the replica axis moved to the front; Transpose is
+ * added only when a dim before that axis is not 1;
+ * - all branches have the same operations, attributes and constants, so the batched branch computes every
+ * replica; Add uses numpy broadcast;
+ * - every branch node feeds only the next one and MaxPool indices are unused, otherwise the original branches
+ * would stay in the graph next to the batched one;
+ * - branch nodes and Concat are not TypeRelaxed: Gather and Concat are replaced by new Reshape and Transpose
+ * nodes, which would drop a type override;
+ * - branch nodes have no control dependencies or dependents: cloning does not keep the dependencies, and the
+ * dependents would keep the original branch alive.
+ */
+class BatchReplicatedBranches : public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("ov::intel_gpu::BatchReplicatedBranches");
+ BatchReplicatedBranches();
+};
+
+} // namespace ov::intel_gpu
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index e18e72a339f..cae4d2810c1 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -94,6 +94,7 @@
#include "transformations/op_conversions/convert_grouped_matmul_to_compressed.hpp"
#include "plugin/transformations/convert_matmul_to_fc.hpp"
#include "plugin/transformations/fuse_moe_shared_expert.hpp"
+#include "plugin/transformations/batch_replicated_branches.hpp"
#include "plugin/transformations/fuse_rope_input_deinterleave.hpp"
#include "transformations/common_optimizations/moe_op_fusion.hpp"
#include "transformations/op_conversions/convert_gather_matmul_to_compressed.hpp"
@@ -1302,6 +1303,8 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
manager.register_pass<ov::intel_gpu::FuseGroupedDepthToSpace>();
manager.register_pass<ov::pass::CommonOptimizations>();
pass_config->disable<ov::pass::GroupQueryAttentionDecomposition>();
+ // After CommonOptimizations: ShapeOf consumers of static branches are folded and do not block batching
+ manager.register_pass<ov::intel_gpu::BatchReplicatedBranches>();
manager.register_pass<ov::intel_gpu::GroupQueryAttentionDecomposition>(device_info.supports_immad);
// In the case of "zp/scale -> reshape -> transpose -> MOE",
diff --git a/src/plugins/intel_gpu/tests/functional/sources.cmake b/src/plugins/intel_gpu/tests/functional/sources.cmake
index bd821c876e1..b9a15ee6c5f 100644
--- a/src/plugins/intel_gpu/tests/functional/sources.cmake
+++ b/src/plugins/intel_gpu/tests/functional/sources.cmake
@@ -324,6 +324,7 @@ set(GPU_FUNCTIONAL_TEST_SOURCES
${CMAKE_CURRENT_LIST_DIR}/single_layer_tests/reduce.cpp
${CMAKE_CURRENT_LIST_DIR}/single_layer_tests/selective_ssm.cpp
${CMAKE_CURRENT_LIST_DIR}/single_layer_tests/tensor_iterator.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/subgraph_tests/batch_replicated_branches.cpp
${CMAKE_CURRENT_LIST_DIR}/subgraph_tests/condition.cpp
${CMAKE_CURRENT_LIST_DIR}/subgraph_tests/convolution_in_place_crop.cpp
${CMAKE_CURRENT_LIST_DIR}/subgraph_tests/convolution_sum_fusion.cpp
diff --git a/src/plugins/intel_gpu/tests/functional/subgraph_tests/batch_replicated_branches.cpp b/src/plugins/intel_gpu/tests/functional/subgraph_tests/batch_replicated_branches.cpp
new file mode 100644
index 00000000000..6dce3a38567
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/functional/subgraph_tests/batch_replicated_branches.cpp
@@ -0,0 +1,105 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <memory>
+#include <sstream>
+#include <string>
+#include <tuple>
+#include <vector>
+
+#include "common_test_utils/node_builders/constant.hpp"
+#include "common_test_utils/ov_tensor_utils.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/concat.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convolution.hpp"
+#include "openvino/op/gather.hpp"
+#include "openvino/op/max_pool.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/relu.hpp"
+#include "shared_test_classes/base/ov_subgraph.hpp"
+
+namespace {
+
+// replica count, element type, whether the branches contain MaxPool -> Add -> Relu after Convolution
+using BatchReplicatedBranchesParams = std::tuple<size_t, ov::element::Type, bool>;
+
+// N branches Gather(i) -> Convolution -> [MaxPool -> Add -> Relu] concatenated along the width,
+// expected to be executed as one batched Convolution
+class BatchReplicatedBranches : public testing::WithParamInterface<BatchReplicatedBranchesParams>, virtual public ov::test::SubgraphBaseStaticTest {
+public:
+ static std::string getTestCaseName(const testing::TestParamInfo<BatchReplicatedBranchesParams>& info) {
+ const auto& [replicas, type, post_ops] = info.param;
+ std::ostringstream result;
+ result << "replicas=" << replicas << "_type=" << type << "_post_ops=" << post_ops;
+ return result.str();
+ }
+
+protected:
+ const ov::test::utils::InputGenerateData data_range{-1, 2, 1000, 1};
+
+ void SetUp() override {
+ targetDevice = ov::test::utils::DEVICE_GPU;
+ const auto& [replicas, type, post_ops] = GetParam();
+ inType = outType = type;
+ if (type == ov::element::f16) {
+ // Each output accumulates 3 * 3 * 3 products of values in [-1, 1] in f16
+ abs_threshold = 0.02f;
+ }
+
+ const ov::Shape input_shape{1, replicas, 3, 16, 16};
+ auto input = std::make_shared<ov::op::v0::Parameter>(type, input_shape);
+ auto axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {1});
+ auto weights = ov::test::utils::make_constant(type, ov::Shape{8, 3, 3, 3}, data_range);
+ auto bias = ov::test::utils::make_constant(type, ov::Shape{8, 1, 1}, data_range);
+
+ ov::OutputVector branches;
+ for (size_t i = 0; i < replicas; ++i) {
+ auto index = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {i});
+ auto gather = std::make_shared<ov::op::v8::Gather>(input, index, axis);
+ ov::Output<ov::Node> branch = std::make_shared<ov::op::v1::Convolution>(gather,
+ weights,
+ ov::Strides{1, 1},
+ ov::CoordinateDiff{1, 1},
+ ov::CoordinateDiff{1, 1},
+ ov::Strides{1, 1});
+ if (post_ops) {
+ branch = std::make_shared<ov::op::v8::MaxPool>(branch, ov::Strides{2, 2}, ov::Strides{1, 1}, ov::Shape{0, 0}, ov::Shape{0, 0}, ov::Shape{2, 2})
+ ->output(0);
+ branch = std::make_shared<ov::op::v1::Add>(branch, bias);
+ branch = std::make_shared<ov::op::v0::Relu>(branch);
+ }
+ branches.push_back(branch);
+ }
+ auto concat = std::make_shared<ov::op::v0::Concat>(branches, 3);
+ function = std::make_shared<ov::Model>(ov::OutputVector{concat}, ov::ParameterVector{input});
+ }
+
+ // By default the input gets the Gather data range from ranges.hpp ([0, 8]): the f16 Convolution outputs then
+ // grow large enough for their rounding error to exceed abs_threshold. Use the constants' range [-1, 1] instead
+ void generate_inputs(const std::vector<ov::Shape>& target_input_static_shapes) override {
+ inputs.clear();
+ const auto& input = function->get_parameters().front();
+ inputs.insert({input, ov::test::utils::create_and_fill_tensor(input->get_element_type(), target_input_static_shapes.front(), data_range)});
+ }
+
+ void validate() override {
+ ov::test::SubgraphBaseTest::validate();
+ // Without the pass every branch keeps its Gather and Convolution, and the outputs are joined by Concat.
+ // With it the batched input is a Reshape of the data, so only one Convolution and no Gather or Concat remain
+ ov::test::CheckNumberOfNodesWithType(compiledModel, "Convolution", 1);
+ ov::test::CheckNumberOfNodesWithTypes(compiledModel, {"Gather", "Concat"}, 0);
+ }
+};
+
+TEST_P(BatchReplicatedBranches, Inference) {
+ run();
+}
+
+INSTANTIATE_TEST_SUITE_P(smoke_BatchReplicatedBranches,
+ BatchReplicatedBranches,
+ ::testing::Combine(::testing::Values(2, 4), ::testing::Values(ov::element::f32, ov::element::f16), ::testing::Bool()),
+ BatchReplicatedBranches::getTestCaseName);
+
+} // namespace
diff --git a/src/plugins/intel_gpu/tests/unit/sources.cmake b/src/plugins/intel_gpu/tests/unit/sources.cmake
index 8f7937f7d6c..274618c05da 100644
--- a/src/plugins/intel_gpu/tests/unit/sources.cmake
+++ b/src/plugins/intel_gpu/tests/unit/sources.cmake
@@ -314,6 +314,7 @@ set(GPU_UNIT_TESTS_SRCS
${CMAKE_CURRENT_LIST_DIR}/test_utils/test_utils.cpp
${CMAKE_CURRENT_LIST_DIR}/test_utils/test_utils.h
${CMAKE_CURRENT_LIST_DIR}/test_utils/uniform_quantized_real_distribution.hpp
+ ${CMAKE_CURRENT_LIST_DIR}/transformations/batch_replicated_branches_test.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/bcast_and_pad_zp_buffers_test.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/clamp_fp16_output_test.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/convert_binary_conv_to_conv_test.cpp
@@ -376,6 +377,8 @@ set(GPU_UNIT_TESTS_SRCS
)
set(GPU_UNIT_TESTS_TRANSFORMATIONS_SRCS
+ ${CMAKE_HOME_DIRECTORY}/src/plugins/intel_gpu/src/plugin/transformations/batch_replicated_branches.cpp
+ ${CMAKE_HOME_DIRECTORY}/src/plugins/intel_gpu/src/plugin/transformations/batch_replicated_branches.hpp
${CMAKE_HOME_DIRECTORY}/src/plugins/intel_gpu/src/plugin/transformations/bcast_and_pad_zp_buffers.cpp
${CMAKE_HOME_DIRECTORY}/src/plugins/intel_gpu/src/plugin/transformations/bcast_and_pad_zp_buffers.hpp
${CMAKE_HOME_DIRECTORY}/src/plugins/intel_gpu/src/plugin/transformations/binary_conv_to_conv.cpp
diff --git a/src/plugins/intel_gpu/tests/unit/transformations/batch_replicated_branches_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/batch_replicated_branches_test.cpp
new file mode 100644
index 00000000000..b0fcee436ea
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/unit/transformations/batch_replicated_branches_test.cpp
@@ -0,0 +1,304 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "plugin/transformations/batch_replicated_branches.hpp"
+
+#include <gtest/gtest.h>
+
+#include <memory>
+#include <sstream>
+#include <string>
+#include <tuple>
+#include <vector>
+
+#include "common_test_utils/ov_test_utils.hpp"
+#include "openvino/core/graph_util.hpp"
+#include "openvino/core/model.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/concat.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/convolution.hpp"
+#include "openvino/op/gather.hpp"
+#include "openvino/op/max_pool.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/relu.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/result.hpp"
+#include "openvino/op/shape_of.hpp"
+#include "openvino/op/transpose.hpp"
+#include "ov_ops/type_relaxed.hpp"
+#include "transformations/rt_info/decompression.hpp"
+
+using namespace testing;
+using namespace ov;
+
+namespace {
+
+// Each branch maps a [1, 3, 8, 8] slice to [1, 4, 4, 4] ([1, 4, 2, 2] with MaxPool)
+struct BranchConfig {
+ size_t replicas = 4;
+ int64_t replica_axis = 1;
+ int64_t concat_axis = 3;
+ bool decompress_weights = false;
+ bool max_pool = false;
+ bool bias = false;
+ bool relu = false;
+ bool port_rt_info = false;
+};
+
+std::ostream& operator<<(std::ostream& os, const BranchConfig& config) {
+ return os << "replicas=" << config.replicas << "_replica_axis=" << config.replica_axis << "_concat_axis=" << config.concat_axis
+ << (config.decompress_weights ? "_decompress" : "") << (config.max_pool ? "_pool" : "") << (config.bias ? "_bias" : "")
+ << (config.relu ? "_relu" : "") << (config.port_rt_info ? "_port_rt_info" : "");
+}
+
+// Original: Gather(i) -> branch -> Concat for i = 0..N-1
+// Batched: [Transpose] -> Reshape of data to [N, 3, 8, 8] -> one branch -> Transpose -> Reshape
+std::shared_ptr<Model> getModel(const BranchConfig& config, bool batched) {
+ Shape data_shape{1, 3, 8, 8};
+ data_shape.insert(data_shape.begin() + config.replica_axis, config.replicas);
+ auto data = std::make_shared<op::v0::Parameter>(element::f32, data_shape);
+
+ // Each call creates its own (equal) weights and bias, so the original branches don't share constants
+ auto make_branch = [&](const Output<Node>& input) -> Output<Node> {
+ Output<Node> weights = op::v0::Constant::create(config.decompress_weights ? element::f16 : element::f32, Shape{4, 3, 2, 2}, {0.5f});
+ if (config.decompress_weights) {
+ auto convert = std::make_shared<op::v0::Convert>(weights, element::f32);
+ mark_as_decompression(convert);
+ weights = convert;
+ }
+ auto conv = std::make_shared<op::v1::Convolution>(input, weights, Strides{2, 2}, CoordinateDiff{0, 0}, CoordinateDiff{0, 0}, Strides{1, 1});
+ if (config.port_rt_info) {
+ conv->input(0).get_rt_info()["port_info"] = std::string{"data"};
+ conv->output(0).get_rt_info()["port_info"] = std::string{"conv_output"};
+ }
+ Output<Node> branch = conv;
+ if (config.max_pool)
+ branch = std::make_shared<op::v8::MaxPool>(branch, Strides{2, 2}, Strides{1, 1}, Shape{0, 0}, Shape{0, 0}, Shape{2, 2})->output(0);
+ if (config.bias)
+ branch = std::make_shared<op::v1::Add>(branch, op::v0::Constant::create(element::f32, Shape{4, 1, 1}, {0.1f, 0.2f, 0.3f, 0.4f}));
+ if (config.relu)
+ branch = std::make_shared<op::v0::Relu>(branch);
+ return branch;
+ };
+
+ if (!batched) {
+ OutputVector branches;
+ for (size_t i = 0; i < config.replicas; ++i) {
+ auto gather = std::make_shared<op::v8::Gather>(data,
+ op::v0::Constant::create(element::i64, Shape{}, {i}),
+ op::v0::Constant::create(element::i64, Shape{}, {config.replica_axis}));
+ branches.push_back(make_branch(gather));
+ }
+ auto concat = std::make_shared<op::v0::Concat>(branches, config.concat_axis);
+ return std::make_shared<Model>(OutputVector{concat}, ParameterVector{data});
+ }
+
+ Output<Node> batched_input = data;
+ // Dims before the replica axis are 1 for axes 0 and 1; axis 2 ([1, 3, N, 8, 8]) needs a Transpose
+ if (config.replica_axis > 1) {
+ std::vector<int64_t> order{config.replica_axis};
+ for (int64_t axis = 0; axis < 5; ++axis) {
+ if (axis != config.replica_axis)
+ order.push_back(axis);
+ }
+ batched_input = std::make_shared<op::v1::Transpose>(data, op::v0::Constant::create(element::i64, Shape{5}, order));
+ }
+ const Shape batched_input_shape{config.replicas, 3, 8, 8};
+ batched_input = std::make_shared<op::v1::Reshape>(batched_input, op::v0::Constant::create(element::i64, Shape{4}, batched_input_shape), false);
+ auto batched_branch = make_branch(batched_input);
+
+ const auto branch_shape = batched_branch.get_shape();
+ std::vector<int64_t> order;
+ std::vector<int64_t> output_shape{1};
+ for (int64_t axis = 1; axis < 4; ++axis) {
+ if (axis == config.concat_axis)
+ order.push_back(0);
+ order.push_back(axis);
+ output_shape.push_back(static_cast<int64_t>(branch_shape[axis] * (axis == config.concat_axis ? config.replicas : 1)));
+ }
+ auto transpose = std::make_shared<op::v1::Transpose>(batched_branch, op::v0::Constant::create(element::i64, Shape{4}, order));
+ auto reshape = std::make_shared<op::v1::Reshape>(transpose, op::v0::Constant::create(element::i64, Shape{4}, output_shape), false);
+ return std::make_shared<Model>(OutputVector{reshape}, ParameterVector{data});
+}
+
+std::shared_ptr<Node> get_concat(const std::shared_ptr<Model>& model) {
+ return model->get_results()[0]->get_input_node_shared_ptr(0);
+}
+
+class BatchReplicatedBranchesTests : public TransformationTestsF {
+public:
+ BatchReplicatedBranchesTests() {
+ comparator.enable(FunctionsComparator::ATTRIBUTES);
+ comparator.enable(FunctionsComparator::CONST_VALUES);
+ comparator.enable(FunctionsComparator::CONSUMERS_COUNT);
+ }
+
+protected:
+ void SetUp() override {
+ TransformationTestsF::SetUp();
+ manager.register_pass<ov::intel_gpu::BatchReplicatedBranches>();
+ }
+};
+
+// Layout (replicas, replica axis, Concat axis), decompress_weights, max_pool, bias, relu
+using BatchReplicatedBranchesParams = std::tuple<BranchConfig, bool, bool, bool, bool>;
+
+class BatchReplicatedBranchesParamTests : public BatchReplicatedBranchesTests, public WithParamInterface<BatchReplicatedBranchesParams> {
+public:
+ static BranchConfig get_config(const BatchReplicatedBranchesParams& params) {
+ auto [config, decompress_weights, max_pool, bias, relu] = params;
+ config.decompress_weights = decompress_weights;
+ config.max_pool = max_pool;
+ config.bias = bias;
+ config.relu = relu;
+ return config;
+ }
+
+ static std::string getTestCaseName(const TestParamInfo<BatchReplicatedBranchesParams>& info) {
+ std::ostringstream ss;
+ ss << get_config(info.param);
+ return ss.str();
+ }
+};
+
+TEST_P(BatchReplicatedBranchesParamTests, BatchesBranches) {
+ const auto config = get_config(GetParam());
+ model = getModel(config, false);
+ model_ref = getModel(config, true);
+}
+
+INSTANTIATE_TEST_SUITE_P(BatchReplicatedBranches,
+ BatchReplicatedBranchesParamTests,
+ Combine(Values(BranchConfig{4, 1, 3}, BranchConfig{2, 0, 1}, BranchConfig{3, 2, 2}), Bool(), Bool(), Bool(), Bool()),
+ BatchReplicatedBranchesParamTests::getTestCaseName);
+
+TEST_F(BatchReplicatedBranchesTests, BatchesBranchesWithPortRtInfo) {
+ BranchConfig config{};
+ config.port_rt_info = true;
+ model = getModel(config, false);
+ model_ref = getModel(config, true);
+}
+
+TEST_F(BatchReplicatedBranchesTests, IsIdempotent) {
+ const BranchConfig config{};
+ model = getModel(config, false);
+ model_ref = getModel(config, true);
+ manager.register_pass<ov::intel_gpu::BatchReplicatedBranches>();
+}
+
+TEST_F(BatchReplicatedBranchesTests, BatchesBranchesWithGatherV1) {
+ const BranchConfig config{};
+ model = getModel(config, false);
+ for (const auto& branch : get_concat(model)->input_values()) {
+ const auto gather = branch.get_node()->get_input_node_shared_ptr(0);
+ ASSERT_TRUE(is_type<op::v8::Gather>(gather));
+ replace_node(gather, std::make_shared<op::v1::Gather>(gather->input_value(0), gather->input_value(1), gather->input_value(2)));
+ }
+ model_ref = getModel(config, true);
+}
+
+TEST_F(BatchReplicatedBranchesTests, RejectsNonExactReplicaOrder) {
+ model = getModel(BranchConfig{}, false);
+ const auto concat = get_concat(model);
+ const auto input2 = concat->input_value(2);
+ concat->input(2).replace_source_output(concat->input_value(3));
+ concat->input(3).replace_source_output(input2);
+}
+
+TEST_F(BatchReplicatedBranchesTests, RejectsPartialReplicaDimension) {
+ model = getModel(BranchConfig{}, false);
+ model->get_parameters()[0]->set_partial_shape(PartialShape{1, 5, 3, 8, 8});
+ model->validate_nodes_and_infer_types();
+}
+
+TEST_F(BatchReplicatedBranchesTests, RejectsConcatOnBatchAxis) {
+ BranchConfig config{};
+ config.concat_axis = 0;
+ model = getModel(config, false);
+}
+
+TEST_F(BatchReplicatedBranchesTests, RejectsDifferentBranchTopology) {
+ model = getModel(BranchConfig{}, false);
+ const auto concat = get_concat(model);
+ concat->input(2).replace_source_output(std::make_shared<op::v0::Relu>(concat->input_value(2)));
+}
+
+TEST_F(BatchReplicatedBranchesTests, RejectsDifferentWeights) {
+ model = getModel(BranchConfig{}, false);
+ const auto conv = get_concat(model)->get_input_node_shared_ptr(1);
+ ASSERT_TRUE(is_type<op::v1::Convolution>(conv));
+ conv->input(1).replace_source_output(op::v0::Constant::create(element::f32, Shape{4, 3, 2, 2}, {1.0f}));
+}
+
+TEST_F(BatchReplicatedBranchesTests, RejectsDynamicShape) {
+ model = getModel(BranchConfig{}, false);
+ model->get_parameters()[0]->set_partial_shape(PartialShape{1, 4, 3, Dimension::dynamic(), 8});
+ model->validate_nodes_and_infer_types();
+}
+
+TEST_F(BatchReplicatedBranchesTests, RejectsAddWithoutNumpyBroadcast) {
+ BranchConfig config{};
+ config.bias = true;
+ model = getModel(config, false);
+ for (const auto& add : get_concat(model)->input_values()) {
+ replace_node(add.get_node_shared_ptr(),
+ std::make_shared<op::v1::Add>(add.get_node()->input_value(0),
+ op::v0::Constant::create(element::f32, Shape{1, 4, 4, 4}, {0.1f}),
+ op::AutoBroadcastType::NONE));
+ }
+}
+
+TEST_F(BatchReplicatedBranchesTests, RejectsExternalConsumer) {
+ model = getModel(BranchConfig{}, false);
+ const auto branch = get_concat(model)->input_value(0);
+ model->add_results({std::make_shared<op::v0::Result>(std::make_shared<op::v0::Relu>(branch))});
+}
+
+TEST_F(BatchReplicatedBranchesTests, RejectsShapeOfConsumer) {
+ model = getModel(BranchConfig{}, false);
+ const auto branch = get_concat(model)->input_value(0);
+ model->add_results({std::make_shared<op::v0::Result>(std::make_shared<op::v3::ShapeOf>(branch))});
+}
+
+TEST_F(BatchReplicatedBranchesTests, RejectsUsedMaxPoolIndices) {
+ BranchConfig config{};
+ config.max_pool = true;
+ model = getModel(config, false);
+ const auto max_pool = get_concat(model)->get_input_node_shared_ptr(0);
+ ASSERT_TRUE(is_type<op::v8::MaxPool>(max_pool));
+ model->add_results({std::make_shared<op::v0::Result>(max_pool->output(1))});
+}
+
+TEST_F(BatchReplicatedBranchesTests, RejectsControlDependency) {
+ model = getModel(BranchConfig{}, false);
+ const auto conv = get_concat(model)->get_input_node_shared_ptr(1);
+ ASSERT_TRUE(is_type<op::v1::Convolution>(conv));
+ conv->add_control_dependency(model->get_parameters()[0]);
+}
+
+TEST_F(BatchReplicatedBranchesTests, RejectsTypeRelaxedBranch) {
+ // The attributes comparator has no handler for the TypeVector attributes of TypeRelaxed nodes and reports
+ // them as an error even for equal nodes, so only the topology and constants are compared
+ comparator.disable(FunctionsComparator::ATTRIBUTES);
+ model = getModel(BranchConfig{}, false);
+ for (const auto& branch : get_concat(model)->input_values()) {
+ const auto conv = as_type_ptr<op::v1::Convolution>(branch.get_node_shared_ptr());
+ ASSERT_NE(conv, nullptr);
+ replace_node(conv, std::make_shared<op::TypeRelaxed<op::v1::Convolution>>(*conv, element::f32));
+ }
+}
+
+TEST_F(BatchReplicatedBranchesTests, RejectsTypeRelaxedConcat) {
+ // The attributes comparator has no handler for the TypeVector attributes of TypeRelaxed nodes and reports
+ // them as an error even for equal nodes, so only the topology and constants are compared
+ comparator.disable(FunctionsComparator::ATTRIBUTES);
+ model = getModel(BranchConfig{}, false);
+ const auto concat = as_type_ptr<op::v0::Concat>(get_concat(model));
+ ASSERT_NE(concat, nullptr);
+ replace_node(concat, std::make_shared<op::TypeRelaxed<op::v0::Concat>>(*concat, element::f32));
+}
+
+} // namespace