distributed_gpu_executor.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from abc import abstractmethod
  2. from typing import Any, Dict, List, 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(f"# GPU blocks: {num_gpu_blocks}, "
  34. f"# CPU blocks: {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) -> List[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. def save_sharded_state(
  64. self,
  65. path: str,
  66. pattern: Optional[str] = None,
  67. max_size: Optional[int] = None,
  68. ) -> None:
  69. self._run_workers("save_sharded_state",
  70. path=path,
  71. pattern=pattern,
  72. max_size=max_size)
  73. @abstractmethod
  74. def _run_workers(
  75. self,
  76. method: str,
  77. *args,
  78. driver_args: Optional[Tuple[Any, ...]] = None,
  79. driver_kwargs: Optional[Dict[str, Any]] = None,
  80. max_concurrent_workers: Optional[int] = None,
  81. **kwargs,
  82. ) -> Any:
  83. """Runs the given method on all workers."""
  84. raise NotImplementedError
  85. class DistributedGPUExecutorAsync(DistributedGPUExecutor, ExecutorAsyncBase):
  86. @abstractmethod
  87. async def _run_workers_async(
  88. self,
  89. method: str,
  90. *args,
  91. driver_args: Optional[Tuple[Any, ...]] = None,
  92. driver_kwargs: Optional[Dict[str, Any]] = None,
  93. **kwargs,
  94. ) -> Any:
  95. """Runs the given method on all workers."""
  96. raise NotImplementedError
  97. async def execute_model_async(self, *args,
  98. **kwargs) -> List[SamplerOutput]:
  99. all_outputs = await self._run_workers_async("execute_model",
  100. driver_args=args,
  101. driver_kwargs=kwargs)
  102. # Only the driver worker returns the sampling results.
  103. return all_outputs[0]