def query(self, time_indices): """Query the values at given time indices. Args: time_indices: 0-based time indices to query, as a `list` of `int`. Returns: Values as a list of `numpy.ndarray` (for time indices in memory) or `None` (for time indices discarded). """ if self._disposed: raise ValueError( 'Cannot query: this _WatchStore instance is already disposed') if not isinstance(time_indices, (tuple, list)): time_indices = [time_indices] output = [] for time_index in time_indices: if isinstance(self._data[time_index], _TensorValueDiscarded): output.append(None) else: data_item = self._data[time_index] if (hasattr(data_item, 'dtype') and tensor_helper.translate_dtype(data_item.dtype) == 'string'): _, _, data_item = tensor_helper.array_view(data_item) data_item = np.array( tensor_helper.process_buffers_for_display(data_item), dtype=np.object) output.append(data_item) return output
def testNestedArrayMixed(self): x = [[b"\x01\x02\x03", b"foo_bar"], [b"\x01", b"f"]] self.assertEqual( [ [ b"=01=02 (length-3 truncated at 2 bytes)", b"fo (length-7 truncated at 2 bytes)", ], [b"=01", b"f"], ], tensor_helper.process_buffers_for_display(x, 2), )
def _comm_tensor_data(device_name, node_name, maybe_base_expanded_node_name, output_slot, debug_op, tensor_value, wall_time): """Create a dict() as the outgoing data in the tensor data comm route. Note: The tensor data in the comm route does not include the value of the tensor in its entirety in general. Only if a tensor satisfies the following conditions will its entire value be included in the return value of this method: 1. Has a numeric data type (e.g., float32, int32) and has fewer than 5 elements. 2. Is a string tensor and has fewer than 5 elements. Each string element is up to 40 bytes. Args: device_name: Name of the device that the tensor is on. node_name: (Original) name of the node that produces the tensor. maybe_base_expanded_node_name: Possbily base-expanded node name. output_slot: Output slot number. debug_op: Name of the debug op. tensor_value: Value of the tensor, as a numpy.ndarray. wall_time: Wall timestamp for the tensor. Returns: A dict representing the tensor data. """ output_slot = int(output_slot) tf.logging.info('Recording tensor value: %s, %d, %s', node_name, output_slot, debug_op) tensor_values = None if isinstance(tensor_value, debug_data.InconvertibleTensorProto): if not tensor_value.initialized: tensor_dtype = UNINITIALIZED_TAG tensor_shape = UNINITIALIZED_TAG else: tensor_dtype = UNSUPPORTED_TAG tensor_shape = UNSUPPORTED_TAG tensor_values = NA_TAG else: tensor_dtype = tensor_helper.translate_dtype(tensor_value.dtype) tensor_shape = tensor_value.shape # The /comm endpoint should respond with tensor values only if the tensor is # small enough. Otherwise, the detailed values sould be queried through a # dedicated tensor_data that supports slicing. if tensor_helper.numel(tensor_shape) < 5: _, _, tensor_values = tensor_helper.array_view(tensor_value) if tensor_dtype == 'string' and tensor_value is not None: tensor_values = tensor_helper.process_buffers_for_display( tensor_values, limit=STRING_ELEMENT_MAX_LEN) return { 'type': 'tensor', 'timestamp': wall_time, 'data': { 'device_name': device_name, 'node_name': node_name, 'maybe_base_expanded_node_name': maybe_base_expanded_node_name, 'output_slot': output_slot, 'debug_op': debug_op, 'dtype': tensor_dtype, 'shape': tensor_shape, 'values': tensor_values, }, }
def testNestedArrayMixed(self): x = [[b'\x01\x02\x03', b'foo_bar'], [b'\x01', b'f']] self.assertEqual([[ b'=01=02 (length-3 truncated at 2 bytes)', b'fo (length-7 truncated at 2 bytes)' ], [b'=01', b'f']], tensor_helper.process_buffers_for_display(x, 2))
def testAsciiScalarAboveLimit(self): x = b'foo_bar' self.assertEqual(b'foo_ (length-7 truncated at 4 bytes)', tensor_helper.process_buffers_for_display(x, 4))
def testBinaryScalarAboveLimit(self): x = b'\x01\x02\x03' self.assertEqual( binascii.b2a_qp(x[:2]) + b' (length-3 truncated at 2 bytes)', tensor_helper.process_buffers_for_display(x, 2))
def testAsciiScalarBelowLimit(self): x = b'foo_bar' self.assertEqual(b'foo_bar', tensor_helper.process_buffers_for_display(x, 10))
def testBinaryScalarBelowLimit(self): x = b'\x01\x02\x03' self.assertEqual(binascii.b2a_qp(x), tensor_helper.process_buffers_for_display(x, 10))
def _comm_tensor_data(device_name, node_name, maybe_base_expanded_node_name, output_slot, debug_op, tensor_value, wall_time): """Create a dict() as the outgoing data in the tensor data comm route. Note: The tensor data in the comm route does not include the value of the tensor in its entirety in general. Only if a tensor satisfies the following conditions will its entire value be included in the return value of this method: 1. Has a numeric data type (e.g., float32, int32) and has fewer than 5 elements. 2. Is a string tensor and has fewer than 5 elements. Each string element is up to 40 bytes. Args: device_name: Name of the device that the tensor is on. node_name: (Original) name of the node that produces the tensor. maybe_base_expanded_node_name: Possbily base-expanded node name. output_slot: Output slot number. debug_op: Name of the debug op. tensor_value: Value of the tensor, as a numpy.ndarray. wall_time: Wall timestamp for the tensor. Returns: A dict representing the tensor data. """ output_slot = int(output_slot) tf.logging.info( 'Recording tensor value: %s, %d, %s', node_name, output_slot, debug_op) tensor_values = None if isinstance(tensor_value, debug_data.InconvertibleTensorProto): if not tensor_value.initialized: tensor_dtype = UNINITIALIZED_TAG tensor_shape = UNINITIALIZED_TAG else: tensor_dtype = UNSUPPORTED_TAG tensor_shape = UNSUPPORTED_TAG tensor_values = NA_TAG else: tensor_dtype = tensor_helper.translate_dtype(tensor_value.dtype) tensor_shape = tensor_value.shape # The /comm endpoint should respond with tensor values only if the tensor is # small enough. Otherwise, the detailed values sould be queried through a # dedicated tensor_data that supports slicing. if tensor_helper.numel(tensor_shape) < 5: _, _, tensor_values = tensor_helper.array_view(tensor_value) if tensor_dtype == 'string' and tensor_value is not None: tensor_values = tensor_helper.process_buffers_for_display( tensor_values, limit=STRING_ELEMENT_MAX_LEN) return { 'type': 'tensor', 'timestamp': wall_time, 'data': { 'device_name': device_name, 'node_name': node_name, 'maybe_base_expanded_node_name': maybe_base_expanded_node_name, 'output_slot': output_slot, 'debug_op': debug_op, 'dtype': tensor_dtype, 'shape': tensor_shape, 'values': tensor_values, }, }