Skip to content

Conversation

@askhade
Copy link
Contributor

@askhade askhade commented Feb 22, 2022

Description: Adding a handler for reshape node in transpose optimizer. This handler checks whether the Transpose->Reshape
combination can be removed.

Motivation and Context

  • Why is this change required? What problem does it solve?
    This is a perf optimization. Trying to remove as many layout changing transposes as possible

  • If it fixes an open issue, please link to the issue here.

if (shape_data == nullptr || shape_data->Data().size() == 0) {
return false;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need to check that there's only one dim that is not == 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Consider X->Transpose->Y->Reshape->Z, we check shape(X) == shape(Z), that is why I did not add another check for validating only 1 dim is not 1.

Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't that assume the Reshape is always undo-ing the Transpose?

If more than one dim has a value of 1, the Reshape is not equivalent to a Transpose. Not sure if that occurs in a model, but it seems risky for that not to be explicitly validated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, we remove the 2 nodes only when they cancel each other out. In all other cases we keep them.

static bool HandleReshape(HandlerArgs& args) {
// We check for a very specific case where Tranpose is replaced by Reshape
// for performance. For example Transpose(input {1, 1, 1, X}, perm{0, 3, 2, 1}) can be replaced by Reshape
// Reshape(input{1, 1, 1, X}, shape{1, X, 1, 1})
Copy link
Contributor

@guoyu-wang guoyu-wang Feb 23, 2022

Choose a reason for hiding this comment

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

Can this be generalized to handle {n, 1, 1, x} => {n, x, 1, 1}? Can be something in the future

return false;
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

std::count_if would make it a one-liner.

Copy link
Contributor

@skottmckay skottmckay left a comment

Choose a reason for hiding this comment

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

:shipit:

@askhade askhade merged commit b993de9 into master Feb 24, 2022
@askhade askhade deleted the askhade/transpose_optimizer_updates branch February 24, 2022 20:32
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.

4 participants