char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
825eb91a6
char/aliceai-t5-moe
1import os 2import re 3import argparse 4 5 6def expand_includes (shader ,input_dir ): 7""" 8Replace #include "file" lines in the text with the contents of that file. 9Searches for files relative to input_dir. 10""" 11include_pattern = re .compile (r'^\s*#include\s+"([^"]+)"\s*$' ,re .MULTILINE ) 12 13def replacer (match ): 14fname = match .group (1 ) 15file_path = os .path .join (input_dir ,fname ) 16if not os .path .exists (file_path ): 17raise FileNotFoundError (f"Included file not found: { file_path } " ) 18with open (file_path ,"r" ,encoding = "utf-8" )as f : 19included_code = f .read () 20# Recursively expand includes inside the included file 21return expand_includes (included_code ,input_dir ) 22 23return include_pattern .sub (replacer ,shader ) 24 25 26def chunk_shader (shader_code ,max_chunk_len = 60000 ): 27"""Split shader_code into safe raw-string sized chunks.""" 28return [shader_code [i :i + max_chunk_len ]for i in range (0 ,len (shader_code ),max_chunk_len )] 29 30 31def raw_delim (shader_code ): 32"""Pick a raw-string delimiter that does not appear in the shader.""" 33delim = "wgsl" 34while f") { delim } \"" in shader_code : 35delim += "_x" 36return delim 37 38 39def write_shader (shader_name ,shader_code ,output_dir ,outfile ,input_dir ): 40shader_code = expand_includes (shader_code ,input_dir ) 41 42if output_dir : 43wgsl_filename = os .path .join (output_dir ,f" { shader_name } .wgsl" ) 44with open (wgsl_filename ,"w" ,encoding = "utf-8" )as f_out : 45f_out .write (shader_code ) 46 47delim = raw_delim (shader_code ) 48chunks = chunk_shader (shader_code ) 49 50if len (chunks )== 1 : 51outfile .write (f'const char* wgsl_ { shader_name } = R" { delim } ( { shader_code } ) { delim } ";\n\n' ) 52else : 53for idx ,chunk in enumerate (chunks ): 54outfile .write (f'static const char wgsl_ { shader_name } _part { idx } [] = R" { delim } ( { chunk } ) { delim } ";\n\n' ) 55outfile .write (f'static const std::string& wgsl_ { shader_name } _str() {{\n' ) 56outfile .write (' static const std::string s = []{\n' ) 57outfile .write (' std::string tmp;\n' ) 58outfile .write (f' tmp.reserve( { len ( shader_code ) } );\n' ) 59for idx in range (len (chunks )): 60outfile .write (f' tmp.append(wgsl_ { shader_name } _part { idx } );\n' ) 61outfile .write (' return tmp;\n' ) 62outfile .write (' }();\n' ) 63outfile .write (' return s;\n' ) 64outfile .write ('}\n' ) 65outfile .write (f'const char* wgsl_ { shader_name } = wgsl_ { shader_name } _str().c_str();\n\n' ) 66 67 68def main (): 69parser = argparse .ArgumentParser () 70parser .add_argument ("--input_dir" ,required = True ) 71parser .add_argument ("--output_file" ,required = True ) 72args = parser .parse_args () 73 74with open (args .output_file ,"w" ,encoding = "utf-8" )as out : 75out .write ("// Auto-generated shader embedding\n" ) 76out .write ("#include <string>\n\n" ) 77for fname in sorted (os .listdir (args .input_dir )): 78if fname .endswith (".wgsl" ): 79shader_path = os .path .join (args .input_dir ,fname ) 80shader_name = fname .replace (".wgsl" ,"" ) 81 82with open (shader_path ,"r" ,encoding = "utf-8" )as f : 83shader_code = f .read () 84 85write_shader (shader_name ,shader_code ,None ,out ,args .input_dir ) 86 87 88if __name__ == "__main__" : 89main ()