-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[OpenCL] Add vectorization to cuda conv2d_nhwc schedule #8636
Merged
jwfromm
merged 5 commits into
apache:main
from
echuraev:echuraev/improve_opencl_conv2d_nhwc_schedule
Sep 30, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b66ac7f
Add vectorization to cuda conv2d_nhwc schedule
echuraev 66135c9
Apply comment
echuraev d8cf61f
Move schedule to topi/gpu dir
echuraev 8014e80
Add vectorization to inner loop
echuraev 828cdfa
Update values of vectorization factor
echuraev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# pylint: disable=invalid-name, unused-argument | ||
"""Schedule for conv2d operator""" | ||
from tvm import te, autotvm | ||
|
||
from .. import nn | ||
from ..utils import traverse_inline | ||
from .conv2d_nhwc import schedule_conv2d_nhwc_direct | ||
|
||
|
||
@autotvm.register_topi_compute("conv2d_nhwc.gpu") | ||
def conv2d_nhwc(cfg, data, kernel, strides, padding, dilation, out_dtype="float32"): | ||
"""Compute conv2d with NHWC layout""" | ||
return nn.conv2d_nhwc(data, kernel, strides, padding, dilation, out_dtype) | ||
|
||
|
||
@autotvm.register_topi_schedule("conv2d_nhwc.gpu") | ||
def schedule_conv2d_nhwc(cfg, outs): | ||
"""Create the schedule for conv2d_nhwc""" | ||
outs = [outs] if isinstance(outs, te.tensor.Tensor) else outs | ||
s = te.create_schedule([x.op for x in outs]) | ||
|
||
def _callback(op): | ||
if op.tag == "conv2d_nhwc": | ||
schedule_conv2d_nhwc_direct(cfg, s, op.output(0)) | ||
|
||
traverse_inline(s, outs[0].op, _callback) | ||
return s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this supposed to vectorize the conv2d inner loop? Based on generated code, I think it only vectorize the last stage, which can be copying local to global mem or fused activation computation. I wonder where 6-7x perf improvement comes from?
Here is an example of generated code where
vec_factor
is fixed to 2.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late reply, I was on vacation. Thank you for your question @masahi!
Today in the latest commit I added vectorization to the conv2d inner loop. The diff of generated OpenCL kernel you can see below:
With this vectorization, the execution time didn't change in comparison with previous code generation.
What about performance boost. First, let me share my performance numbers (the numbers are average execution time in 10 runs) which I got today on the Samsung Galaxy A71:
Bug fix. In the first commit, I also fixed one accuracy problem in OpenCL. Here after fusing
hi
andwi
it was possible that the value of OpenCLglobal_work_size
forclEnqueueNDRangeKernel
was too high and some values in output tensor were not calculated.To answer on this question, let's compare the generated OpenCL code for version with bug fix and with the latest code:
I suppose that the performance boost is connected with decreasing memory latency. We read more data in one execution unit and store them in vector data types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @echuraev for providing details on the performance gains here, quite a drastic improvement here.