def _find_temp_tensors(grads, forward_ops):
    """
    find all the tensors that are used for computing grads and has been
    computed during forward
    :param grads:
    :param forward_ops:
    :return:
    """
    import sys
    is_py2 = sys.version[0] == '2'
    if is_py2:
        import Queue as queue
    else:
        import queue as queue
    queue = queue.Queue()
    for grad in grads:
        queue.put(grad)
    temp_tensors = set()
    visited = set()
    while not queue.empty():
        tensor = queue.get()
        if tensor is None:
            continue
        else:
            visited.add(tensor.name)
            if tensor.op.type == 'Placeholder':
                continue
            if tensor.op.name in forward_ops:
                temp_tensors.add(tensor.name)
                continue
            for input_tensor in tensor.op.inputs:
                if input_tensor.name not in visited:
                    queue.put(input_tensor)
    return temp_tensors
