# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # 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. from __future__ import print_function from . import core from . import framework from . import executor from . import compiler import sys from .framework import default_main_program, Variable __all__ = ['add_feed_fetch_op'] def _has_feed_operators(block, feed_targets, feed_holder_name): """ Check whether the block already has feed operators. Return false if the block does not have any feed operators. If some feed operators have been prepended to the block, check that the info contained in these feed operators matches the feed_targets and feed_holder_name. Raise exception when any mismatch is found. Return true when the block has feed operators with matching info. Args: block: a block instance (typically global block of a program) feed_targets: a dictionary of {feed_target_name: feed_target_data} feed_holder_name: the name of the variable that holds the data of all feed targets. The type of this feed_holder variable is FEED_MINIBATCH, which is essentially vector. Returns: A boolean value that indicates whether a block has feed operators that match the info contained in feed_targets and feed_holder_name. """ feed_count = 0 for op in block.ops: if op.desc.type() == 'feed': feed_count += 1 assert op.desc.input('X')[0] == feed_holder_name feed_target_name = op.desc.output('Out')[0] if feed_target_name not in feed_targets: raise Exception("'feed_targets' does not have {} variable". format(feed_target_name)) else: break if feed_count > 0 and feed_count != len(feed_targets): raise Exception( "Feed operators in program desc do not match 'feed_targets'") return feed_count > 0 def _has_fetch_operators(block, fetch_targets, fetch_holder_name): """ Check whether the block already has fetch operators. Return false if the block does not have any fetch operators. If some fetch operators have been appended to the block, check that the info contained in these fetch operators matches the fetch_targets and fetch_holder_name. Raise exception when any mismatch is found. Return true when the block has fetch operators with matching info. Args: block: a block instance (typically global block of a program) fetch_targets: a dictionary of {fetch_target_name: fetch_target_data} fetch_holder_name: the name of the variable that holds the data of all fetch targets. The type of this fetch_holder variable is FETCH_LIST, which is essentially vector. Return: A boolean value that indicates whether a block has fetch operators that match the info contained in fetch_targets and fetch_holder_name. """ fetch_count = 0 for op in block.ops: if op.desc.type() == 'fetch': fetch_count += 1 assert op.desc.output('Out')[0] == fetch_holder_name fetch_target_name = op.desc.input('X')[0] if fetch_target_name not in [ var.desc.name() for var in fetch_targets ]: raise Exception("'fetch_targets' does not have {} variable". format(fetch_target_name)) idx = op.desc.attr('col') assert fetch_target_name == fetch_targets[idx].desc.name() if fetch_count > 0 and fetch_count != len(fetch_targets): raise Exception( "Fetch operators in program desc do not match 'fetch_targets'") return fetch_count > 0 def _add_feed_fetch_ops(program, feed, fetch_list, feed_var_name='feed', fetch_var_name='fetch'): tmp_program = program.clone() global_block = tmp_program.global_block() if feed_var_name in global_block.vars: feed_var = global_block.var(feed_var_name) else: feed_var = global_block.create_var( name=feed_var_name, type=core.VarDesc.VarType.FEED_MINIBATCH, persistable=True) if fetch_var_name in global_block.vars: fetch_var = global_block.var(fetch_var_name) else: fetch_var = global_block.create_var( name=fetch_var_name, type=core.VarDesc.VarType.FETCH_LIST, persistable=True) # prepend feed operators if not _has_feed_operators(global_block, feed, feed_var_name): for i, name in enumerate(feed): out = global_block.var(name) global_block._prepend_op( type='feed', inputs={'X': [feed_var]}, outputs={'Out': [out]}, attrs={'col': i}) # append fetch_operators if not _has_fetch_operators(global_block, fetch_list, fetch_var_name): for i, var in enumerate(fetch_list): assert isinstance(var, Variable) or isinstance( var, six.string_types), ("Wrong type for fetch_list[%s]: %s" % (i, type(var))) global_block.append_op( type='fetch', inputs={'X': [var]}, outputs={'Out': [fetch_var]}, attrs={'col': i}) return tmp_program def add_feed_fetch_op(program, feed, fetch_list, scope, place): if program is None: program = default_main_program() program = _add_feed_fetch_ops( program=program, feed=feed, fetch_list=fetch_list) return program