-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
support excluded_layers for amp.decorate #52871
Merged
zhangting2020
merged 4 commits into
PaddlePaddle:develop
from
zhangting2020:amp_decorate
Apr 18, 2023
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed 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. | ||
|
||
import unittest | ||
|
||
import paddle | ||
import paddle.nn.functional as F | ||
|
||
|
||
class ConvBNLayer(paddle.nn.Layer): | ||
def __init__( | ||
self, | ||
num_channels, | ||
num_filters, | ||
filter_size, | ||
stride=1, | ||
groups=1, | ||
act=None, | ||
): | ||
super().__init__() | ||
|
||
self._conv = paddle.nn.Conv2D( | ||
in_channels=num_channels, | ||
out_channels=num_filters, | ||
kernel_size=filter_size, | ||
stride=stride, | ||
padding=(filter_size - 1) // 2, | ||
groups=groups, | ||
bias_attr=None, | ||
) | ||
|
||
self._batch_norm = paddle.nn.BatchNorm(num_filters, act=act) | ||
|
||
def forward(self, inputs): | ||
y = self._conv(inputs) | ||
y = self._batch_norm(y) | ||
|
||
return y | ||
|
||
|
||
class Model(paddle.nn.Layer): | ||
def __init__( | ||
self, input_channel, hidden_size, fp16_conv=True, fp16_linear=True | ||
): | ||
super().__init__() | ||
self.conv = ConvBNLayer(input_channel, 8, 3) | ||
self.linear = paddle.nn.Linear(8, hidden_size) | ||
self.layernorm = paddle.nn.Sequential( | ||
paddle.nn.LayerNorm(hidden_size), | ||
paddle.nn.LayerNorm(hidden_size), | ||
) | ||
self.fp16_conv = fp16_conv | ||
self.fp16_linear = fp16_linear | ||
|
||
def forward(self, inputs): | ||
with paddle.amp.auto_cast(enable=self.fp16_conv): | ||
if not self.fp16_conv: | ||
inputs = inputs.astype('float32') | ||
x = self.conv(inputs) | ||
with paddle.amp.auto_cast(enable=self.fp16_linear): | ||
if not self.fp16_linear: | ||
x = x.astype('float32') | ||
x = self.linear(x) | ||
x = F.relu(x) | ||
x = self.layernorm(x) | ||
return x | ||
|
||
|
||
class TestAMPDecorate(unittest.TestCase): | ||
def check_results(self, fp32_layers=[], fp16_layers=[]): | ||
for idx in range(len(fp32_layers)): | ||
for layer in fp32_layers[idx].sublayers(include_self=False): | ||
self.assertEqual(layer.weight.dtype, paddle.float32) | ||
self.assertEqual(layer.bias.dtype, paddle.float32) | ||
|
||
for idx in range(len(fp16_layers)): | ||
for layer in fp16_layers[idx].sublayers(include_self=False): | ||
self.assertEqual(layer.weight.dtype, paddle.float16) | ||
self.assertEqual(layer.bias.dtype, paddle.float16) | ||
|
||
def test_excluded_layers(self): | ||
if not paddle.amp.is_float16_supported(): | ||
return | ||
model = Model(4, 8, fp16_conv=False) | ||
model = paddle.amp.decorate( | ||
models=model, | ||
level='O2', | ||
dtype='float16', | ||
excluded_layers=model.conv, | ||
) | ||
with paddle.amp.auto_cast(level='O2'): | ||
out = model(paddle.rand(shape=[2, 4, 8, 8], dtype='float32')) | ||
self.check_results( | ||
fp32_layers=[model.conv, model.layernorm], | ||
fp16_layers=[model.linear], | ||
) | ||
|
||
def test_excluded_layers_attr_list(self): | ||
if not paddle.amp.is_float16_supported(): | ||
return | ||
model = Model(4, 8, fp16_conv=False, fp16_linear=False) | ||
model = paddle.amp.decorate( | ||
models=model, | ||
level='O2', | ||
dtype='float16', | ||
excluded_layers=[model.conv, model.linear], | ||
) | ||
|
||
with paddle.amp.auto_cast(level='O2'): | ||
out = model(paddle.rand(shape=[2, 4, 8, 8], dtype='float32')) | ||
|
||
self.check_results( | ||
fp32_layers=[model.conv, model.linear, model.layernorm] | ||
) | ||
|
||
def test_excluded_layers_attr_types(self): | ||
if not paddle.amp.is_float16_supported(): | ||
return | ||
model = Model(4, 8) | ||
model = paddle.amp.decorate( | ||
models=model, | ||
level='O2', | ||
dtype='float16', | ||
excluded_layers=[paddle.nn.Conv2D, model.linear], | ||
) | ||
|
||
with paddle.amp.auto_cast(level='O2'): | ||
out = model(paddle.rand(shape=[2, 4, 8, 8], dtype='float16')) | ||
|
||
self.check_results( | ||
fp32_layers=[model.conv, model.linear, model.layernorm] | ||
) | ||
|
||
def test_excluded_layers_attr_none(self): | ||
if not paddle.amp.is_float16_supported(): | ||
return | ||
model = Model(4, 8) | ||
model = paddle.amp.decorate( | ||
models=model, | ||
level='O2', | ||
dtype='float16', | ||
excluded_layers=None, | ||
) | ||
|
||
with paddle.amp.auto_cast(level='O2'): | ||
out = model(paddle.rand(shape=[2, 4, 8, 8], dtype='float16')) | ||
|
||
self.check_results( | ||
fp32_layers=[model.layernorm, model.conv._batch_norm], | ||
fp16_layers=[model.conv._conv, model.linear], | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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.
感觉这个逻辑,后面还可以优化、统一下