gpu_executor.py 6.5 KB

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