a
    d&                     @  s   d dl mZ d dlmZ d dlmZmZmZmZm	Z	m
Z
mZmZmZmZ d dlZd dlZd dlmZ d dlmZmZmZ d dlmZ edd	d
Zeeee
e e
e df Ze	ee  ZG dd	 d	ejZ eejej!j!e f Z"ejZ#dS )    )annotations)
ModuleType)
AnyCallableListMappingOptionalSequenceTupleTypeTypeVarUnionN)DisableTorchFunctionSubclass)_device_dtype_size)InterpolationModeD	Datapoint)boundc                
      s  e Zd ZU dZded< eddddddd	d
dZedddddddZe	j
jdd e	j
jdd e	j
jdd iZedddddddddZddd fdd Zed!d"d#d$Zed%d" fd&d'Zed(d" fd)d*Zeddd+d, fd-d.Zed/d" fd0d1Zd d"d2d3Zd d"d4d5Zejdd6fd7d8d9d:d d;d<d=Zd(d(d(d(d d>d?d@Zd7d dAdBdCZejd6fd(d(d(d(d7d8d:d dDdEdFZdd7dHdd dIdJdKZej dLddfdMd8dNdOdPd dQdRdSZ!ej ddfdTdUdMdUd8dPdOd dVdWdXZ"ejddfdYdYd8dPdOd dZd[d\Z#ejdfdd8dPd d]d^d_Z$dd(d dadbdcZ%dMd dddedfZ&dMd dgdhdiZ'dMd djdkdlZ(dMd dmdndoZ)dMd dpdqdrZ*ddMdMd dsdtduZ+d(d dvdwdxZ,dMd dydzd{Z-d d"d|d}Z.d d"d~dZ/d d"ddZ0dd7dOd dddZ1  Z2S )r   NzOptional[ModuleType]_Datapoint__Fr   zOptional[torch.dtype]z'Optional[Union[torch.device, str, int]]zOptional[bool]ztorch.Tensor)datadtypedevicerequires_gradreturnc                 C  s4   |d u rt | tjr| jnd}tj| ||d|S )NF)r   r   )
isinstancetorchTensorr   Z	as_tensorrequires_grad_)r   r   r   r    r    j/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchvision/datapoints/_datapoint.py
_to_tensor   s    zDatapoint._to_tensorzType[D]r   )clsothertensorr   c                 C  s   t d S N)NotImplementedError)r#   r$   r%   r    r    r!   	wrap_like    s    zDatapoint.wrap_likec                 C  s   |  ||S r&   r(   r#   inputoutputr    r    r!   <lambda>%       zDatapoint.<lambda>c                 C  s   |  ||S r&   r)   r*   r    r    r!   r-   &   r.   c                 C  s   |S r&   r    r*   r    r    r!   r-   )   r.   r    zCallable[..., torch.Tensor]zTuple[Type[torch.Tensor], ...]zSequence[Any]zOptional[Mapping[str, Any]])functypesargskwargsr   c                   s   t  fdd|D stS t  ||i |p0t } j|}|rrt|d  rr| |d |W  d   S t| r|tj	W  d   S |W  d   S 1 s0    Y  dS )a   For general information about how the __torch_function__ protocol works,
        see https://pytorch.org/docs/stable/notes/extending.html#extending-torch

        TL;DR: Every time a PyTorch operator is called, it goes through the inputs and looks for the
        ``__torch_function__`` method. If one is found, it is invoked with the operator as ``func`` as well as the
        ``args`` and ``kwargs`` of the original call.

        The default behavior of :class:`~torch.Tensor`'s is to retain a custom tensor type. For the :class:`Datapoint`
        use case, this has two downsides:

        1. Since some :class:`Datapoint`'s require metadata to be constructed, the default wrapping, i.e.
           ``return cls(func(*args, **kwargs))``, will fail for them.
        2. For most operations, there is no way of knowing if the input type is still valid for the output.

        For these reasons, the automatic output wrapping is turned off for most operators. The only exceptions are
        listed in :attr:`Datapoint._NO_WRAPPING_EXCEPTIONS`
        c                 3  s   | ]}t  |V  qd S r&   )
issubclass).0tr#   r    r!   	<genexpr>H   r.   z/Datapoint.__torch_function__.<locals>.<genexpr>r   N)
allNotImplementedr   dict_NO_WRAPPING_EXCEPTIONSgetr   Zas_subclassr   r   )r#   r/   r0   r1   r2   r,   wrapperr    r6   r!   __torch_function__,   s    
zDatapoint.__torch_function__str)r2   r   c                   s6   d dd | D }t  d d  d| dS )Nz, c                 s  s    | ]\}}| d | V  qdS )=Nr    )r4   keyvaluer    r    r!   r7   b   r.   z'Datapoint._make_repr.<locals>.<genexpr>))joinitemssuper__repr__)selfr2   Z
extra_repr	__class__r    r!   
_make_repr_   s    zDatapoint._make_reprr   )r   c                 C  s"   t jd u rddlm} |t _t jS )N   )
functional)r   r   Ztransforms.v2rN   )rI   rN   r    r    r!   _Fe   s    
zDatapoint._Fr   c                   s2   t   t jW  d    S 1 s$0    Y  d S r&   )r   rG   shaperI   rJ   r    r!   rP   s   s    zDatapoint.shapeintc                   s2   t   t jW  d    S 1 s$0    Y  d S r&   )r   rG   ndimrQ   rJ   r    r!   rS   x   s    zDatapoint.ndimr   )r1   r2   r   c                   s2   t   t jW  d    S 1 s$0    Y  d S r&   )r   rG   r   )rI   r1   r2   rJ   r    r!   r   }   s    zDatapoint.devicer   c                   s2   t   t jW  d    S 1 s$0    Y  d S r&   )r   rG   r   rQ   rJ   r    r!   r      s    zDatapoint.dtypec                 C  s   | S r&   r    rQ   r    r    r!   horizontal_flip   s    zDatapoint.horizontal_flipc                 C  s   | S r&   r    rQ   r    r    r!   vertical_flip   s    zDatapoint.vertical_flipwarnz	List[int]zUnion[InterpolationMode, int]zOptional[int]zOptional[Union[str, bool]])sizeinterpolationmax_size	antialiasr   c                 C  s   | S r&   r    )rI   rW   rX   rY   rZ   r    r    r!   resize   s    zDatapoint.resize)topleftheightwidthr   c                 C  s   | S r&   r    )rI   r\   r]   r^   r_   r    r    r!   crop   s    zDatapoint.crop)output_sizer   c                 C  s   | S r&   r    )rI   ra   r    r    r!   center_crop   s    zDatapoint.center_crop)r\   r]   r^   r_   rW   rX   rZ   r   c                 C  s   | S r&   r    )rI   r\   r]   r^   r_   rW   rX   rZ   r    r    r!   resized_crop   s    
zDatapoint.resized_cropconstantz(Optional[Union[int, float, List[float]]])paddingfillpadding_moder   c                 C  s   | S r&   r    )rI   re   rf   rg   r    r    r!   pad   s    zDatapoint.padFfloatboolzOptional[List[float]]_FillTypeJIT)anglerX   expandcenterrf   r   c                 C  s   | S r&   r    )rI   rl   rX   rm   rn   rf   r    r    r!   rotate   s    zDatapoint.rotatezUnion[int, float]zList[float])rl   	translatescaleshearrX   rf   rn   r   c                 C  s   | S r&   r    )rI   rl   rp   rq   rr   rX   rf   rn   r    r    r!   affine   s    
zDatapoint.affinezOptional[List[List[int]]])startpoints	endpointsrX   rf   coefficientsr   c                 C  s   | S r&   r    )rI   rt   ru   rX   rf   rv   r    r    r!   perspective   s    zDatapoint.perspective)displacementrX   rf   r   c                 C  s   | S r&   r    )rI   rx   rX   rf   r    r    r!   elastic   s    zDatapoint.elastic   )num_output_channelsr   c                 C  s   | S r&   r    )rI   r{   r    r    r!   rgb_to_grayscale   s    zDatapoint.rgb_to_grayscale)brightness_factorr   c                 C  s   | S r&   r    )rI   r}   r    r    r!   adjust_brightness   s    zDatapoint.adjust_brightness)saturation_factorr   c                 C  s   | S r&   r    )rI   r   r    r    r!   adjust_saturation   s    zDatapoint.adjust_saturation)contrast_factorr   c                 C  s   | S r&   r    )rI   r   r    r    r!   adjust_contrast   s    zDatapoint.adjust_contrast)sharpness_factorr   c                 C  s   | S r&   r    )rI   r   r    r    r!   adjust_sharpness   s    zDatapoint.adjust_sharpness)
hue_factorr   c                 C  s   | S r&   r    )rI   r   r    r    r!   
adjust_hue   s    zDatapoint.adjust_hue)gammagainr   c                 C  s   | S r&   r    )rI   r   r   r    r    r!   adjust_gamma   s    zDatapoint.adjust_gamma)bitsr   c                 C  s   | S r&   r    )rI   r   r    r    r!   	posterize   s    zDatapoint.posterize)	thresholdr   c                 C  s   | S r&   r    )rI   r   r    r    r!   solarize   s    zDatapoint.solarizec                 C  s   | S r&   r    rQ   r    r    r!   autocontrast   s    zDatapoint.autocontrastc                 C  s   | S r&   r    rQ   r    r    r!   equalize   s    zDatapoint.equalizec                 C  s   | S r&   r    rQ   r    r    r!   invert   s    zDatapoint.invert)kernel_sizesigmar   c                 C  s   | S r&   r    )rI   r   r   r    r    r!   gaussian_blur   s    zDatapoint.gaussian_blur)NNN)r    N)Nrd   )rz   )rz   )N)3__name__
__module____qualname__r   __annotations__staticmethodr"   classmethodr(   r   r   clonetor   r;   r>   rL   propertyrO   rP   rS   r   r   rT   rU   r   ZBILINEARr[   r`   rb   rc   rh   ZNEARESTro   rs   rw   ry   r|   r~   r   r   r   r   r   r   r   r   r   r   r   __classcell__r    r    rJ   r!   r      s   
   
  2	
  )$
__future__r   r0   r   typingr   r   r   r   r   r	   r
   r   r   r   Z	PIL.ImageZPILr   Ztorch._Cr   Ztorch.typesr   r   r   Ztorchvision.transformsr   r   rR   ri   Z	_FillTyperk   r   r   ZImageZ
_InputTypeZ_InputTypeJITr    r    r    r!   <module>   s   0 q