
    vj                        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
 	 d dlmZmZmZmZ nv# e$ rn  ej         ej        e                    Z e
d ej        edd           ej        edd	           ej        edd
          gd          Zd dlmZmZmZmZ Y nw xY w G d dej        j                  Z G d dej        j                  Zdej        dej        dej        dej        fdZdej        dej        dej        f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                       e Zd Ze eej                  dej        dej        dej        fd                        Zee	dej        de
ej                 fd                        ZdS )	LUTTransformFunctioncast_inputsimglutreturnc                    |                                 }|                                 }|                                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   outputs       q/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/modelscope/ops/ailut/pyinterfaces.pyforwardzLUTTransformFunction.forward   s     nnnn~~1$$$Q %$$~~3&&&K '&& XXa[[#((1++sxx{{CHHQKK@B BS#v&&&c3'''    grad_outputc                     |                                 }| j        \  }}t          j        |          }t          j        |          }t	          |||||           ||fS N)r    saved_tensorstorch
zeros_liker	   )r%   r*   r   r   grad_imggrad_luts         r'   backwardzLUTTransformFunction.backward/   sb     ",,..$S#C((#C((k3Xx@@@!!r)   N)__name__
__module____qualname__staticmethodr   r.   float32Tensorr(   r   r   r2    r)   r'   r   r      s        ZEM***%, U\ el    +* \$ 
"5< 
"E%,4G 
" 
" 
" Z \
" 
" 
"r)   r   c            
           e Zd Ze eej                  dej        dej        dej        dej        fd                        Z	ee
dej        deej                 fd                        Zd	S )
AiLUTTransformFunctionr   r   r   verticesr   c                 b   |                                 }|                                 }|                                 }|                                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   r<   r&   s        r'   r(   zAiLUTTransformFunction.forward@   s#   
 nnnn&&((~~1$$$Q %$$~~3&&&K '&&""$$)))Y *)) XXa[[#((1++sxx{{CHHQKK@B BsC6222c3111r)   r*   c           	          |                                 }| j        \  }}}t          j        |          }t          j        |          }t          j        |          }t	          |||||||           |||fS r,   )r    r-   r.   r/   r   )r%   r*   r   r   r<   r0   r1   grad_vers           r'   r2   zAiLUTTransformFunction.backwardX   s     ",,.. .S(#C((#C((#H--S#x8 	" 	" 	" 8++r)   N)r3   r4   r5   r6   r   r.   r7   r8   tensorr(   r   r   r2   r9   r)   r'   r;   r;   >   s        ZEM***%, U\ ,+0<   +* \, ,5< ,E%,4G , , , Z \, , ,r)   r;   r   r   r<   r   c                 :    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).
    )r;   apply)r   r   r<   s      r'   ailut_transformrC   j   s     "''S(;;;r)   c                 8    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   rB   )r   r   s     r'   lut_transformrE   y   s      %%c3///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   r;   r8   rC   rE   r9   r)   r'   <module>rT      sR                1 1 1 1 1 1 1 1 * * * * * *-- - - - - - - - - - - - - - - -ck+#+h//00GDCHWg'ABBCHWg'EFFCHWg'EFF

   M- - - - - - - - - - - - - --"" "" "" "" ""5>2 "" "" ""J), ), ), ), ),U^4 ), ), ),X< <EL <#l</4|< < < <	0u| 	0%, 	05< 	0 	0 	0 	0 	0 	0s   - A0B B 