dnn(onnx): eliminate consecutive Transpose pairs with identity composition

Port of the 4.x patch to 5.x. Registers a new
ConsecutiveTransposePairsSubgraph in onnx_graph_simplifier.cpp's
simplifySubgraphs() so that consecutive Transpose nodes whose composed
permutation is identity are eliminated at the ONNX proto level. This
runs before either engine takes over, so both ENGINE_CLASSIC and
ENGINE_NEW benefit.

Equivalent optimizations exist in tf2onnx (TransposeOptimizer._transpose_handler)
and ONNX Runtime (HandleTransposeImpl, 'Permutations cancel' branch).
This commit is contained in:
Kevin Lin
2026-05-16 12:07:20 +08:00
parent 873a4635c6
commit 7be1853b0b
3 changed files with 239 additions and 0 deletions

View File

@@ -2999,6 +2999,20 @@ void Net::Impl::getLayerTypes(std::vector<String>& layersTypes) const
// TODO drop?
int Net::Impl::getLayersCount(const String& layerType) const
{
if (mainGraph) {
int count = 0;
for (const Ptr<Graph>& g: allgraphs) {
const std::vector<Ptr<Layer> >& prog = g->prog();
for (const Ptr<Layer>& layer: prog) {
if (!layer)
continue;
if (layer->type == layerType)
count++;
}
}
return count;
}
int count = 0;
for (Impl::MapIdToLayerData::const_iterator it = layers.begin();
it != layers.end(); it++)