tools/amd_build/pyHIPIFY/hipify-python.py
def add_static_casts(filepath, KernelTemplateParams):
    """Add static casts to kernel launches in order to keep launch argument types and kernel definition types matching.

       Example:
           old_kernel_launch: ' createBatchGemmBuffer, grid, block, 0, THCState_getCurrentStream(state),
              (const real**)d_result, THCTensor_(data)(state, ra__),
              ra__->stride[0], num_batches'

           new_kernel_launch: ' createBatchGemmBuffer, grid, block, 0, THCState_getCurrentStream(state),
              (const real**)d_result, THCTensor_(data)(state, ra__),
              static_cast<int64_t>(ra__->stride[0]), static_cast<int64_t>(num_batches)'
    """

    # These are the types that generally have issues with hipKernelLaunch.
    static_cast_types = ["int", "const int", "int64_t", "THCIndex_t *",
                         "const int *", "ptrdiff_t", "long", "const int64_t*", "int64_t *", "double"]

    with openf(filepath, "r+") as fileobj:
        input_source = fileobj.read()
        new_output_source = input_source
        for kernel in re.finditer("hipLaunchKernelGGL\(", input_source):
            arguments = extract_arguments(kernel.end() - 1, input_source)

            # Check if we have templating + static_cast information
            argument_strings = [input_source[arg["start"]:arg["end"]] for arg in arguments]
            original_kernel_name_with_template = argument_strings[0].strip()
            kernel_name = original_kernel_name_with_template.split("<")[0].strip()
            ignore = ["upscale"]
            if kernel_name in KernelTemplateParams and kernel_name not in ignore:
                # Add template to the kernel
                # Add static_casts to relevant arguments
                kernel_name_with_template = KernelTemplateParams[kernel_name]["kernel_with_template"]
                argument_types = KernelTemplateParams[kernel_name]["arg_types"]

                # The first 5 arguments are simply (function, number blocks, dimension blocks, shared memory, stream)
                # old_kernel_launch_parameters - will contain the actual arguments to the function itself.
                old_kernel_launch_parameters = input_source[arguments[5]["start"]:arguments[-1]["end"]]
                new_kernel_launch_parameters = old_kernel_launch_parameters

                # full_old_kernel_launch - will contain the entire kernel launch closure.
                full_old_kernel_launch = input_source[arguments[0]["start"]:arguments[-1]["end"]]
                full_new_kernel_launch = full_old_kernel_launch

                kernel_params = argument_strings[5:]
                for arg_idx, arg in enumerate(kernel_params):
                    if arg_idx in argument_types:
                        the_type = argument_types[arg_idx]
                        the_arg = arg.replace("\n", "").replace("\\", "").strip()
                        # Not all types have issues with the hipLaunchKernelGGL.
                        if the_type in static_cast_types:
                            static_argument = "static_cast<{0}>({1})".format(the_type, the_arg)

                            def replace_arg(match):
                                return match.group(1) + static_argument + match.group(3)
                            # Update to static_cast, account for cases where argument is at start/end of string
                            new_kernel_launch_parameters = re.sub(r'(^|\W)({0})(\W|$)'.format(
                                re.escape(the_arg)), replace_arg, new_kernel_launch_parameters)

                # replace kernel arguments in full kernel launch arguments w/ static_cast ones
                full_new_kernel_launch = full_new_kernel_launch.replace(
                    old_kernel_launch_parameters, new_kernel_launch_parameters)

                # PyTorch Specific: Add template type
                # Here the template value will be resolved from <real> to <Dtype>.
                if "THCUNN" in filepath.split("/") and "generic" not in filepath.split("/"):
                    kernel_name_with_template = kernel_name_with_template.replace("<real>", "<Dtype>")

                full_new_kernel_launch = re.sub(r'\b{0}\b'.format(re.escape(original_kernel_name_with_template)),
                                                lambda x: kernel_name_with_template, full_new_kernel_launch)

                # Replace Launch
                new_output_source = new_output_source.replace(full_old_kernel_launch, full_new_kernel_launch)

        # Overwrite file contents
        fileobj.seek(0)
        fileobj.write(new_output_source)
        fileobj.truncate()
        fileobj.flush()

        # Flush to disk
        os.fsync(fileobj)
Add static casts to kernel launches in order to keep launch argument types and kernel definition types matching.

       Example:
           old_kernel_launch: ' createBatchGemmBuffer, grid, block, 0, THCState_getCurrentStream(state),
              (const real**)d_result, THCTensor_(data)(state, ra__),
              ra__->stride[0], num_batches'

           new_kernel_launch: ' createBatchGemmBuffer, grid, block, 0, THCState_getCurrentStream(state),
              (const real**)d_result, THCTensor_(data)(state, ra__),
              static_cast<int64_t>(ra__->stride[0]), static_cast<int64_t>(num_batches)'
    
Add static casts to kernel launches in order to keep launch argument types and kernel definition types matching.
Add static casts to kernel launches in order to keep launch argument types and kernel definition types matching .
def add_static_casts(filepath, KernelTemplateParams):
    static_cast_types = ["int", "const int", "int64_t", "THCIndex_t *",
                         "const int *", "ptrdiff_t", "long", "const int64_t*", "int64_t *", "double"]
    with openf(filepath, "r+") as fileobj:
        input_source = fileobj.read()
        new_output_source = input_source
        for kernel in re.finditer("hipLaunchKernelGGL\(", input_source):
            arguments = extract_arguments(kernel.end() - 1, input_source)
            argument_strings = [input_source[arg["start"]:arg["end"]] for arg in arguments]
            original_kernel_name_with_template = argument_strings[0].strip()
            kernel_name = original_kernel_name_with_template.split("<")[0].strip()
            ignore = ["upscale"]
            if kernel_name in KernelTemplateParams and kernel_name not in ignore:
                kernel_name_with_template = KernelTemplateParams[kernel_name]["kernel_with_template"]
                argument_types = KernelTemplateParams[kernel_name]["arg_types"]
                old_kernel_launch_parameters = input_source[arguments[5]["start"]:arguments[-1]["end"]]
                new_kernel_launch_parameters = old_kernel_launch_parameters
                full_old_kernel_launch = input_source[arguments[0]["start"]:arguments[-1]["end"]]
                full_new_kernel_launch = full_old_kernel_launch
                kernel_params = argument_strings[5:]
                for arg_idx, arg in enumerate(kernel_params):
                    if arg_idx in argument_types:
                        the_type = argument_types[arg_idx]
                        the_arg = arg.replace("\n", "").replace("\\", "").strip()
                        if the_type in static_cast_types:
                            static_argument = "static_cast<{0}>({1})".format(the_type, the_arg)
                            def replace_arg(match):
                                return match.group(1) + static_argument + match.group(3)
                            new_kernel_launch_parameters = re.sub(r'(^|\W)({0})(\W|$)'.format(
                                re.escape(the_arg)), replace_arg, new_kernel_launch_parameters)
                full_new_kernel_launch = full_new_kernel_launch.replace(
                    old_kernel_launch_parameters, new_kernel_launch_parameters)
                if "THCUNN" in filepath.split("/") and "generic" not in filepath.split("/"):
                    kernel_name_with_template = kernel_name_with_template.replace("<real>", "<Dtype>")
                full_new_kernel_launch = re.sub(r'\b{0}\b'.format(re.escape(original_kernel_name_with_template)),
                                                lambda x: kernel_name_with_template, full_new_kernel_launch)
                new_output_source = new_output_source.replace(full_old_kernel_launch, full_new_kernel_launch)
        fileobj.seek(0)
        fileobj.write(new_output_source)
        fileobj.truncate()
        fileobj.flush()
        os.fsync(fileobj)
def add_static_casts ( filepath , KernelTemplateParams ) : static_cast_types = [ "int" , "const int" , "int64_t" , "THCIndex_t *" , "const int *" , "ptrdiff_t" , "long" , "const int64_t*" , "int64_t *" , "double" ] with openf ( filepath , "r+" ) as fileobj : input_source = fileobj . read ( ) new_output_source = input_source for kernel in re . finditer ( "hipLaunchKernelGGL\(" , input_source ) : arguments = extract_arguments ( kernel . end ( ) - 1 , input_source ) argument_strings = [ input_source [ arg [ "start" ] : arg [ "end" ] ] for arg in arguments ] original_kernel_name_with_template = argument_strings [ 0 ] . strip ( ) kernel_name = original_kernel_name_with_template . split ( "<" ) [ 0 ] . strip ( ) ignore = [ "upscale" ] if kernel_name in KernelTemplateParams and kernel_name not in ignore : kernel_name_with_template = KernelTemplateParams [ kernel_name ] [ "kernel_with_template" ] argument_types = KernelTemplateParams [ kernel_name ] [ "arg_types" ] old_kernel_launch_parameters = input_source [ arguments [ 5 ] [ "start" ] : arguments [ - 1 ] [ "end" ] ] new_kernel_launch_parameters = old_kernel_launch_parameters full_old_kernel_launch = input_source [ arguments [ 0 ] [ "start" ] : arguments [ - 1 ] [ "end" ] ] full_new_kernel_launch = full_old_kernel_launch kernel_params = argument_strings [ 5 : ] for arg_idx , arg in enumerate ( kernel_params ) : if arg_idx in argument_types : the_type = argument_types [ arg_idx ] the_arg = arg . replace ( "\n" , "" ) . replace ( "\\" , "" ) . strip ( ) if the_type in static_cast_types : static_argument = "static_cast<{0}>({1})" . format ( the_type , the_arg ) def replace_arg ( match ) : return match . group ( 1 ) + static_argument + match . group ( 3 ) new_kernel_launch_parameters = re . sub ( r'(^|\W)({0})(\W|$)' . format ( re . escape ( the_arg ) ) , replace_arg , new_kernel_launch_parameters ) full_new_kernel_launch = full_new_kernel_launch . replace ( old_kernel_launch_parameters , new_kernel_launch_parameters ) if "THCUNN" in filepath . split ( "/" ) and "generic" not in filepath . split ( "/" ) : kernel_name_with_template = kernel_name_with_template . replace ( "<real>" , "<Dtype>" ) full_new_kernel_launch = re . sub ( r'\b{0}\b' . format ( re . escape ( original_kernel_name_with_template ) ) , lambda x : kernel_name_with_template , full_new_kernel_launch ) new_output_source = new_output_source . replace ( full_old_kernel_launch , full_new_kernel_launch ) fileobj . seek ( 0 ) fileobj . write ( new_output_source ) fileobj . truncate ( ) fileobj . flush ( ) os . fsync ( fileobj )
Add static casts to kernel launches in order to keep launch argument types and kernel definition types matching.
Add static casts to kernel launches in order to keep launch argument types and kernel definition types matching .
"""Add static casts to kernel launches in order to keep launch argument types and kernel definition types matching.

       Example:
           old_kernel_launch: ' createBatchGemmBuffer, grid, block, 0, THCState_getCurrentStream(state),
              (const real**)d_result, THCTensor_(data)(state, ra__),
              ra__->stride[0], num_batches'

           new_kernel_launch: ' createBatchGemmBuffer, grid, block, 0, THCState_getCurrentStream(state),
              (const real**)d_result, THCTensor_(data)(state, ra__),
              static_cast<int64_t>(ra__->stride[0]), static_cast<int64_t>(num_batches)'
    """ # These are the types that generally have issues with hipKernelLaunch. # Check if we have templating + static_cast information # Add template to the kernel # Add static_casts to relevant arguments # The first 5 arguments are simply (function, number blocks, dimension blocks, shared memory, stream) # old_kernel_launch_parameters - will contain the actual arguments to the function itself. # full_old_kernel_launch - will contain the entire kernel launch closure. # Not all types have issues with the hipLaunchKernelGGL. # Update to static_cast, account for cases where argument is at start/end of string # replace kernel arguments in full kernel launch arguments w/ static_cast ones # PyTorch Specific: Add template type # Here the template value will be resolved from <real> to <Dtype>. # Replace Launch # Overwrite file contents # Flush to disk
filepath
KernelTemplateParams
examples
' createBatchGemmBuffer, grid, block, 0, THCState_getCurrentStream(state),
(const real**)d_result, THCTensor_(data)(state, ra__),
ra__->stride[0], num_batches'

' createBatchGemmBuffer, grid, block, 0, THCState_getCurrentStream(state),
(const real**)d_result, THCTensor_(data)(state, ra__),
static_cast(ra__->stride[0]), static_cast(num_batches)'
' createBatchGemmBuffer grid block 0 THCState_getCurrentStream ( state ) ( const real ** ) d_result THCTensor_ ( data ) ( state ra__ ) ra__ - > stride [ 0 ] num_batches ' ' createBatchGemmBuffer grid block 0 THCState_getCurrentStream ( state ) ( const real ** ) d_result THCTensor_ ( data ) ( state ra__ ) static_cast ( ra__ - > stride [ 0 ] ) static_cast ( num_batches ) '