distributed_gpu_executor.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from abc import abstractmethod
  2. from typing import Any, Dict, Optional, Set, Tuple
  3. from loguru import logger
  4. from aphrodite.executor.executor_base import ExecutorAsyncBase
  5. from aphrodite.executor.gpu_executor import GPUExecutor
  6. from aphrodite.lora.request import LoRARequest
  7. from aphrodite.common.sequence import SamplerOutput
  8. class DistributedGPUExecutor(GPUExecutor):
  9. """Abstract superclass of multi-GPU executor implementations."""
  10. def determine_num_available_blocks(self) -> Tuple[int, int]:
  11. """Determine the number of available KV blocks.
  12. This invokes `determine_num_available_blocks` on each worker and takes
  13. the min of the results, guaranteeing that the selected cache sizes are
  14. compatible with all workers.
  15. Returns:
  16. - tuple[num_gpu_blocks, num_cpu_blocks]
  17. """
  18. # Get the maximum number of blocks that can be allocated on GPU and CPU.
  19. num_blocks = self._run_workers("determine_num_available_blocks", )
  20. # Since we use a shared centralized controller, we take the minimum
  21. # number of blocks across all workers to make sure all the memory
  22. # operators can be applied to all workers.
  23. num_gpu_blocks = min(b[0] for b in num_blocks)
  24. num_cpu_blocks = min(b[1] for b in num_blocks)
  25. return num_gpu_blocks, num_cpu_blocks
  26. def initialize_cache(self, num_gpu_blocks: int,
  27. num_cpu_blocks: int) -> None:
  28. """Initialize the KV cache in all workers.
  29. """
  30. # NOTE: We log here to avoid multiple logs when number of workers is
  31. # greater than one. We could log in the engine, but not all executors
  32. # have GPUs.
  33. logger.info("# GPU blocks: %d, # CPU blocks: %d", num_gpu_blocks,
  34. num_cpu_blocks)
  35. logger.info(
  36. f"Minimum concurrency: {num_gpu_blocks * self.cache_config.block_size / self.scheduler_config.max_model_len:.2f}x" # noqa: E501
  37. )
  38. self.cache_config.num_gpu_blocks = num_gpu_blocks
  39. self.cache_config.num_cpu_blocks = num_cpu_blocks
  40. self._run_workers("initialize_cache",
  41. num_gpu_blocks=num_gpu_blocks,
  42. num_cpu_blocks=num_cpu_blocks)
  43. def execute_model(self, *args, **kwargs) -> SamplerOutput:
  44. all_outputs = self._run_workers("execute_model",
  45. driver_args=args,
  46. driver_kwargs=kwargs)
  47. # Only the driver worker returns the sampling results.
  48. return all_outputs[0]
  49. def add_lora(self, lora_request: LoRARequest) -> bool:
  50. assert lora_request.lora_int_id > 0, "lora_id must be greater than 0."
  51. return self._run_workers(
  52. "add_lora",
  53. lora_request=lora_request,
  54. )
  55. def remove_lora(self, lora_id: int) -> bool:
  56. assert lora_id > 0, "lora_id must be greater than 0."
  57. return self._run_workers(
  58. "remove_lora",
  59. lora_id=lora_id,
  60. )
  61. def list_loras(self) -> Set[int]:
  62. return self._run_workers("list_loras")
  63. @abstractmethod
  64. def _run_workers(
  65. self,
  66. method: str,
  67. *args,
  68. driver_args: Optional[Tuple[Any, ...]] = None,
  69. driver_kwargs: Optional[Dict[str, Any]] = None,
  70. max_concurrent_workers: Optional[int] = None,
  71. **kwargs,
  72. ) -> Any:
  73. """Runs the given method on all workers."""
  74. raise NotImplementedError
  75. class DistributedGPUExecutorAsync(DistributedGPUExecutor, ExecutorAsyncBase):
  76. @abstractmethod
  77. async def _run_workers_async(
  78. self,
  79. method: str,
  80. *args,
  81. driver_args: Optional[Tuple[Any, ...]] = None,
  82. driver_kwargs: Optional[Dict[str, Any]] = None,
  83. **kwargs,
  84. ) -> Any:
  85. """Runs the given method on all workers."""
  86. raise NotImplementedError
  87. async def execute_model_async(self, *args, **kwargs) -> SamplerOutput:
  88. all_outputs = await self._run_workers_async("execute_model",
  89. driver_args=args,
  90. driver_kwargs=kwargs)
  91. # Only the driver worker returns the sampling results.
  92. return all_outputs[0]