a
    NSic                     @   s`   U d dl Z d dl mZ d dlmZmZ d dlZg Zee ed< G dd dZ	eeddd	Z
dS )
    N)Tensor)CallableList__all__c                   @   s   e Zd ZedddZdS )_CodeParsercode_stringc                 C   s   d}d}d}d}d}d}d}|d | | | | | | | | | | | }	t |	|t j}
|
d u rvtd	| |
d
 | _|
d | _|
d | _|
d | _|
d | _d S )Nz\s*z\s+z(?P<template_params>\<.+\>)z(?P<return_type>\w+)z(?P<function_name>\w+)z(?P<function_params>\(.+\))z(?P<function_body>\{.+\})templatez0Couldn't parse code, please check correctness:
 template_paramsreturn_typefunction_namefunction_paramsfunction_body)	rematchDOTALL	Exceptionr
   r   r   r   r   )selfr   Zoptional_wsZrequired_wsr
   r   r   r   r   patternresult r   P/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/cuda/jiterator.py__init__
   sR    




z_CodeParser.__init__N)__name__
__module____qualname__strr   r   r   r   r   r   	   s   r   )r   returnc                 K   s   G dd d}|| fi |S )a  
    Create a jiterator-generated cuda kernel for an elementwise op.

    The code string has to be a valid CUDA function that describes the computation for a single element. The code
    string has to follow the c++ template pattern, as shown in the example below. This function will be inlined
    into elementwise kernel template, and compiled on the fly. Compiled kernel will be cached in memory, as well as
    local temp dir.

    Jiterator-generated kernels accepts noncontiguous tensors, and supports boardcasting and type promotion.

    Args:
        code_string (string): CUDA code string to be compiled by jiterator.
        kwargs (Dict, optional): Keyword arguments for generated function

    Example:
        >>> code_string = "template <typename T> T my_kernel(T x, T y, T alpha) { return  -x + alpha * y; }"
        >>> jitted_fn = create_jit_fn(code_string, alpha=1.0)
        >>> a = torch.rand(3, device='cuda')
        >>> b = torch.rand(3, device='cuda')
        >>> # invoke jitted function like a regular python function
        >>> result = jitted_fn(a, b, alpha=3.14)


    Jiterator can be used together with python registration to override an operator's cuda kernel

    Following example is overriding gelu's cuda kernel with relu:
        >>> code_string = "template <typename T> T my_gelu(T a) { return a > 0 ? a : 0; }"
        >>> my_gelu = create_jit_fn(code_string)
        >>> my_lib = torch.library.Library("aten", "IMPL")
        >>> my_lib.impl('aten::gelu', my_gelu, "CUDA")
        >>> # torch.nn.GELU and torch.nn.function.gelu are now overridden
        >>> a = torch.rand(3, device='cuda')
        >>> torch.allclose(torch.nn.functional.gelu(a), torch.nn.functional.relu(a))


    .. warning::
        This API is in beta and may change in future releases.

    .. warning::
        Jiterator only supports up to 8 tensor inputs

    .. warning::
        All input tensors must live in CUDA device

    c                   @   s(   e Zd ZedddZedddZdS )z&_create_jit_fn.<locals>.JittedFunctionr   c                 [   s,   || _ t|}|j| _|| _tj | _d S )N)	r   r   r   kernel_namekwargs_dicttorchcudais_availableis_cuda_available)r   r   kwargsZparsed_coder   r   r   r   X   s
    z/_create_jit_fn.<locals>.JittedFunction.__init__)tensorsc                 _   sv   | j sJ dt|dks"J d| j }| D ]*\}}|| jv rP|||< q4t| dq4tj| j	| j
||S )NzEJiterator is only supported on CUDA GPUs, no CUDA GPUs are available.   z.jiterator only supports up to 8 tensor inputs.z' is not declared in function definition)r#   lenr   copyitemsKeyErrorr    _C)_cuda_jiterator_compile_and_launch_kernelr   r   )r   r%   r$   Zexpanded_kwargskeyvaluer   r   r   __call__a   s    


z/_create_jit_fn.<locals>.JittedFunction.__call__N)r   r   r   r   r   r   r/   r   r   r   r   JittedFunctionW   s   	r0   r   )r   r$   r0   r   r   r   _create_jit_fn)   s    .r1   )r    r   typingr   r   r   r   r   __annotations__r   r1   r   r   r   r   <module>   s   
 