Skip to content

Commit f45818e

Browse files
committed
create new varible in scope
1 parent 6e735e1 commit f45818e

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

python/paddle/fluid/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
'clip',
6868
'SimpleDistributeTranspiler',
6969
'DistributeTranspiler',
70+
'InferenceTranspiler',
7071
'memory_optimize',
7172
'release_memory',
7273
'profiler',

python/paddle/fluid/inference_transpiler.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,20 @@
2121
class InferenceTranspiler:
2222
def transpile(self, program, scope, place):
2323
'''
24-
Transpile the program to a inference program by fused batch normalization.
24+
Transpile the program. Support only fuse batch normalization now.
25+
26+
:param program: program to transpile
27+
:type program: Program
28+
:param scope: inference scope
29+
:type scope: Scope
30+
:param place: inference place
31+
:type place: Place
32+
'''
33+
self.fuse_batch_norm(program, scope, place)
34+
35+
def fuse_batch_norm(self, program, scope, place):
36+
'''
37+
Transpile the program by fused batch normalization.
2538
2639
The batch normalization followed the convolution or fully connected layer
2740
can be integrated with them. Doing so will give us a forward acceleration,
@@ -57,8 +70,6 @@ def transpile(self, program, scope, place):
5770
:type scope: Scope
5871
:param place: inference place
5972
:type place: Place
60-
:return: program by fused batch normalization
61-
:rtype: Program
6273
'''
6374
self.scope = scope
6475
self.place = place
@@ -96,7 +107,7 @@ def transpile(self, program, scope, place):
96107
# TODO(luotao): use clone() method to flush the program.desc in force,
97108
# since some large program.desc will not be flushed immediately.
98109
# And a better solution will be considered later.
99-
return program.clone()
110+
program = program.clone()
100111

101112
# ====================== private transpiler functions =====================
102113
def _insert_bias_op(self, index, current_op, bn_op):
@@ -142,11 +153,25 @@ def _fuse_param(self, current_op, bn_op, bias_op, with_bias):
142153
:type with_bias: Int
143154
'''
144155

145-
def _load_tensor(param_name):
146-
return self.scope.find_var(param_name[0]).get_tensor()
156+
def _update_param(op, old_param_name, new_param):
157+
# For the sake of remaining the original variables the same as before,
158+
# create new variables in scope to store the new parameters.
159+
old_param_name = old_param_name[0]
160+
old_var = self.block.vars[old_param_name]
161+
new_param_name = old_param_name + '_fuse_bn'
162+
new_var = self.block.create_parameter(
163+
name=new_param_name.encode('ascii'),
164+
type=old_var.type,
165+
dtype=old_var.dtype,
166+
shape=old_var.shape)
167+
op.rename_input(old_param_name, new_param_name)
168+
self.scope.var(new_param_name)
169+
170+
tensor = self.scope.find_var(new_param_name).get_tensor()
171+
tensor.set(np.array(new_param), self.place)
147172

148173
def _load_param(param_name):
149-
return np.array(_load_tensor(param_name))
174+
return np.array(self.scope.find_var(param_name[0]).get_tensor())
150175

151176
bias_bn = _load_param(bn_op.input("Bias")) #Bias
152177
scale_bn = _load_param(bn_op.input("Scale")) #Scale
@@ -155,8 +180,6 @@ def _load_param(param_name):
155180

156181
# TODO(luotao1): consider only conv2d now. fc would be delt later.
157182
current_param = _load_param(current_op.input("Filter"))
158-
current_tensor = _load_tensor(current_op.input("Filter"))
159-
160183
std_bn = np.float32(np.sqrt(np.add(var_bn, 1e-5)))
161184
tmp = np.float32(np.divide(scale_bn, std_bn))
162185

@@ -167,17 +190,16 @@ def _load_param(param_name):
167190
bias = np.zeros(bias_bn.shape)
168191
bias = np.float32(
169192
np.add(np.multiply(np.subtract(bias, mean_bn), tmp), bias_bn))
170-
bias_tensor = _load_tensor(bias_op.input("Y"))
171-
bias_tensor.set(bias, self.place)
172193

173194
# re-compute weight of conv2d
174195
tmp = tmp.reshape(tmp.shape[0], -1)
175196
dst_param = current_param.reshape((tmp.shape[0], -1))
176197
dst_param = np.float32(np.multiply(dst_param, tmp))
177198
dst_param = dst_param.reshape(current_param.shape)
178199

179-
# set the updated parameters
180-
current_tensor.set(np.array(dst_param), self.place)
200+
# update parameters
201+
_update_param(current_op, current_op.input("Filter"), dst_param)
202+
_update_param(bias_op, bias_op.input("Y"), bias)
181203

182204
# collect the renamed input
183205
self.input_map[bn_op.output("Y")[0]] = bias_op.output("Out")[0]

python/paddle/fluid/tests/book/test_image_classification.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,17 @@ def infer(use_cuda, save_dirname=None):
226226
batch_size = 1
227227
tensor_img = numpy.random.rand(batch_size, 3, 32, 32).astype("float32")
228228

229+
# Use inference_transpiler to speedup
230+
inference_transpiler_program = inference_program.clone()
231+
t = fluid.InferenceTranspiler()
232+
t.transpile(inference_transpiler_program, inference_scope, place)
233+
229234
# Construct feed as a dictionary of {feed_target_name: feed_target_data}
230235
# and results will contain a list of data corresponding to fetch_targets.
231236
results = exe.run(inference_program,
232237
feed={feed_target_names[0]: tensor_img},
233238
fetch_list=fetch_targets)
234239

235-
# Use inference_transpiler to speedup
236-
t = fluid.InferenceTranspiler()
237-
inference_transpiler_program = t.transpile(inference_program,
238-
inference_scope, place)
239240
transpiler_results = exe.run(inference_transpiler_program,
240241
feed={feed_target_names[0]: tensor_img},
241242
fetch_list=fetch_targets)

0 commit comments

Comments
 (0)