From 4ab0607634c5e99f4d4f4e7b7fd3ebab279cd103 Mon Sep 17 00:00:00 2001 From: Zhen Wang Date: Tue, 12 Mar 2019 13:24:25 +0800 Subject: [PATCH 1/2] fix NaN bugs. --- python/paddle/fluid/compiler.py | 10 ++++------ .../contrib/slim/tests/test_quantization_pass.py | 12 ++++++++++-- python/paddle/fluid/framework.py | 6 +++--- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/python/paddle/fluid/compiler.py b/python/paddle/fluid/compiler.py index 4441481093..8f60f6f8b5 100644 --- a/python/paddle/fluid/compiler.py +++ b/python/paddle/fluid/compiler.py @@ -224,12 +224,10 @@ class CompiledProgram(object): self._build_strategy.trainers_endpoints = tps self._persistable_vars = [] - for block_id in range(self._program_desc.num_blocks()): - bdesc = self._program_desc.block(block_id) - self._persistable_vars.extend([ - cpt.to_text(v.name()) for v in bdesc.all_vars() - if v.persistable() and v.type() != core.VarDesc.VarType.RAW - ]) + for node in self._graph.nodes(): + if node.is_var() and node.var() is not None and node.var().persistable() and \ + node.var().type() != core.VarDesc.VarType.RAW: + self._persistable_vars.append(cpt.to_text(node.name())) places = list(map(_place_obj, self._places)) diff --git a/python/paddle/fluid/contrib/slim/tests/test_quantization_pass.py b/python/paddle/fluid/contrib/slim/tests/test_quantization_pass.py index 3b82380f94..c6a301b7f4 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_quantization_pass.py +++ b/python/paddle/fluid/contrib/slim/tests/test_quantization_pass.py @@ -12,6 +12,7 @@ # see the license for the specific language governing permissions and # limitations under the license. +import os import unittest import random import numpy as np @@ -25,6 +26,9 @@ from paddle.fluid.contrib.slim.quantization import ConvertToInt8Pass from paddle.fluid.contrib.slim.quantization import TransformForMobilePass from paddle.fluid import core +os.environ["CUDA_VISIBLE_DEVICES"] = "0" +os.environ["CPU_NUM"] = "1" + def linear_fc(num): data = fluid.layers.data(name='image', shape=[1, 32, 32], dtype='float32') @@ -249,7 +253,11 @@ class TestQuantizationFreezePass(unittest.TestCase): marked_nodes.add(op) test_graph.draw('.', 'test' + dev_name + quant_type, marked_nodes) - quantized_main_program = main_graph.to_program() + build_strategy = fluid.BuildStrategy() + build_strategy.memory_optimize = False + build_strategy.enable_inplace = False + binary = fluid.CompiledProgram(main_graph.graph).with_data_parallel( + loss_name=loss.name, build_strategy=build_strategy) quantized_test_program = test_graph.to_program() iters = 5 batch_size = 8 @@ -264,7 +272,7 @@ class TestQuantizationFreezePass(unittest.TestCase): with fluid.scope_guard(scope): for _ in range(iters): data = next(train_reader()) - loss_v = exe.run(program=quantized_main_program, + loss_v = exe.run(binary, feed=feeder.feed(data), fetch_list=[loss]) if not for_ci: diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 5b9dd86931..bde70c7c78 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -2232,10 +2232,10 @@ class IrGraph(object): Notes: the `graph` cannot contain a circle. Returns: - set(IrNode): nodes in topology order. + list(IrNode): nodes in topology order. """ ordered_nodes = core.topology_sort(self.graph) - return {IrNode(n) for n in ordered_nodes} + return [IrNode(n) for n in ordered_nodes] def build_adjacency_list(self): """ @@ -2303,7 +2303,7 @@ class IrGraph(object): """ Convert the graph into a Program. - Notes: When the graph includes backward operator nodes, the + WARN: When the graph includes backward operator nodes, the conversion process may be failed. Usually, this function is only used to convert a test graph. From 5685a48c23ac9d7bbc1ba7f24c1b83f58fcd83c7 Mon Sep 17 00:00:00 2001 From: Zhen Wang Date: Tue, 12 Mar 2019 14:32:50 +0800 Subject: [PATCH 2/2] Add some fixme. test=develop --- paddle/fluid/framework/details/inplace_op_pass.cc | 5 +++++ paddle/fluid/framework/details/memory_optimize_pass.cc | 5 +++++ paddle/fluid/framework/ir/graph.cc | 2 +- paddle/fluid/framework/ir/graph.h | 8 +++++++- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/framework/details/inplace_op_pass.cc b/paddle/fluid/framework/details/inplace_op_pass.cc index c91fc81b2d..8d4717ad19 100644 --- a/paddle/fluid/framework/details/inplace_op_pass.cc +++ b/paddle/fluid/framework/details/inplace_op_pass.cc @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -263,6 +264,10 @@ void InplacePass::WithdrawModify(const NodeSwapQueue& nodes, void InplacePass::TryInplaceOpInputOutput(ir::Node* op, ir::Graph* graph) const { VLOG(4) << "Try to inplace op " << op->Name(); + // FIXME(liuwei1031): Graph is not aware of the existence of BlockDescs and + // ProgramDescs. + // The operations related to BlockDesc or ProgramDesc should perform on Graph + // or Node directly! PADDLE_ENFORCE(op->Op() != nullptr && op->Op()->Block() != nullptr, "op_desc is nullptr"); // some pre-requirments need to meet if the op want to inplaced. diff --git a/paddle/fluid/framework/details/memory_optimize_pass.cc b/paddle/fluid/framework/details/memory_optimize_pass.cc index e7284ea644..80720af32d 100644 --- a/paddle/fluid/framework/details/memory_optimize_pass.cc +++ b/paddle/fluid/framework/details/memory_optimize_pass.cc @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "gflags/gflags.h" #include "paddle/fluid/framework/data_type.h" @@ -191,6 +192,10 @@ void MemoryOptimizePass::SubGraphOptimize(OpDesc* op_desc) const { // immediately to make the subblock variable reuse strategy take // effect. Because it is a single op in graph. No need to // update the ir nodes. + // FIXME(liuwei1031): Graph is not aware of the existence of + // BlockDescs and ProgramDescs. + // The operations related to BlockDesc or ProgramDesc should perform + // on Graph or Node directly! sub_op_desc->Rename(var->Name(), cache->Name()); if (sub_op_desc->Block() != nullptr && sub_op_desc->Block()->HasVar(var->Name())) { diff --git a/paddle/fluid/framework/ir/graph.cc b/paddle/fluid/framework/ir/graph.cc index 5e954fa9c4..0721ee230b 100644 --- a/paddle/fluid/framework/ir/graph.cc +++ b/paddle/fluid/framework/ir/graph.cc @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #include -#include +#include #include "paddle/fluid/framework/ir/graph.h" #include "paddle/fluid/framework/op_proto_maker.h" diff --git a/paddle/fluid/framework/ir/graph.h b/paddle/fluid/framework/ir/graph.h index cfd974e4bd..eb4fb73c89 100644 --- a/paddle/fluid/framework/ir/graph.h +++ b/paddle/fluid/framework/ir/graph.h @@ -17,6 +17,7 @@ limitations under the License. */ #include #include #include +#include #include #include "paddle/fluid/framework/ir/node.h" @@ -199,7 +200,12 @@ class Graph { // WARN: After a series of passes, the current graph can be quite // different from OriginProgram. Caller shouldn't assume much from // the returned OriginProgram. - const ProgramDesc &OriginProgram() const { return program_; } + const ProgramDesc &OriginProgram() const { + LOG(WARNING) << "WARN: After a series of passes, the current graph can be " + "quite different from OriginProgram. So, please avoid " + "using the `OriginProgram()` method!"; + return program_; + } // This method takes ownership of `node`. ir::Node *AddNode(ir::Node *node) {