Skip to content

[GraphOptimizer] Fix bug in post-lowering optimizations for OpenCL #3493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
shajrawi opened this issue Sep 5, 2019 · 2 comments
Closed

[GraphOptimizer] Fix bug in post-lowering optimizations for OpenCL #3493

shajrawi opened this issue Sep 5, 2019 · 2 comments
Assignees

Comments

@shajrawi
Copy link
Contributor

shajrawi commented Sep 5, 2019

As part of #3452 I created a layout verifier for the OpenCL backend. It caught a bug in our optimizations: see smallConv backend correctness test with the OpenCL backend for a tiny repro. We have a NHWC conv. which we turn into NCHW with a transpose of the inputs (which is fine). then the bug occurs when we optimize post lowering: OptimizeTransposeIntoReshape turns the transpose (of a placeholder) into a reshape and DCE removes the reshape. this is obviously incorrect because the placeholder is in NHWC format.

@shajrawi shajrawi self-assigned this Sep 5, 2019
@nadavrot
Copy link
Contributor

nadavrot commented Sep 5, 2019

Great catch @shajrawi !

@shajrawi
Copy link
Contributor Author

shajrawi commented Sep 5, 2019

Found out why DCE deletes the Reshape node, at some point the conv. is no longer its user: it is done in the OptimizeReshape optimization, we do Reshape(Constant) -> Constant'. after the placeholder has been turned into a constant in placeholder->constant optimization.
This is fine, the verifier complained because it needs to know the layout of any constants / placeholders. The original constant had NHWC layout, and if we try to find out the layout of Constant->Conv. using Glow's canonical layout requirements then the new layout is NHWC too. OptimizeRehsape is not aware that it is running on OpenCL backend and as such it created a constant with NHWC instead.

shajrawi added a commit to shajrawi/glow that referenced this issue Nov 6, 2019
Fixes pytorch#3452

Also Fixes pytorch#3493 and pytorch#3500 GraphOptimizer bugs which were found after adding the layout verifier.

Provides a workaround for the pytorch#3499 issue which was also found via the verifier.

Note: I did not want to break the `enum ConvolutionLayout` introduced in 5074a72, As such, I used it in the verifier / did not change the creation of said nodes.
HOWEVER: We should use the more-generic string-based layout, which I introduce to Transpose node in this commit: it is basically an extendable enum that can be used in the backends without touching the generic code base. as a bonus, it makes differentiation easier: see how it is done for transpose now in `Function *glow::differentiate`.

Getting rid of said enum is a proposed TODO / follow-up.

Also note that some nodes *need* layout requirements, which have been added, namely we need to know the layout for placeholders and constants (obviously) and for reshapes (in case we optimized a transpose into a reshape.

An additional nice-to-have feature of the string-based layout is the wildcard / any-layout option. Some operations, such as data parallel nodes, might accept any layout.

A potential follow-up is to get create a "Solver" that automatically inserts transposes if the layouts do not match, this might greatly simplify the loader: we no longer need to insert transposes based on if we are importing NHWC or NCHW (for example). We just need to annotate the placeholder with the layout information we've get at load-time, and which we "forget" afterwards.

The verifier is useful even without creating said solver, it exposed a couple of bugs which are mentioned in this commit, as such any proposed solvers are not a must-have to demonstrate the usefulness of this commit.
vdantu pushed a commit to vdantu/glow that referenced this issue Jul 12, 2020
…pytorch#3503)

Summary:
Note: I did not want to break the `enum ConvolutionLayout` introduced in 5074a72, As such, I used it in the verifier / did not change the creation of said nodes.
HOWEVER: We should use the more-generic string-based layout, which I introduce to Transpose node in this commit: it is basically an extendable enum that can be used in the backends without touching the generic code base. as a bonus, it makes differentiation easier: see how it is done for transpose now in `Function *glow::differentiate`.

Getting rid of said enum is a proposed TODO / follow-up.

Also note that some nodes *need* layout requirements, which have been added, namely we need to know the layout for placeholders and constants (obviously) and for reshapes (in case we optimized a transpose into a reshape).

An additional nice-to-have feature of the string-based layout is the wildcard / any-layout option. Some operations, such as data parallel nodes, might accept any layout.

A potential follow-up is to get create a "Solver" that automatically inserts transposes if the layouts do not match, this might greatly simplify the loader: we no longer need to insert transposes based on if we are importing NHWC or NCHW (for example). We just need to annotate the placeholder with the layout information we've get at load-time, and which we "forget" afterwards.

The verifier is useful even without creating said solver, it exposed a couple of bugs which are mentioned in this commit, as such any proposed solvers are not a must-have to demonstrate the usefulness of this commit.

Fixes pytorch#3452

Also Fixes pytorch#3493 and Fixes pytorch#3500 GraphOptimizer bugs which were found after adding the layout verifier.

Provides a workaround for the pytorch#3499 issue which was also found via the verifier.
Pull Request resolved: pytorch#3503

Test Plan: `ninja test`

Differential Revision: D18357369

Pulled By: shajrawi

fbshipit-source-id: 45f91fbe120b234c2a85879cee9ee0de6c100b50
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 a pull request may close this issue.

2 participants