def testLocateTensorElementAnnotationsUnavailable(self): out = tensor_format.format_tensor(None, "a") self.assertEqual(["Tensor \"a\":", "", "Uninitialized tensor"], out.lines) with self.assertRaisesRegexp( AttributeError, "tensor_metadata is not available in annotations"): tensor_format.locate_tensor_element(out, [0])
def testLocateTensorElement1DNoEllipsisBatchMode(self): a = np.zeros(20) out = tensor_format.format_tensor(a, "a", np_printoptions={"linewidth": 40}) self.assertEqual([ "Tensor \"a\":", "", "array([ 0., 0., 0., 0., 0., 0.,", " 0., 0., 0., 0., 0., 0.,", " 0., 0., 0., 0., 0., 0.,", " 0., 0.])", ], out.lines) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0]]) self.assertEqual([False], are_omitted) self.assertEqual([2], rows) self.assertEqual([8], start_cols) self.assertEqual([10], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0], [5]]) self.assertEqual([False, False], are_omitted) self.assertEqual([2, 2], rows) self.assertEqual([8, 33], start_cols) self.assertEqual([10, 35], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0], [6]]) self.assertEqual([False, False], are_omitted) self.assertEqual([2, 3], rows) self.assertEqual([8, 8], start_cols) self.assertEqual([10, 10], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0], [5], [6]]) self.assertEqual([False, False, False], are_omitted) self.assertEqual([2, 2, 3], rows) self.assertEqual([8, 33, 8], start_cols) self.assertEqual([10, 35, 10], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element( out, [[0], [5], [6], [19]]) self.assertEqual([False, False, False, False], are_omitted) self.assertEqual([2, 2, 3, 5], rows) self.assertEqual([8, 33, 8, 13], start_cols) self.assertEqual([10, 35, 10, 15], end_cols)
def testLocateTensorElementAnnotationsUnavailable(self): tensor_proto = tensor_pb2.TensorProto( dtype=types_pb2.DataType.Value("DT_FLOAT"), tensor_shape=tensor_shape_pb2.TensorShapeProto( dim=[tensor_shape_pb2.TensorShapeProto.Dim(size=1)])) out = tensor_format.format_tensor( debug_data.InconvertibleTensorProto(tensor_proto, False), "a") self.assertEqual(["Tensor \"a\":", "", "Uninitialized tensor:"], out.lines[:3]) with self.assertRaisesRegexp( AttributeError, "tensor_metadata is not available in annotations"): tensor_format.locate_tensor_element(out, [0])
def testBatchModeWithErrors(self): a = np.zeros(20) out = tensor_format.format_tensor( a, "a", np_printoptions={"linewidth": 40}) self.assertEqual([ "Tensor \"a\":", "", "array([ 0., 0., 0., 0., 0., 0.,", " 0., 0., 0., 0., 0., 0.,", " 0., 0., 0., 0., 0., 0.,", " 0., 0.])", ], out.lines) with self.assertRaisesRegexp(ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [[0, 0], [0]]) with self.assertRaisesRegexp(ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [[0], [20]]) with self.assertRaisesRegexp(ValueError, r"Indices contain negative value\(s\)"): tensor_format.locate_tensor_element(out, [[0], [-1]]) with self.assertRaisesRegexp( ValueError, "Input indices sets are not in ascending order"): tensor_format.locate_tensor_element(out, [[5], [0]])
def testBatchModeWithErrors(self): a = np.zeros(20) out = tensor_format.format_tensor(a, "a", np_printoptions={"linewidth": 40}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) with self.assertRaisesRegex(ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [[0, 0], [0]]) with self.assertRaisesRegex(ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [[0], [20]]) with self.assertRaisesRegex(ValueError, r"Indices contain negative value\(s\)"): tensor_format.locate_tensor_element(out, [[0], [-1]]) with self.assertRaisesRegex( ValueError, "Input indices sets are not in ascending order"): tensor_format.locate_tensor_element(out, [[5], [0]])
def testLocateTensorElement3DWithEllipsesBatchMode(self): a = (np.arange(11 * 11 * 11) + 1000).reshape([11, 11, 11]).astype(np.int32) out = tensor_format.format_tensor(a, "a", False, np_printoptions={ "threshold": 100, "edgeitems": 2 }) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) actual_row_0_0_0, actual_col_0_0_0 = self._findFirst(out.lines, "1000") actual_row_0_0_10, _ = self._findFirst(out.lines, "1010") actual_row_10_10_10, _ = self._findFirst(out.lines, "2330") (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0, 0, 0]]) self.assertEqual([False], are_omitted) self.assertEqual([actual_row_0_0_0], rows) self.assertEqual([actual_col_0_0_0], start_cols) self.assertEqual([actual_col_0_0_0 + 4], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element( out, [[0, 0, 0], [0, 0, 10]]) self.assertEqual([False, False], are_omitted) self.assertEqual([actual_row_0_0_0, actual_row_0_0_10], rows) self.assertEqual([actual_col_0_0_0, None], start_cols) self.assertEqual([actual_col_0_0_0 + 4, None], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element( out, [[0, 0, 0], [0, 2, 0]]) self.assertEqual([False, True], are_omitted) self.assertEqual([2, 4], rows) self.assertEqual(2, len(start_cols)) self.assertEqual(2, len(end_cols)) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element( out, [[0, 0, 0], [10, 10, 10]]) self.assertEqual([False, False], are_omitted) self.assertEqual([actual_row_0_0_0, actual_row_10_10_10], rows) self.assertEqual([actual_col_0_0_0, None], start_cols) self.assertEqual([actual_col_0_0_0 + 4, None], end_cols)
def testLocateTensorElement1DNoEllipsisBatchMode(self): a = np.zeros(20) out = tensor_format.format_tensor( a, "a", np_printoptions={"linewidth": 40}) self.assertEqual([ "Tensor \"a\":", "", "array([ 0., 0., 0., 0., 0., 0.,", " 0., 0., 0., 0., 0., 0.,", " 0., 0., 0., 0., 0., 0.,", " 0., 0.])", ], out.lines) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0]]) self.assertEqual([False], are_omitted) self.assertEqual([2], rows) self.assertEqual([8], start_cols) self.assertEqual([10], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0], [5]]) self.assertEqual([False, False], are_omitted) self.assertEqual([2, 2], rows) self.assertEqual([8, 33], start_cols) self.assertEqual([10, 35], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0], [6]]) self.assertEqual([False, False], are_omitted) self.assertEqual([2, 3], rows) self.assertEqual([8, 8], start_cols) self.assertEqual([10, 10], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0], [5], [6]]) self.assertEqual([False, False, False], are_omitted) self.assertEqual([2, 2, 3], rows) self.assertEqual([8, 33, 8], start_cols) self.assertEqual([10, 35, 10], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0], [5], [6], [19]]) self.assertEqual([False, False, False, False], are_omitted) self.assertEqual([2, 2, 3, 5], rows) self.assertEqual([8, 33, 8, 13], start_cols) self.assertEqual([10, 35, 10, 15], end_cols)
def _checkTensorElementLocations(self, out, a): """Check the results of locate_tensor_element on an ndarray representation. that represents a numpy.ndarray. Args: out: An instance of RichTextLines representing a numpy.ndarray. a: The numpy.ndarray being represented. Raises: ValueError: if any ellipses ("...") are found in the lines representing the array. """ # First, locate the beginning of the tensor value section. begin_line_num = 0 while not out.lines[begin_line_num].startswith("array"): begin_line_num += 1 # Second, find all matches to tensor-value regex. element_index = 0 for line_num in range(begin_line_num, len(out.lines)): line = out.lines[line_num] if "..." in line: raise ValueError( "Unexpected found ellipses in line representing array") matches = re.finditer(self._ELEMENT_REGEX, line) for match in matches: subscripts = list(np.unravel_index(element_index, a.shape)) is_omitted, row, start_col, end_col = ( tensor_format.locate_tensor_element(out, subscripts)) self.assertFalse(is_omitted) self.assertEqual(line_num, row) self.assertEqual(match.start(), start_col) self.assertEqual(match.end(), end_col) element_index += 1 self.assertEqual(element_index, np.size(a))
def _checkTensorElementLocations(self, out, a): """Check the results of locate_tensor_element on an ndarray representation. that represents a numpy.ndaray. Args: out: An instance of RichTextLines representing a numpy.ndarray. a: The numpy.ndarray being represented. Raises: ValueError: if any ellipses ("...") are found in the lines representing the array. """ # First, locate the beginning of the tensor value section. begin_line_num = 0 while not out.lines[begin_line_num].startswith("array"): begin_line_num += 1 # Second, find all matches to tensor-value regex. element_index = 0 for line_num in range(begin_line_num, len(out.lines)): line = out.lines[line_num] if "..." in line: raise ValueError("Unexpected found ellipses in line representing array") matches = re.finditer(self._ELEMENT_REGEX, line) for match in matches: subscripts = list(np.unravel_index(element_index, a.shape)) is_omitted, row, start_col, end_col = ( tensor_format.locate_tensor_element(out, subscripts)) self.assertFalse(is_omitted) self.assertEqual(line_num, row) self.assertEqual(match.start(), start_col) self.assertEqual(match.end(), end_col) element_index += 1 self.assertEqual(element_index, np.size(a))
def testLocateTensorElement3DWithEllipsesBatchMode(self): a = (np.arange(11 * 11 * 11) + 1000).reshape([11, 11, 11]).astype(np.int32) out = tensor_format.format_tensor( a, "a", False, np_printoptions={"threshold": 100, "edgeitems": 2}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) actual_row_0_0_0, actual_col_0_0_0 = self._findFirst(out.lines, "1000") actual_row_0_0_10, _ = self._findFirst(out.lines, "1010") actual_row_10_10_10, _ = self._findFirst(out.lines, "2330") (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0, 0, 0]]) self.assertEqual([False], are_omitted) self.assertEqual([actual_row_0_0_0], rows) self.assertEqual([actual_col_0_0_0], start_cols) self.assertEqual([actual_col_0_0_0 + 4], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0, 0, 0], [0, 0, 10]]) self.assertEqual([False, False], are_omitted) self.assertEqual([actual_row_0_0_0, actual_row_0_0_10], rows) self.assertEqual([actual_col_0_0_0, None], start_cols) self.assertEqual([actual_col_0_0_0 + 4, None], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0, 0, 0], [0, 2, 0]]) self.assertEqual([False, True], are_omitted) self.assertEqual([2, 4], rows) self.assertEqual(2, len(start_cols)) self.assertEqual(2, len(end_cols)) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0, 0, 0], [10, 10, 10]]) self.assertEqual([False, False], are_omitted) self.assertEqual([actual_row_0_0_0, actual_row_10_10_10], rows) self.assertEqual([actual_col_0_0_0, None], start_cols) self.assertEqual([actual_col_0_0_0 + 4, None], end_cols)
def testLocateTensorElement2DNoEllipsisWithNumericSummary(self): a = np.linspace(0.0, 1.0 - 1.0 / 16.0, 16).reshape([4, 4]) out = tensor_format.format_tensor(a, "a", include_numeric_summary=True) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", "", "Numeric summary:", "| 0 + | total |", "| 1 15 | 16 |", "| min max mean std |"], out.lines[:6]) cli_test_utils.assert_array_lines_close( self, [0.0, 0.9375, 0.46875, 0.28811076429], out.lines[6:7]) cli_test_utils.assert_array_lines_close(self, a, out.lines[8:]) self._checkTensorElementLocations(out, a) with self.assertRaisesRegexp( ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [1, 4]) with self.assertRaisesRegexp( ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 2]) with self.assertRaisesRegexp( ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0])
def testLocateTensorElement2DNoEllipsisWithNumericSummary(self): a = np.linspace(0.0, 1.0 - 1.0 / 16.0, 16).reshape([4, 4]) out = tensor_format.format_tensor(a, "a", include_numeric_summary=True) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, [ "Tensor \"a\":", "", "Numeric summary:", "| 0 + | total |", "| 1 15 | 16 |", "| min max mean std |" ], out.lines[:6]) cli_test_utils.assert_array_lines_close( self, [0.0, 0.9375, 0.46875, 0.28811076429], out.lines[6:7]) cli_test_utils.assert_array_lines_close(self, a, out.lines[8:]) self._checkTensorElementLocations(out, a) with self.assertRaisesRegex(ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [1, 4]) with self.assertRaisesRegex(ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 2]) with self.assertRaisesRegex(ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0])
def testLocateTensorElement1DTinyAndNanValues(self): a = np.ones([3, 3]) * 1e-8 a[1, 0] = np.nan a[1, 2] = np.inf out = tensor_format.format_tensor(a, "a", np_printoptions={"linewidth": 100}) self.assertEqual([ "Tensor \"a\":", "", "array([[ 1.00000000e-08, 1.00000000e-08, 1.00000000e-08],", " [ nan, 1.00000000e-08, inf],", " [ 1.00000000e-08, 1.00000000e-08, 1.00000000e-08]])", ], out.lines) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 0]) self.assertFalse(is_omitted) self.assertEqual(2, row) self.assertEqual(10, start_col) self.assertEqual(24, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 2]) self.assertFalse(is_omitted) self.assertEqual(2, row) self.assertEqual(46, start_col) self.assertEqual(60, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [1, 0]) self.assertFalse(is_omitted) self.assertEqual(3, row) self.assertEqual(21, start_col) self.assertEqual(24, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [1, 1]) self.assertFalse(is_omitted) self.assertEqual(3, row) self.assertEqual(28, start_col) self.assertEqual(42, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [1, 2]) self.assertFalse(is_omitted) self.assertEqual(3, row) self.assertEqual(57, start_col) self.assertEqual(60, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [2, 2]) self.assertFalse(is_omitted) self.assertEqual(4, row) self.assertEqual(46, start_col) self.assertEqual(60, end_col)
def testLocateTensorElement1DTinyAndNanValues(self): a = np.ones([3, 3]) * 1e-8 a[1, 0] = np.nan a[1, 2] = np.inf out = tensor_format.format_tensor( a, "a", np_printoptions={"linewidth": 100}) self.assertEqual([ "Tensor \"a\":", "", "array([[ 1.00000000e-08, 1.00000000e-08, 1.00000000e-08],", " [ nan, 1.00000000e-08, inf],", " [ 1.00000000e-08, 1.00000000e-08, 1.00000000e-08]])", ], out.lines) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 0]) self.assertFalse(is_omitted) self.assertEqual(2, row) self.assertEqual(10, start_col) self.assertEqual(24, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 2]) self.assertFalse(is_omitted) self.assertEqual(2, row) self.assertEqual(46, start_col) self.assertEqual(60, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [1, 0]) self.assertFalse(is_omitted) self.assertEqual(3, row) self.assertEqual(21, start_col) self.assertEqual(24, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [1, 1]) self.assertFalse(is_omitted) self.assertEqual(3, row) self.assertEqual(28, start_col) self.assertEqual(42, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [1, 2]) self.assertFalse(is_omitted) self.assertEqual(3, row) self.assertEqual(57, start_col) self.assertEqual(60, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [2, 2]) self.assertFalse(is_omitted) self.assertEqual(4, row) self.assertEqual(46, start_col) self.assertEqual(60, end_col)
def testBatchModeWithErrors(self): a = np.zeros(20) out = tensor_format.format_tensor( a, "a", np_printoptions={"linewidth": 40}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) with self.assertRaisesRegexp(ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [[0, 0], [0]]) with self.assertRaisesRegexp(ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [[0], [20]]) with self.assertRaisesRegexp(ValueError, r"Indices contain negative value\(s\)"): tensor_format.locate_tensor_element(out, [[0], [-1]]) with self.assertRaisesRegexp( ValueError, "Input indices sets are not in ascending order"): tensor_format.locate_tensor_element(out, [[5], [0]])
def testLocateTensorElement2DNoEllipsis(self): a = np.linspace(0.0, 1.0 - 1.0 / 16.0, 16).reshape([4, 4]) out = tensor_format.format_tensor(a, "a") cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorElementLocations(out, a) with self.assertRaisesRegex(ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [1, 4]) with self.assertRaisesRegex(ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 2]) with self.assertRaisesRegex(ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0])
def testLocateTensorElement1DNoEllipsis(self): a = np.zeros(20) out = tensor_format.format_tensor(a, "a", np_printoptions={"linewidth": 40}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorElementLocations(out, a) with self.assertRaisesRegex(ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [20]) with self.assertRaisesRegex(ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1]) with self.assertRaisesRegex(ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0, 0])
def testLocateTensorElement2DNoEllipsis(self): a = np.linspace(0.0, 1.0 - 1.0 / 16.0, 16).reshape([4, 4]) out = tensor_format.format_tensor(a, "a") cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorElementLocations(out, a) with self.assertRaisesRegexp( ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [1, 4]) with self.assertRaisesRegexp( ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 2]) with self.assertRaisesRegexp( ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0])
def testLocateTensorElement1DNoEllipsis(self): a = np.zeros(20) out = tensor_format.format_tensor( a, "a", np_printoptions={"linewidth": 40}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorElementLocations(out, a) with self.assertRaisesRegexp( ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [20]) with self.assertRaisesRegexp( ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1]) with self.assertRaisesRegexp( ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0, 0])
def testLocateTensorElement3DWithEllipsesBatchMode(self): a = np.zeros([11, 11, 11]) out = tensor_format.format_tensor( a, "a", False, np_printoptions={"threshold": 100, "edgeitems": 2}) self.assertEqual([ "Tensor \"a\":", "", "array([[[ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.],", " ..., ", " [ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.]],", "", " [[ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.],", " ..., ", " [ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.]],", "", " ..., ", " [[ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.],", " ..., ", " [ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.]],", "", " [[ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.],", " ..., ", " [ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.]]])", ], out.lines) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0, 0, 0]]) self.assertEqual([False], are_omitted) self.assertEqual([2], rows) self.assertEqual([10], start_cols) self.assertEqual([12], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0, 0, 0], [0, 0, 10]]) self.assertEqual([False, False], are_omitted) self.assertEqual([2, 2], rows) self.assertEqual([10, None], start_cols) self.assertEqual([12, None], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0, 0, 0], [0, 2, 0]]) self.assertEqual([False, True], are_omitted) self.assertEqual([2, 4], rows) self.assertEqual([10, None], start_cols) self.assertEqual([12, None], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0, 0, 0], [10, 10, 10]]) self.assertEqual([False, False], are_omitted) self.assertEqual([2, 25], rows) self.assertEqual([10, None], start_cols) self.assertEqual([12, None], end_cols)
def testLocateTensorElement3DWithEllipses(self): a = np.zeros([11, 11, 11]) out = tensor_format.format_tensor( a, "a", False, np_printoptions={"threshold": 100, "edgeitems": 2}) self.assertEqual([ "Tensor \"a\":", "", "array([[[ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.],", " ..., ", " [ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.]],", "", " [[ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.],", " ..., ", " [ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.]],", "", " ..., ", " [[ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.],", " ..., ", " [ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.]],", "", " [[ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.],", " ..., ", " [ 0., 0., ..., 0., 0.],", " [ 0., 0., ..., 0., 0.]]])", ], out.lines) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 0, 0]) self.assertFalse(is_omitted) self.assertEqual(2, row) self.assertEqual(10, start_col) self.assertEqual(12, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 0, 10]) self.assertFalse(is_omitted) self.assertEqual(2, row) self.assertIsNone(start_col) # Passes ellipsis. self.assertIsNone(end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 1, 0]) self.assertFalse(is_omitted) self.assertEqual(3, row) self.assertEqual(10, start_col) self.assertEqual(12, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 2, 0]) self.assertTrue(is_omitted) # In omitted line. self.assertEqual(4, row) self.assertIsNone(start_col) self.assertIsNone(end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 2, 10]) self.assertTrue(is_omitted) # In omitted line. self.assertEqual(4, row) self.assertIsNone(start_col) self.assertIsNone(end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 8, 10]) self.assertTrue(is_omitted) # In omitted line. self.assertEqual(4, row) self.assertIsNone(start_col) self.assertIsNone(end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 10, 1]) self.assertFalse(is_omitted) self.assertEqual(6, row) self.assertEqual(15, start_col) self.assertEqual(17, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [5, 1, 1]) self.assertTrue(is_omitted) # In omitted line. self.assertEqual(14, row) self.assertIsNone(start_col) self.assertIsNone(end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [10, 10, 10]) self.assertFalse(is_omitted) self.assertEqual(25, row) self.assertIsNone(start_col) # Past ellipsis. self.assertIsNone(end_col) with self.assertRaisesRegexp( ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [11, 5, 5]) with self.assertRaisesRegexp( ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 5, 5]) with self.assertRaisesRegexp( ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [5, 5])
def testLocateTensorElement2DNoEllipsis(self): a = np.linspace(0.0, 1.0 - 1.0 / 16.0, 16).reshape([4, 4]) out = tensor_format.format_tensor(a, "a") self.assertEqual([ "Tensor \"a\":", "", "array([[ 0. , 0.0625, 0.125 , 0.1875],", " [ 0.25 , 0.3125, 0.375 , 0.4375],", " [ 0.5 , 0.5625, 0.625 , 0.6875],", " [ 0.75 , 0.8125, 0.875 , 0.9375]])", ], out.lines) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 0]) self.assertFalse(is_omitted) self.assertEqual(2, row) self.assertEqual(9, start_col) self.assertEqual(11, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 3]) self.assertFalse(is_omitted) self.assertEqual(2, row) self.assertEqual(36, start_col) self.assertEqual(42, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [1, 0]) self.assertFalse(is_omitted) self.assertEqual(3, row) self.assertEqual(9, start_col) self.assertEqual(13, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [1, 3]) self.assertFalse(is_omitted) self.assertEqual(3, row) self.assertEqual(36, start_col) self.assertEqual(42, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [3, 3]) self.assertFalse(is_omitted) self.assertEqual(5, row) self.assertEqual(36, start_col) self.assertEqual(42, end_col) with self.assertRaisesRegexp( ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [1, 4]) with self.assertRaisesRegexp( ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 2]) with self.assertRaisesRegexp( ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0])
def testLocateTensorElement3DWithEllipses(self): a = (np.arange(11 * 11 * 11) + 1000).reshape([11, 11, 11]).astype(np.int32) out = tensor_format.format_tensor( a, "a", False, np_printoptions={"threshold": 100, "edgeitems": 2}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) actual_row_0_0_0, actual_col_0_0_0 = self._findFirst(out.lines, "1000") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 0, 0]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_0_0, row) self.assertEqual(actual_col_0_0_0, start_col) self.assertEqual(actual_col_0_0_0 + 4, end_col) actual_row_0_0_10, _ = self._findFirst(out.lines, "1010") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 0, 10]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_0_10, row) self.assertIsNone(start_col) # Passes ellipsis. self.assertIsNone(end_col) actual_row_0_1_0, actual_col_0_1_0 = self._findFirst(out.lines, "1011") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 1, 0]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_1_0, row) self.assertEqual(actual_col_0_1_0, start_col) self.assertEqual(actual_col_0_1_0 + 4, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 2, 0]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 2, 10]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 8, 10]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) actual_row_0_10_1, actual_col_0_10_1 = self._findFirst(out.lines, "1111") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 10, 1]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_10_1, row) self.assertEqual(actual_col_0_10_1, start_col) self.assertEqual(actual_col_0_10_1 + 4, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [5, 1, 1]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) actual_row_10_10_10, _ = self._findFirst(out.lines, "2330") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [10, 10, 10]) self.assertFalse(is_omitted) self.assertEqual(actual_row_10_10_10, row) self.assertIsNone(start_col) # Past ellipsis. self.assertIsNone(end_col) with self.assertRaisesRegexp( ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [11, 5, 5]) with self.assertRaisesRegexp( ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 5, 5]) with self.assertRaisesRegexp( ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [5, 5])
def _dispatch_command(self, command): """Dispatch user command. Args: command: (str) Command to dispatch. Returns: An exit token object. None value means that the UI loop should not exit. A non-None value means the UI loop should exit. """ if command in self.CLI_EXIT_COMMANDS: # Explicit user command-triggered exit: EXPLICIT_USER_EXIT as the exit # token. return debugger_cli_common.EXPLICIT_USER_EXIT if command: self._command_history_store.add_command(command) if (command.startswith(self.REGEX_SEARCH_PREFIX) and self._curr_unwrapped_output): if len(command) > len(self.REGEX_SEARCH_PREFIX): # Command is like "/regex". Perform regex search. regex = command[len(self.REGEX_SEARCH_PREFIX):] self._curr_search_regex = regex self._display_output(self._curr_unwrapped_output, highlight_regex=regex) elif self._unwrapped_regex_match_lines: # Command is "/". Continue scrolling down matching lines. self._display_output( self._curr_unwrapped_output, is_refresh=True, highlight_regex=self._curr_search_regex) self._command_pointer = 0 self._pending_command = "" return elif command.startswith(self.TENSOR_INDICES_NAVIGATION_PREFIX): indices_str = command[1:].strip() if indices_str: try: indices = command_parser.parse_indices(indices_str) omitted, line_index, _, _ = tensor_format.locate_tensor_element( self._curr_wrapped_output, indices) if not omitted: self._scroll_output( self._SCROLL_TO_LINE_INDEX, line_index=line_index) except Exception as e: # pylint: disable=broad-except self._error_toast(str(e)) else: self._error_toast("Empty indices.") return prefix, args = self._parse_command(command) if not prefix: # Empty command: take no action. Should not exit. return screen_info = {"cols": self._max_x} exit_token = None if self._command_handler_registry.is_registered(prefix): try: screen_output = self._command_handler_registry.dispatch_command( prefix, args, screen_info=screen_info) except debugger_cli_common.CommandLineExit as e: exit_token = e.exit_token else: screen_output = debugger_cli_common.RichTextLines([ self.ERROR_MESSAGE_PREFIX + "Invalid command prefix \"%s\"" % prefix ]) # Clear active command history. Until next up/down history navigation # occurs, it will stay empty. self._active_command_history = [] if exit_token is not None: return exit_token self._display_output(screen_output) self._command_pointer = 0 self._pending_command = ""
def testLocateTensorElement1DNoEllipsis(self): a = np.zeros(20) out = tensor_format.format_tensor( a, "a", np_printoptions={"linewidth": 40}) self.assertEqual([ "Tensor \"a\":", "", "array([ 0., 0., 0., 0., 0., 0.,", " 0., 0., 0., 0., 0., 0.,", " 0., 0., 0., 0., 0., 0.,", " 0., 0.])", ], out.lines) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0]) self.assertFalse(is_omitted) self.assertEqual(2, row) self.assertEqual(8, start_col) self.assertEqual(10, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [5]) self.assertFalse(is_omitted) self.assertEqual(2, row) self.assertEqual(33, start_col) self.assertEqual(35, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [6]) self.assertFalse(is_omitted) self.assertEqual(3, row) self.assertEqual(8, start_col) self.assertEqual(10, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [11]) self.assertFalse(is_omitted) self.assertEqual(3, row) self.assertEqual(33, start_col) self.assertEqual(35, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [12]) self.assertFalse(is_omitted) self.assertEqual(4, row) self.assertEqual(8, start_col) self.assertEqual(10, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [18]) self.assertFalse(is_omitted) self.assertEqual(5, row) self.assertEqual(8, start_col) self.assertEqual(10, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [19]) self.assertFalse(is_omitted) self.assertEqual(5, row) self.assertEqual(13, start_col) self.assertEqual(15, end_col) with self.assertRaisesRegexp( ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [20]) with self.assertRaisesRegexp( ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1]) with self.assertRaisesRegexp( ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0, 0])
def _dispatch_command(self, command): """Dispatch user command. Args: command: (str) Command to dispatch. Returns: An exit token object. None value means that the UI loop should not exit. A non-None value means the UI loop should exit. """ if command in self.CLI_EXIT_COMMANDS: # Explicit user command-triggered exit: EXPLICIT_USER_EXIT as the exit # token. return debugger_cli_common.EXPLICIT_USER_EXIT if command: self._command_history_store.add_command(command) if (command.startswith(self.REGEX_SEARCH_PREFIX) and self._curr_unwrapped_output): if len(command) > len(self.REGEX_SEARCH_PREFIX): # Command is like "/regex". Perform regex search. regex = command[len(self.REGEX_SEARCH_PREFIX):] self._curr_search_regex = regex self._display_output(self._curr_unwrapped_output, highlight_regex=regex) elif self._unwrapped_regex_match_lines: # Command is "/". Continue scrolling down matching lines. self._display_output(self._curr_unwrapped_output, is_refresh=True, highlight_regex=self._curr_search_regex) self._command_pointer = 0 self._pending_command = "" return elif command.startswith(self.TENSOR_INDICES_NAVIGATION_PREFIX): indices_str = command[1:].strip() if indices_str: try: indices = command_parser.parse_indices(indices_str) omitted, line_index, _, _ = tensor_format.locate_tensor_element( self._curr_wrapped_output, indices) if not omitted: self._scroll_output(self._SCROLL_TO_LINE_INDEX, line_index=line_index) except Exception as e: # pylint: disable=broad-except self._error_toast(str(e)) else: self._error_toast("Empty indices.") return try: prefix, args, output_file_path = self._parse_command(command) except SyntaxError as e: self._error_toast(str(e)) return if not prefix: # Empty command: take no action. Should not exit. return screen_info = {"cols": self._max_x} exit_token = None if self._command_handler_registry.is_registered(prefix): try: screen_output = self._command_handler_registry.dispatch_command( prefix, args, screen_info=screen_info) except debugger_cli_common.CommandLineExit as e: exit_token = e.exit_token else: screen_output = debugger_cli_common.RichTextLines([ self.ERROR_MESSAGE_PREFIX + "Invalid command prefix \"%s\"" % prefix ]) # Clear active command history. Until next up/down history navigation # occurs, it will stay empty. self._active_command_history = [] if exit_token is not None: return exit_token self._display_output(screen_output) if output_file_path: try: screen_output.write_to_file(output_file_path) self._info_toast("Wrote output to %s" % output_file_path) except Exception: # pylint: disable=broad-except self._error_toast("Failed to write output to %s" % output_file_path) self._command_pointer = 0 self._pending_command = ""
def testLocateTensorElement3DWithEllipses(self): a = (np.arange(11 * 11 * 11) + 1000).reshape([11, 11, 11]).astype(np.int32) out = tensor_format.format_tensor(a, "a", False, np_printoptions={ "threshold": 100, "edgeitems": 2 }) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) actual_row_0_0_0, actual_col_0_0_0 = self._findFirst(out.lines, "1000") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 0, 0]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_0_0, row) self.assertEqual(actual_col_0_0_0, start_col) self.assertEqual(actual_col_0_0_0 + 4, end_col) actual_row_0_0_10, _ = self._findFirst(out.lines, "1010") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 0, 10]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_0_10, row) self.assertIsNone(start_col) # Passes ellipsis. self.assertIsNone(end_col) actual_row_0_1_0, actual_col_0_1_0 = self._findFirst(out.lines, "1011") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 1, 0]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_1_0, row) self.assertEqual(actual_col_0_1_0, start_col) self.assertEqual(actual_col_0_1_0 + 4, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 2, 0]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 2, 10]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 8, 10]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) actual_row_0_10_1, actual_col_0_10_1 = self._findFirst( out.lines, "1111") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 10, 1]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_10_1, row) self.assertEqual(actual_col_0_10_1, start_col) self.assertEqual(actual_col_0_10_1 + 4, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [5, 1, 1]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) actual_row_10_10_10, _ = self._findFirst(out.lines, "2330") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [10, 10, 10]) self.assertFalse(is_omitted) self.assertEqual(actual_row_10_10_10, row) self.assertIsNone(start_col) # Past ellipsis. self.assertIsNone(end_col) with self.assertRaisesRegex(ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [11, 5, 5]) with self.assertRaisesRegex(ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 5, 5]) with self.assertRaisesRegex(ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [5, 5])
def testLocateTensorElement2DNoEllipsisWithNumericSummary(self): a = np.linspace(0.0, 1.0 - 1.0 / 16.0, 16).reshape([4, 4]) out = tensor_format.format_tensor(a, "a", include_numeric_summary=True) self.assertEqual([ "Tensor \"a\":", "", "Numeric summary:", "| 0 + | total |", "| 1 15 | 16 |", "| min max mean std |", "| 0.0 0.9375 0.46875 0.28811076429 |", "", "array([[ 0. , 0.0625, 0.125 , 0.1875],", " [ 0.25 , 0.3125, 0.375 , 0.4375],", " [ 0.5 , 0.5625, 0.625 , 0.6875],", " [ 0.75 , 0.8125, 0.875 , 0.9375]])", ], out.lines) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 0]) self.assertFalse(is_omitted) self.assertEqual(8, row) self.assertEqual(9, start_col) self.assertEqual(11, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 3]) self.assertFalse(is_omitted) self.assertEqual(8, row) self.assertEqual(36, start_col) self.assertEqual(42, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [1, 0]) self.assertFalse(is_omitted) self.assertEqual(9, row) self.assertEqual(9, start_col) self.assertEqual(13, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [1, 3]) self.assertFalse(is_omitted) self.assertEqual(9, row) self.assertEqual(36, start_col) self.assertEqual(42, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [3, 3]) self.assertFalse(is_omitted) self.assertEqual(11, row) self.assertEqual(36, start_col) self.assertEqual(42, end_col) with self.assertRaisesRegexp(ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [1, 4]) with self.assertRaisesRegexp(ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 2]) with self.assertRaisesRegexp(ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0])