L iUddlZddlmZddlZddlmZgZeeed<GddZ GddZ d ed efd Z d ed e d efd Z y)N)Callable)Tensor__all__ceZdZdefdZy) _CodeParser code_stringc:d}d}d}d}d}d}d}|dz|z|z|z|z|z|z|z|z|z|z|z} tj| |tj} | td || d |_| d |_| d |_| d |_| d|_y)Nz\s*z\s+z(?P\<.+\>)z(?P\w+)z(?P\w+)z(?P\(.+\))z(?P\{.+\})templatez0Couldn't parse code, please check correctness: template_params return_type function_namefunction_params function_body) rematchDOTALL Exceptionr r r rr) selfr optional_ws required_wsr r r rrpatternresults Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/cuda/jiterator.py__init__z_CodeParser.__init__ s!  8- 1 84                      ["))  >CK=Q  &&78!-0#O4%&78#O4N)__name__ __module__ __qualname__strrrrrr s&5C&5rrc,eZdZdededefdZdefdZy)_JittedFunctionr return_by_ref num_outputsc ||_|s |dk(sJd||_||_t|}|j|_||_tjj|_ y)Nz.Return by value only works for single output. ) rr#r$rr kernel_name kwargs_dicttorchcuda is_availableis_cuda_available)rrr#r$kwargs parsed_codes rrz_JittedFunction.__init__7sm' q 0 < 0+&!+. &44!!&!8!8!:rtensorsc|jsJdt|dksJd|jj}|j D]&\}}||jvr|||<t |dt jj|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(copyitemsKeyErrorr)_C)_cuda_jiterator_compile_and_launch_kernelrr'r#r$)rr/r-expanded_kwargskeyvalues r__call__z_JittedFunction.__call__Hs%% T %7|q R"RR **//1 ,,. PJCd&&&',$#&MNOO  P xxAA                 rN) rrrrboolintrrr;r rrr"r"6s+;;/3;BE;"  rr"rreturnc t|fddd|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 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 T util_fn(T x, T y) { return ::sin(x) + ::cos(y); }" ) code_string += "template 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 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")rr-s r_create_jit_fnrBbsx ; Ue Uf UUrr$c t|fd|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 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 Tr@rA)rr$r-s r_create_multi_output_jit_fnrDs&4  #'[ DJ r)rtypingrr)rrlistr__annotations__rr"rBr=rDr rrrHsk  c'5'5T) ) X<V<V(<V~#& r