o
    ॵi                     @   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_ailut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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$   U/home/ubuntu/.local/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   r6   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   r6   r,   r-   grad_verr$   r$   r%   r.   X   s   



zAiLUTTransformFunction.backwardN)r/   r0   r1   r2   r   r*   r3   r4   tensorr&   r   r   r.   r$   r$   r$   r%   r5   >   s    
"r5   r   r   r6   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).
    )r5   apply)r   r   r6   r$   r$   r%   ailut_transformj   s   r:   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   r9   )r   r   r$   r$   r%   lut_transformy   s   	r;   )os.pathpathosptypingr   r*   torch.cuda.ampr   r   torch.utils.cpp_extensionr   r
   r   r   r   r	   ImportErrorabspathdirname__file__CUR_DIRjoinautogradFunctionr   r5   r4   r:   r;   r$   r$   r$   r%   <module>   s6    %,
 