1
0

gpu_executor.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. from typing import Any, Dict, List, Optional, Set, Tuple, Union
  2. from loguru import logger
  3. from aphrodite.common.sequence import (ExecuteModelRequest, PoolerOutput,
  4. SamplerOutput)
  5. from aphrodite.common.utils import (get_distributed_init_method, get_ip,
  6. get_open_port, make_async)
  7. from aphrodite.executor.executor_base import ExecutorAsyncBase, ExecutorBase
  8. from aphrodite.lora.request import LoRARequest
  9. from aphrodite.prompt_adapter.request import PromptAdapterRequest
  10. from aphrodite.task_handler.worker_base import WorkerWrapperBase
  11. def create_worker(worker_module_name, worker_class_name, **kwargs):
  12. wrapper = WorkerWrapperBase(
  13. worker_module_name=worker_module_name,
  14. worker_class_name=worker_class_name,
  15. )
  16. wrapper.init_worker(**kwargs)
  17. return wrapper.worker
  18. class GPUExecutor(ExecutorBase):
  19. uses_ray: bool = False
  20. def _init_executor(self) -> None:
  21. """Initialize the worker and load the model.
  22. """
  23. assert self.parallel_config.world_size == 1, (
  24. "GPUExecutor only supports single GPU.")
  25. self.driver_worker = self._create_worker()
  26. self.driver_worker.init_device()
  27. self.driver_worker.load_model()
  28. def _get_worker_kwargs(
  29. self,
  30. local_rank: int = 0,
  31. rank: int = 0,
  32. distributed_init_method: Optional[str] = None) -> Dict[str, Any]:
  33. """Return worker init args for a given rank."""
  34. if distributed_init_method is None:
  35. distributed_init_method = get_distributed_init_method(
  36. get_ip(), get_open_port())
  37. return dict(
  38. model_config=self.model_config,
  39. parallel_config=self.parallel_config,
  40. scheduler_config=self.scheduler_config,
  41. device_config=self.device_config,
  42. cache_config=self.cache_config,
  43. load_config=self.load_config,
  44. local_rank=local_rank,
  45. rank=rank,
  46. distributed_init_method=distributed_init_method,
  47. lora_config=self.lora_config,
  48. multimodal_config=self.multimodal_config,
  49. speculative_config=self.speculative_config,
  50. prompt_adapter_config=self.prompt_adapter_config,
  51. is_driver_worker=(not self.parallel_config)
  52. or (rank % self.parallel_config.tensor_parallel_size == 0),
  53. )
  54. def _get_create_worker_kwargs(
  55. self,
  56. local_rank: int = 0,
  57. rank: int = 0,
  58. distributed_init_method: Optional[str] = None) -> Dict:
  59. worker_kwargs = self._get_worker_kwargs(local_rank, rank,
  60. distributed_init_method)
  61. if self.speculative_config is None:
  62. worker_kwargs.update(
  63. worker_module_name="aphrodite.task_handler.worker",
  64. worker_class_name="Worker")
  65. else:
  66. worker_kwargs.update(
  67. worker_module_name="aphrodite.spec_decode.spec_decode_worker",
  68. worker_class_name="create_spec_worker")
  69. return worker_kwargs
  70. def _create_worker(self,
  71. local_rank: int = 0,
  72. rank: int = 0,
  73. distributed_init_method: Optional[str] = None):
  74. return create_worker(**self._get_create_worker_kwargs(
  75. local_rank=local_rank,
  76. rank=rank,
  77. distributed_init_method=distributed_init_method))
  78. def determine_num_available_blocks(self) -> Tuple[int, int]:
  79. """Determine the number of available KV blocks by invoking the
  80. underlying worker.
  81. """
  82. return self.driver_worker.determine_num_available_blocks()
  83. def initialize_cache(self, num_gpu_blocks: int, num_cpu_blocks) -> None:
  84. """Initialize the KV cache by invoking the underlying worker.
  85. """
  86. # NOTE: This is logged in the executor because there can be >1 worker
  87. # with other executors. We could log in the engine level, but work
  88. # remains to abstract away the device for non-GPU configurations.
  89. logger.info(f"# GPU blocks: {num_gpu_blocks}, "
  90. f"# CPU blocks: {num_cpu_blocks}")
  91. logger.info(
  92. f"Minimum concurrency: {num_gpu_blocks * self.cache_config.block_size / self.scheduler_config.max_model_len:.2f}x" # noqa: E501
  93. )
  94. self.driver_worker.initialize_cache(num_gpu_blocks, num_cpu_blocks)
  95. def execute_model(
  96. self, execute_model_req: ExecuteModelRequest
  97. ) -> Optional[List[Union[SamplerOutput, PoolerOutput]]]:
  98. output = self.driver_worker.execute_model(execute_model_req)
  99. return output
  100. def add_lora(self, lora_request: LoRARequest) -> bool:
  101. assert lora_request.lora_int_id > 0, "lora_id must be greater than 0."
  102. return self.driver_worker.add_lora(lora_request)
  103. def remove_lora(self, lora_id: int) -> bool:
  104. assert lora_id > 0, "lora_id must be greater than 0."
  105. return self.driver_worker.remove_lora(lora_id)
  106. def list_loras(self) -> Set[int]:
  107. return self.driver_worker.list_loras()
  108. def pin_lora(self, lora_id: int) -> bool:
  109. assert lora_id > 0, "lora_id must be greater than 0."
  110. return self.driver_worker.pin_lora(lora_id)
  111. def add_prompt_adapter(
  112. self, prompt_adapter_request: PromptAdapterRequest) -> bool:
  113. assert prompt_adapter_request.prompt_adapter_id > 0, \
  114. "prompt_adapter_id must be greater than 0."
  115. return self.driver_worker.add_prompt_adapter(prompt_adapter_request)
  116. def remove_prompt_adapter(self, prompt_adapter_id: int) -> bool:
  117. assert prompt_adapter_id > 0, \
  118. "prompt_adapter_id must be greater than 0."
  119. return self.driver_worker.remove_prompt_adapter(prompt_adapter_id)
  120. def pin_prompt_adapter(self, prompt_adapter_id: int) -> bool:
  121. assert prompt_adapter_id > 0, \
  122. "prompt_adapter_id must be greater than 0."
  123. return self.driver_worker.pin_prompt_adapter(prompt_adapter_id)
  124. def list_prompt_adapters(self) -> Set[int]:
  125. return self.driver_worker.list_prompt_adapters()
  126. def check_health(self) -> None:
  127. # GPUExecutor will always be healthy as long as
  128. # it's running.
  129. return
  130. class GPUExecutorAsync(GPUExecutor, ExecutorAsyncBase):
  131. async def execute_model_async(
  132. self,
  133. execute_model_req: ExecuteModelRequest,
  134. ) -> List[Union[SamplerOutput, PoolerOutput]]:
  135. output = await make_async(self.driver_worker.execute_model
  136. )(execute_model_req=execute_model_req, )
  137. return output