def has_fetch_operators(block, fetch_targets, fetch_holder_name):
    ' Check whether the block already has fetch operators.\n\n    Return false if the block does not have any fetch operators.\n    If some fetch operators have been appended to the block, check that\n    the info contained in these fetch operators matches the fetch_targets\n    and fetch_holder_name. Raise exception when any mismatch is found.\n    Return true when the block has fetch operators with matching info.\n\n    Args:\n        block: a block instance (typically global block of a program)\n        fetch_targets: a dictionary of {fetch_target_name: fetch_target_data}\n        fetch_holder_name: the name of the variable that holds the data of\n            all fetch targets. The type of this fetch_holder variable is\n            FETCH_LIST, which is essentially vector<LoDTensor>.\n\n    Return:\n        A boolean value that indicates whether a block has fetch operators\n        that match the info contained in fetch_targets and fetch_holder_name.\n    '
    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)
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<LoDTensor>.

Return:
    A boolean value that indicates whether a block has fetch operators
    that match the info contained in fetch_targets and fetch_holder_name.
def has_fetch_operators(block, fetch_targets, fetch_holder_name):
    ' Check whether the block already has fetch operators.\n\n    Return false if the block does not have any fetch operators.\n    If some fetch operators have been appended to the block, check that\n    the info contained in these fetch operators matches the fetch_targets\n    and fetch_holder_name. Raise exception when any mismatch is found.\n    Return true when the block has fetch operators with matching info.\n\n    Args:\n        block: a block instance (typically global block of a program)\n        fetch_targets: a dictionary of {fetch_target_name: fetch_target_data}\n        fetch_holder_name: the name of the variable that holds the data of\n            all fetch targets. The type of this fetch_holder variable is\n            FETCH_LIST, which is essentially vector<LoDTensor>.\n\n    Return:\n        A boolean value that indicates whether a block has fetch operators\n        that match the info contained in fetch_targets and fetch_holder_name.\n    '
    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)