[docs]def_create_jit_fn(code_string:str,**kwargs)->Callable:""" 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 """classJittedFunction:def__init__(self,code_string:str,**kwargs):self.code_string=code_stringparsed_code=_CodeParser(code_string)self.kernel_name=parsed_code.function_nameself.kwargs_dict=kwargsself.is_cuda_available=torch.cuda.is_available()def__call__(self,*tensors:Tensor,**kwargs):# Jiterator follow torch.cuda's lazy initialization behavior# Defer checking cuda's availability at the function invocation timeassertself.is_cuda_available,"Jiterator is only supported on CUDA GPUs, no CUDA GPUs are available."assertlen(tensors)<=8,"jiterator only supports up to 8 tensor inputs."expanded_kwargs=self.kwargs_dict.copy()forkey,valueinkwargs.items():ifkeyinself.kwargs_dict:expanded_kwargs[key]=valueelse:raiseKeyError(f"{key} is not declared in function definition")returntorch._C._cuda_jiterator_compile_and_launch_kernel(self.code_string,self.kernel_name,tensors,expanded_kwargs)returnJittedFunction(code_string,**kwargs)
Docs
Access comprehensive developer documentation for PyTorch
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.