test_priority_pool.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import multiprocessing as mp
  2. import platform
  3. import time
  4. import pytest
  5. import torch
  6. from hivemind.moe.server.runtime import Runtime
  7. from petals.server.task_pool import PrioritizedTaskPool
  8. def _submit_tasks(runtime_ready, pools, results_valid):
  9. runtime_ready.wait()
  10. futures = []
  11. futures.append(pools[0].submit_task(torch.tensor([0]), priority=1))
  12. futures.append(pools[0].submit_task(torch.tensor([1]), priority=1))
  13. time.sleep(0.01)
  14. futures.append(pools[1].submit_task(torch.tensor([2]), priority=1))
  15. futures.append(pools[0].submit_task(torch.tensor([3]), priority=2))
  16. futures.append(pools[0].submit_task(torch.tensor([4]), priority=10))
  17. futures.append(pools[0].submit_task(torch.tensor([5]), priority=0))
  18. futures.append(pools[0].submit_task(torch.tensor([6]), priority=1))
  19. futures.append(pools[1].submit_task(torch.tensor([7]), priority=11))
  20. futures.append(pools[1].submit_task(torch.tensor([8]), priority=1))
  21. for i, f in enumerate(futures):
  22. assert f.result()[0].item() == i**2
  23. results_valid.set()
  24. @pytest.mark.skipif(platform.system() == "Darwin", reason="Flapping on macOS due to multiprocessing quirks")
  25. @pytest.mark.forked
  26. def test_priority_pools():
  27. outputs_queue = mp.SimpleQueue()
  28. runtime_ready = mp.Event()
  29. results_valid = mp.Event()
  30. def dummy_pool_func(args, kwargs):
  31. (x,) = args # TODO modify the PriorityPool code such that dummy_pool_func can accept x directly
  32. time.sleep(0.1)
  33. y = x**2
  34. outputs_queue.put((x, y))
  35. return (y,)
  36. class DummyBackend:
  37. def __init__(self, pools):
  38. self.pools = pools
  39. def get_pools(self):
  40. return self.pools
  41. pools = (
  42. PrioritizedTaskPool(dummy_pool_func, name="A", max_batch_size=1),
  43. PrioritizedTaskPool(dummy_pool_func, name="B", max_batch_size=1),
  44. )
  45. # Simulate requests coming from ConnectionHandlers
  46. proc = mp.context.ForkProcess(target=_submit_tasks, args=(runtime_ready, pools, results_valid))
  47. proc.start()
  48. runtime = Runtime({str(i): DummyBackend([pool]) for i, pool in enumerate(pools)}, prefetch_batches=0)
  49. runtime.ready = runtime_ready
  50. runtime.start()
  51. proc.join()
  52. assert results_valid.is_set()
  53. ordered_outputs = []
  54. while not outputs_queue.empty():
  55. ordered_outputs.append(outputs_queue.get()[0].item())
  56. assert ordered_outputs == [0, 5, 1, 2, 6, 8, 3, 4, 7]
  57. # 0 - first batch is loaded immediately, before everything else
  58. # 5 - highest priority task overall
  59. # 1 - first of several tasks with equal lowest priority (1)
  60. # 2 - second earliest task with priority 1, fetched from pool B
  61. # 6 - third earliest task with priority 1, fetched from pool A again
  62. # 8 - last priority-1 task, pool B
  63. # 3 - task with priority 2 from pool A
  64. # 4 - task with priority 10 from pool A
  65. # 7 - task with priority 11 from pool B
  66. runtime.shutdown()