1# Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Graph-only versions of a few op functions, for internal use only.""" 16 17# Must be separate from array_ops to avoid a cyclic dependency. 18 19from tensorflow.core.framework import attr_value_pb2 20from tensorflow.python.framework import op_callbacks 21from tensorflow.python.framework import ops 22from tensorflow.python.framework import tensor_shape 23 24 25def graph_placeholder(dtype, shape, name=None): 26 """Graph-only version of tf.compat.v1.placeholder(), for internal use only.""" 27 dtype = dtype.base_dtype 28 dtype_value = attr_value_pb2.AttrValue(type=dtype.as_datatype_enum) 29 if isinstance(shape, (list, tuple)): 30 shape = tensor_shape.TensorShape(shape) 31 shape = attr_value_pb2.AttrValue(shape=shape.as_proto()) 32 g = ops.get_default_graph() 33 attrs = {"dtype": dtype_value, "shape": shape} 34 op = g._create_op_internal( # pylint: disable=protected-access 35 "Placeholder", [], [dtype], input_types=[], 36 attrs=attrs, name=name) 37 result, = op.outputs 38 if op_callbacks.should_invoke_op_callbacks(): 39 # TODO(b/147670703): Once the special-op creation code paths 40 # are unified. Remove this `if` block. 41 callback_outputs = op_callbacks.invoke_op_callbacks( 42 "Placeholder", tuple(), attrs, tuple(op.outputs), 43 op_name=name, graph=g) 44 if callback_outputs is not None: 45 result, = callback_outputs 46 return result 47