@@ -85,4 +85,31 @@ INSTANTIATE_TEST_SUITE_P(Transpose, TransposeQU8, rank_params);
8585INSTANTIATE_TEST_SUITE_P (Transpose, TransposeF16, rank_params);
8686INSTANTIATE_TEST_SUITE_P (Transpose, TransposeF32, rank_params);
8787
88+ #ifndef XNNPACK_USE_YNNPACK
89+ TEST (Transpose, reshape_rejects_input_rank_mismatch) {
90+ ASSERT_EQ (xnn_status_success, xnn_initialize (nullptr /* allocator */ ));
91+
92+ // The permutation fixes the operator rank at define time. An external input
93+ // may be reshaped to a different rank at runtime; the reshape must reject the
94+ // mismatch instead of transposing with a stale dimension count and reading
95+ // past the input buffer.
96+ const std::vector<size_t > perm = {2 , 0 , 1 };
97+ SubgraphTester subgraph (2 );
98+ subgraph.AddInputTensor (3 , xnn_datatype_fp32, 0 )
99+ .AddOutputTensor (3 , xnn_datatype_fp32, 1 )
100+ .AddTranspose (perm, 0 , 1 );
101+ if (subgraph.CreateRuntime () == xnn_status_unsupported_hardware) {
102+ GTEST_SKIP ();
103+ }
104+
105+ std::vector<float > data (6 , 0 .0f );
106+ subgraph.ReshapeExternalTensor (std::vector<size_t >({2 , 3 }), data.data (), 0 )
107+ .ReshapeRuntime ();
108+ EXPECT_EQ (subgraph.Status (), xnn_status_invalid_parameter);
109+ }
110+ #else
111+ // In YNNPACK transpose can add or remove dimensions, so a rank mismatch is not
112+ // an error.
113+ #endif // XNNPACK_USE_YNNPACK
114+
88115} // namespace xnnpack
0 commit comments