You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_paddle_utils.py 7.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import pytest
  2. from fastNLP.core.utils.paddle_utils import paddle_to, paddle_move_data_to_device
  3. from fastNLP.envs.imports import _NEED_IMPORT_PADDLE
  4. if _NEED_IMPORT_PADDLE:
  5. import paddle
  6. ############################################################################
  7. #
  8. # 测试仅将单个paddle张量迁移到指定设备
  9. #
  10. ############################################################################
  11. @pytest.mark.paddle
  12. class TestPaddleToDevice:
  13. def test_case(self):
  14. tensor = paddle.rand((4, 5))
  15. res = paddle_to(tensor, "gpu")
  16. assert res.place.is_gpu_place()
  17. assert res.place.gpu_device_id() == 0
  18. res = paddle_to(tensor, "cpu")
  19. assert res.place.is_cpu_place()
  20. res = paddle_to(tensor, "gpu:2")
  21. assert res.place.is_gpu_place()
  22. assert res.place.gpu_device_id() == 2
  23. res = paddle_to(tensor, "gpu:1")
  24. assert res.place.is_gpu_place()
  25. assert res.place.gpu_device_id() == 1
  26. ############################################################################
  27. #
  28. # 测试将参数中包含的所有paddle张量迁移到指定设备
  29. #
  30. ############################################################################
  31. class TestPaddleMoveDataToDevice:
  32. def check_gpu(self, tensor, idx):
  33. """
  34. 检查张量是否在指定的设备上的工具函数
  35. """
  36. assert tensor.place.is_gpu_place()
  37. assert tensor.place.gpu_device_id() == idx
  38. def check_cpu(self, tensor):
  39. """
  40. 检查张量是否在cpu上的工具函数
  41. """
  42. assert tensor.place.is_cpu_place()
  43. def test_tensor_transfer(self):
  44. """
  45. 测试单个张量的迁移
  46. """
  47. paddle_tensor = paddle.rand((3, 4, 5)).cpu()
  48. res = paddle_move_data_to_device(paddle_tensor, device=None, data_device=None)
  49. self.check_cpu(res)
  50. res = paddle_move_data_to_device(paddle_tensor, device="gpu:0", data_device=None)
  51. self.check_gpu(res, 0)
  52. res = paddle_move_data_to_device(paddle_tensor, device="gpu:1", data_device=None)
  53. self.check_gpu(res, 1)
  54. res = paddle_move_data_to_device(paddle_tensor, device="gpu:0", data_device="cpu")
  55. self.check_gpu(res, 0)
  56. res = paddle_move_data_to_device(paddle_tensor, device=None, data_device="gpu:0")
  57. self.check_gpu(res, 0)
  58. res = paddle_move_data_to_device(paddle_tensor, device=None, data_device="gpu:1")
  59. self.check_gpu(res, 1)
  60. def test_list_transfer(self):
  61. """
  62. 测试张量列表的迁移
  63. """
  64. paddle_list = [paddle.rand((6, 4, 2)) for i in range(10)]
  65. res = paddle_move_data_to_device(paddle_list, device=None, data_device="gpu:1")
  66. assert isinstance(res, list)
  67. for r in res:
  68. self.check_gpu(r, 1)
  69. res = paddle_move_data_to_device(paddle_list, device="cpu", data_device="gpu:1")
  70. assert isinstance(res, list)
  71. for r in res:
  72. self.check_cpu(r)
  73. res = paddle_move_data_to_device(paddle_list, device="gpu:0", data_device=None)
  74. assert isinstance(res, list)
  75. for r in res:
  76. self.check_gpu(r, 0)
  77. res = paddle_move_data_to_device(paddle_list, device="gpu:1", data_device="cpu")
  78. assert isinstance(res, list)
  79. for r in res:
  80. self.check_gpu(r, 1)
  81. def test_tensor_tuple_transfer(self):
  82. """
  83. 测试张量元组的迁移
  84. """
  85. paddle_list = [paddle.rand((6, 4, 2)) for i in range(10)]
  86. paddle_tuple = tuple(paddle_list)
  87. res = paddle_move_data_to_device(paddle_tuple, device=None, data_device="gpu:1")
  88. assert isinstance(res, tuple)
  89. for r in res:
  90. self.check_gpu(r, 1)
  91. res = paddle_move_data_to_device(paddle_tuple, device="cpu", data_device="gpu:1")
  92. assert isinstance(res, tuple)
  93. for r in res:
  94. self.check_cpu(r)
  95. res = paddle_move_data_to_device(paddle_tuple, device="gpu:0", data_device=None)
  96. assert isinstance(res, tuple)
  97. for r in res:
  98. self.check_gpu(r, 0)
  99. res = paddle_move_data_to_device(paddle_tuple, device="gpu:1", data_device="cpu")
  100. assert isinstance(res, tuple)
  101. for r in res:
  102. self.check_gpu(r, 1)
  103. def test_dict_transfer(self):
  104. """
  105. 测试字典结构的迁移
  106. """
  107. paddle_dict = {
  108. "tensor": paddle.rand((3, 4)),
  109. "list": [paddle.rand((6, 4, 2)) for i in range(10)],
  110. "dict":{
  111. "list": [paddle.rand((6, 4, 2)) for i in range(10)],
  112. "tensor": paddle.rand((3, 4))
  113. },
  114. "int": 2,
  115. "string": "test string"
  116. }
  117. res = paddle_move_data_to_device(paddle_dict, device="gpu:0", data_device=None)
  118. assert isinstance(res, dict)
  119. self.check_gpu(res["tensor"], 0)
  120. assert isinstance(res["list"], list)
  121. for t in res["list"]:
  122. self.check_gpu(t, 0)
  123. assert isinstance(res["int"], int)
  124. assert isinstance(res["string"], str)
  125. assert isinstance(res["dict"], dict)
  126. assert isinstance(res["dict"]["list"], list)
  127. for t in res["dict"]["list"]:
  128. self.check_gpu(t, 0)
  129. self.check_gpu(res["dict"]["tensor"], 0)
  130. res = paddle_move_data_to_device(paddle_dict, device="gpu:0", data_device="cpu")
  131. assert isinstance(res, dict)
  132. self.check_gpu(res["tensor"], 0)
  133. assert isinstance(res["list"], list)
  134. for t in res["list"]:
  135. self.check_gpu(t, 0)
  136. assert isinstance(res["int"], int)
  137. assert isinstance(res["string"], str)
  138. assert isinstance(res["dict"], dict)
  139. assert isinstance(res["dict"]["list"], list)
  140. for t in res["dict"]["list"]:
  141. self.check_gpu(t, 0)
  142. self.check_gpu(res["dict"]["tensor"], 0)
  143. res = paddle_move_data_to_device(paddle_dict, device=None, data_device="gpu:1")
  144. assert isinstance(res, dict)
  145. self.check_gpu(res["tensor"], 1)
  146. assert isinstance(res["list"], list)
  147. for t in res["list"]:
  148. self.check_gpu(t, 1)
  149. assert isinstance(res["int"], int)
  150. assert isinstance(res["string"], str)
  151. assert isinstance(res["dict"], dict)
  152. assert isinstance(res["dict"]["list"], list)
  153. for t in res["dict"]["list"]:
  154. self.check_gpu(t, 1)
  155. self.check_gpu(res["dict"]["tensor"], 1)
  156. res = paddle_move_data_to_device(paddle_dict, device="cpu", data_device="gpu:0")
  157. assert isinstance(res, dict)
  158. self.check_cpu(res["tensor"])
  159. assert isinstance(res["list"], list)
  160. for t in res["list"]:
  161. self.check_cpu(t)
  162. assert isinstance(res["int"], int)
  163. assert isinstance(res["string"], str)
  164. assert isinstance(res["dict"], dict)
  165. assert isinstance(res["dict"]["list"], list)
  166. for t in res["dict"]["list"]:
  167. self.check_cpu(t)
  168. self.check_cpu(res["dict"]["tensor"])