def testPrintOneTensorEagerOnOpCreate(self): with self.cached_session(): with context.eager_mode(): tensor = math_ops.range(10) expected = "[0 1 2 ... 7 8 9]" with self.captureWritesToStream(sys.stderr) as printed: logging_ops.print_v2(tensor) self.assertTrue((expected + "\n") in printed.contents())
def testInvalidOutputStreamRaisesError(self): with self.cached_session(): tensor = math_ops.range(10) with self.assertRaises(ValueError): print_op = logging_ops.print_v2( tensor, output_stream="unknown") self.evaluate(print_op)
def testPrintNoTensors(self): with self.cached_session(): with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(23, [23, 5], {"6": 12}) self.evaluate(print_op) expected = "23 [23, 5] {'6': 12}" self.assertTrue((expected + "\n") in printed.contents())
def testPrintStringScalarDifferentEnd(self): with self.cached_session(): tensor = ops.convert_to_tensor("scalar") with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(tensor, end="<customend>") self.evaluate(print_op) expected = "scalar<customend>" self.assertIn(expected, printed.contents())
def testPrintTwoTensorsDifferentSep(self): with self.cached_session(): tensor = math_ops.range(10) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(tensor, tensor * 10, sep="<separator>") self.evaluate(print_op) expected = "[0 1 2 ... 7 8 9]<separator>[0 10 20 ... 70 80 90]" self.assertIn(expected + "\n", printed.contents())
def testPrintTwoTensors(self): with self.cached_session(): tensor = math_ops.range(10) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(tensor, tensor * 10) self.evaluate(print_op) expected = "[0 1 2 ... 7 8 9] [0 10 20 ... 70 80 90]" self.assertTrue((expected + "\n") in printed.contents())
def testPrintStringScalar(self): with self.cached_session(): tensor = ops.convert_to_tensor("scalar") with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(tensor) self.evaluate(print_op) expected = "scalar" self.assertTrue((expected + "\n") in printed.contents())
def testPrintPlaceholderGeneration(self): with self.cached_session(): tensor = math_ops.range(10) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2("{}6", {"{}": tensor * 10}) self.evaluate(print_op) expected = "{}6 {'{}': [0 10 20 ... 70 80 90]}" self.assertTrue((expected + "\n") in printed.contents())
def testNoDuplicateFormatOpGraphModeAfterExplicitFormat(self): tensor = math_ops.range(10) formatted_string = string_ops.string_format("{}", tensor) print_op = logging_ops.print_v2(formatted_string) self.evaluate(print_op) graph_ops = ops.get_default_graph().get_operations() format_ops = [op for op in graph_ops if op.type == "StringFormat"] # Should be only 1 format_op for graph mode. self.assertEqual(len(format_ops), 1)
def testPrintOneStringTensor(self): tensor = ops.convert_to_tensor( [char for char in string.ascii_lowercase]) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(tensor) self.evaluate(print_op) expected = "[\"a\" \"b\" \"c\" ... \"x\" \"y\" \"z\"]" self.assertIn((expected + "\n"), printed.contents())
def testPrintOneStringTensor(self): with self.cached_session(): tensor = ops.convert_to_tensor([char for char in string.ascii_lowercase]) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(tensor) self.evaluate(print_op) expected = "[\"a\" \"b\" \"c\" ... \"x\" \"y\" \"z\"]" self.assertIn((expected + "\n"), printed.contents())
def testPrintOneTensorLogError(self): with self.test_session(): tensor = math_ops.range(10) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2( tensor, output_stream=tf_logging.error) self.evaluate(print_op) expected = "[0 1 2 ... 7 8 9]" self.assertTrue(expected in printed.contents())
def testPrintTwoTensorsDifferentSep(self): tensor = math_ops.range(10) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(tensor, tensor * 10, sep="<separator>") self.evaluate(print_op) expected = "[0 1 2 ... 7 8 9]<separator>[0 10 20 ... 70 80 90]" self.assertIn(expected + "\n", printed.contents())
def testPrintOneTensorStdout(self): with self.cached_session(): tensor = math_ops.range(10) with self.captureWritesToStream(sys.stdout) as printed: print_op = logging_ops.print_v2( tensor, output_stream=sys.stdout) self.evaluate(print_op) expected = "[0 1 2 ... 7 8 9]" self.assertTrue((expected + "\n") in printed.contents())
def testPrintOneVariable(self): var = variables.Variable(math_ops.range(10)) if not context.executing_eagerly(): self.evaluate(variables.global_variables_initializer()) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(var) self.evaluate(print_op) expected = "[0 1 2 ... 7 8 9]" self.assertIn((expected + "\n"), printed.contents())
def get_ix_slices_values_without_nan(): """ Gets the indexed slice values without NaN """ ix_slice_values_without_nans = tf.where(gen_math_ops.is_finite(input_tensor.values), input_tensor.values, gen_array_ops.zeros_like(input_tensor.values)) print_op = logging_ops.print_v2('WARNING - Tensor %s has NaN or Inf values. %s' % (input_tensor.name, name or '')) with ops.control_dependencies([ix_slice_values_without_nans, print_op]): return array_ops.identity(ix_slice_values_without_nans)
def testPrintOneTensorStdout(self): with self.cached_session(): tensor = math_ops.range(10) with self.captureWritesToStream(sys.stdout) as printed: print_op = logging_ops.print_v2(tensor, output_stream=sys.stdout) self.evaluate(print_op) expected = "[0 1 2 ... 7 8 9]" self.assertTrue((expected + "\n") in printed.contents())
def testNoDuplicateFormatOpGraphModeAfterExplicitFormat(self): with self.cached_session(): tensor = math_ops.range(10) formatted_string = string_ops.string_format("{}", tensor) print_op = logging_ops.print_v2(formatted_string) self.evaluate(print_op) graph_ops = ops.get_default_graph().get_operations() format_ops = [op for op in graph_ops if op.type == "StringFormat"] # Should be only 1 format_op for graph mode. self.assertEqual(len(format_ops), 1)
def testPrintFloatScalar(self): for dtype in [ dtypes.bfloat16, dtypes.half, dtypes.float32, dtypes.float64 ]: tensor = ops.convert_to_tensor(43.5, dtype=dtype) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(tensor) self.evaluate(print_op) expected = "43.5" self.assertIn((expected + "\n"), printed.contents())
def testPrintOneVariable(self): with self.cached_session(): var = variables.Variable(math_ops.range(10)) if not context.executing_eagerly(): variables.global_variables_initializer().run() with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(var) self.evaluate(print_op) expected = "[0 1 2 ... 7 8 9]" self.assertTrue((expected + "\n") in printed.contents())
def testPrintOneTensorLogError(self): with self.cached_session(): tensor = math_ops.range(10) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2( tensor, output_stream=tf_logging.error) self.evaluate(print_op) self.assertTrue("E" in printed.contents()) expected = "[0 1 2 ... 7 8 9]" self.assertTrue(expected in printed.contents())
def testRaggedPrint(self, inputs, expected, summarize=None): if callable(inputs): inputs = inputs() with tempfile.TemporaryDirectory() as tmpdirname: path = os.path.join(tmpdirname, 'print_output') kwargs = {'output_stream': 'file://{}'.format(path)} if summarize is not None: kwargs.update(summarize=summarize) self.evaluate(logging_ops.print_v2(*inputs, **kwargs)) actual = open(path, 'r').read() self.assertEqual(repr(actual), repr(expected))
def testPrintTwoVariablesInStructWithAssignAdd(self): var_one = variables.Variable(2.14) plus_one = var_one.assign_add(1.0) var_two = variables.Variable(math_ops.range(10)) if not context.executing_eagerly(): self.evaluate(variables.global_variables_initializer()) with self.captureWritesToStream(sys.stderr) as printed: self.evaluate(plus_one) print_op = logging_ops.print_v2(var_one, {"second": var_two}) self.evaluate(print_op) expected = "3.14 {'second': [0 1 2 ... 7 8 9]}" self.assertIn((expected + "\n"), printed.contents())
def testPrintTensorsToFile(self): tmpfile_name = tempfile.mktemp(".printv2_test") tensor_0 = math_ops.range(0, 10) print_op_0 = logging_ops.print_v2(tensor_0, output_stream="file://"+tmpfile_name) self.evaluate(print_op_0) tensor_1 = math_ops.range(11, 20) print_op_1 = logging_ops.print_v2(tensor_1, output_stream="file://"+tmpfile_name) self.evaluate(print_op_1) try: f = open(tmpfile_name, "r") line_0 = f.readline() expected_0 = "[0 1 2 ... 7 8 9]" self.assertTrue(expected_0 in line_0) line_1 = f.readline() expected_1 = "[11 12 13 ... 17 18 19]" self.assertTrue(expected_1 in line_1) f.close() os.remove(tmpfile_name) except IOError as e: self.fail(e)
def testPrintTwoVariablesInStructWithAssignAdd(self): with self.cached_session(): var_one = variables.Variable(2.14) plus_one = var_one.assign_add(1.0) var_two = variables.Variable(math_ops.range(10)) if not context.executing_eagerly(): variables.global_variables_initializer().run() with self.captureWritesToStream(sys.stderr) as printed: self.evaluate(plus_one) print_op = logging_ops.print_v2(var_one, {"second": var_two}) self.evaluate(print_op) expected = "3.14 {'second': [0 1 2 ... 7 8 9]}" self.assertTrue((expected + "\n") in printed.contents())
def testPrintOneTensorVarySummarize(self): with self.cached_session(): tensor = math_ops.range(10) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(tensor, summarize=1) self.evaluate(print_op) expected = "[0 ... 9]" self.assertTrue((expected + "\n") in printed.contents()) with self.cached_session(): tensor = math_ops.range(10) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(tensor, summarize=2) self.evaluate(print_op) expected = "[0 1 ... 8 9]" self.assertTrue((expected + "\n") in printed.contents()) with self.cached_session(): tensor = math_ops.range(10) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(tensor, summarize=3) self.evaluate(print_op) expected = "[0 1 2 ... 7 8 9]" self.assertTrue((expected + "\n") in printed.contents()) with self.cached_session(): tensor = math_ops.range(10) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(tensor, summarize=-1) self.evaluate(print_op) expected = "[0 1 2 3 4 5 6 7 8 9]" self.assertTrue((expected + "\n") in printed.contents())
def testPrintComplexTensorStruct(self): tensor = math_ops.range(10) small_tensor = constant_op.constant([0.3, 12.4, -16.1]) big_tensor = math_ops.mul(tensor, 10) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2("first:", tensor, "middle:", { "small": small_tensor, "Big": big_tensor }, 10, [tensor * 2, tensor]) self.evaluate(print_op) # Note that the keys in the dict will always be sorted, # so 'Big' comes before 'small' expected = ("first: [0 1 2 ... 7 8 9] " "middle: {'Big': [0 10 20 ... 70 80 90], " "'small': [0.3 12.4 -16.1]} " "10 [[0 2 4 ... 14 16 18], [0 1 2 ... 7 8 9]]") self.assertIn((expected + "\n"), printed.contents())
def testPrintComplexTensorStruct(self): with self.cached_session(): tensor = math_ops.range(10) small_tensor = constant_op.constant([0.3, 12.4, -16.1]) big_tensor = math_ops.mul(tensor, 10) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2( "first:", tensor, "middle:", {"small": small_tensor, "Big": big_tensor}, 10, [tensor * 2, tensor]) self.evaluate(print_op) # Note that the keys in the dict will always be sorted, # so 'Big' comes before 'small' expected = ("first: [0 1 2 ... 7 8 9] " "middle: {'Big': [0 10 20 ... 70 80 90], " "'small': [0.3 12.4 -16.1]} " "10 [[0 2 4 ... 14 16 18], [0 1 2 ... 7 8 9]]") self.assertTrue((expected + "\n") in printed.contents())
def _print_tensor(tensor_name, num_elements, tensor, output_tensor): """Prints a tensor value to a file. Args: tensor_name: name of the tensor being traced. num_elements: number of elements to print (-1 means print all). tensor: the tensor needs to be returned. output_tensor: the tensor needs to be printed. Returns: The same tensor passed via the "tensor" argument. Raises: ValueError: If tensor_name is not already in self._tensorname_idx_map. """ if self._submode == _SUBMODE_BRIEF: if tensor_name not in self._tensorname_idx_map: raise ValueError( 'Tensor name %s is not in the tensorname_idx_map' % tensor_name) msg = '%d' % self._tensorname_idx_map[tensor_name] else: msg = '"%s"' % tensor_name if self._trace_file_path: output_stream = _OUTPUT_STREAM_ESCAPE + self._trace_file_path else: output_stream = sys.stderr print_op = logging_ops.print_v2(msg, array_ops.shape(output_tensor), '@', self._replica_id, '\n', output_tensor, '\n', summarize=num_elements, output_stream=output_stream) with ops.control_dependencies([print_op]): return array_ops.identity(tensor).op
def testPrintSparseTensorInDataStruct(self): ind = [[0, 0], [1, 0], [1, 3], [4, 1], [1, 4], [3, 2], [3, 3]] val = [0, 10, 13, 4, 14, 32, 33] shape = [5, 6] sparse = sparse_tensor.SparseTensor( constant_op.constant(ind, dtypes.int64), constant_op.constant(val, dtypes.int64), constant_op.constant(shape, dtypes.int64)) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2([sparse]) self.evaluate(print_op) expected = ("['SparseTensor(indices=[[0 0]\n" " [1 0]\n" " [1 3]\n" " ...\n" " [1 4]\n" " [3 2]\n" " [3 3]], values=[0 10 13 ... 14 32 33], shape=[5 6])']") self.assertIn((expected + "\n"), printed.contents())
def testPrintSparseTensorInDataStruct(self): with self.cached_session(): ind = [[0, 0], [1, 0], [1, 3], [4, 1], [1, 4], [3, 2], [3, 3]] val = [0, 10, 13, 4, 14, 32, 33] shape = [5, 6] sparse = sparse_tensor.SparseTensor( constant_op.constant(ind, dtypes.int64), constant_op.constant(val, dtypes.int64), constant_op.constant(shape, dtypes.int64)) with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2([sparse]) self.evaluate(print_op) expected = ("['SparseTensor(indices=[[0 0]\n" " [1 0]\n" " [1 3]\n" " ...\n" " [1 4]\n" " [3 2]\n" " [3 3]], values=[0 10 13 ... 14 32 33], shape=[5 6])']") self.assertTrue((expected + "\n") in printed.contents())
def _print_tensor(op_name, output_idx, num_elements, tensor, output_tensor): """Prints a tensor value to a file. Args: op_name: the name of the Op that outputs the tensor to be printed. output_idx: which output of the Op it is (0 means the first output). num_elements: number of elements to print. tensor: the tensor needs to be returned. output_tensor: the tensor needs to be printed. Returns: The same tensor passed via the "tensor" argument. """ msg = '"%s:%d" '%(op_name, output_idx) output_stream = _OUTPUT_STREAM_ESCAPE + self._trace_file_path print_op = logging_ops.print_v2(msg, array_ops.shape(output_tensor), ' @', self._replica_id, '\n', output_tensor, summarize=num_elements, output_stream=output_stream) with ops.control_dependencies([print_op]): return array_ops.identity(tensor).op
def _print_tensor(tensor_name, num_elements, tensor, output_tensor): """Prints a tensor value to a file. Args: tensor_name: name of the tensor being traced. num_elements: number of elements to print (-1 means print all). tensor: the tensor needs to be returned. output_tensor: the tensor needs to be printed. Returns: The same tensor passed via the "tensor" argument. Raises: ValueError: If tensor_name is not already in self._tensorname_idx_map. """ if self._submode == _SUBMODE_BRIEF: if tensor_name not in self._tensorname_idx_map: raise ValueError( 'Tensor name %s is not in the tensorname_idx_map'%tensor_name) msg = '%d'%self._tensorname_idx_map[tensor_name] else: msg = '"%s"'%tensor_name if self._trace_file_path: output_stream = _OUTPUT_STREAM_ESCAPE + self._trace_file_path else: output_stream = sys.stderr print_op = logging_ops.print_v2(msg, array_ops.shape(output_tensor), '@', self._replica_id, '\n', output_tensor, '\n', summarize=num_elements, output_stream=output_stream) with ops.control_dependencies([print_op]): return array_ops.identity(tensor).op
def outside_fn(): logging_ops.print_v2("Outside compiled")
def outside_fn(x): logging_ops.print_v2("Outside compiled", x) return x + 6.0
def prints(): logging_ops.print_v2("A") logging_ops.print_v2("B") logging_ops.print_v2("C")
def testPrintOpName(self): with self.cached_session(): tensor = math_ops.range(10) print_op = logging_ops.print_v2(tensor, name="print_name") self.assertEqual(print_op.name, "print_name")
def f(): tensor = math_ops.range(10) logging_ops.print_v2(tensor) return tensor
def testPrintOpName(self): tensor = math_ops.range(10) print_op = logging_ops.print_v2(tensor, name="print_name") self.assertEqual(print_op.name, "print_name")
def testInvalidOutputStreamRaisesError(self): tensor = math_ops.range(10) with self.assertRaises(ValueError): print_op = logging_ops.print_v2(tensor, output_stream="unknown") self.evaluate(print_op)
def train_step(x): x2 = x + 5.0 logging_ops.print_v2(x2) x2 = tpu.outside_compilation(host_computation, x2) return x2 + 4.0
def fn_with_side_effect(arg): logging_ops.print_v2(arg) return arg
def logging(): logging_ops.print_v2('some message')
def testPrintNoTensors(self): with self.captureWritesToStream(sys.stderr) as printed: print_op = logging_ops.print_v2(23, [23, 5], {"6": 12}) self.evaluate(print_op) expected = "23 [23, 5] {'6': 12}" self.assertIn((expected + "\n"), printed.contents())