vulkan-shaders-gen.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <stdexcept>
  6. #include <array>
  7. #include <vector>
  8. #include <map>
  9. #include <thread>
  10. #include <mutex>
  11. #include <future>
  12. #include <queue>
  13. #include <condition_variable>
  14. #include <cstdio>
  15. #include <cstring>
  16. #include <cstdlib>
  17. #include <sys/stat.h>
  18. #include <sys/types.h>
  19. #ifdef _WIN32
  20. #include <windows.h>
  21. #include <direct.h> // For _mkdir on Windows
  22. #include <algorithm> // For std::replace on w64devkit
  23. #else
  24. #include <unistd.h>
  25. #include <sys/wait.h>
  26. #include <fcntl.h>
  27. #endif
  28. #define ASYNCIO_CONCURRENCY 64
  29. std::mutex lock;
  30. std::vector<std::pair<std::string, std::string>> shader_fnames;
  31. std::string GLSLC = "glslc";
  32. std::string input_dir = "vulkan-shaders";
  33. std::string output_dir = "/tmp";
  34. std::string target_hpp = "ggml-vulkan-shaders.hpp";
  35. std::string target_cpp = "ggml-vulkan-shaders.cpp";
  36. bool no_clean = false;
  37. const std::vector<std::string> type_names = {
  38. "f32",
  39. "f16",
  40. "q4_0",
  41. "q4_1",
  42. "q5_0",
  43. "q5_1",
  44. "q8_0",
  45. "q2_k",
  46. "q3_k",
  47. "q4_k",
  48. "q5_k",
  49. "q6_k",
  50. "iq4_nl"
  51. };
  52. void execute_command(const std::string& command, std::string& stdout_str, std::string& stderr_str) {
  53. #ifdef _WIN32
  54. HANDLE stdout_read, stdout_write;
  55. HANDLE stderr_read, stderr_write;
  56. SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
  57. if (!CreatePipe(&stdout_read, &stdout_write, &sa, 0) ||
  58. !SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0)) {
  59. throw std::runtime_error("Failed to create stdout pipe");
  60. }
  61. if (!CreatePipe(&stderr_read, &stderr_write, &sa, 0) ||
  62. !SetHandleInformation(stderr_read, HANDLE_FLAG_INHERIT, 0)) {
  63. throw std::runtime_error("Failed to create stderr pipe");
  64. }
  65. PROCESS_INFORMATION pi;
  66. STARTUPINFOA si = { sizeof(STARTUPINFOA) };
  67. si.dwFlags = STARTF_USESTDHANDLES;
  68. si.hStdOutput = stdout_write;
  69. si.hStdError = stderr_write;
  70. std::vector<char> cmd(command.begin(), command.end());
  71. cmd.push_back('\0');
  72. if (!CreateProcessA(NULL, cmd.data(), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
  73. throw std::runtime_error("Failed to create process");
  74. }
  75. CloseHandle(stdout_write);
  76. CloseHandle(stderr_write);
  77. std::array<char, 128> buffer;
  78. DWORD bytes_read;
  79. while (ReadFile(stdout_read, buffer.data(), buffer.size(), &bytes_read, NULL) && bytes_read > 0) {
  80. stdout_str.append(buffer.data(), bytes_read);
  81. }
  82. while (ReadFile(stderr_read, buffer.data(), buffer.size(), &bytes_read, NULL) && bytes_read > 0) {
  83. stderr_str.append(buffer.data(), bytes_read);
  84. }
  85. CloseHandle(stdout_read);
  86. CloseHandle(stderr_read);
  87. WaitForSingleObject(pi.hProcess, INFINITE);
  88. CloseHandle(pi.hProcess);
  89. CloseHandle(pi.hThread);
  90. #else
  91. int stdout_pipe[2];
  92. int stderr_pipe[2];
  93. if (pipe(stdout_pipe) != 0 || pipe(stderr_pipe) != 0) {
  94. throw std::runtime_error("Failed to create pipes");
  95. }
  96. pid_t pid = fork();
  97. if (pid < 0) {
  98. throw std::runtime_error("Failed to fork process");
  99. }
  100. if (pid == 0) {
  101. close(stdout_pipe[0]);
  102. close(stderr_pipe[0]);
  103. dup2(stdout_pipe[1], STDOUT_FILENO);
  104. dup2(stderr_pipe[1], STDERR_FILENO);
  105. close(stdout_pipe[1]);
  106. close(stderr_pipe[1]);
  107. execl("/bin/sh", "sh", "-c", command.c_str(), (char*) nullptr);
  108. _exit(EXIT_FAILURE);
  109. } else {
  110. close(stdout_pipe[1]);
  111. close(stderr_pipe[1]);
  112. std::array<char, 128> buffer;
  113. ssize_t bytes_read;
  114. while ((bytes_read = read(stdout_pipe[0], buffer.data(), buffer.size())) > 0) {
  115. stdout_str.append(buffer.data(), bytes_read);
  116. }
  117. while ((bytes_read = read(stderr_pipe[0], buffer.data(), buffer.size())) > 0) {
  118. stderr_str.append(buffer.data(), bytes_read);
  119. }
  120. close(stdout_pipe[0]);
  121. close(stderr_pipe[0]);
  122. waitpid(pid, nullptr, 0);
  123. }
  124. #endif
  125. }
  126. bool directory_exists(const std::string& path) {
  127. struct stat info;
  128. if (stat(path.c_str(), &info) != 0) {
  129. return false; // Path doesn't exist or can't be accessed
  130. }
  131. return (info.st_mode & S_IFDIR) != 0; // Check if it is a directory
  132. }
  133. bool create_directory(const std::string& path) {
  134. #ifdef _WIN32
  135. return _mkdir(path.c_str()) == 0 || errno == EEXIST; // EEXIST means the directory already exists
  136. #else
  137. return mkdir(path.c_str(), 0755) == 0 || errno == EEXIST; // 0755 is the directory permissions
  138. #endif
  139. }
  140. std::string to_uppercase(const std::string& input) {
  141. std::string result = input;
  142. for (char& c : result) {
  143. c = std::toupper(c);
  144. }
  145. return result;
  146. }
  147. bool string_ends_with(const std::string& str, const std::string& suffix) {
  148. if (suffix.size() > str.size()) {
  149. return false;
  150. }
  151. return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
  152. }
  153. static const char path_separator = '/';
  154. std::string join_paths(const std::string& path1, const std::string& path2) {
  155. return path1 + path_separator + path2;
  156. }
  157. std::string basename(const std::string &path) {
  158. return path.substr(path.find_last_of("/\\") + 1);
  159. }
  160. void string_to_spv(const std::string& _name, const std::string& in_fname, const std::map<std::string, std::string>& defines, bool fp16 = true) {
  161. std::string name = _name + (fp16 ? "" : "_fp32");
  162. std::string out_fname = join_paths(output_dir, name + ".spv");
  163. std::string in_path = join_paths(input_dir, in_fname);
  164. #ifdef _WIN32
  165. std::vector<std::string> cmd = {GLSLC, "-fshader-stage=compute", "--target-env=vulkan1.2", "-O", "\"" + in_path + "\"", "-o", "\"" + out_fname + "\""};
  166. #else
  167. std::vector<std::string> cmd = {GLSLC, "-fshader-stage=compute", "--target-env=vulkan1.2", "-O", in_path, "-o", out_fname};
  168. #endif
  169. for (const auto& define : defines) {
  170. cmd.push_back("-D" + define.first + "=" + define.second);
  171. }
  172. std::string command;
  173. for (const auto& part : cmd) {
  174. command += part + " ";
  175. }
  176. std::string stdout_str, stderr_str;
  177. try {
  178. // std::cout << "Executing command: ";
  179. // for (const auto& part : cmd) {
  180. // std::cout << part << " ";
  181. // }
  182. // std::cout << std::endl;
  183. execute_command(command, stdout_str, stderr_str);
  184. if (!stderr_str.empty()) {
  185. std::cerr << "cannot compile " << name << "\n\n" << command << "\n\n" << stderr_str << std::endl;
  186. return;
  187. }
  188. std::lock_guard<std::mutex> guard(lock);
  189. shader_fnames.push_back(std::make_pair(name, out_fname));
  190. } catch (const std::exception& e) {
  191. std::cerr << "Error executing command for " << name << ": " << e.what() << std::endl;
  192. }
  193. }
  194. std::map<std::string, std::string> merge_maps(const std::map<std::string, std::string>& a, const std::map<std::string, std::string>& b) {
  195. std::map<std::string, std::string> result = a;
  196. result.insert(b.begin(), b.end());
  197. return result;
  198. }
  199. void matmul_shaders(std::vector<std::future<void>>& tasks, bool fp16, bool matmul_id) {
  200. std::string load_vec = fp16 ? "8" : "4";
  201. std::string aligned_b_type_f32 = fp16 ? "mat2x4" : "vec4";
  202. std::string aligned_b_type_f16 = fp16 ? "f16mat2x4" : "f16vec4";
  203. std::map<std::string, std::string> base_dict = {{"FLOAT_TYPE", fp16 ? "float16_t" : "float"}};
  204. std::string shader_name = "matmul";
  205. if (matmul_id) {
  206. base_dict["MUL_MAT_ID"] = "1";
  207. shader_name = "matmul_id";
  208. }
  209. if (fp16) {
  210. base_dict["FLOAT16"] = "1";
  211. }
  212. // Shaders with f16 B_TYPE
  213. tasks.push_back(std::async(std::launch::async, [=] {
  214. string_to_spv(shader_name + "_f32_f16", "mul_mm.comp", merge_maps(base_dict, {{"DATA_A_F32", "1"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float"}}), fp16);
  215. }));
  216. tasks.push_back(std::async(std::launch::async, [=] {
  217. string_to_spv(shader_name + "_f32_f16_aligned", "mul_mm.comp", merge_maps(base_dict, {{"DATA_A_F32", "1"}, {"LOAD_VEC_A", load_vec}, {"LOAD_VEC_B", load_vec}, {"B_TYPE", aligned_b_type_f16}, {"D_TYPE", "float"}}), fp16);
  218. }));
  219. tasks.push_back(std::async(std::launch::async, [=] {
  220. string_to_spv(shader_name + "_f16", "mul_mm.comp", merge_maps(base_dict, {{"DATA_A_F16", "1"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float"}}), fp16);
  221. }));
  222. tasks.push_back(std::async(std::launch::async, [=] {
  223. string_to_spv(shader_name + "_f16_aligned", "mul_mm.comp", merge_maps(base_dict, {{"DATA_A_F16", "1"}, {"LOAD_VEC_A", load_vec}, {"LOAD_VEC_B", load_vec}, {"B_TYPE", aligned_b_type_f16}, {"D_TYPE", "float"}}), fp16);
  224. }));
  225. for (const auto& tname : type_names) {
  226. std::string data_a_key = "DATA_A_" + to_uppercase(tname);
  227. // For unaligned, load one at a time for f32/f16, or two at a time for quants
  228. std::string load_vec_a_unaligned = (tname == "f32" || tname == "f16") ? "1" : "2";
  229. // For aligned matmul loads
  230. std::string load_vec_a = (tname == "f32" || tname == "f16") ? load_vec : "2";
  231. tasks.push_back(std::async(std::launch::async, [=] {
  232. string_to_spv(shader_name + "_" + tname + "_f32", "mul_mm.comp", merge_maps(base_dict, {{data_a_key, "1"}, {"LOAD_VEC_A", load_vec_a_unaligned}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}), fp16);
  233. }));
  234. tasks.push_back(std::async(std::launch::async, [=] {
  235. string_to_spv(shader_name + "_" + tname + "_f32_aligned", "mul_mm.comp", merge_maps(base_dict, {{data_a_key, "1"}, {"LOAD_VEC_A", load_vec_a}, {"LOAD_VEC_B", load_vec}, {"B_TYPE", aligned_b_type_f32}, {"D_TYPE", "float"}}), fp16);
  236. }));
  237. }
  238. }
  239. void process_shaders(std::vector<std::future<void>>& tasks) {
  240. std::cout << "ggml_vulkan: Generating and compiling shaders to SPIR-V" << std::endl;
  241. std::map<std::string, std::string> base_dict = {{"FLOAT_TYPE", "float"}};
  242. for (const auto& fp16 : {false, true}) {
  243. matmul_shaders(tasks, fp16, false);
  244. matmul_shaders(tasks, fp16, true);
  245. }
  246. for (const auto& tname : type_names) {
  247. // mul mat vec
  248. std::string data_a_key = "DATA_A_" + to_uppercase(tname);
  249. std::string shader = (string_ends_with(tname, "_k")) ? "mul_mat_vec_" + tname + ".comp" : "mul_mat_vec.comp";
  250. tasks.push_back(std::async(std::launch::async, [=] {
  251. string_to_spv("mul_mat_vec_" + tname + "_f32_f32", shader, merge_maps(base_dict, {{data_a_key, "1"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));
  252. }));
  253. tasks.push_back(std::async(std::launch::async, [=] {
  254. string_to_spv("mul_mat_vec_" + tname + "_f16_f32", shader, merge_maps(base_dict, {{data_a_key, "1"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float"}}));
  255. }));
  256. tasks.push_back(std::async(std::launch::async, [=] {
  257. string_to_spv("mul_mat_vec_id_" + tname + "_f32", shader, merge_maps(base_dict, {{"MUL_MAT_ID", "1"}, {data_a_key, "1"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));
  258. }));
  259. // Dequant shaders
  260. if (tname != "f16") {
  261. tasks.push_back(std::async(std::launch::async, [=] {
  262. string_to_spv("dequant_" + tname, "dequant_" + tname + ".comp", merge_maps(base_dict, {{data_a_key, "1"}, {"D_TYPE", "float16_t"}}));
  263. }));
  264. }
  265. if (!string_ends_with(tname, "_k")) {
  266. shader = (tname == "f32" || tname == "f16") ? "get_rows.comp" : "get_rows_quant.comp";
  267. if (tname == "f16") {
  268. tasks.push_back(std::async(std::launch::async, [=] {
  269. string_to_spv("get_rows_" + tname, shader, {{data_a_key, "1"}, {"B_TYPE", "int"}, {"D_TYPE", "float16_t"}, {"OPTIMIZATION_ERROR_WORKAROUND", "1"}});
  270. }));
  271. } else {
  272. tasks.push_back(std::async(std::launch::async, [=] {
  273. string_to_spv("get_rows_" + tname, shader, {{data_a_key, "1"}, {"B_TYPE", "int"}, {"D_TYPE", "float16_t"}});
  274. }));
  275. }
  276. tasks.push_back(std::async(std::launch::async, [=] {
  277. string_to_spv("get_rows_" + tname + "_f32", shader, {{data_a_key, "1"}, {"B_TYPE", "int"}, {"D_TYPE", "float"}});
  278. }));
  279. }
  280. }
  281. tasks.push_back(std::async(std::launch::async, [] {
  282. string_to_spv("mul_mat_vec_p021_f16_f32", "mul_mat_vec_p021.comp", {{"A_TYPE", "float16_t"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}});
  283. }));
  284. tasks.push_back(std::async(std::launch::async, [] {
  285. string_to_spv("mul_mat_vec_nc_f16_f32", "mul_mat_vec_nc.comp", {{"A_TYPE", "float16_t"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}});
  286. }));
  287. // Norms
  288. tasks.push_back(std::async(std::launch::async, [=] {
  289. string_to_spv("norm_f32", "norm.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float"}}));
  290. }));
  291. tasks.push_back(std::async(std::launch::async, [=] {
  292. string_to_spv("group_norm_f32", "group_norm.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float"}}));
  293. }));
  294. tasks.push_back(std::async(std::launch::async, [=] {
  295. string_to_spv("rms_norm_f32", "rms_norm.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float"}}));
  296. }));
  297. tasks.push_back(std::async(std::launch::async, [] {
  298. string_to_spv("cpy_f32_f32", "copy.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  299. }));
  300. tasks.push_back(std::async(std::launch::async, [] {
  301. string_to_spv("cpy_f32_f16", "copy.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float16_t"}});
  302. }));
  303. tasks.push_back(std::async(std::launch::async, [] {
  304. string_to_spv("cpy_f16_f16", "copy.comp", {{"A_TYPE", "float16_t"}, {"D_TYPE", "float16_t"}, {"OPTIMIZATION_ERROR_WORKAROUND", "1"}});
  305. }));
  306. tasks.push_back(std::async(std::launch::async, [] {
  307. string_to_spv("add_f32", "add.comp", {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
  308. }));
  309. tasks.push_back(std::async(std::launch::async, [] {
  310. string_to_spv("add_f16_f32_f16", "add.comp", {{"A_TYPE", "float16_t"}, {"B_TYPE", "float"}, {"D_TYPE", "float16_t"}, {"FLOAT_TYPE", "float"}});
  311. }));
  312. tasks.push_back(std::async(std::launch::async, [] {
  313. string_to_spv("split_k_reduce", "mul_mat_split_k_reduce.comp", {});
  314. }));
  315. tasks.push_back(std::async(std::launch::async, [] {
  316. string_to_spv("mul_f32", "mul.comp", {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
  317. }));
  318. tasks.push_back(std::async(std::launch::async, [] {
  319. string_to_spv("div_f32", "div.comp", {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
  320. }));
  321. tasks.push_back(std::async(std::launch::async, [] {
  322. string_to_spv("repeat_f32", "repeat.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  323. }));
  324. tasks.push_back(std::async(std::launch::async, [] {
  325. string_to_spv("scale_f32", "scale.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
  326. }));
  327. tasks.push_back(std::async(std::launch::async, [] {
  328. string_to_spv("sqr_f32", "square.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
  329. }));
  330. tasks.push_back(std::async(std::launch::async, [] {
  331. string_to_spv("clamp_f32", "clamp.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
  332. }));
  333. tasks.push_back(std::async(std::launch::async, [] {
  334. string_to_spv("pad_f32", "pad.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  335. }));
  336. tasks.push_back(std::async(std::launch::async, [] {
  337. string_to_spv("concat_f32", "concat.comp", {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}});
  338. }));
  339. tasks.push_back(std::async(std::launch::async, [] {
  340. string_to_spv("concat_f16", "concat.comp", {{"A_TYPE", "float16_t"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float16_t"}, {"OPTIMIZATION_ERROR_WORKAROUND", "1"}});
  341. }));
  342. tasks.push_back(std::async(std::launch::async, [] {
  343. string_to_spv("concat_i32", "concat.comp", {{"A_TYPE", "int"}, {"B_TYPE", "int"}, {"D_TYPE", "int"}});
  344. }));
  345. tasks.push_back(std::async(std::launch::async, [] {
  346. string_to_spv("upscale_f32", "upscale.comp", {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}});
  347. }));
  348. tasks.push_back(std::async(std::launch::async, [] {
  349. string_to_spv("gelu_f32", "gelu.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  350. }));
  351. tasks.push_back(std::async(std::launch::async, [] {
  352. string_to_spv("gelu_quick_f32", "gelu_quick.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  353. }));
  354. tasks.push_back(std::async(std::launch::async, [] {
  355. string_to_spv("silu_f32", "silu.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  356. }));
  357. tasks.push_back(std::async(std::launch::async, [] {
  358. string_to_spv("relu_f32", "relu.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  359. }));
  360. tasks.push_back(std::async(std::launch::async, [] {
  361. string_to_spv("leaky_relu_f32", "leaky_relu.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  362. }));
  363. tasks.push_back(std::async(std::launch::async, [] {
  364. string_to_spv("tanh_f32", "tanh.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  365. }));
  366. tasks.push_back(std::async(std::launch::async, [] {
  367. string_to_spv("diag_mask_inf_f32", "diag_mask_inf.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  368. }));
  369. tasks.push_back(std::async(std::launch::async, [=] {
  370. string_to_spv("soft_max_f32", "soft_max.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));
  371. }));
  372. tasks.push_back(std::async(std::launch::async, [=] {
  373. string_to_spv("soft_max_f32_f16", "soft_max.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float"}}));
  374. }));
  375. tasks.push_back(std::async(std::launch::async, [] {
  376. string_to_spv("rope_norm_f32", "rope_norm.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  377. }));
  378. tasks.push_back(std::async(std::launch::async, [] {
  379. string_to_spv("rope_norm_f16", "rope_norm.comp", {{"A_TYPE", "float16_t"}, {"D_TYPE", "float16_t"}});
  380. }));
  381. tasks.push_back(std::async(std::launch::async, [] {
  382. string_to_spv("rope_neox_f32", "rope_neox.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
  383. }));
  384. tasks.push_back(std::async(std::launch::async, [] {
  385. string_to_spv("rope_neox_f16", "rope_neox.comp", {{"A_TYPE", "float16_t"}, {"D_TYPE", "float16_t"}});
  386. }));
  387. tasks.push_back(std::async(std::launch::async, [] {
  388. string_to_spv("argsort_f32", "argsort.comp", {{"A_TYPE", "float"}});
  389. }));
  390. tasks.push_back(std::async(std::launch::async, [=] {
  391. string_to_spv("sum_rows_f32", "sum_rows.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float"}}));
  392. }));
  393. tasks.push_back(std::async(std::launch::async, [=] {
  394. string_to_spv("im2col_f32", "im2col.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float"}}));
  395. }));
  396. tasks.push_back(std::async(std::launch::async, [=] {
  397. string_to_spv("im2col_f32_f16", "im2col.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float16_t"}}));
  398. }));
  399. tasks.push_back(std::async(std::launch::async, [=] {
  400. string_to_spv("timestep_embedding_f32", "timestep_embedding.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float"}}));
  401. }));
  402. }
  403. void write_output_files() {
  404. FILE* hdr = fopen(target_hpp.c_str(), "w");
  405. FILE* src = fopen(target_cpp.c_str(), "w");
  406. fprintf(hdr, "#include <cstdint>\n\n");
  407. fprintf(src, "#include \"%s\"\n\n", basename(target_hpp).c_str());
  408. for (const auto& pair : shader_fnames) {
  409. const std::string& name = pair.first;
  410. #ifdef _WIN32
  411. std::string path = pair.second;
  412. std::replace(path.begin(), path.end(), '/', '\\' );
  413. #else
  414. const std::string& path = pair.second;
  415. #endif
  416. FILE* spv = fopen(path.c_str(), "rb");
  417. if (!spv) {
  418. std::cerr << "Error opening SPIR-V file: " << path << " (" << strerror(errno) << ")\n";
  419. continue;
  420. }
  421. fseek(spv, 0, SEEK_END);
  422. size_t size = ftell(spv);
  423. fseek(spv, 0, SEEK_SET);
  424. std::vector<unsigned char> data(size);
  425. size_t read_size = fread(data.data(), 1, size, spv);
  426. fclose(spv);
  427. if (read_size != size) {
  428. std::cerr << "Error reading SPIR-V file: " << path << " (" << strerror(errno) << ")\n";
  429. continue;
  430. }
  431. fprintf(hdr, "extern unsigned char %s_data[%zu];\n", name.c_str(), size);
  432. fprintf(hdr, "const uint64_t %s_len = %zu;\n\n", name.c_str(), size);
  433. fprintf(src, "unsigned char %s_data[%zu] = {\n", name.c_str(), size);
  434. for (size_t i = 0; i < size; ++i) {
  435. fprintf(src, "0x%02x,", data[i]);
  436. if ((i + 1) % 12 == 0) fprintf(src, "\n");
  437. }
  438. fprintf(src, "\n};\n\n");
  439. if (!no_clean) {
  440. std::remove(path.c_str());
  441. }
  442. }
  443. fclose(hdr);
  444. fclose(src);
  445. }
  446. int main(int argc, char** argv) {
  447. std::map<std::string, std::string> args;
  448. for (int i = 1; i < argc; i += 2) {
  449. if (i + 1 < argc) {
  450. args[argv[i]] = argv[i + 1];
  451. }
  452. }
  453. if (args.find("--glslc") != args.end()) {
  454. GLSLC = args["--glslc"]; // Path to glslc
  455. }
  456. if (args.find("--input-dir") != args.end()) {
  457. input_dir = args["--input-dir"]; // Directory containing shader sources
  458. }
  459. if (args.find("--output-dir") != args.end()) {
  460. output_dir = args["--output-dir"]; // Directory for containing SPIR-V output
  461. }
  462. if (args.find("--target-hpp") != args.end()) {
  463. target_hpp = args["--target-hpp"]; // Path to generated header file
  464. }
  465. if (args.find("--target-cpp") != args.end()) {
  466. target_cpp = args["--target-cpp"]; // Path to generated cpp file
  467. }
  468. if (args.find("--no-clean") != args.end()) {
  469. no_clean = true; // Keep temporary SPIR-V files in output-dir after build
  470. }
  471. if (!directory_exists(input_dir)) {
  472. std::cerr << "\"" << input_dir << "\" must be a valid directory containing shader sources" << std::endl;
  473. return EXIT_FAILURE;
  474. }
  475. if (!directory_exists(output_dir)) {
  476. if (!create_directory(output_dir)) {
  477. std::cerr << "Error creating output directory: " << output_dir << "\n";
  478. return EXIT_FAILURE;
  479. }
  480. }
  481. std::vector<std::future<void>> tasks;
  482. process_shaders(tasks);
  483. for (auto& task : tasks) {
  484. task.get();
  485. }
  486. write_output_files();
  487. return EXIT_SUCCESS;
  488. }