1
0

xpu_executor.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from typing import List, Optional, Tuple, Union
  2. import torch
  3. from loguru import logger
  4. from aphrodite.common.config import (CacheConfig, DeviceConfig, LoadConfig,
  5. LoRAConfig, ModelConfig, ParallelConfig,
  6. PromptAdapterConfig, SchedulerConfig,
  7. SpeculativeConfig)
  8. from aphrodite.common.sequence import (ExecuteModelRequest, PoolerOutput,
  9. SamplerOutput)
  10. from aphrodite.common.utils import make_async
  11. from aphrodite.executor.executor_base import ExecutorAsyncBase
  12. from aphrodite.executor.gpu_executor import GPUExecutor
  13. class XPUExecutor(GPUExecutor):
  14. uses_ray: bool = False
  15. def __init__(
  16. self,
  17. model_config: ModelConfig,
  18. cache_config: CacheConfig,
  19. parallel_config: ParallelConfig,
  20. scheduler_config: SchedulerConfig,
  21. device_config: DeviceConfig,
  22. load_config: LoadConfig,
  23. lora_config: Optional[LoRAConfig],
  24. prompt_adapter_config: Optional[PromptAdapterConfig],
  25. speculative_config: Optional[SpeculativeConfig],
  26. ) -> None:
  27. assert device_config.device_type == "xpu"
  28. assert (not speculative_config
  29. ), "Speculative decoding not yet supported for XPU backend"
  30. model_config = _verify_and_get_model_config(model_config)
  31. self.model_config = model_config
  32. self.cache_config = cache_config
  33. self.load_config = load_config
  34. self.lora_config = lora_config
  35. self.parallel_config = parallel_config
  36. self.scheduler_config = scheduler_config
  37. self.device_config = device_config
  38. self.prompt_adapter_config = prompt_adapter_config
  39. self.speculative_config = None
  40. # Instantiate the worker and load the model to GPU.
  41. self._init_executor()
  42. def _get_worker_module_and_class(self) -> Tuple[str, str]:
  43. if self.speculative_config is not None:
  44. raise NotImplementedError(
  45. "XPU does not support speculative decoding")
  46. else:
  47. worker_module_name = "aphrodite.task_handler.xpu_worker"
  48. worker_class_name = "XPUWorker"
  49. return (worker_module_name, worker_class_name)
  50. def execute_model(
  51. self, execute_model_req: ExecuteModelRequest
  52. ) -> Optional[List[Union[SamplerOutput, PoolerOutput]]]:
  53. output = self.driver_worker.execute_model(execute_model_req)
  54. return output
  55. class XPUExecutorAsync(XPUExecutor, ExecutorAsyncBase):
  56. async def execute_model_async(
  57. self,
  58. execute_model_req: ExecuteModelRequest,
  59. ) -> List[SamplerOutput]:
  60. output = await make_async(self.driver_worker.execute_model
  61. )(execute_model_req=execute_model_req)
  62. return output
  63. def _verify_and_get_model_config(config: ModelConfig) -> ModelConfig:
  64. if config.dtype == torch.bfloat16:
  65. logger.warning(
  66. "bfloat16 is not fully supported on XPU, casting to float16.")
  67. config.dtype = torch.float16
  68. if not config.enforce_eager:
  69. logger.warning(
  70. "CUDA graph is not supported on XPU, fallback to the eager "
  71. "mode.")
  72. config.enforce_eager = True
  73. return config