gpu_executor.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. speculative_config=self.speculative_config,
  49. prompt_adapter_config=self.prompt_adapter_config,
  50. is_driver_worker=(not self.parallel_config)
  51. or (rank % self.parallel_config.tensor_parallel_size == 0),
  52. )
  53. def _get_worker_module_and_class(self) -> Tuple[str, str]:
  54. if self.scheduler_config.is_multi_step:
  55. worker_module_name = "aphrodite.task_handler.multi_step_worker"
  56. worker_class_name = "MultiStepWorker"
  57. elif self.speculative_config:
  58. worker_module_name = "aphrodite.spec_decode.spec_decode_worker"
  59. worker_class_name = "create_spec_worker"
  60. else:
  61. worker_module_name = "aphrodite.task_handler.worker"
  62. worker_class_name = "Worker"
  63. return (worker_module_name, worker_class_name)
  64. def _get_create_worker_kwargs(
  65. self,
  66. local_rank: int = 0,
  67. rank: int = 0,
  68. distributed_init_method: Optional[str] = None) -> Dict:
  69. worker_kwargs = self._get_worker_kwargs(local_rank, rank,
  70. distributed_init_method)
  71. (worker_module_name,
  72. worker_class_name) = self._get_worker_module_and_class()
  73. worker_kwargs.update(worker_module_name=worker_module_name,
  74. worker_class_name=worker_class_name)
  75. return worker_kwargs
  76. def _create_worker(self,
  77. local_rank: int = 0,
  78. rank: int = 0,
  79. distributed_init_method: Optional[str] = None):
  80. return create_worker(**self._get_create_worker_kwargs(
  81. local_rank=local_rank,
  82. rank=rank,
  83. distributed_init_method=distributed_init_method))
  84. def determine_num_available_blocks(self) -> Tuple[int, int]:
  85. """Determine the number of available KV blocks by invoking the
  86. underlying worker.
  87. """
  88. return self.driver_worker.determine_num_available_blocks()
  89. def initialize_cache(self, num_gpu_blocks: int, num_cpu_blocks) -> None:
  90. """Initialize the KV cache by invoking the underlying worker.
  91. """
  92. # NOTE: This is logged in the executor because there can be >1 worker
  93. # with other executors. We could log in the engine level, but work
  94. # remains to abstract away the device for non-GPU configurations.
  95. logger.info(f"# GPU blocks: {num_gpu_blocks}, "
  96. f"# CPU blocks: {num_cpu_blocks}")
  97. logger.info(
  98. f"Minimum concurrency: {num_gpu_blocks * self.cache_config.block_size / self.scheduler_config.max_model_len:.2f}x" # noqa: E501
  99. )
  100. self.driver_worker.initialize_cache(num_gpu_blocks, num_cpu_blocks)
  101. def execute_model(
  102. self, execute_model_req: ExecuteModelRequest
  103. ) -> Optional[List[Union[SamplerOutput, PoolerOutput]]]:
  104. output = self.driver_worker.execute_model(execute_model_req)
  105. return output
  106. def add_lora(self, lora_request: LoRARequest) -> bool:
  107. assert lora_request.lora_int_id > 0, "lora_id must be greater than 0."
  108. return self.driver_worker.add_lora(lora_request)
  109. def remove_lora(self, lora_id: int) -> bool:
  110. assert lora_id > 0, "lora_id must be greater than 0."
  111. return self.driver_worker.remove_lora(lora_id)
  112. def list_loras(self) -> Set[int]:
  113. return self.driver_worker.list_loras()
  114. def pin_lora(self, lora_id: int) -> bool:
  115. assert lora_id > 0, "lora_id must be greater than 0."
  116. return self.driver_worker.pin_lora(lora_id)
  117. def add_prompt_adapter(
  118. self, prompt_adapter_request: PromptAdapterRequest) -> bool:
  119. assert prompt_adapter_request.prompt_adapter_id > 0, \
  120. "prompt_adapter_id must be greater than 0."
  121. return self.driver_worker.add_prompt_adapter(prompt_adapter_request)
  122. def remove_prompt_adapter(self, prompt_adapter_id: int) -> bool:
  123. assert prompt_adapter_id > 0, \
  124. "prompt_adapter_id must be greater than 0."
  125. return self.driver_worker.remove_prompt_adapter(prompt_adapter_id)
  126. def pin_prompt_adapter(self, prompt_adapter_id: int) -> bool:
  127. assert prompt_adapter_id > 0, \
  128. "prompt_adapter_id must be greater than 0."
  129. return self.driver_worker.pin_prompt_adapter(prompt_adapter_id)
  130. def list_prompt_adapters(self) -> Set[int]:
  131. return self.driver_worker.list_prompt_adapters()
  132. def check_health(self) -> None:
  133. # GPUExecutor will always be healthy as long as
  134. # it's running.
  135. return
  136. class GPUExecutorAsync(GPUExecutor, ExecutorAsyncBase):
  137. async def execute_model_async(
  138. self,
  139. execute_model_req: ExecuteModelRequest,
  140. ) -> List[Union[SamplerOutput, PoolerOutput]]:
  141. output = await make_async(self.driver_worker.execute_model
  142. )(execute_model_req=execute_model_req, )
  143. return output