
    .`i#                     @   U d dl Z d dlZd dlmZ d dlm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 d dlmZ  ee          Zdeed	f         d
ej        defdZdZdZdaed         ed<    G d d          ZdefdZddZ	 ddej        dedz  ddfdZ ddZ!ddZ"dS )    N)
accumulate)prod)Optional)init_loggerround_up)dbo_current_ubatch_idshape.dtypereturnc                 0    t          |           |j        z  S N)r   itemsize)r
   r   s     l/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/v1/worker/workspace.py_compute_bytesr      s    ;;''    i   i   @WorkspaceManager_managerc                       e Zd ZdZddej        dedz  fdZedej	        dz  defd            Z
dd	Zdefd
Zdeeedf         ej        f         deej	                 fdZdedej	        fdZdS )r   zManager for workspace allocation.

    Manages workspace buffers for DBO (Dual Batch Overlap) execution.
    Can be locked to prevent further growth during execution.
    Ndevicenum_ubatchesc                 J    || _         ||nd| _        d d g| _        d| _        d S )N   F)_device_num_ubatches_current_workspaces_locked)selfr   r   s      r   __init__zWorkspaceManager.__init__'   s1    -9-E\\1?CTl "r   	workspacer   c                 \    | dS |                                  |                                 z  S )zGet size of workspace in bytes.Nr   )numelelement_size)r    s    r   _workspace_size_bytesz&WorkspaceManager._workspace_size_bytes.   s0     1  9#9#9#;#;;;r   c                      d _         t          j        r.t                              d fd j        D                        dS dS )zLock the workspace to prevent further growth.

        After locking, any attempt to allocate a larger workspace will raise
        an assertion error. This ensures workspace size is fixed during execution.
        Tz5[WORKSPACE DEBUG] Workspace locked. Current sizes: %sc                 N    g | ]!}|                     |          t          z  "S r   )r$   _MB).0wsr   s     r   
<listcomp>z)WorkspaceManager.lock.<locals>.<listcomp>?   s9       ~ ..r22S8%~~r   N)r   envsVLLM_DEBUG_WORKSPACEloggerinfor   r   s   `r   lockzWorkspaceManager.lock5   sm     $ 	KKG   "6      	 	r   c                     | j         S )zCheck if workspace is locked.)r   r/   s    r   	is_lockedzWorkspaceManager.is_lockedF   s
    |r   shapes_and_dtypes.c                 (   d D             d D             }t          |          }t          t          dg|dd         z                       |                     |          fdt	          t                              D             S )a
  Get multiple workspace tensors simultaneously from a single allocation.

        Args:
            *shapes_and_dtypes: One or more (shape, dtype) tuples.

        Returns:
            List of tensor views into the workspace buffer, one per shape/dtype pair.
        c                 4    g | ]\  }}t          ||          S  )r   )r(   sds      r   r*   z5WorkspaceManager.get_simultaneous.<locals>.<listcomp>U   s&    KKKAq!,,KKKr   c                 .    g | ]}t          |d           S )   r   )r(   actuals     r   r*   z5WorkspaceManager.get_simultaneous.<locals>.<listcomp>V   s"    JJJ6&#..JJJr   r   Nc                     g | ]_}|         |         |         z                                 |         d                                        |         d                   `S )r   r   )viewreshape)r(   iactual_bytescurrent_workspaceoffsetsr3   s     r   r*   z5WorkspaceManager.get_simultaneous.<locals>.<listcomp>^   sv     
 
 
  gaj71:Q+GGHT#A&q)**W&q)!,--
 
 
r   )sumlistr   _ensure_workspace_sizerangelen)r   r3   aligned_bytestotal_bytesrA   rB   rC   s    `  @@@r   get_simultaneousz!WorkspaceManager.get_simultaneousJ   s     LK9JKKKJJ\JJJ-(( z1#crc(:":;;<< 77DD
 
 
 
 
 
 
 301122	
 
 
 	
r   required_bytesc           	         t                      }| j        |         }|                     |          }||k     r/dt          fd}| j        r3t          d |             d|t          z  dd|t          z  dd          t          | j                  D ]d}| j        |         }||                     |          |k     r:d| j        |<   ~t          j
        |ft          j        | j        	          | j        |<   et          j        rLt                              d
 |            |t          z  |t          z  | j        || j        z  t          z             | j        t                               }|S )zEnsure workspace is allocated and large enough, return current workspace.

        Args:
            required_bytes: The number of bytes required.

        Returns:
            The current workspace tensor.
        r   c                  4   t          j                    } | dS | j        } | wt          | j                            d          t                    r| j        } 7t          j        	                    | j
        j                  }| d| j         d| j
        j         S dS )z*Find first frame outside WorkspaceManager.Nunknownr   :)inspectcurrentframef_back
isinstancef_localsgetr   ospathbasenamef_codeco_filenamef_linenoco_name)
curr_framefilenames     r   get_caller_infoz@WorkspaceManager._ensure_workspace_size.<locals>.get_caller_infot   s    $133
%$9'.
 , "*"5"9"9&"A"ACSTT !%/%6
 !w//
0A0MNNH#WWj&9WWJ<M<UWW !yr   z)Workspace is locked but allocation from 'z' requires z.2fz MB, current size is z3 MB. Workspace growth is not allowed after locking.N)r   r   ze[WORKSPACE DEBUG] Resized workspace from '%s': %.2f MB -> %.2f MB (%d ubatches, total memory %.2f MB))r	   r   r$   strr   AssertionErrorr'   rG   r   torchemptyuint8r   r+   r,   r-   r.   )r   rL   	ubatch_idrB   current_sizer`   s         r   rF   z'WorkspaceManager._ensure_workspace_sizee   s    *++	 4Y?112CDD.((!S ! ! ! !, | $E@Q@Q E E . 4OE E#c)4E E E   #4#566  	$($<Y$G!%-112CDD~UU ;?D,Y7):?+')T\; ; ;D,Y7 ( 	B#O%% 3&"S(&"T%77#=   !% 89N9P9P Q  r   r   r   N)__name__
__module____qualname____doc__rc   r   intr   staticmethodTensorr$   r0   boolr2   tupler   rE   rK   rF   r6   r   r   r   r       s        # #u| #3: # # # # <)< < < < < \<   "4    
"'c3h(D"E
	el	
 
 
 
6K!S K!U\ K! K! K! K! K! K!r   c                      t           duS )zCheck if workspace manager has been initialized.

    Returns:
        True if workspace manager is initialized, False otherwise.
    Nr   r6   r   r    is_workspace_manager_initializedrt      s     4r   c                  2    t           
J d            t           S )zGet the current workspace manager instance.

    Raises:
        AssertionError: If workspace manager has not been initialized.
    NzoWorkspaceManager not initialized. Call init_workspace_manager() with a device before using workspace functions.rs   r6   r   r   current_workspace_managerrv      s$     	:   Or   r   r   c                     t           &t                              dt           j        |            t	          | |          a dS )a  Initialize the workspace manager with a device.

    Must be called before using any workspace functions. Typically called
    from GPUModelRunner.__init__.

    Args:
        device: The device to allocate workspace on.
        num_ubatches: Number of micro-batches. Defaults to 1.
    NzNWorkspaceManager already initialized on device %s, reinitializing on device %s)r   r-   warningr   r   )r   r   s     r   init_workspace_managerry      sC     *		
 	
 	
  55HHHr   c                  F    t                                                       dS )ae  Lock the workspace to prevent further growth.

    After calling this function, any attempt to allocate a workspace larger
    than the current size will raise an AssertionError. This ensures that
    workspace size is fixed during execution and prevents unexpected memory
    allocations in the hot path.

    Example:
        # During initialization
        init_workspace_manager(device)
        reserve_workspace(shape1, dtype1)
        reserve_workspace(shape2, dtype2)

        # Lock after warmup/profiling
        lock_workspace()

        # Now all get_workspace calls must fit in pre-allocated size
    N)rv   r0   r6   r   r   lock_workspacer{      s"    & $$&&&&&r   c                  
    da dS )zReset the workspace manager to uninitialized state.

    This is primarily intended for testing purposes to allow tests
    to reinitialize the workspace manager cleanly.
    Nrs   r6   r   r   reset_workspace_managerr}      s     HHHr   )r   r   r   rh   )#rQ   rW   	itertoolsr   mathr   typingr   rc   	vllm.envsr+   vllm.loggerr   vllm.utils.math_utilsr   vllm.v1.worker.ubatchingr	   ri   r-   rq   rm   r   r   r'   _GiBr   __annotations__r   rp   rt   rv   r   ry   r{   r}   r6   r   r   <module>r      s    				                                # # # # # # * * * * * * : : : : : :	X		(%S/ (%+ (# ( ( ( (
  *.(%
& - - -P! P! P! P! P! P! P! P!f $        
 
 
 
 6:6 6L6(+d
6	6 6 6 6.' ' ' ',     r   