test_aux_functions.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import subprocess
  2. import sys
  3. import pytest
  4. import torch
  5. from petals import AutoDistributedConfig
  6. from petals.server.throughput import measure_compute_rps
  7. from petals.utils.convert_block import QuantType
  8. from test_utils import MODEL_NAME
  9. def test_bnb_not_imported_when_unnecessary():
  10. """
  11. We avoid importing bitsandbytes when it's not used,
  12. since bitsandbytes doesn't always find correct CUDA libs and may raise exceptions because of that.
  13. If this test fails, please change your code to import bitsandbytes and/or petals.utils.peft
  14. in the function's/method's code when it's actually needed instead of importing them in the beginning of the file.
  15. This won't slow down the code - importing a module for the 2nd time doesn't rerun module code.
  16. """
  17. subprocess.check_call([sys.executable, "-c", "import petals, sys; assert 'bitsandbytes' not in sys.modules"])
  18. @pytest.mark.forked
  19. @pytest.mark.parametrize("inference", [False, True])
  20. @pytest.mark.parametrize("n_tokens", [1, 16])
  21. @pytest.mark.parametrize("tensor_parallel", [False, True])
  22. def test_compute_throughput(inference: bool, n_tokens: int, tensor_parallel: bool):
  23. config = AutoDistributedConfig.from_pretrained(MODEL_NAME)
  24. tensor_parallel_devices = ("cpu", "cpu") if tensor_parallel else ()
  25. compute_rps = measure_compute_rps(
  26. config,
  27. device=torch.device("cpu"),
  28. dtype=torch.bfloat16,
  29. quant_type=QuantType.NONE,
  30. tensor_parallel_devices=tensor_parallel_devices,
  31. n_tokens=n_tokens,
  32. n_steps=5,
  33. inference=inference,
  34. )
  35. assert isinstance(compute_rps, float) and compute_rps > 0