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 6.3 kB

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