Skip to content

Commit

Permalink
export enable_manual_shard to python sid (PaddlePaddle#67)
Browse files Browse the repository at this point in the history
* add ipu_stage for pipeline

* Update popart_canonicalization_pass.cc

* del hasattr

* export enable_manual_shard to python side
  • Loading branch information
XBWGC authored Aug 17, 2021
1 parent 791b954 commit 24268f3
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
10 changes: 7 additions & 3 deletions paddle/fluid/framework/ipu/ipu_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,15 @@ void IpuBackend::Prepare() {
paddle::platform::errors::InvalidArgument(
"loss_id = %s doesn't exist in popart graph.", optimizer_.loss_));
session_ = popart::TrainingSession::createFromOnnxModel(
proto, dataFlow, it->second, *popart_optimizer, curr_device_);
proto, dataFlow, it->second, *popart_optimizer, curr_device_,
popart::InputShapeInfo(), ipu_strategy_->popart_options_,
popart::Patterns(popart::PatternsLevel::Default));
} else {
VLOG(1) << "Creating InferenceSession from Onnx Model...";
session_ = popart::InferenceSession::createFromOnnxModel(proto, dataFlow,
curr_device_);
session_ = popart::InferenceSession::createFromOnnxModel(
proto, dataFlow, curr_device_, popart::InputShapeInfo(),
ipu_strategy_->popart_options_,
popart::Patterns(popart::PatternsLevel::Default));
}
VLOG(1) << "Creating session from Onnx Model...done";

Expand Down
2 changes: 2 additions & 0 deletions paddle/fluid/framework/ipu/ipu_strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace framework {

namespace ipu {

using VirtualGraphMode = popart::VirtualGraphMode;

struct IpuStrategy {
int num_ipus_ = 1;
bool is_training_ = true;
Expand Down
19 changes: 19 additions & 0 deletions paddle/fluid/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3230,6 +3230,25 @@ All parameter, weight, gradient are variables in Paddle.
},
R"DOC(
Bool type, True enable pipeline, otherwise disable. Default False.
)DOC")
.def_property(
"enable_manual_shard",
[](const ipu::IpuStrategy &self) {
return self.popart_options_.virtualGraphMode ==
ipu::VirtualGraphMode::Manual;
},
[](ipu::IpuStrategy &self, bool enable_ipu_shard) {
if (enable_ipu_shard) {
self.popart_options_.virtualGraphMode =
ipu::VirtualGraphMode::Manual;
} else {
self.popart_options_.virtualGraphMode =
ipu::VirtualGraphMode::Off;
}
},
R"DOC(
Bool type, True enable model sharding, otherwise disable. Default "
"False.
)DOC");
#endif

Expand Down
3 changes: 1 addition & 2 deletions python/paddle/fluid/tests/unittests/ipu/ipu_conv_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def _test(self, run_ipu=True):
name='image', shape=[1, 3, 10, 10], dtype='float32')
conv1 = paddle.static.nn.conv2d(
image, num_filters=3, filter_size=3, bias_attr=False)
conv2 = conv1 + conv1
loss = paddle.mean(conv2)
loss = paddle.mean(conv1)

adam = paddle.optimizer.Adam(learning_rate=1e-2)
adam.minimize(loss)
Expand Down
57 changes: 57 additions & 0 deletions python/paddle/fluid/tests/unittests/ipu/ipu_strategy_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (c) 2021 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 numpy as np
import unittest
import sys
import paddle
import paddle.fluid as fluid
import paddle.fluid.compiler as compiler

paddle.enable_static()
SEED = 2021


@unittest.skipIf(not paddle.is_compiled_with_ipu(),
"core is not compiled with IPU")
class TestConvNet(unittest.TestCase):
def test_training(self):
ipu_strategy = compiler.get_ipu_strategy()

assert ipu_strategy.num_ipus == 1, "Default num_ipus must be 1"
assert ipu_strategy.is_training == True, "Default is_training is True"
assert ipu_strategy.enable_pipelining == False, \
"Default enable_pipelining is False"
assert ipu_strategy.enable_manual_shard == False, \
"Default enable_manual_shard is False"

ipu_strategy.num_ipus = 2
assert ipu_strategy.num_ipus == 2, "Set num_ipus Failed"

ipu_strategy.is_training = False
assert ipu_strategy.is_training == False, "Set is_training Failed"

ipu_strategy.enable_pipelining = True
assert ipu_strategy.enable_pipelining == True, \
"Set enable_pipelining Failed"

ipu_strategy.enable_manual_shard = True
assert ipu_strategy.enable_manual_shard == True, \
"Set enable_manual_shard Failed"


if __name__ == "__main__":
unittest.main()

0 comments on commit 24268f3

Please sign in to comment.