Skip to content

Commit

Permalink
Rename IpuBuildStrategy -> IpuStrategy (PaddlePaddle#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
XBWGC authored Aug 13, 2021
1 parent 525f36b commit 77f9be7
Show file tree
Hide file tree
Showing 20 changed files with 76 additions and 79 deletions.
4 changes: 2 additions & 2 deletions paddle/fluid/framework/ipu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ set(POPART_CANONICALIZATION_SRC

cc_library(ipu_device SRCS device.cc DEPS enforce popart)
cc_library(ipu_utils SRCS ipu_utils.cc DEPS memory framework_proto popart)
cc_library(ipu_build_strategy SRCS ipu_build_strategy.cc DEPS popart graph framework_proto enforce)
cc_library(ipu_backend SRCS ipu_backend.cc DEPS popart graph framework_proto enforce ipu_utils ipu_build_strategy ipu_device graph_helper)
cc_library(ipu_strategy SRCS ipu_strategy.cc DEPS popart graph framework_proto enforce)
cc_library(ipu_backend SRCS ipu_backend.cc DEPS popart graph framework_proto enforce ipu_utils ipu_strategy ipu_device graph_helper)
cc_library(popart_canonicalization_utils SRCS ${POPART_CANONICALIZATION_SRC} DEPS framework_proto enforce ipu_utils)
4 changes: 2 additions & 2 deletions paddle/fluid/framework/ipu/ipu_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void IpuBackend::Prepare() {
platform::errors::Unavailable("IPU device isn't attached, please call "
"IpuBackend::AttachDevice(id) first."));

if (ipu_build_strategy_ != nullptr && ipu_build_strategy_->is_training_) {
if (ipu_strategy_ != nullptr && ipu_strategy_->is_training_) {
VLOG(1) << "Creating TrainingSession from Onnx Model...";
auto popart_optimizer = GetPopartOptimizer();
auto it = tensors_.find(optimizer_.loss_);
Expand Down Expand Up @@ -368,7 +368,7 @@ void IpuBackend::LowerBody(const ir::Graph* graph) {
auto inputs = GetOpInputs(op);
auto outputs = op->Output("__outputs__");
// num_outputs training mode 5, inference mode 1
auto num_outputs = ipu_build_strategy_->is_training_ ? 5 : 1;
auto num_outputs = ipu_strategy_->is_training_ ? 5 : 1;
auto epsilon = BOOST_GET_CONST(float, op->GetAttr("epsilon"));
auto momentum = BOOST_GET_CONST(float, op->GetAttr("momentum"));
auto result = builder_->aiOnnxOpset11().batchnormalization(
Expand Down
10 changes: 5 additions & 5 deletions paddle/fluid/framework/ipu/ipu_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ limitations under the License. */

#include "paddle/fluid/framework/feed_fetch_type.h"
#include "paddle/fluid/framework/ipu/device.h"
#include "paddle/fluid/framework/ipu/ipu_build_strategy.h"
#include "paddle/fluid/framework/ipu/ipu_strategy.h"
#include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/framework/tensor.h"
Expand All @@ -42,7 +42,7 @@ namespace paddle {
namespace framework {
namespace ipu {

using ipu::IpuBuildStrategy;
using ipu::IpuStrategy;
struct Optimizer {
std::string type_;
std::string loss_;
Expand Down Expand Up @@ -88,8 +88,8 @@ class IpuBackend {

auto *GetLRFromScope(const std::string &name) { return scope_->GetVar(name); }

void SetIpuBuildStrategy(const IpuBuildStrategy &strategy) {
ipu_build_strategy_ = &strategy;
void SetIpuStrategy(const IpuStrategy &strategy) {
ipu_strategy_ = &strategy;
}
size_t GetNumDevices();
std::vector<int> GetDeviceIds();
Expand All @@ -114,7 +114,7 @@ class IpuBackend {
Optimizer optimizer_;
bool is_prepared_ = false;
const Scope *scope_ = nullptr;
const IpuBuildStrategy *ipu_build_strategy_ = nullptr;
const IpuStrategy *ipu_strategy_ = nullptr;

std::vector<popart::TensorId> inputs_;
std::vector<popart::TensorId> outputs_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ 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. */

#include "paddle/fluid/framework/ipu/ipu_build_strategy.h"
#include "paddle/fluid/framework/ipu/ipu_strategy.h"
#include "paddle/fluid/framework/ir/ipu/ipu_graph_builder_pass.h"

#include <glog/logging.h>
#include "paddle/fluid/framework/ir/graph_printer.h"

namespace paddle {
namespace framework {
namespace ipu {

}
namespace ipu {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace framework {

namespace ipu {

struct IpuBuildStrategy {
struct IpuStrategy {
bool is_training_ = true;
popart::SessionOptions popart_options_;
};
Expand Down
12 changes: 6 additions & 6 deletions paddle/fluid/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3208,19 +3208,19 @@ All parameter, weight, gradient are variables in Paddle.
.def("device_count", &ParallelExecutor::DeviceCount);

#ifdef PADDLE_WITH_IPU
py::class_<framework::ipu::IpuBackend, std::shared_ptr<framework::ipu::IpuBackend>>(
m, "IpuBackend")
py::class_<framework::ipu::IpuBackend,
std::shared_ptr<framework::ipu::IpuBackend>>(m, "IpuBackend")
.def(py::init(&ipu::IpuBackend::GetInstance))
.def("set_scope", &ipu::IpuBackend::SetScope)
.def("set_ipu_build_strategy", &ipu::IpuBackend::SetIpuBuildStrategy);
.def("set_ipu_strategy", &ipu::IpuBackend::SetIpuStrategy);

// TODO(xiaobingw): maybe refactor at future
py::class_<framework::ipu::IpuBuildStrategy>(m, "IpuBuildStrategy")
py::class_<framework::ipu::IpuStrategy>(m, "IpuStrategy")
.def(py::init())
.def_property(
"is_training",
[](const ipu::IpuBuildStrategy &self) { return self.is_training_; },
[](ipu::IpuBuildStrategy &self, bool is_training) {
[](const ipu::IpuStrategy &self) { return self.is_training_; },
[](ipu::IpuStrategy &self, bool is_training) {
self.is_training_ = is_training;
});
#endif
Expand Down
24 changes: 12 additions & 12 deletions python/paddle/fluid/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,14 @@ class IpuCompiler(object):
Args:
program(framework.Program): This argument is the Program being executed.
scope: This argument is the scope which contains model parameters.
ipu_build_strategy: This argument is used to build the program with the
ipu_strategy: This argument is used to build the program with the
specified options, such as operators' replacement, dtype, etc.
Returns:
framework.Program
"""

def __init__(self, program, scope=None, ipu_build_strategy=None):
def __init__(self, program, scope=None, ipu_strategy=None):
if not isinstance(program, framework.Program):
raise TypeError(
"The type of program is wrong, expected Program, but got %s" %
Expand All @@ -513,14 +513,14 @@ def __init__(self, program, scope=None, ipu_build_strategy=None):
else:
self._scope = paddle.static.global_scope()

if ipu_build_strategy is not None:
self._ipu_build_strategy = ipu_build_strategy
if ipu_strategy is not None:
self._ipu_strategy = ipu_strategy
else:
self._ipu_build_strategy = get_ipu_build_strategy()
self._ipu_strategy = get_ipu_strategy()

self._backend = core.IpuBackend()
self._backend.set_scope(self._scope)
self._backend.set_ipu_build_strategy(self._ipu_build_strategy)
self._backend.set_ipu_strategy(self._ipu_strategy)
self._graph_passes = [
"optimizer_extract_pass", "forward_graph_extract_pass",
"popart_canonicalization_pass"
Expand Down Expand Up @@ -550,17 +550,17 @@ def compile(self, feed_list, fetch_list, scope=None):
return program


def get_ipu_build_strategy():
def get_ipu_strategy():
"""
Create and return IpuBuildStrategy instance. We get IpuBuildStrategy from
python side, and the set by IpuBackend.set_ipu_build_strategy.
Create and return IpuStrategy instance. We get IpuStrategy from
python side, and the set by IpuBackend.set_ipu_strategy.
"""
if not core.is_compiled_with_ipu():
raise ValueError(
"Can't get ipu_build_strategy, since PaddlePaddle is not compiled" \
"Can't get ipu_strategy, since PaddlePaddle is not compiled" \
" with IPU"
)

ipu_build_strategy = core.IpuBuildStrategy()
ipu_strategy = core.IpuStrategy()

return ipu_build_strategy
return ipu_strategy
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/ipu/ernie_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,9 +787,9 @@ def get_task_output(self, task, task_labels):
main_prog = paddle.static.default_main_program()

if args.run_on_ipu:
ipu_build_strategy = compiler.get_ipu_build_strategy()
ipu_strategy = compiler.get_ipu_strategy()
ipu_compiler = compiler.IpuCompiler(
main_prog, ipu_build_strategy=ipu_build_strategy)
main_prog, ipu_strategy=ipu_strategy)
program = ipu_compiler.compile(feed_list, fetch_list)
else:
program = main_prog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ def _test(self, run_ipu=True):
if run_ipu:
feed_list = [image.name]
fetch_list = [loss.name]
ipu_build_strategy = compiler.get_ipu_build_strategy()
ipu_build_strategy.is_training = True
ipu_strategy = compiler.get_ipu_strategy()
ipu_strategy.is_training = True
program = compiler.IpuCompiler(
main_prog, ipu_build_strategy=ipu_build_strategy).compile(
feed_list, fetch_list)
main_prog, ipu_strategy=ipu_strategy).compile(feed_list,
fetch_list)
else:
program = main_prog

Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/ipu/ipu_conv_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ def _test(self, run_ipu=True):
if run_ipu:
feed_list = [image.name]
fetch_list = [loss.name]
ipu_build_strategy = compiler.get_ipu_build_strategy()
ipu_build_strategy.is_training = True
ipu_strategy = compiler.get_ipu_strategy()
ipu_strategy.is_training = True
program = compiler.IpuCompiler(
main_prog, ipu_build_strategy=ipu_build_strategy).compile(
feed_list, fetch_list)
main_prog, ipu_strategy=ipu_strategy).compile(feed_list,
fetch_list)
else:
program = main_prog

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
y = paddle.static.nn.fc(x=x, size=2, name='y')

# 运行期:先运行一次startup program初始化网络参数,然后调用飞桨的Executor和CompiledProgram API运行网络。
place = paddle.IPUPlace() # 使用何种设备运行网络,IPUPlace表示使用IPU运行
place = paddle.IPUPlace(0) # 使用何种设备运行网络,IPUPlace表示使用IPU运行
executor = paddle.static.Executor(place) # 创建执行器
compiled_program = paddle.static.CompiledProgram(
paddle.static.default_startup_program())
Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/ipu/ipu_groupnorm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ def _test(self, run_ipu=True):
if run_ipu:
feed_list = [image.name]
fetch_list = [loss.name]
ipu_build_strategy = compiler.get_ipu_build_strategy()
ipu_build_strategy.is_training = True
ipu_strategy = compiler.get_ipu_strategy()
ipu_strategy.is_training = True
program = compiler.IpuCompiler(
main_prog, ipu_build_strategy=ipu_build_strategy).compile(
feed_list, fetch_list)
main_prog, ipu_strategy=ipu_strategy).compile(feed_list,
fetch_list)
else:
program = main_prog

Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/ipu/ipu_pool_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ def _test(self, run_ipu=True):
if run_ipu:
feed_list = [image.name]
fetch_list = [loss.name]
ipu_build_strategy = compiler.get_ipu_build_strategy()
ipu_build_strategy.is_training = True
ipu_strategy = compiler.get_ipu_strategy()
ipu_strategy.is_training = True
program = compiler.IpuCompiler(
main_prog, ipu_build_strategy=ipu_build_strategy).compile(
feed_list, fetch_list)
main_prog, ipu_strategy=ipu_strategy).compile(feed_list,
fetch_list)
else:
program = main_prog

Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/ipu/ipu_relu_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def _test(self, run_ipu=True):
if run_ipu:
feed_list = [image.name]
fetch_list = [out.name]
ipu_build_strategy = compiler.get_ipu_build_strategy()
ipu_build_strategy.is_training = False
ipu_strategy = compiler.get_ipu_strategy()
ipu_strategy.is_training = False
print(main_prog)
program = compiler.IpuCompiler(
main_prog, ipu_build_strategy=ipu_build_strategy).compile(
feed_list, fetch_list)
main_prog, ipu_strategy=ipu_strategy).compile(feed_list,
fetch_list)
print(program)
else:
program = main_prog
Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/ipu/ipu_sgd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ def _test_sgd(self, run_ipu=True):
if run_ipu:
feed_list = [image.name]
fetch_list = [loss.name]
ipu_build_strategy = compiler.get_ipu_build_strategy()
ipu_build_strategy.is_training = True
ipu_strategy = compiler.get_ipu_strategy()
ipu_strategy.is_training = True
program = compiler.IpuCompiler(
main_prog, ipu_build_strategy=ipu_build_strategy).compile(
feed_list, fetch_list)
main_prog, ipu_strategy=ipu_strategy).compile(feed_list,
fetch_list)
else:
program = main_prog

Expand Down
7 changes: 3 additions & 4 deletions python/paddle/fluid/tests/unittests/ipu/ipu_training_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@
main_prog = paddle.static.default_main_program()

if run_on_ipu:
ipu_build_strategy = compiler.get_ipu_build_strategy()
ipu_build_strategy.is_training = False # default True
ipu_strategy = compiler.get_ipu_strategy()
ipu_strategy.is_training = False # default True
program = compiler.IpuCompiler(
main_prog, ipu_build_strategy=ipu_build_strategy).compile(
feed_list, fetch_list)
main_prog, ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
else:
program = main_prog

Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/ipu/test_mul_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def _test_mul(self, run_ipu=True):
if run_ipu:
feed_list = []
fetch_list = [out.name]
ipu_build_strategy = compiler.get_ipu_build_strategy()
ipu_build_strategy.is_training = False
ipu_strategy = compiler.get_ipu_strategy()
ipu_strategy.is_training = False
program = compiler.IpuCompiler(
main_prog, ipu_build_strategy=ipu_build_strategy).compile(
feed_list, fetch_list)
main_prog, ipu_strategy=ipu_strategy).compile(feed_list,
fetch_list)
else:
program = main_prog

Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/ipu/test_pow_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ def _test_pow(self, run_ipu=True):
if run_ipu:
feed_list = []
fetch_list = [out.name]
ipu_build_strategy = compiler.get_ipu_build_strategy()
ipu_build_strategy.is_training = False
ipu_strategy = compiler.get_ipu_strategy()
ipu_strategy.is_training = False
program = compiler.IpuCompiler(
main_prog, ipu_build_strategy=ipu_build_strategy).compile(
feed_list, fetch_list)
main_prog, ipu_strategy=ipu_strategy).compile(feed_list,
fetch_list)
else:
program = main_prog

Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/ipu/test_softmax_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ def _test_softmax(self, run_ipu=True):
if run_ipu:
feed_list = [a.name]
fetch_list = [out.name]
ipu_build_strategy = compiler.get_ipu_build_strategy()
ipu_build_strategy.is_training = False
ipu_strategy = compiler.get_ipu_strategy()
ipu_strategy.is_training = False
program = compiler.IpuCompiler(
main_prog, ipu_build_strategy=ipu_build_strategy).compile(
feed_list, fetch_list)
main_prog, ipu_strategy=ipu_strategy).compile(feed_list,
fetch_list)
else:
program = main_prog

Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/ipu/test_sum_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ def _test_sum(self, run_ipu=True):
if run_ipu:
feed_list = []
fetch_list = [out.name]
ipu_build_strategy = compiler.get_ipu_build_strategy()
ipu_build_strategy.is_training = False
ipu_strategy = compiler.get_ipu_strategy()
ipu_strategy.is_training = False
program = compiler.IpuCompiler(
main_prog, ipu_build_strategy=ipu_build_strategy).compile(
feed_list, fetch_list)
main_prog, ipu_strategy=ipu_strategy).compile(feed_list,
fetch_list)
else:
program = main_prog

Expand Down

0 comments on commit 77f9be7

Please sign in to comment.