Copies HandleData for variant and resource type tensors if available. The CppShapeInferenceResult::HandleData proto contains information about the shapes and types of the element tensors of resource/variant type tensors. We need to copy this across function boundaries, i.e., when capturing a
(source_t, target_t)
| 42 | |
| 43 | |
| 44 | def copy_handle_data(source_t, target_t): |
| 45 | """Copies HandleData for variant and resource type tensors if available. |
| 46 | |
| 47 | The CppShapeInferenceResult::HandleData proto contains information about the |
| 48 | shapes and types of the element tensors of resource/variant type tensors. |
| 49 | We need to copy this across function boundaries, i.e., when capturing a |
| 50 | placeholder or when returning a function tensor as output. If we don't do this |
| 51 | the element tensors will have unknown shapes, e.g., if a TensorList variant |
| 52 | tensor is captured as a placeholder, elements popped from that list would have |
| 53 | unknown shape. |
| 54 | |
| 55 | Args: |
| 56 | source_t: The tensor to copy HandleData from. |
| 57 | target_t: The tensor to copy HandleData to. |
| 58 | """ |
| 59 | if (target_t.dtype == dtypes.resource or |
| 60 | target_t.dtype == dtypes.variant): |
| 61 | if isinstance(source_t, ops.EagerTensor): |
| 62 | handle_data = source_t._handle_data # pylint: disable=protected-access |
| 63 | else: |
| 64 | handle_data = resource_variable_ops.get_resource_handle_data(source_t) |
| 65 | if (handle_data is not None |
| 66 | and handle_data.is_set |
| 67 | and handle_data.shape_and_type): |
| 68 | # pylint: disable=protected-access |
| 69 | pywrap_tensorflow.SetHandleShapeAndType(target_t.graph._c_graph, |
| 70 | target_t._as_tf_output(), |
| 71 | handle_data.SerializeToString()) |
| 72 | # pylint: enable=protected-access |
| 73 | # Ensure that shapes and dtypes are propagated. |
| 74 | shapes, types = zip(*[(pair.shape, pair.dtype) |
| 75 | for pair in handle_data.shape_and_type]) |
| 76 | ranks = [len(s.dim) if not s.unknown_rank else -1 for s in shapes] |
| 77 | shapes = [[d.size for d in s.dim] # pylint: disable=g-complex-comprehension |
| 78 | if not s.unknown_rank else None for s in shapes] |
| 79 | pywrap_tensorflow.TF_GraphSetOutputHandleShapesAndTypes_wrapper( |
| 80 | target_t._op._graph._c_graph, # pylint: disable=protected-access |
| 81 | target_t._as_tf_output(), # pylint: disable=protected-access |
| 82 | shapes, ranks, types) |
| 83 | |
| 84 | |
| 85 | @tf_export("custom_gradient") |
no test coverage detected