mirror of https://github.com/google/gemma.cpp.git
cl kernel embedder upload
This commit is contained in:
parent
e70abdba24
commit
cf435b77f9
|
|
@ -0,0 +1,76 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def embed_head(entry_name:str)->str:
|
||||||
|
|
||||||
|
head:str="""
|
||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
class cl_embed {
|
||||||
|
public:
|
||||||
|
"""
|
||||||
|
return head
|
||||||
|
|
||||||
|
|
||||||
|
def embed_tail(entry_name:str)->str:
|
||||||
|
return entry_name + "\n};"
|
||||||
|
|
||||||
|
|
||||||
|
def get_all_kernels(embed_:str)->str:
|
||||||
|
|
||||||
|
for i in os.listdir("./"):
|
||||||
|
if i.endswith(".cl"):
|
||||||
|
|
||||||
|
clfile=open("./"+i, "r",encoding="utf-8")
|
||||||
|
#read_and_embed(clfile, embed_)
|
||||||
|
embed_ = read_and_embed(clfile, embed_)
|
||||||
|
clfile.close()
|
||||||
|
|
||||||
|
return embed_tail(embed_)
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def get_title(text:str)->str:
|
||||||
|
void_loc = text.find("void ") + 5
|
||||||
|
arg_loc = text.find("(")
|
||||||
|
|
||||||
|
return text[void_loc:arg_loc]
|
||||||
|
|
||||||
|
def add_title(title:str, main_text:str)->str:
|
||||||
|
return "std::string "+title+" = \n"+main_text + "\t;\n"
|
||||||
|
|
||||||
|
def read_and_embed(file, got_str:str)->str:
|
||||||
|
main_text:str=""
|
||||||
|
title:str = ""
|
||||||
|
for i in file.readlines():
|
||||||
|
line:str=i[:-1]
|
||||||
|
if line.find("//-ne")!=-1 or line.find("//No_Embed")!=-1 or line.find("printf")!=-1:
|
||||||
|
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
if line.find("__kernel")!=-1:
|
||||||
|
title = get_title(line)
|
||||||
|
line="\t\""+line+"\\n\""+"\n"
|
||||||
|
main_text+=line
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return got_str + add_title(title, main_text)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
embed_string:str=""
|
||||||
|
embed_string = embed_head(embed_string)
|
||||||
|
|
||||||
|
out_file=open("./cl_embedded.h","w", encoding="utf-8")
|
||||||
|
out_file.write(get_all_kernels(embed_string))
|
||||||
|
out_file.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
//No_Embed <-----This reservation word will allow you to ignore the line when embedded.
|
||||||
|
//or
|
||||||
|
//-ne
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void feel_free_to_make_functions()//but you can't use recursive function
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Kernel entry point, equals to main() function
|
||||||
|
__kernel void sample_entry_code(__global int* from_main_code)
|
||||||
|
{
|
||||||
|
int myid = get_global_id(0);
|
||||||
|
printf("test %d sample",from_main_code[myid]);//also printf will be ignored in embedded
|
||||||
|
feel_free_to_make_functions();
|
||||||
|
printf("this line will be ignored after embedding");//-ne
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
class cl_embed {
|
||||||
|
public:
|
||||||
|
std::string sample_entry_code =
|
||||||
|
"//or\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"void feel_free_to_make_functions()//but you can't use recursive function\n"
|
||||||
|
"{\n"
|
||||||
|
" return;\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"//Kernel entry point, equals to main() function\n"
|
||||||
|
"__kernel void sample_entry_code(__global int* from_main_code)\n"
|
||||||
|
"{\n"
|
||||||
|
" int myid = get_global_id(0);\n"
|
||||||
|
" feel_free_to_make_functions();\n"
|
||||||
|
"}\n"
|
||||||
|
;
|
||||||
|
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue