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

Commit 305317c

Browse files
authored
Develop xiaobingw (PaddlePaddle#21)
* 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 * apply lint * add LowerConst * update test case and resolve comments * add TensorCopy(tensor_util.cc)
1 parent d02eef1 commit 305317c

File tree

8 files changed

+76
-48
lines changed

8 files changed

+76
-48
lines changed

paddle/fluid/framework/ipu/ipu_backend.cc

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ void IpuBackend::Compile(ir::Graph* graph,
7070
}
7171
}
7272

73+
LowerWeights(graph);
7374
LowerBody(graph);
7475

7576
VLOG(1) << "-- fetch_list --";
@@ -121,7 +122,7 @@ void IpuBackend::Prepare() {
121122
}
122123

123124
void IpuBackend::Run(const std::vector<const Tensor*>& inputs,
124-
std::vector<Tensor*>& outputs) {
125+
const std::vector<Tensor*>& outputs) {
125126
Prepare();
126127

127128
std::map<popart::TensorId, popart::IArray&> popart_inputs;
@@ -171,6 +172,36 @@ std::vector<std::string> IpuBackend::GetOpInputs(const OpDesc* op) {
171172
return inputs;
172173
}
173174

175+
void IpuBackend::LowerWeights(const ir::Graph* graph) {
176+
PADDLE_ENFORCE_NOT_NULL(scope_,
177+
platform::errors::PreconditionNotMet(
178+
"You should call set_scope before LowerWeights"));
179+
180+
// at this step, i think the graph doesn't contains optimizer
181+
// related states
182+
for (const auto* node : graph->Nodes()) {
183+
if (node->IsVar() && !node->IsCtrlVar() && node->Var()) {
184+
if (node->Var()->Persistable()) {
185+
auto var_name = node->Var()->Name();
186+
auto var = scope_->FindVar(var_name);
187+
if (var) {
188+
auto tensor = var->Get<framework::LoDTensor>();
189+
auto dtype = VarType2PopartType(tensor.type());
190+
auto shape = std::vector<int64_t>();
191+
for (size_t i = 0; i < tensor.dims().size(); ++i) {
192+
shape.push_back(tensor.dims().at(i));
193+
}
194+
popart::TensorInfo tensor_info(dtype, shape);
195+
popart::ConstVoidData const_data{tensor.data<void>(), tensor_info};
196+
popart::TensorId result =
197+
builder_->addInitializedInputTensor(const_data);
198+
tensors_.emplace(var_name, result);
199+
}
200+
}
201+
}
202+
}
203+
}
204+
174205
void IpuBackend::LowerBody(const ir::Graph* graph) {
175206
auto nodes = TopologySortOperations(*graph);
176207
for (const auto* node : nodes) {
@@ -230,7 +261,8 @@ void IpuBackend::LowerBody(const ir::Graph* graph) {
230261
builder_->aiOnnxOpset11().reducemean(inputs, axes, keepdims);
231262
tensors_.emplace(outputs[0], result);
232263
} else {
233-
PADDLE_THROW(platform::errors::Unimplemented("Unimplemented."));
264+
PADDLE_THROW(platform::errors::Unimplemented("Unimplemented op type %s.",
265+
op_type));
234266
}
235267
}
236268
}

paddle/fluid/framework/ipu/ipu_backend.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ limitations under the License. */
1515
#pragma once
1616

1717
#include <map>
18+
#include <string>
19+
#include <vector>
20+
1821
#include <popart/builder.hpp>
1922
#include <popart/dataflow.hpp>
2023
#include <popart/devicemanager.hpp>
@@ -24,12 +27,9 @@ limitations under the License. */
2427
#include <popart/sessionoptions.hpp>
2528
#include <popart/stepio.hpp>
2629
#include <popart/tensorinfo.hpp>
27-
#include <string>
28-
#include <unordered_set>
29-
#include <vector>
3030

31-
#include "paddle/fluid/framework/ipu/ipu_build_strategy.h"
3231
#include "paddle/fluid/framework/feed_fetch_type.h"
32+
#include "paddle/fluid/framework/ipu/ipu_build_strategy.h"
3333
#include "paddle/fluid/framework/ir/graph.h"
3434
#include "paddle/fluid/framework/lod_tensor.h"
3535
#include "paddle/fluid/framework/scope.h"
@@ -49,13 +49,13 @@ struct Optimizer {
4949

5050
class IpuBackend {
5151
public:
52-
explicit IpuBackend();
52+
IpuBackend();
5353

5454
void Compile(ir::Graph *graph, const std::vector<std::string> &feed_list,
5555
const std::vector<std::string> &fetch_list);
5656

5757
void Run(const std::vector<const Tensor *> &inputs,
58-
std::vector<Tensor *> &outputs);
58+
const std::vector<Tensor *> &outputs);
5959

6060
std::string GetOptimizerType() { return optimizer_.type; }
6161

@@ -74,9 +74,7 @@ class IpuBackend {
7474
}
7575

7676
// SetScope, so we can get model parameters from scope
77-
void SetScope(Scope* scope) {
78-
scope_ = scope;
79-
}
77+
void SetScope(Scope *scope) { scope_ = scope; }
8078

8179
static std::shared_ptr<IpuBackend> GetInstance() {
8280
if (NULL == instance_) {
@@ -87,6 +85,7 @@ class IpuBackend {
8785

8886
private:
8987
void Prepare();
88+
void LowerWeights(const ir::Graph *);
9089
void LowerBody(const ir::Graph *);
9190
std::vector<std::string> GetOpInputs(const OpDesc *op);
9291

paddle/fluid/framework/ir/ipu/ipu_runtime_replacer_pass.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ namespace ir {
3333

3434
void IpuRuntimeReplacerPass::ApplyImpl(ir::Graph* graph) const {
3535
VLOG(10) << "enter IpuRuntimeReplacerPass::ApplyImpl";
36-
3736
VLOG(10) << "Raw Graph: ";
3837
VLOG(10) << DebugString(graph);
3938

paddle/fluid/framework/ir/ipu/optimizer_extract_pass.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "paddle/fluid/framework/ir/ipu/optimizer_extract_pass.h"
1616

1717
#include "paddle/fluid/framework/ipu/ipu_backend.h"
18+
#include "paddle/fluid/framework/ir/pass_tester_helper.h"
1819
#include "paddle/fluid/framework/op_proto_maker.h"
1920

2021
namespace paddle {
@@ -24,6 +25,10 @@ namespace ir {
2425
class Graph;
2526

2627
void IpuOptimizerExtractPass::ApplyImpl(ir::Graph* graph) const {
28+
VLOG(10) << "enter IpuOptimizerExtractPass::ApplyImpl";
29+
VLOG(10) << "Raw Graph: ";
30+
VLOG(10) << DebugString(graph);
31+
2732
auto ipu_backend = paddle::framework::IpuBackend::GetInstance();
2833

2934
for (auto* node : graph->Nodes()) {
@@ -51,11 +56,15 @@ void IpuOptimizerExtractPass::ApplyImpl(ir::Graph* graph) const {
5156
}
5257
}
5358
}
59+
60+
VLOG(10) << "Post Graph: ";
61+
VLOG(10) << DebugString(graph);
62+
VLOG(10) << "leave IpuOptimizerExtractPass::ApplyImpl";
5463
}
5564

5665
} // namespace ir
5766
} // namespace framework
5867
} // namespace paddle
5968

6069
REGISTER_PASS(optimizer_extract_pass,
61-
paddle::framework::ir::IpuOptimizerExtractPass);
70+
paddle::framework::ir::IpuOptimizerExtractPass);

paddle/fluid/framework/tensor_util.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ void TensorCopy(const Tensor& src, const platform::Place& dst_place,
7474
memory::Copy(BOOST_GET_CONST(platform::CPUPlace, dst_place), dst_ptr,
7575
BOOST_GET_CONST(platform::CPUPlace, src_place), src_ptr, size);
7676
}
77+
78+
#ifdef PADDLE_WITH_IPU
79+
if (platform::is_cpu_place(src_place) && platform::is_ipu_place(dst_place)) {
80+
memory::Copy(BOOST_GET_CONST(platform::IPUPlace, dst_place), dst_ptr,
81+
BOOST_GET_CONST(platform::CPUPlace, src_place), src_ptr, size);
82+
}
83+
#endif
84+
7785
#ifdef PADDLE_WITH_XPU
7886
else if (platform::is_xpu_place(src_place) && // NOLINT
7987
platform::is_cpu_place(dst_place)) {

python/paddle/fluid/compiler.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ def __init__(self, program, scope=None, ipu_build_strategy=None):
515515
self._backend = core.IpuBackend()
516516
self._backend.set_scope(self._scope)
517517
self._graph_passes = [
518-
"optimizer_extract_pass", "forward_graph_extract_pass"
518+
"optimizer_extract_pass", "forward_graph_extract_pass",
519+
"popart_canonicalization_pass"
519520
]
520521

521522
def compile(self, feed_list, fetch_list, scope=None):
@@ -528,6 +529,11 @@ def compile(self, feed_list, fetch_list, scope=None):
528529
ipu_graph_builder_pass.set("fetch_list", fetch_list)
529530
ipu_graph_builder_pass.apply(self._graph)
530531

532+
ipu_runtime_replacer_pass = core.get_pass("ipu_runtime_replacer_pass")
533+
ipu_runtime_replacer_pass.set("feed_list", feed_list)
534+
ipu_runtime_replacer_pass.set("fetch_list", fetch_list)
535+
ipu_runtime_replacer_pass.apply(self._graph)
536+
531537
convert_pass = core.get_pass('graph_to_program_pass')
532538
desc = core.ProgramDesc()
533539
convert_pass.set_not_owned('program', desc)

python/paddle/fluid/tests/unittests/ipu/ipu_simple_add.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import numpy as np
1616
import paddle
1717
import paddle.fluid.core as core
18+
import paddle.fluid.compiler as compiler
1819
from paddle.static import Program
1920

2021
paddle.enable_static()
@@ -37,36 +38,10 @@
3738
main_prog = paddle.static.default_main_program()
3839
print(main_prog._to_readable_code())
3940

40-
graph = core.Graph(main_prog.desc)
41-
42-
print(graph)
43-
44-
#graph_viz_pass = core.get_pass("graph_viz_pass")
45-
#graph_viz_path = "./test_viz_pass"
46-
#graph_viz_pass.set('graph_viz_path', graph_viz_path)
47-
#graph = graph_viz_pass.apply(graph)
48-
4941
feed_list = ['a', 'b']
5042
fetch_list = ['tmp_0']
43+
program = compiler.IpuCompiler(main_prog).compile(feed_list, fetch_list)
5144

52-
popart_canonicalization_pass = core.get_pass("popart_canonicalization_pass")
53-
popart_canonicalization_pass.apply(graph)
54-
55-
ipu_graph_builder_pass = core.get_pass("ipu_graph_builder_pass")
56-
ipu_graph_builder_pass.set("feed_list", feed_list)
57-
ipu_graph_builder_pass.set("fetch_list", fetch_list)
58-
ipu_graph_builder_pass.apply(graph)
59-
60-
ipu_runtime_replacer_pass = core.get_pass("ipu_runtime_replacer_pass")
61-
ipu_runtime_replacer_pass.set("feed_list", feed_list)
62-
ipu_runtime_replacer_pass.set("fetch_list", fetch_list)
63-
ipu_runtime_replacer_pass.apply(graph)
64-
65-
convert_pass = core.get_pass('graph_to_program_pass')
66-
desc = core.ProgramDesc()
67-
convert_pass.set_not_owned('program', desc)
68-
convert_pass.apply(graph)
69-
program = Program._construct_from_desc(desc)
7045
print("Program to run:")
7146
print(program._to_readable_code())
7247

python/paddle/fluid/tests/unittests/ipu/ipu_training_test.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,28 @@
2727
adam.minimize(loss)
2828

2929
# 运行期:先运行一次startup program初始化网络参数,然后调用飞桨的Executor和CompiledProgram API运行网络。
30-
place = paddle.IPUPlace() # 使用何种设备运行网络,IPUPlace表示使用IPU运行
30+
place = paddle.IPUPlace(0) # 使用何种设备运行网络,IPUPlace表示使用IPU运行
3131
executor = paddle.static.Executor(place) # 创建执行器
3232
print("---------- startup_program --------------")
3333
prog = paddle.static.default_startup_program()
3434
print(prog._to_readable_code())
35-
executor.run(
36-
paddle.static.default_startup_program()) # 运行startup program进行参数初始化
35+
executor.run(prog) # 运行startup program进行参数初始化
36+
3737
print("---------- main_program --------------")
3838
prog = paddle.static.default_main_program()
3939
print(prog._to_readable_code())
4040

4141
# 再使用CompiledProgram编译网络,准备执行。
42-
compiled_program = paddle.static.CompiledProgram(
43-
paddle.static.default_main_program())
42+
compiled_program = paddle.static.CompiledProgram(prog)
4443

4544
BATCH_NUM = 2
4645
BATCH_SIZE = 32
4746

4847
for batch_id in range(BATCH_NUM):
4948
input_image = np.random.random([BATCH_SIZE, 3, 224, 224]).astype('float32')
50-
loss_numpy, = executor.run(
51-
compiled_program, feed={'image': input_image}, fetch_list=[loss])
49+
loss_numpy, = executor.run(compiled_program,
50+
feed={'image': input_image},
51+
fetch_list=[loss])
5252
print("Batch {}, loss = {}".format(batch_id, loss_numpy))
5353

5454
# 关闭静态图模式

0 commit comments

Comments
 (0)