Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit fedeabf

Browse files
authored
Develop xiaobingw (PaddlePaddle#17)
* apply lint * add ipu_build_strategy * del unused code * add popart_canonicalization_utils to ipu_backend deps * Update compiler.py * Update ipu_build_strategy.cc
1 parent cc6895e commit fedeabf

File tree

6 files changed

+135
-3
lines changed

6 files changed

+135
-3
lines changed

paddle/fluid/framework/ipu/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ set(POPART_CANONICALIZATION_HANDLERS_SRC
22
"popart_canonicalization/other_ops.cpp"
33
)
44
# TODO(alleng) build static library
5-
cc_library(popart_canonicalization_utils SHARED SRCS popart_canonicalization_utils.cc
5+
cc_library(popart_canonicalization_utils SRCS popart_canonicalization_utils.cc
66
${POPART_CANONICALIZATION_HANDLERS_SRC} DEPS framework_proto enforce)
77

88
cc_library(ipu_utils SRCS ipu_utils.cc DEPS memory framework_proto popart)
9-
cc_library(ipu_backend SRCS ipu_backend.cc DEPS popart graph framework_proto enforce ipu_utils)
9+
cc_library(ipu_build_strategy SRCS ipu_build_strategy.cc DEPS popart graph framework_proto enforce)
10+
cc_library(ipu_backend SRCS ipu_backend.cc DEPS popart graph framework_proto enforce ipu_utils
11+
ipu_build_strategy popart_canonicalization_utils)

paddle/fluid/framework/ipu/ipu_backend.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ limitations under the License. */
2828
#include <unordered_set>
2929
#include <vector>
3030

31+
#include "paddle/fluid/framework/ipu/ipu_build_strategy.h"
3132
#include "paddle/fluid/framework/feed_fetch_type.h"
3233
#include "paddle/fluid/framework/ir/graph.h"
3334
#include "paddle/fluid/framework/lod_tensor.h"
@@ -38,6 +39,8 @@ limitations under the License. */
3839
namespace paddle {
3940
namespace framework {
4041

42+
using ipu::IpuBuildStrategy;
43+
4144
struct Optimizer {
4245
std::string type;
4346
// as far as we know, attr is usually float
@@ -84,6 +87,7 @@ class IpuBackend {
8487

8588
private:
8689
Optimizer optimizer_;
90+
IpuBuildStrategy ipu_build_strategy_;
8791

8892
std::vector<popart::TensorId> inputs_;
8993
std::vector<popart::TensorId> outputs_;
@@ -97,4 +101,4 @@ class IpuBackend {
97101
};
98102

99103
} // namespace framework
100-
} // namespace paddle
104+
} // namespace paddle
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/fluid/framework/ipu/ipu_build_strategy.h"
16+
#include "paddle/fluid/framework/ir/ipu/ipu_graph_builder_pass.h"
17+
18+
#include <glog/logging.h>
19+
#include "paddle/fluid/framework/ir/graph_printer.h"
20+
21+
namespace paddle {
22+
namespace framework {
23+
namespace ipu {
24+
25+
}
26+
}
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#pragma once
16+
17+
#include <memory>
18+
#include <string>
19+
#include <unordered_set>
20+
#include <utility>
21+
#include <vector>
22+
23+
#include <popart/sessionoptions.hpp>
24+
25+
#include "boost/optional.hpp"
26+
#include "paddle/fluid/framework/ir/pass_builder.h"
27+
#include "paddle/fluid/framework/program_desc.h"
28+
#include "paddle/fluid/framework/scope.h"
29+
#include "paddle/fluid/platform/device_context.h"
30+
#include "paddle/fluid/platform/enforce.h"
31+
32+
namespace paddle {
33+
namespace framework {
34+
35+
namespace ipu {
36+
37+
struct IpuBuildStrategy {
38+
popart::SessionOptions popart_options;
39+
};
40+
41+
} // namespace ipu
42+
43+
} // namespace framework
44+
} // namespace paddle

paddle/fluid/pybind/pybind.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ limitations under the License. */
118118

119119
#ifdef PADDLE_WITH_IPU
120120
#include "paddle/fluid/platform/ipu_info.h"
121+
#include "paddle/fluid/framework/ipu/ipu_backend.h"
121122
#endif
122123

123124
#ifdef PADDLE_WITH_CRYPTO
@@ -3202,6 +3203,12 @@ All parameter, weight, gradient are variables in Paddle.
32023203
})
32033204
.def("device_count", &ParallelExecutor::DeviceCount);
32043205

3206+
#ifdef PADDLE_WITH_IPU
3207+
py::class_<framework::IpuBackend, std::shared_ptr<framework::IpuBackend>>(m,
3208+
"IpuBackend")
3209+
.def(py::init(&IpuBackend::GetInstance));
3210+
#endif
3211+
32053212
BindFleetWrapper(&m);
32063213

32073214
#ifdef PADDLE_WITH_PSLIB

python/paddle/fluid/compiler.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,51 @@ def _get_places(self, place, place_list):
480480
place_list = cpu_places()
481481
assert place_list, "No places for execution."
482482
return place_list
483+
484+
485+
class IpuCompiler(object):
486+
"""
487+
The IpuCompiler is used to transform a program to a ipu-target program.
488+
489+
Args:
490+
program(framework.Program): This argument is the Program being executed.
491+
ipu_build_strategy: This argument is used to build the program with the
492+
specified options, such as operators' replacement, dtype, etc.
493+
494+
Returns:
495+
framework.Program
496+
"""
497+
498+
def __init__(self, program, ipu_build_strategy=None):
499+
if not isinstance(program, framework.Program):
500+
raise TypeError(
501+
"The type of program is wrong, expected Program, but got %s" %
502+
type(program))
503+
504+
self._scope = None
505+
self._program = program
506+
self._graph = core.Graph(program.desc)
507+
self._ipu_build_strategy = ipu_build_strategy
508+
self._compiled = False
509+
self._backend = core.IpuBackend()
510+
self._graph_passes = ["optimizer_extract_pass",
511+
"forward_graph_extract_pass"]
512+
513+
def compile(self, feed_list, fetch_list, scope=None):
514+
for pass_name in self._graph_passes:
515+
graph_pass = core.get_pass(pass_name)
516+
graph_pass.apply(self._graph)
517+
518+
ipu_graph_builder_pass = core.get_pass("ipu_graph_builder_pass")
519+
ipu_graph_builder_pass.set("feed_list", feed_list)
520+
ipu_graph_builder_pass.set("fetch_list", fetch_list)
521+
ipu_graph_builder_pass.apply(self._graph)
522+
523+
convert_pass = core.get_pass('graph_to_program_pass')
524+
desc = core.ProgramDesc()
525+
convert_pass.set_not_owned('program', desc)
526+
convert_pass.apply(self._graph)
527+
program = framework.Program._construct_from_desc(desc)
528+
529+
return program
530+

0 commit comments

Comments
 (0)