|
24 | 24 | import paddle.nn.functional as F |
25 | 25 | from paddle import base |
26 | 26 | from paddle.base import core |
| 27 | +from paddle.tensor.to_string import DEFAULT_PRINT_OPTIONS |
27 | 28 | from paddle.utils.dlpack import DLDeviceType |
28 | 29 |
|
29 | 30 |
|
@@ -1337,6 +1338,72 @@ def test_tensor_str_fp8_e5m2(self): |
1337 | 1338 |
|
1338 | 1339 | self.assertEqual(a_str, expected) |
1339 | 1340 |
|
| 1341 | + def test_tensor_str_complex64(self): |
| 1342 | + original_opt = copy.deepcopy(DEFAULT_PRINT_OPTIONS) |
| 1343 | + try: |
| 1344 | + paddle.disable_static(paddle.CPUPlace()) |
| 1345 | + a = paddle.to_tensor( |
| 1346 | + [[1.5 + 1j, 1.0 - 2j], [0 - 3j, 0]], dtype="complex64" |
| 1347 | + ).cpu() |
| 1348 | + paddle.set_printoptions(precision=4) |
| 1349 | + a_str = str(a) |
| 1350 | + |
| 1351 | + expected = """Tensor(shape=[2, 2], dtype=complex64, place=Place(cpu), stop_gradient=True, |
| 1352 | + [[(1.5000+1.0000j), (1.0000-2.0000j)], |
| 1353 | + [(0.0000-3.0000j), (0.0000+0.0000j)]])""" |
| 1354 | + |
| 1355 | + self.assertEqual(a_str, expected) |
| 1356 | + |
| 1357 | + paddle.set_printoptions(precision=4, sci_mode=True) |
| 1358 | + a_str = str(a) |
| 1359 | + |
| 1360 | + expected = """Tensor(shape=[2, 2], dtype=complex64, place=Place(cpu), stop_gradient=True, |
| 1361 | + [[(1.5000e+00+1.0000e+00j), (1.0000e+00-2.0000e+00j)], |
| 1362 | + [(0.0000e+00-3.0000e+00j), (0.0000e+00+0.0000e+00j)]])""" |
| 1363 | + |
| 1364 | + self.assertEqual(a_str, expected) |
| 1365 | + finally: |
| 1366 | + paddle.set_printoptions( |
| 1367 | + precision=original_opt.precision, |
| 1368 | + threshold=original_opt.threshold, |
| 1369 | + edgeitems=original_opt.edgeitems, |
| 1370 | + sci_mode=original_opt.sci_mode, |
| 1371 | + linewidth=original_opt.linewidth, |
| 1372 | + ) |
| 1373 | + |
| 1374 | + def test_tensor_str_complex128(self): |
| 1375 | + original_opt = copy.deepcopy(DEFAULT_PRINT_OPTIONS) |
| 1376 | + try: |
| 1377 | + paddle.disable_static(paddle.CPUPlace()) |
| 1378 | + a = paddle.to_tensor( |
| 1379 | + [[1.5 + 1j, 1.0 - 2j], [0 - 3j, 0]], dtype="complex128" |
| 1380 | + ).cpu() |
| 1381 | + paddle.set_printoptions(precision=4) |
| 1382 | + a_str = str(a) |
| 1383 | + |
| 1384 | + expected = """Tensor(shape=[2, 2], dtype=complex128, place=Place(cpu), stop_gradient=True, |
| 1385 | + [[(1.5000+1.0000j), (1.0000-2.0000j)], |
| 1386 | + [(0.0000-3.0000j), (0.0000+0.0000j)]])""" |
| 1387 | + |
| 1388 | + self.assertEqual(a_str, expected) |
| 1389 | + |
| 1390 | + paddle.set_printoptions(precision=4, sci_mode=True) |
| 1391 | + a_str = str(a) |
| 1392 | + |
| 1393 | + expected = """Tensor(shape=[2, 2], dtype=complex128, place=Place(cpu), stop_gradient=True, |
| 1394 | + [[(1.5000e+00+1.0000e+00j), (1.0000e+00-2.0000e+00j)], |
| 1395 | + [(0.0000e+00-3.0000e+00j), (0.0000e+00+0.0000e+00j)]])""" |
| 1396 | + |
| 1397 | + self.assertEqual(a_str, expected) |
| 1398 | + finally: |
| 1399 | + paddle.set_printoptions( |
| 1400 | + precision=original_opt.precision, |
| 1401 | + threshold=original_opt.threshold, |
| 1402 | + edgeitems=original_opt.edgeitems, |
| 1403 | + sci_mode=original_opt.sci_mode, |
| 1404 | + linewidth=original_opt.linewidth, |
| 1405 | + ) |
| 1406 | + |
1340 | 1407 | def test_print_tensor_dtype(self): |
1341 | 1408 | paddle.disable_static(paddle.CPUPlace()) |
1342 | 1409 | a = paddle.rand([1]) |
|
0 commit comments