o
    TÃiž  ã                   @   s`   d dl mZ d dlmZ d dlmZ d dlZd dlmZ G dd„ dƒZ	e	j
Z
dejfd	d
„ZdS )é    )Úreduce)ÚIterable)ÚdefaultdictN)Úget_acceleratorc                   @   s2   e Zd ZeeƒZdejdee	 dejfdd„Z
dS )Ú	AllocatorÚtensorÚshapeÚreturnc                 C   sp   zt j|  | W S  ty7   tdd„ |ƒ}|dkrtdƒ‚|  ¡ d |…  |¡t j|  |< t j|  |  Y S w )Nc                 S   s   | | S ©N© )ÚxÚyr   r   úT/home/ubuntu/.local/lib/python3.10/site-packages/deepspeed/inference/v2/allocator.pyÚ<lambda>   s    z&Allocator.empty_from.<locals>.<lambda>r   z&Cannot create empty tensor with size 0)r   ÚcacheÚKeyErrorr   Ú
ValueErrorÚflattenÚview)r   r   Ú
shape_sizer   r   r   Ú
empty_from   s    ûzAllocator.empty_fromN)Ú__name__Ú
__module__Ú__qualname__r   Údictr   ÚtorchÚTensorr   Úintr   r   r   r   r   r      s    "r   r	   c                    s   ‡ fdd„}|S )zP
    Wraps a method to ensure the returned tensor is on the current device.
    c                    s6   ˆ | g|¢R i |¤Ž}t |tjƒr| tƒ  ¡ ¡S |S r
   )Ú
isinstancer   r   Útor   Úcurrent_device)ÚselfÚargsÚkwargsr   ©Úmethodr   r   Úwrapped$   s   zon_device.<locals>.wrappedr   )r%   r&   r   r$   r   Ú	on_device   s   r'   )Ú	functoolsr   Útypingr   Úcollectionsr   r   Údeepspeed.acceleratorr   r   r   r   r'   r   r   r   r   Ú<module>   s   