rpc_utils.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import unittest
  5. from typing import Dict, List, Type
  6. from torch.testing._internal.common_distributed import MultiProcessTestCase
  7. from torch.testing._internal.common_utils import (
  8. TEST_WITH_DEV_DBG_ASAN,
  9. find_free_port,
  10. IS_SANDCASTLE,
  11. )
  12. from torch.testing._internal.distributed.ddp_under_dist_autograd_test import (
  13. CudaDdpComparisonTest,
  14. DdpComparisonTest,
  15. DdpUnderDistAutogradTest,
  16. )
  17. from torch.testing._internal.distributed.pipe_with_ddp_test import (
  18. PipeWithDDPTest,
  19. )
  20. from torch.testing._internal.distributed.nn.api.remote_module_test import (
  21. CudaRemoteModuleTest,
  22. RemoteModuleTest,
  23. ThreeWorkersRemoteModuleTest,
  24. )
  25. from torch.testing._internal.distributed.rpc.dist_autograd_test import (
  26. DistAutogradTest,
  27. CudaDistAutogradTest,
  28. FaultyAgentDistAutogradTest,
  29. TensorPipeAgentDistAutogradTest,
  30. TensorPipeCudaDistAutogradTest
  31. )
  32. from torch.testing._internal.distributed.rpc.dist_optimizer_test import (
  33. DistOptimizerTest,
  34. )
  35. from torch.testing._internal.distributed.rpc.jit.dist_autograd_test import (
  36. JitDistAutogradTest,
  37. )
  38. from torch.testing._internal.distributed.rpc.jit.rpc_test import JitRpcTest
  39. from torch.testing._internal.distributed.rpc.jit.rpc_test_faulty import (
  40. JitFaultyAgentRpcTest,
  41. )
  42. from torch.testing._internal.distributed.rpc.rpc_agent_test_fixture import (
  43. RpcAgentTestFixture,
  44. )
  45. from torch.testing._internal.distributed.rpc.faulty_agent_rpc_test import (
  46. FaultyAgentRpcTest,
  47. )
  48. from torch.testing._internal.distributed.rpc.rpc_test import (
  49. CudaRpcTest,
  50. RpcTest,
  51. TensorPipeAgentRpcTest,
  52. TensorPipeAgentCudaRpcTest,
  53. )
  54. from torch.testing._internal.distributed.rpc.examples.parameter_server_test import ParameterServerTest
  55. from torch.testing._internal.distributed.rpc.examples.reinforcement_learning_rpc_test import (
  56. ReinforcementLearningRpcTest,
  57. )
  58. def _check_and_set_tcp_init():
  59. # if we are running with TCP init, set main address and port
  60. # before spawning subprocesses, since different processes could find
  61. # different ports.
  62. use_tcp_init = os.environ.get("RPC_INIT_WITH_TCP", None)
  63. if use_tcp_init == "1":
  64. os.environ["MASTER_ADDR"] = '127.0.0.1'
  65. os.environ["MASTER_PORT"] = str(find_free_port())
  66. def _check_and_unset_tcp_init():
  67. use_tcp_init = os.environ.get("RPC_INIT_WITH_TCP", None)
  68. if use_tcp_init == "1":
  69. del os.environ["MASTER_ADDR"]
  70. del os.environ["MASTER_PORT"]
  71. # The tests for the RPC module need to cover multiple possible combinations:
  72. # - different aspects of the API, each one having its own suite of tests;
  73. # - different agents (ProcessGroup, TensorPipe, ...);
  74. # To avoid a combinatorial explosion in code size, and to prevent forgetting to
  75. # add a combination, these are generated automatically by the code in this file.
  76. # Here, we collect all the test suites that we need to cover.
  77. # We then have one separate file for each agent, from which
  78. # we call the generate_tests function of this file, passing to it a fixture for
  79. # the agent, which then gets mixed-in with each test suite.
  80. @unittest.skipIf(
  81. TEST_WITH_DEV_DBG_ASAN, "Skip ASAN as torch + multiprocessing spawn have known issues"
  82. )
  83. class SpawnHelper(MultiProcessTestCase):
  84. def setUp(self):
  85. super().setUp()
  86. _check_and_set_tcp_init()
  87. self._spawn_processes()
  88. def tearDown(self):
  89. _check_and_unset_tcp_init()
  90. super().tearDown()
  91. # This list contains test suites that are agent-agnostic and that only verify
  92. # compliance with the generic RPC interface specification. These tests should
  93. # *not* make use of implementation details of a specific agent (options,
  94. # attributes, ...). These test suites will be instantiated multiple times, once
  95. # for each agent (except the faulty agent, which is special).
  96. GENERIC_TESTS = [
  97. RpcTest,
  98. ParameterServerTest,
  99. DistAutogradTest,
  100. DistOptimizerTest,
  101. JitRpcTest,
  102. JitDistAutogradTest,
  103. RemoteModuleTest,
  104. ThreeWorkersRemoteModuleTest,
  105. DdpUnderDistAutogradTest,
  106. DdpComparisonTest,
  107. ReinforcementLearningRpcTest,
  108. ]
  109. GENERIC_CUDA_TESTS = [
  110. CudaRpcTest,
  111. CudaDistAutogradTest,
  112. CudaRemoteModuleTest,
  113. CudaDdpComparisonTest,
  114. PipeWithDDPTest,
  115. ]
  116. # This list contains test suites that will only be run on the TensorPipeAgent.
  117. # These suites should be standalone, and separate from the ones in the generic
  118. # list (not subclasses of those!).
  119. TENSORPIPE_TESTS = [
  120. TensorPipeAgentRpcTest,
  121. TensorPipeAgentDistAutogradTest,
  122. ]
  123. TENSORPIPE_CUDA_TESTS = [
  124. TensorPipeAgentCudaRpcTest,
  125. TensorPipeCudaDistAutogradTest,
  126. ]
  127. # This list contains test suites that will only be run on the faulty RPC agent.
  128. # That agent is special as it's only used to perform fault injection in order to
  129. # verify the error handling behavior. Thus the faulty agent will only run the
  130. # suites in this list, which were designed to test such behaviors, and not the
  131. # ones in the generic list.
  132. FAULTY_AGENT_TESTS = [
  133. FaultyAgentRpcTest,
  134. FaultyAgentDistAutogradTest,
  135. JitFaultyAgentRpcTest,
  136. ]
  137. def generate_tests(
  138. prefix: str,
  139. mixin: Type[RpcAgentTestFixture],
  140. tests: List[Type[RpcAgentTestFixture]],
  141. module_name: str,
  142. ) -> Dict[str, Type[RpcAgentTestFixture]]:
  143. """Mix in the classes needed to autogenerate the tests based on the params.
  144. Takes a series of test suites, each written against a "generic" agent (i.e.,
  145. derived from the abstract RpcAgentTestFixture class), as the `tests` args.
  146. Takes a concrete subclass of RpcAgentTestFixture, which specializes it for a
  147. certain agent, as the `mixin` arg. Produces all combinations of them.
  148. Returns a dictionary of class names to class type
  149. objects which can be inserted into the global namespace of the calling
  150. module. The name of each test will be a concatenation of the `prefix` arg
  151. and the original name of the test suite.
  152. The `module_name` should be the name of the calling module so
  153. that the classes can be fixed to make it look like they belong to it, which
  154. is necessary for pickling to work on them.
  155. """
  156. ret: Dict[str, Type[RpcAgentTestFixture]] = {}
  157. for test_class in tests:
  158. if IS_SANDCASTLE and TEST_WITH_DEV_DBG_ASAN:
  159. print(
  160. f'Skipping test {test_class} on sandcastle for the following reason: '
  161. 'Skip dev-asan as torch + multiprocessing spawn have known issues', file=sys.stderr)
  162. continue
  163. name = f"{prefix}{test_class.__name__}"
  164. class_ = type(name, (test_class, mixin, SpawnHelper), {})
  165. class_.__module__ = module_name
  166. ret[name] = class_
  167. return ret