o
    *ÎjÚ  ã                   @   s0  d dl mZ d dlmZ d dlZd dlmZmZ d dl	m
Z
 zd dlmZmZmZmZ W n6 eyb   e e e¡¡Ze
de edd¡e edd	¡e edd
¡gddZd dlmZmZmZmZ Y nw G dd„ dejjƒZG dd„ dejjƒZdejdejdejdejfdd„Zdejdejdejfdd„ZdS )é    N)ÚTuple)Ú
custom_bwdÚ
custom_fwd)Úload)Úailut_cbackwardÚailut_cforwardÚlut_cbackwardÚlut_cforwardÚcudaops_ailutZAilutzcsrc/ailut_transform.cppzcsrc/ailut_transform_cpu.cppzcsrc/ailut_transform_cuda.cuT)ÚnameÚsourcesÚverbosec                   @   sZ   e Zd Zeeejddejdejdejfdd„ƒƒZee	dejde
ej fdd	„ƒƒZd
S )ÚLUTTransformFunction©Zcast_inputsÚimgÚlutÚreturnc                 C   s~   |  ¡ }|  ¡ }| ¡ dksJ dƒ‚| ¡ dv sJ dƒ‚| | d¡| d¡| d¡| d¡f¡}t|||ƒ |  ||¡ |S )	Né   úConly support 2D image with batch and channel dimensions (4D tensor)©é   ú=only support 3D lookup table with batch dimension (5D tensor)r   é   é   é   )Ú
contiguousÚ
ndimensionÚ	new_zerosÚsizer	   Úsave_for_backward)Úctxr   r   Úoutput© r"   úb/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/ops/ailut/pyinterfaces.pyÚforward   s   ÿÿ"ÿzLUTTransformFunction.forwardÚgrad_outputc                 C   s>   |  ¡ }| j\}}t |¡}t |¡}t|||||ƒ ||fS ©N)r   Úsaved_tensorsÚtorchÚ
zeros_liker   )r    r%   r   r   Úgrad_imgÚgrad_lutr"   r"   r#   Úbackward/   s   


zLUTTransformFunction.backwardN)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr   r(   Úfloat32ÚTensorr$   r   r   r,   r"   r"   r"   r#   r      s    
 "r   c                
   @   s`   e Zd Zeeejddejdejdejdejfdd„ƒƒZ	ee
dejdeej fd	d
„ƒƒZdS )ÚAiLUTTransformFunctionr   r   r   Úverticesr   c                 C   sž   |  ¡ }|  ¡ }|  ¡ }| ¡ dksJ dƒ‚| ¡ dv s J dƒ‚| ¡ dks*J dƒ‚| | d¡| d¡| d	¡| d¡f¡}t||||ƒ |  |||¡ |S )
Nr   r   r   r   r   zKonly support 1D vertices list with batch and channel dimensions (3D tensor)r   r   r   )r   r   r   r   r   r   )r    r   r   r4   r!   r"   r"   r#   r$   @   s$   ÿÿÿ"ÿzAiLUTTransformFunction.forwardr%   c                 C   sP   |  ¡ }| j\}}}t |¡}t |¡}t |¡}t|||||||ƒ |||fS r&   )r   r'   r(   r)   r   )r    r%   r   r   r4   r*   r+   Zgrad_verr"   r"   r#   r,   X   s   


ÿ
zAiLUTTransformFunction.backwardN)r-   r.   r/   r0   r   r(   r1   r2   Ztensorr$   r   r   r,   r"   r"   r"   r#   r3   >   s    
ÿÿ"r3   r   r   r4   r   c                 C   s   t  | ||¡S )a   Adaptive Interval 3D Lookup Table Transform (AiLUT-Transform).

    Args:
        img (torch.Tensor): input image of shape (b, 3, h, w).
        lut (torch.Tensor): output values of the 3D LUT, shape (b, 3, d, d, d).
        vertices (torch.Tensor): sampling coordinates along each dimension of
            the 3D LUT, shape (b, 3, d).
    Returns:
        torch.Tensor: transformed image of shape (b, 3, h, w).
    )r3   Úapply)r   r   r4   r"   r"   r#   Úailut_transformj   s   r6   c                 C   s   t  | |¡S )a  Standard 3D Lookup Table Transform.

    Args:
        img (torch.Tensor): input image of shape (b, 3, h, w).
        lut (torch.Tensor): output values of the 3D LUT, shape (b, 3, d, d, d).
    Returns:
        torch.Tensor: transformed image of shape (b, 3, h, w).
    )r   r5   )r   r   r"   r"   r#   Úlut_transformy   s   	r7   )Zos.pathÚpathZospÚtypingr   r(   Ztorch.cuda.ampr   r   Ztorch.utils.cpp_extensionr   r
   r   r   r   r	   ÚImportErrorÚabspathÚdirnameÚ__file__ZCUR_DIRÚjoinZautogradÚFunctionr   r3   r2   r6   r7   r"   r"   r"   r#   Ú<module>   s6    ýùö%,ÿ
ÿ 