o
    ۷ia#                     @   s
  d dl Z d dlZd dlZd dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZmZ d dl m!Z! d dl"m#Z# d dl$m%Z% erw	 e	e&Z'G dd deZ(dS )    N)TYPE_CHECKING)
VllmConfig)init_logger)MULTIMODAL_REGISTRYMultiModalRegistry)cached_tokenizer_from_config)init_tracer)(maybe_register_config_serialize_by_value)UsageContext)deprecate_kwargs)AsyncLLM)EngineCoreClient)Executor)StatLoggerFactoryStatLoggerManager)AsyncOmniEngineArgs)OmniInputProcessor)MultimodalOutputProcessorc                   @   s   e Zd ZdZejedddddddf	deded	e	e
 d
ededededededee dB deeef dB dededdfddZeeddddejdddddddf	dededededee dB dededeeef dB dedededdfddZdS )AsyncOmniLLMa  Async single-stage LLM engine for use within a stage worker process.

    This class extends the base vLLM AsyncLLM class with omni-specific
    processors for handling multimodal inputs and outputs. It is used
    internally by AsyncOmniStage workers and should not be instantiated
    directly by users.

    Args:
        engine_args: AsyncOmniEngineArgs containing engine configuration
        vllm_config: Global vLLM configuration
        executor_class: Executor implementation class, e.g. MultiprocExecutor
        log_stats: Whether to log statistics
        usage_context: Usage context of the LLM (default: ENGINE_CONTEXT)
        mm_registry: Multi-modal registry for processing multimodal inputs
        use_cached_outputs: Whether to use cached outputs
        log_requests: Whether to log requests
        start_engine_loop: Whether to start the engine loop automatically
        stat_loggers: Customized stat loggers for the engine.
            If not provided, default stat loggers will be used.
            Note: Stat logger interface may change in V1.
        client_addresses: Optional dictionary mapping client names to addresses
        client_count: Total number of clients (default: 1)
        client_index: Index of this client (default: 0)
    FTN   r   engine_argsvllm_configexecutor_class	log_statsusage_contextmm_registryuse_cached_outputslog_requestsstart_engine_loopstat_loggersclient_addressesclient_countclient_indexreturnc                 C   s  t   |j| _|| _|j| _|| _|p|
du| _|s#|
dur#td | jjr*d}nt	|jd}t
||d| _t|| j|jd| _| jjdurRtd| jj}|| j_t | _d| _tj||| j|||d| _d| _| jr~t|| jj|
||d	| _| j  d| _z
t  |   W n	 t y   Y nw |j!j"d
kr|j!j#s|j!j$}td| t%&  dt'(  d}t)j"j*t)j"j+j,g|j!j-t)j"j.|||j!j/dd| _"dS d| _"dS )a  
        Create an AsyncOmniLLM.

        Args:
            vllm_config: global configuration.
            executor_class: an Executor impl, e.g. MultiprocExecutor.
            log_stats: Whether to log stats.
            usage_context: Usage context of the LLM.
            mm_registry: Multi-modal registry.
            use_cached_outputs: Whether to use cached outputs.
            log_requests: Whether to log requests.
            start_engine_loop: Whether to start the engine loop.
            stat_loggers: customized stat loggers for the engine.
                If not provided, default stat loggers will be used.
                PLEASE BE AWARE THAT STAT LOGGER IS NOT STABLE
                IN V1, AND ITS BASE CLASS INTERFACE MIGHT CHANGE.

        Returns:
            None
        NzuAsyncLLM created with log_stats=False and non-empty custom logger list; enabling logging without default stat loggers)model_config)r   r   )	tokenizerr   engine_core_output_typezvllm.llm_engineF)r   r   r   r    r!   r"   )r   engine_idxscustom_stat_loggersenable_default_loggersr!   torchzJTorch profiler enabled. AsyncOmniLLM CPU traces will be collected under %s_z.async_omni_llm)worker_nameuse_gzip)
activities
with_stackon_trace_ready)0r	   r$   r   observability_configr   r   loggerinfoskip_tokenizer_initr   r   input_processorr   engine_output_typeoutput_processorotlp_traces_endpointr   tracerasyncio	Condition_pause_cond_pausedr   make_async_mp_clientengine_corelogger_managerr   engine_ranks_managedlog_engine_initializedoutput_handlerget_running_loop_run_output_handlerRuntimeErrorprofiler_configprofilerignore_frontendtorch_profiler_dirsocketgethostnameosgetpidr*   profileProfilerActivityCPUtorch_profiler_with_stacktensorboard_trace_handlertorch_profiler_use_gzip)selfr   r   r   r   r   r   r   r   r   r   r    r!   r"   r%   r9   profiler_dirr,    rW   Z/home/ubuntu/vllm_env/lib/python3.10/site-packages/vllm_omni/entrypoints/async_omni_llm.py__init__:   s   %



zAsyncOmniLLM.__init__disable_log_requestszEThis argument will have no effect. Use `enable_log_requests` instead.)additional_messageenable_log_requestsdisable_log_statsr   c                 C   s&   | |t ||||| |||	|
|dS )N)r   r   r   r   r   r   r   r    r!   r"   r   )r   	get_class)clsr   r   r   r   r   r\   r]   r    r!   r"   rZ   rW   rW   rX   from_vllm_config   s   zAsyncOmniLLM.from_vllm_config)__name__
__module____qualname____doc__r
   ENGINE_CONTEXTr   r   r   typer   boolr   listr   dictstrintrY   classmethodr   r`   rW   rW   rW   rX   r       s    	


 
	
r   ))r:   rM   rK   typingr   r*   vllm.configr   vllm.loggerr   vllm.multimodalr   r   vllm.tokenizersr   vllm.tracingr   vllm.transformers_utils.configr	   vllm.usage.usage_libr
   vllm.utils.func_utilsr   vllm.v1.engine.async_llmr   vllm.v1.engine.core_clientr   vllm.v1.executor.abstractr   vllm.v1.metrics.loggersr   r   vllm_omni.engine.arg_utilsr    vllm_omni.engine.input_processorr   !vllm_omni.engine.output_processorr   ra   r2   r   rW   rW   rW   rX   <module>   s0   