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 __future__ import absolute_import 20from __future__ import division 21from __future__ import print_function 22 23from tensorflow.core.framework import attr_value_pb2 24from tensorflow.python.framework import op_callbacks 25from tensorflow.python.framework import ops 26from tensorflow.python.framework import tensor_shape 27 28 29def graph_placeholder(dtype, shape, name=None): 30 """Graph-only version of tf.compat.v1.placeholder(), for internal use only.""" 31 dtype = dtype.base_dtype 32 dtype_value = attr_value_pb2.AttrValue(type=dtype.as_datatype_enum) 33 if isinstance(shape, (list, tuple)): 34 shape = tensor_shape.TensorShape(shape) 35 shape = attr_value_pb2.AttrValue(shape=shape.as_proto()) 36 g = ops.get_default_graph() 37 attrs = {"dtype": dtype_value, "shape": shape} 38 op = g._create_op_internal( # pylint: disable=protected-access 39 "Placeholder", [], [dtype], input_types=[], 40 attrs=attrs, name=name) 41 result, = op.outputs 42 if op_callbacks.should_invoke_op_callbacks(): 43 # TODO(b/147670703): Once the special-op creation code paths 44 # are unified. Remove this `if` block. 45 callback_outputs = op_callbacks.invoke_op_callbacks( 46 "Placeholder", tuple(), attrs, tuple(op.outputs), 47 op_name=name, graph=g) 48 if callback_outputs is not None: 49 result, = callback_outputs 50 return result 51