Skip to content

reject input rank mismatch in transpose reshape#10545

Merged
copybara-service[bot] merged 2 commits into
google:masterfrom
aizu-m:transpose-rank-mismatch
Jun 22, 2026
Merged

reject input rank mismatch in transpose reshape#10545
copybara-service[bot] merged 2 commits into
google:masterfrom
aizu-m:transpose-rank-mismatch

Conversation

@aizu-m

@aizu-m aizu-m commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Was poking at transpose with mismatched runtime shapes. A rank-3 transpose fed a rank-2 external input slips straight through reshape instead of being rejected:

Transpose.reshape_rejects_input_rank_mismatch
Expected equality of these values:
  subgraph.Status()              Which is: 0   (xnn_status_success)
  xnn_status_invalid_parameter   Which is: 2

reshape_transpose_operator takes its working rank from the permutation length, which is fixed at define time, and only ties it to the live input rank with an assert. xnn_reshape_external_value lets an external input be reshaped to any rank at run time, so under NDEBUG the assert is gone and the mismatched input is accepted. The op then transposes with a stale dimension count: it reads input->shape.dim[perm[i]] for slots past the live rank (stale values left over from the larger declared shape) and ends up walking the input buffer past what it actually holds.

The permutation length and the input rank must agree. Swapped the assert for a real check, same shape as the softmax and fully-connected runtime rank guards. The regression test reshapes a rank-3 transpose input down to rank 2 and expects invalid_parameter.

@dsharlet dsharlet left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for (another) fix!

INSTANTIATE_TEST_SUITE_P(Transpose, TransposeF16, rank_params);
INSTANTIATE_TEST_SUITE_P(Transpose, TransposeF32, rank_params);

TEST(Transpose, reshape_rejects_input_rank_mismatch) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add #ifndef XNNPACK_USE_YNNPACK around this test. In YNNPACK, transpose can add or remove dimensions, so this check is overly conservative.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, wrapped it in #ifndef XNNPACK_USE_YNNPACK with an #else note, same shape as the softmax test. The reshape check itself lives in src/subgraph/static-transpose.c, which YNNPACK doesn't route through (it has its own static_transpose), so only the test needed the guard.

@copybara-service copybara-service Bot merged commit b0e7c33 into google:master Jun 22, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants