executor.py 2.1 KB
Newer Older
W
whs 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15 16
from ....compiler import CompiledProgram
from ....data_feeder import DataFeeder
W
whs 已提交
17
from .... import executor
18
from .graph_wrapper import GraphWrapper
W
whs 已提交
19

20
__all__ = ['SlimGraphExecutor']
W
whs 已提交
21 22


23 24 25 26
class SlimGraphExecutor(object):
    """
    Wrapper of executor used to run GraphWrapper.
    """
W
whs 已提交
27 28 29

    def __init__(self, place):
        self.exe = executor.Executor(place)
30
        self.place = place
W
whs 已提交
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    def run(self, graph, scope, data=None):
        """
        Runing a graph with a batch of data.
        Args:
            graph(GraphWrapper): The graph to be executed.
            scope(fluid.core.Scope): The scope to be used.
            data(list<tuple>): A batch of data. Each tuple in this list is a sample.
                               It will feed the items of tuple to the in_nodes of graph.
        Returns:
            results(list): A list of result with the same order indicated by graph.out_nodes.
        """
        assert isinstance(graph, GraphWrapper)
        if data is not None:
            feeder = DataFeeder(
                feed_list=graph.in_nodes.values(),
                place=self.place,
                program=graph.program)
            feed = feeder.feed(data)

        fetch_list = graph.out_nodes.values()
        program = graph.compiled_graph if graph.compiled_graph else graph.program
        results = self.exe.run(program,
W
whs 已提交
54 55 56 57
                               scope=scope,
                               fetch_list=fetch_list,
                               feed=feed)
        return results