test_utils.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import os
  2. import ray
  3. from aphrodite.common.utils import (cuda_device_count_stateless,
  4. update_environment_variables)
  5. CUDA_VISIBLE_DEVICES = os.environ.get("CUDA_VISIBLE_DEVICES", None)
  6. @ray.remote
  7. class _CUDADeviceCountStatelessTestActor:
  8. def get_count(self):
  9. return cuda_device_count_stateless()
  10. def set_cuda_visible_devices(self, cuda_visible_devices: str):
  11. update_environment_variables(
  12. {"CUDA_VISIBLE_DEVICES": cuda_visible_devices})
  13. def get_cuda_visible_devices(self):
  14. return CUDA_VISIBLE_DEVICES
  15. def test_cuda_device_count_stateless():
  16. """Test that cuda_device_count_stateless changes return value if
  17. CUDA_VISIBLE_DEVICES is changed."""
  18. actor = _CUDADeviceCountStatelessTestActor.options( # type: ignore
  19. num_gpus=2).remote()
  20. assert sorted(ray.get(
  21. actor.get_cuda_visible_devices.remote()).split(",")) == ["0", "1"]
  22. assert ray.get(actor.get_count.remote()) == 2
  23. ray.get(actor.set_cuda_visible_devices.remote("0"))
  24. assert ray.get(actor.get_count.remote()) == 1
  25. ray.get(actor.set_cuda_visible_devices.remote(""))
  26. assert ray.get(actor.get_count.remote()) == 0