-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
354 additions
and
2 deletions.
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
91 changes: 91 additions & 0 deletions
91
python/paddle/fluid/tests/unittests/parallel_dygraph_sync_batch_norm.py
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,91 @@ | ||
# Copyright (c) 2018 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. | ||
|
||
from __future__ import print_function | ||
|
||
import os | ||
import contextlib | ||
import unittest | ||
import numpy as np | ||
import six | ||
import pickle | ||
|
||
import paddle | ||
import paddle.fluid as fluid | ||
import paddle.fluid.dygraph as dygraph | ||
from paddle.fluid import core | ||
from paddle.fluid.optimizer import SGDOptimizer | ||
#from paddle.nn import Conv2D, Pool2D, Linear, SyncBatchNorm | ||
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear, SyncBatchNorm | ||
from paddle.fluid.dygraph.base import to_variable | ||
|
||
from test_dist_base import runtime_main, TestParallelDyGraphRunnerBase | ||
|
||
|
||
class TestLayer(fluid.dygraph.Layer): | ||
def __init__(self, | ||
num_channels, | ||
num_filters, | ||
filter_size, | ||
stride=1, | ||
groups=1, | ||
act=None): | ||
super(TestLayer, self).__init__() | ||
|
||
self._conv = Conv2D( | ||
num_channels=num_channels, | ||
num_filters=num_filters, | ||
filter_size=filter_size, | ||
stride=stride, | ||
padding=(filter_size - 1) // 2, | ||
groups=groups, | ||
act=None, | ||
bias_attr=False) | ||
|
||
self._sync_batch_norm = SyncBatchNorm(num_filters) | ||
|
||
def forward(self, inputs): | ||
y = self._conv(inputs) | ||
y = self._sync_batch_norm(y) | ||
|
||
return y | ||
|
||
|
||
class TestSyncBatchNorm(TestParallelDyGraphRunnerBase): | ||
def get_model(self): | ||
model = TestLayer(3, 64, 7) | ||
train_reader = paddle.batch( | ||
paddle.dataset.flowers.test(use_xmap=False), | ||
batch_size=32, | ||
drop_last=True) | ||
opt = fluid.optimizer.Adam( | ||
learning_rate=1e-3, parameter_list=model.parameters()) | ||
return model, train_reader, opt | ||
|
||
def run_one_loop(self, model, opt, data): | ||
batch_size = len(data) | ||
dy_x_data = np.array([x[0].reshape(3, 224, 224) | ||
for x in data]).astype('float32') | ||
img = to_variable(dy_x_data) | ||
img.stop_gradient = False | ||
|
||
out = model(img) | ||
|
||
out = fluid.layers.mean(out) | ||
|
||
return out | ||
|
||
|
||
if __name__ == "__main__": | ||
runtime_main(TestSyncBatchNorm) |
40 changes: 40 additions & 0 deletions
40
python/paddle/fluid/tests/unittests/test_parallel_dygraph_sync_batch_norm.py
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,40 @@ | ||
# Copyright (c) 2018 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. | ||
|
||
from __future__ import print_function | ||
import unittest | ||
from test_dist_base import TestDistBase | ||
import paddle.fluid as fluid | ||
|
||
import os | ||
flag_name = os.path.splitext(__file__)[0] | ||
|
||
|
||
class TestParallelDygraphMnist(TestDistBase): | ||
def _setup_config(self): | ||
self._sync_mode = False | ||
self._nccl2_mode = True | ||
self._dygraph = True | ||
|
||
def test_mnist(self): | ||
if fluid.core.is_compiled_with_cuda(): | ||
self.check_with_place( | ||
"parallel_dygraph_sync_batch_norm.py", | ||
delta=1e-5, | ||
check_error_log=True, | ||
log_name=flag_name) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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
Oops, something went wrong.