test_notebook.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Copyright 2022 The HuggingFace Team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. Test file to ensure that in general certain situational setups for notebooks work.
  16. """
  17. import os
  18. import time
  19. from multiprocessing import Queue
  20. from pytest import mark, raises
  21. from torch.distributed.elastic.multiprocessing.errors import ChildFailedError
  22. from accelerate import PartialState, notebook_launcher
  23. from accelerate.test_utils import require_bnb
  24. from accelerate.utils import is_bnb_available
  25. def basic_function():
  26. # Just prints the PartialState
  27. print(f"PartialState:\n{PartialState()}")
  28. def tough_nut_function(queue: Queue):
  29. if queue.empty():
  30. return
  31. trial = queue.get()
  32. if trial > 0:
  33. queue.put(trial - 1)
  34. raise RuntimeError("The nut hasn't cracked yet! Try again.")
  35. print(f"PartialState:\n{PartialState()}")
  36. def bipolar_sleep_function(sleep_sec: int):
  37. state = PartialState()
  38. if state.process_index % 2 == 0:
  39. raise RuntimeError("I'm an even process. I don't like to sleep.")
  40. else:
  41. time.sleep(sleep_sec)
  42. NUM_PROCESSES = int(os.environ.get("ACCELERATE_NUM_PROCESSES", 1))
  43. def test_can_initialize():
  44. notebook_launcher(basic_function, (), num_processes=NUM_PROCESSES)
  45. @mark.skipif(NUM_PROCESSES < 2, reason="Need at least 2 processes to test static rendezvous backends")
  46. def test_static_rdzv_backend():
  47. notebook_launcher(basic_function, (), num_processes=NUM_PROCESSES, rdzv_backend="static")
  48. @mark.skipif(NUM_PROCESSES < 2, reason="Need at least 2 processes to test c10d rendezvous backends")
  49. def test_c10d_rdzv_backend():
  50. notebook_launcher(basic_function, (), num_processes=NUM_PROCESSES, rdzv_backend="c10d")
  51. @mark.skipif(NUM_PROCESSES < 2, reason="Need at least 2 processes to test fault tolerance")
  52. def test_fault_tolerant(max_restarts: int = 3):
  53. queue = Queue()
  54. queue.put(max_restarts)
  55. notebook_launcher(tough_nut_function, (queue,), num_processes=NUM_PROCESSES, max_restarts=max_restarts)
  56. @mark.skipif(NUM_PROCESSES < 2, reason="Need at least 2 processes to test monitoring")
  57. def test_monitoring(monitor_interval: float = 0.01, sleep_sec: int = 100):
  58. start_time = time.time()
  59. with raises(ChildFailedError, match="I'm an even process. I don't like to sleep."):
  60. notebook_launcher(
  61. bipolar_sleep_function,
  62. (sleep_sec,),
  63. num_processes=NUM_PROCESSES,
  64. monitor_interval=monitor_interval,
  65. )
  66. assert time.time() - start_time < sleep_sec, "Monitoring did not stop the process in time."
  67. @require_bnb
  68. def test_problematic_imports():
  69. with raises(RuntimeError, match="Please keep these imports"):
  70. import bitsandbytes as bnb # noqa: F401
  71. notebook_launcher(basic_function, (), num_processes=NUM_PROCESSES)
  72. def main():
  73. print("Test basic notebook can be ran")
  74. test_can_initialize()
  75. print("Test static rendezvous backend")
  76. test_static_rdzv_backend()
  77. print("Test c10d rendezvous backend")
  78. test_c10d_rdzv_backend()
  79. print("Test fault tolerant")
  80. test_fault_tolerant()
  81. print("Test monitoring")
  82. test_monitoring()
  83. if is_bnb_available():
  84. print("Test problematic imports (bnb)")
  85. test_problematic_imports()
  86. if NUM_PROCESSES > 1:
  87. PartialState().destroy_process_group()
  88. if __name__ == "__main__":
  89. main()