o
    `۷i\                     @   s   U d dl Z d dlZd dlmZmZmZmZmZmZm	Z	m
Z
 d dlZd dlmZ i Zeedf ed< e	dZe	dZded	ed
efddZG dd dejjeeef ZdS )    N)AnyDictGenericListTupleTypeTypeVarUnion)DAGNodeBase_PyObjScanner
_instances
SourceTypeTransformedTypeinstance_id
node_indexreturnc                 C   s   t |  |S )zGet the node instance.

    Note: This function should be static and globally importable,
    otherwise the serialization overhead would be very significant.
    )r   _replace_index)r   r    r   L/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/dag/py_obj_scanner.py	_get_node   s   r   c                       s   e Zd ZdZefdeeef f fddZ fddZ	de
dee fd	d
Zdeeef de
fddZdedefddZdd Zdd Z  ZS )r   a  Utility to find and replace the `source_type` in Python objects.

    `source_type` can either be a single type or a tuple of multiple types.

    The caller must first call `find_nodes()`, then compute a replacement table and
    pass it to `replace_nodes`.

    This uses cloudpickle under the hood, so all sub-objects that are not `source_type`
    must be serializable.

    Args:
        source_type: the type(s) of object to find and replace. Default to DAGNodeBase.
    source_typec                    s@   || _ t | _d | _g | _d | _| tt| < t	 
| j d S N)r   ioBytesIO_buf_found_objects_replace_tabler   idsuper__init__)selfr   	__class__r   r   r    (   s   
z_PyObjScanner.__init__c                    s>   t || jrt| j}| j| tt| |ffS t |S )zHook for reducing objects.

        Objects of `self.source_type` are saved to `self._found` and a global map so
        they can later be replaced.

        All other objects fall back to the default `CloudPickler` serialization.
        )	
isinstancer   lenr   appendr   r   r   reducer_override)r!   objindexr"   r   r   r'   7   s
   
z_PyObjScanner.reducer_overrider(   r   c                 C   s.   | j du s	J dg | _ g | _| | | j S )z
        Serialize `obj` and store all instances of `source_type` found in `_found`.

        Args:
            obj: The object to scan for `source_type`.
        Returns:
            A list of all instances of `source_type` found in `obj`.
        NzDfind_nodes cannot be called twice on the same PyObjScanner instance.)r   r   dump)r!   r(   r   r   r   
find_nodesF   s   

z_PyObjScanner.find_nodestablec                 C   s0   | j dus	J d|| _| jd t| jS )z6Replace previously found DAGNodes per the given table.Nzfind_nodes must be called firstr   )r   r   r   seekpickleload)r!   r,   r   r   r   replace_nodesW   s   z_PyObjScanner.replace_nodesic                 C   s   | j | j|  S r   )r   r   )r!   r1   r   r   r   r   ^   s   z_PyObjScanner._replace_indexc                 C   s   t | tv rtt | = dS dS )z%Clear the scanner from the _instancesN)r   r   r!   r   r   r   cleara   s   z_PyObjScanner.clearc                 C   s   |    d S r   )r3   r2   r   r   r   __del__f   s   z_PyObjScanner.__del__)__name__
__module____qualname____doc__r
   r	   r   r   r    r'   r   r   r   r+   r   r   r0   intr   r3   r4   __classcell__r   r   r"   r   r      s    )r   r.   typingr   r   r   r   r   r   r   r	   rayray.dag.baser
   r   r9   __annotations__r   r   r   cloudpickleCloudPicklerr   r   r   r   r   <module>   s   
 ("	