
    Ǆg                         U d dl Z d dlmZmZ d dlZd dlmZ g Zee   ed<    G d d      Z	 G d d      Z
d	ed
efdZd	eded
efdZy)    N)CallableList)Tensor__all__c                       e Zd ZdefdZy)_CodeParsercode_stringc                 :   d}d}d}d}d}d}d}|dz   |z   |z   |z   |z   |z   |z   |z   |z   |z   |z   |z   }	t        j                  |	|t         j                        }
|
t        d	|       |
d
   | _        |
d   | _        |
d   | _        |
d   | _        |
d   | _        y )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	   optional_wsrequired_wsr   r   r   r   r   patternresults              \/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/cuda/jiterator.py__init__z_CodeParser.__init__   s!   8-184   	
     	 
   	  ["))
 >CK=Q   &&78!-0#O4%&78#O4    N)__name__
__module____qualname__strr    r   r   r   r      s    &5C &5r   r   c                   ,    e Zd ZdededefdZdefdZy)_JittedFunctionr	   return_by_refnum_outputsc                     || _         |s|dk(  sJ d       || _        || _        t        |      }|j                  | _        || _        t        j                  j                         | _
        y )N   z.Return by value only works for single output. )r	   r$   r%   r   r   kernel_namekwargs_dicttorchcudais_availableis_cuda_available)r   r	   r$   r%   kwargsparsed_codes         r   r   z_JittedFunction.__init__7   sm     ' [A-	<;	<-*&!+.&44!!&!8!8!:r   tensorsc                    | j                   sJ d       t        |      dk  sJ d       | j                  j                         }|j	                         D ]&  \  }}|| j                  v r|||<   t        | d       t        j                  j                  | j                  | j                  | j                  | j                  ||      S )NzFJiterator is only supported on CUDA and ROCm GPUs, none 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   r0   r.   expanded_kwargskeyvalues         r   __call__z_JittedFunction.__call__H   s     ""	TS	T" 7|q R"RR **//1 ,,. 	PJCd&&&',$#&MNOO		P xxAA
 	
r   N)	r   r   r   r    boolintr   r   r<   r!   r   r   r#   r#   6   s+    ;;/3;BE;"
 
r   r#   r	   returnc                      t        | fddd|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 broadcasting and type promotion.

    Args:
        code_string (str): CUDA code string to be compiled by jiterator. The entry functor must return by value.
        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)

    code_string also allows multiple function definitions, and the last function will be treated as the entry function.

    Example::

        code_string = "template <typename T> T util_fn(T x, T y) { return ::sin(x) + ::cos(y); }"
        code_string += "template <typename T> T my_kernel(T x, T y, T val) { return ::min(val, util_fn(x, y)); }"
        jitted_fn = create_jit_fn(code_string, val=0.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)  # using default val=0.0

    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.

    Example::

        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::
        This API only supports up to 8 inputs and 1 output

    .. warning::
        All input tensors must live in CUDA device
    Fr'   r$   r%   r#   )r	   r.   s     r   _create_jit_fnrC   b   s    t ;UeUfUUr   r%   c                      t        | fd|d|S )a  
    Create a jiterator-generated cuda kernel for an elementwise op that supports returning one or more outputs.

    Args:
        code_string (str): CUDA code string to be compiled by jiterator. The entry functor must return value by reference.
        num_outputs(int): number of outputs return by the kernel
        kwargs (Dict, optional): Keyword arguments for generated function

    Example::

        code_string = "template <typename T> void my_kernel(T x, T y, T alpha, T& out) { out = -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)

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

    .. warning::
        This API only supports up to 8 inputs and 8 outputs
    TrA   rB   )r	   r%   r.   s      r   _create_multi_output_jit_fnrE      s&    4 #'[DJ r   )r   typingr   r   r*   r   r   r    __annotations__r   r#   rC   r>   rE   r!   r   r   <module>rH      sk    	 !   c '5 '5T)
 )
X:V :V( :Vz#&r   