Skip to content

Commit 54ae8e4

Browse files
authored
Merge pull request #10741 from jacquesqiao/inferencer-support-multi-gpu
Inferencer support parallel_executor
2 parents 67b8a30 + d2d671e commit 54ae8e4

File tree

6 files changed

+42
-28
lines changed

6 files changed

+42
-28
lines changed

python/paddle/fluid/inferencer.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,68 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import contextlib
16+
1517
import core
1618

1719
import executor
1820
import framework
1921
import io
22+
import parallel_executor
2023
import unique_name
2124
from trainer import check_and_get_place
2225

2326
__all__ = ['Inferencer', ]
2427

2528

2629
class Inferencer(object):
27-
def __init__(self, infer_func, param_path, place=None):
30+
def __init__(self, infer_func, param_path, place=None, parallel=False):
2831
"""
2932
:param infer_func: a function that will return predict Variable
3033
:param param_path: the path where the inference model is saved by fluid.io.save_params
3134
:param place: place to do the inference
35+
:param parallel: use parallel_executor to run the inference, it will use multi CPU/GPU.
3236
"""
3337
self.param_path = param_path
3438
self.scope = core.Scope()
39+
self.parallel = parallel
40+
self.place = check_and_get_place(place)
3541

3642
self.inference_program = framework.Program()
3743
with framework.program_guard(self.inference_program):
3844
with unique_name.guard():
3945
self.predict_var = infer_func()
4046

41-
self.exe = executor.Executor(check_and_get_place(place))
42-
with executor.scope_guard(self.scope):
47+
with self._prog_and_scope_guard():
4348
# load params from param_path into scope
44-
io.load_params(self.exe, param_path, self.inference_program)
49+
io.load_params(executor.Executor(self.place), param_path)
50+
51+
if parallel:
52+
with self._prog_and_scope_guard():
53+
self.exe = parallel_executor.ParallelExecutor(
54+
use_cuda=isinstance(self.place, core.CUDAPlace),
55+
loss_name=self.predict_var.name)
56+
else:
57+
self.exe = executor.Executor(self.place)
4558

46-
def infer(self, inputs, return_numpy=True):
59+
def infer(self, inputs):
4760
"""
4861
:param inputs: a map of {"input_name": input_var} that will be feed into the inference program
4962
to get the predict value
50-
:param return_numpy: if return numpy value for row tensor
5163
:return: the predict value of the inference model
5264
"""
5365
if not isinstance(inputs, dict):
5466
raise ValueError(
5567
"inputs should be a map of {'input_name': input_var}")
5668

57-
with executor.scope_guard(self.scope):
58-
results = self.exe.run(self.inference_program,
59-
feed=inputs,
60-
fetch_list=[self.predict_var],
61-
return_numpy=return_numpy)
69+
with self._prog_and_scope_guard():
70+
results = self.exe.run(feed=inputs,
71+
fetch_list=[self.predict_var.name])
6272

6373
return results
74+
75+
@contextlib.contextmanager
76+
def _prog_and_scope_guard(self):
77+
with framework.program_guard(main_program=self.inference_program):
78+
with executor.scope_guard(self.scope):
79+
yield

python/paddle/fluid/tests/book/high-level-api/fit_a_line/test_fit_a_line.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def infer(use_cuda, inference_program, save_dirname=None):
9494
tensor_x = numpy.random.uniform(0, 10, [batch_size, 13]).astype("float32")
9595

9696
results = inferencer.infer({'x': tensor_x})
97-
print("infer results: ", results[0])
97+
print("infer results: ", numpy.array(results[0]))
9898

9999

100100
def main(use_cuda):

python/paddle/fluid/tests/book/high-level-api/recognize_digits/test_recognize_digits_conv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def infer(use_cuda, inference_program, save_dirname=None):
112112

113113
results = inferencer.infer({'img': tensor_img})
114114

115-
print("infer results: ", results[0])
115+
print("infer results: ", numpy.array(results[0]))
116116

117117

118118
def main(use_cuda):

python/paddle/fluid/tests/book/high-level-api/recognize_digits/test_recognize_digits_mlp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def infer(use_cuda, inference_program, save_dirname=None):
9393

9494
results = inferencer.infer({'img': tensor_img})
9595

96-
print("infer results: ", results[0])
96+
print("infer results: ", numpy.array(results[0]))
9797

9898

9999
def main(use_cuda):

python/paddle/fluid/tests/book/high-level-api/word2vec/no_test_word2vec_new_api.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,12 @@ def infer(use_cuda, inference_program, save_path):
127127
third_word = create_random_lodtensor(lod, place, low=0, high=dict_size - 1)
128128
fourth_word = create_random_lodtensor(lod, place, low=0, high=dict_size - 1)
129129

130-
result = inferencer.infer(
131-
{
132-
'firstw': first_word,
133-
'secondw': second_word,
134-
'thirdw': third_word,
135-
'forthw': fourth_word
136-
},
137-
return_numpy=False)
130+
result = inferencer.infer({
131+
'firstw': first_word,
132+
'secondw': second_word,
133+
'thirdw': third_word,
134+
'forthw': fourth_word
135+
})
138136
print(np.array(result[0]))
139137

140138

python/paddle/fluid/trainer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import contextlib
1516
import os
17+
1618
import core
17-
import framework
18-
import executor
19+
1920
import data_feeder
20-
import contextlib
21+
import executor
22+
import framework
2123
import io
22-
import unique_name
23-
import parallel_executor
24-
2524
# optimizer is same as the parameter of Trainer.__init__. Rename it to opt_module
2625
import optimizer as opt_module
26+
import parallel_executor
2727
from transpiler import distribute_transpiler
2828

2929
__all__ = [

0 commit comments

Comments
 (0)