o
    ԰ih                     @   s   d dl Z d dlmZ d dlmZmZmZmZ d dlm	Z	 d dl
Z
dddZe jjfdeee j e jf d	efd
dZede jje jje jjfdedede jde jde jd	e jfddZdS )    N)contextmanager)OptionalListUnionCallablewraps   c                    s    fdd}|S )zCustom caching decorator that uses a provided key function

    Args:
        key_fn: Function that generates cache key from the input arguments
        maxsize: Maximum size of the cache
    c                    s"   i  t  fdd}|S )Nc                     sV   | i |}| v r | S | i |}t  kr% tt  | |< |S )N)lenpopnextiter)argskwargskeyresult)cachefunckey_fnmaxsize ?/home/ubuntu/.local/lib/python3.10/site-packages/cudnn/graph.pywrapper   s   z/graph_cache.<locals>.decorator.<locals>.wrapperr   r   r   r   r   )r   r   r   	decorator   s   zgraph_cache.<locals>.decoratorr   )r   r   r   r   r   r   graph_cache   s   r   
heur_modesreturnc                    s    t  ts g  fdd}|S )a  
    Decorator that automatically builds a graph with specified heuristic modes.

    Args:
        heur_modes: Single heuristic mode or list of modes for graph building.
        **kwargs: Additional configuration options for graph building.

    Returns:
        Callable: Decorated context manager function that returns (graph, tensor_uids).

    Example:
        >>> handle = cudnn.create_handle()
        >>> @cudnn.jit(heur_modes=[cudnn.heur_mode.A, cudnn.heur_mode.B])
        ... def my_graph():
        ...     with graph(handle) as g:
        ...         X = g.tensor(name="X", dim=[8, 64, 56, 56],
        ...                     stride=[56*56*64, 1, 56*64, 64])
        ...         return g, [X]  # Return graph and list of tensors to get UIDs for
    c                    s   t   fdd}|S )Nc                     s:    | i |\}}|  dkr| |dd |D fS )Nr   c                 S   s   g | ]}|  qS r   )get_uid).0tr   r   r   
<listcomp>E   s    z;jit.<locals>.decorator.<locals>.wrapper.<locals>.<listcomp>)get_execution_plan_countbuild)r   r   gtensors)r   r   r   r   r   @   s   
z'jit.<locals>.decorator.<locals>.wrapperr   r   r   )r   r   r   ?   s   zjit.<locals>.decorator)
isinstancelist)r   r   r   r   r'   r   jit%   s   

r*   cudnn_graphhandlenameio_data_typeintermediate_data_typecompute_data_typec                 c   s$    t j| ||||d}|g fV  dS )a  
    Context manager for creating and managing a CUDNN graph object.

    Args:
        handle: CUDNN handle created with cudnn.create_handle().
        name: Name of the graph for debugging purposes.
        io_data_type: Data type for input/output tensors.
        compute_data_type: Data type for computation.

    Yields:
        Tuple[cudnn.pygraph, List]: (graph object, list of tensors to get UIDs for)
    )r,   r-   r.   r/   r0   N)cudnnpygraph)r,   r-   r.   r/   r0   r%   r   r   r   graphL   s   r3   )r	   )r1   
contextlibr   typingr   r   r   r   	functoolsr   warningsr   	heur_modeAr*   	data_typeHALFFLOATobjectstrr2   r3   r   r   r   r   <module>   s<    

'