main.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. #include "ggml.h"
  2. #include "utils.h"
  3. #include <cassert>
  4. #include <cinttypes>
  5. #include <cmath>
  6. #include <cstdio>
  7. #include <cstring>
  8. #include <fstream>
  9. #include <iostream>
  10. #include <map>
  11. #include <string>
  12. #include <vector>
  13. #if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
  14. #include <signal.h>
  15. #include <unistd.h>
  16. #elif defined (_WIN32)
  17. #include <signal.h>
  18. #endif
  19. #if defined (_WIN32)
  20. #pragma comment(lib,"kernel32.lib")
  21. extern "C" __declspec(dllimport) void* __stdcall GetStdHandle(unsigned long nStdHandle);
  22. extern "C" __declspec(dllimport) int __stdcall GetConsoleMode(void* hConsoleHandle, unsigned long* lpMode);
  23. extern "C" __declspec(dllimport) int __stdcall SetConsoleMode(void* hConsoleHandle, unsigned long dwMode);
  24. #endif
  25. #define ANSI_COLOR_RED "\x1b[31m"
  26. #define ANSI_COLOR_GREEN "\x1b[32m"
  27. #define ANSI_COLOR_YELLOW "\x1b[33m"
  28. #define ANSI_COLOR_BLUE "\x1b[34m"
  29. #define ANSI_COLOR_MAGENTA "\x1b[35m"
  30. #define ANSI_COLOR_CYAN "\x1b[36m"
  31. #define ANSI_COLOR_RESET "\x1b[0m"
  32. #define ANSI_BOLD "\x1b[1m"
  33. static const int EOS_TOKEN_ID = 2;
  34. // determine number of model parts based on the dimension
  35. static const std::map<int, int> LLAMA_N_PARTS = {
  36. { 4096, 1 },
  37. { 5120, 2 },
  38. { 6656, 4 },
  39. { 8192, 8 },
  40. };
  41. // default hparams (LLaMA 7B)
  42. struct llama_hparams {
  43. int32_t n_vocab = 32000;
  44. int32_t n_ctx = 512; // this is provided as user input?
  45. int32_t n_embd = 4096;
  46. int32_t n_mult = 256;
  47. int32_t n_head = 32;
  48. int32_t n_layer = 32;
  49. int32_t n_rot = 64;
  50. int32_t f16 = 1;
  51. };
  52. struct llama_layer {
  53. // normalization
  54. struct ggml_tensor * attention_norm;
  55. // attention
  56. struct ggml_tensor * wq;
  57. struct ggml_tensor * wk;
  58. struct ggml_tensor * wv;
  59. struct ggml_tensor * wo;
  60. // normalization
  61. struct ggml_tensor * ffn_norm;
  62. // ff
  63. struct ggml_tensor * w1;
  64. struct ggml_tensor * w2;
  65. struct ggml_tensor * w3;
  66. };
  67. struct llama_model {
  68. llama_hparams hparams;
  69. struct ggml_tensor * tok_embeddings;
  70. struct ggml_tensor * norm;
  71. struct ggml_tensor * output;
  72. std::vector<llama_layer> layers;
  73. // key + value memory
  74. struct ggml_tensor * memory_k;
  75. struct ggml_tensor * memory_v;
  76. //
  77. struct ggml_context * ctx;
  78. std::map<std::string, struct ggml_tensor *> tensors;
  79. };
  80. // load the model's weights from a file
  81. bool llama_model_load(const std::string & fname, llama_model & model, llama_vocab & vocab, int n_ctx, int n_parts, ggml_type memory_type = GGML_TYPE_F32) {
  82. fprintf(stderr, "%s: loading model from '%s' - please wait ...\n", __func__, fname.c_str());
  83. std::vector<char> f_buf(1024*1024);
  84. auto fin = std::ifstream(fname, std::ios::binary);
  85. fin.rdbuf()->pubsetbuf(f_buf.data(), f_buf.size());
  86. if (!fin) {
  87. fprintf(stderr, "%s: failed to open '%s'\n", __func__, fname.c_str());
  88. return false;
  89. }
  90. // verify magic
  91. {
  92. uint32_t magic;
  93. fin.read((char *) &magic, sizeof(magic));
  94. if (magic == FILE_MAGIC_UNVERSIONED) {
  95. fprintf(stderr, "%s: invalid model file '%s' (too old, regenerate your model files!)\n",
  96. __func__, fname.c_str());
  97. return false;
  98. }
  99. if (magic != FILE_MAGIC) {
  100. fprintf(stderr, "%s: invalid model file '%s' (bad magic)\n", __func__, fname.c_str());
  101. return false;
  102. }
  103. uint32_t format_version;
  104. fin.read((char *) &format_version, sizeof(format_version));
  105. if (format_version != FILE_VERSION) {
  106. fprintf(stderr, "%s: invalid model file '%s' (unsupported format version %" PRIu32 ", expected %d)\n",
  107. __func__, fname.c_str(), format_version, FILE_VERSION);
  108. return false;
  109. }
  110. }
  111. int n_ff = 0;
  112. // load hparams
  113. {
  114. auto & hparams = model.hparams;
  115. fin.read((char *) &hparams.n_vocab, sizeof(hparams.n_vocab));
  116. //fin.read((char *) &hparams.n_ctx, sizeof(hparams.n_ctx));
  117. fin.read((char *) &hparams.n_embd, sizeof(hparams.n_embd));
  118. fin.read((char *) &hparams.n_mult, sizeof(hparams.n_mult));
  119. fin.read((char *) &hparams.n_head, sizeof(hparams.n_head));
  120. fin.read((char *) &hparams.n_layer, sizeof(hparams.n_layer));
  121. fin.read((char *) &hparams.n_rot, sizeof(hparams.n_rot));
  122. fin.read((char *) &hparams.f16, sizeof(hparams.f16));
  123. hparams.n_ctx = n_ctx;
  124. n_ff = ((2*(4*hparams.n_embd)/3 + hparams.n_mult - 1)/hparams.n_mult)*hparams.n_mult;
  125. if (n_parts < 1) {
  126. n_parts = LLAMA_N_PARTS.at(hparams.n_embd);
  127. }
  128. fprintf(stderr, "%s: n_vocab = %d\n", __func__, hparams.n_vocab);
  129. fprintf(stderr, "%s: n_ctx = %d\n", __func__, hparams.n_ctx);
  130. fprintf(stderr, "%s: n_embd = %d\n", __func__, hparams.n_embd);
  131. fprintf(stderr, "%s: n_mult = %d\n", __func__, hparams.n_mult);
  132. fprintf(stderr, "%s: n_head = %d\n", __func__, hparams.n_head);
  133. fprintf(stderr, "%s: n_layer = %d\n", __func__, hparams.n_layer);
  134. fprintf(stderr, "%s: n_rot = %d\n", __func__, hparams.n_rot);
  135. fprintf(stderr, "%s: f16 = %d\n", __func__, hparams.f16);
  136. fprintf(stderr, "%s: n_ff = %d\n", __func__, n_ff);
  137. fprintf(stderr, "%s: n_parts = %d\n", __func__, n_parts);
  138. }
  139. // load vocab
  140. {
  141. std::string word;
  142. std::vector<char> tmp(64);
  143. for (int i = 0; i < model.hparams.n_vocab; i++) {
  144. uint32_t len;
  145. fin.read((char *) &len, sizeof(len));
  146. word.resize(len);
  147. if (len > 0) {
  148. tmp.resize(len);
  149. fin.read(tmp.data(), len);
  150. word.assign(tmp.data(), len);
  151. } else {
  152. word.clear();
  153. }
  154. float score;
  155. fin.read((char *) &score, sizeof(score));
  156. vocab.token_to_id[word] = i;
  157. vocab.id_to_token[i] = word;
  158. vocab.score[i] = score;
  159. }
  160. }
  161. // for the big tensors, we have the option to store the data in 16-bit floats or quantized
  162. // in order to save memory and also to speed up the computation
  163. ggml_type wtype = GGML_TYPE_COUNT;
  164. switch (model.hparams.f16) {
  165. case 0: wtype = GGML_TYPE_F32; break;
  166. case 1: wtype = GGML_TYPE_F16; break;
  167. case 2: wtype = GGML_TYPE_Q4_0; break;
  168. case 3: wtype = GGML_TYPE_Q4_1; break;
  169. default:
  170. {
  171. fprintf(stderr, "%s: invalid model file '%s' (bad f16 value %d)\n",
  172. __func__, fname.c_str(), model.hparams.f16);
  173. return false;
  174. }
  175. }
  176. auto & ctx = model.ctx;
  177. size_t ctx_size = 0;
  178. {
  179. const auto & hparams = model.hparams;
  180. const int n_embd = hparams.n_embd;
  181. const int n_layer = hparams.n_layer;
  182. const int n_ctx = hparams.n_ctx;
  183. const int n_vocab = hparams.n_vocab;
  184. ctx_size += n_embd*n_vocab*ggml_type_sizef(wtype); // tok_embeddings
  185. ctx_size += n_embd*ggml_type_sizef(GGML_TYPE_F32); // norm
  186. ctx_size += n_embd*n_vocab*ggml_type_sizef(wtype); // output
  187. ctx_size += n_layer*(n_embd*ggml_type_sizef(GGML_TYPE_F32)); // attention_norm
  188. ctx_size += n_layer*(n_embd*n_embd*ggml_type_sizef(wtype)); // wq
  189. ctx_size += n_layer*(n_embd*n_embd*ggml_type_sizef(wtype)); // wk
  190. ctx_size += n_layer*(n_embd*n_embd*ggml_type_sizef(wtype)); // wv
  191. ctx_size += n_layer*(n_embd*n_embd*ggml_type_sizef(wtype)); // wo
  192. ctx_size += n_layer*(n_embd*ggml_type_sizef(GGML_TYPE_F32)); // ffn_norm
  193. ctx_size += n_layer*(n_ff*n_embd*ggml_type_sizef(wtype)); // w1
  194. ctx_size += n_layer*(n_ff*n_embd*ggml_type_sizef(wtype)); // w2
  195. ctx_size += n_layer*(n_ff*n_embd*ggml_type_sizef(wtype)); // w3
  196. ctx_size += n_ctx*n_layer*n_embd*ggml_type_sizef(memory_type); // memory_k
  197. ctx_size += n_ctx*n_layer*n_embd*ggml_type_sizef(memory_type); // memory_v
  198. ctx_size += (5 + 10*n_layer)*256; // object overhead
  199. fprintf(stderr, "%s: ggml ctx size = %6.2f MB\n", __func__, ctx_size/(1024.0*1024.0));
  200. }
  201. // create the ggml context
  202. {
  203. struct ggml_init_params params = {
  204. /*.mem_size =*/ ctx_size,
  205. /*.mem_buffer =*/ NULL,
  206. };
  207. model.ctx = ggml_init(params);
  208. if (!model.ctx) {
  209. fprintf(stderr, "%s: ggml_init() failed\n", __func__);
  210. return false;
  211. }
  212. }
  213. // prepare memory for the weights
  214. {
  215. const auto & hparams = model.hparams;
  216. const int n_embd = hparams.n_embd;
  217. const int n_layer = hparams.n_layer;
  218. const int n_vocab = hparams.n_vocab;
  219. model.layers.resize(n_layer);
  220. model.tok_embeddings = ggml_new_tensor_2d(ctx, wtype, n_embd, n_vocab);
  221. model.norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_embd);
  222. model.output = ggml_new_tensor_2d(ctx, wtype, n_embd, n_vocab);
  223. // map by name
  224. model.tensors["tok_embeddings.weight"] = model.tok_embeddings;
  225. model.tensors["norm.weight"] = model.norm;
  226. model.tensors["output.weight"] = model.output;
  227. for (int i = 0; i < n_layer; ++i) {
  228. auto & layer = model.layers[i];
  229. layer.attention_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_embd);
  230. layer.wq = ggml_new_tensor_2d(ctx, wtype, n_embd, n_embd);
  231. layer.wk = ggml_new_tensor_2d(ctx, wtype, n_embd, n_embd);
  232. layer.wv = ggml_new_tensor_2d(ctx, wtype, n_embd, n_embd);
  233. layer.wo = ggml_new_tensor_2d(ctx, wtype, n_embd, n_embd);
  234. layer.ffn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_embd);
  235. layer.w1 = ggml_new_tensor_2d(ctx, wtype, n_embd, n_ff);
  236. layer.w2 = ggml_new_tensor_2d(ctx, wtype, n_ff, n_embd);
  237. layer.w3 = ggml_new_tensor_2d(ctx, wtype, n_embd, n_ff);
  238. // map by name
  239. model.tensors["layers." + std::to_string(i) + ".attention_norm.weight"] = layer.attention_norm;
  240. model.tensors["layers." + std::to_string(i) + ".attention.wq.weight"] = layer.wq;
  241. model.tensors["layers." + std::to_string(i) + ".attention.wk.weight"] = layer.wk;
  242. model.tensors["layers." + std::to_string(i) + ".attention.wv.weight"] = layer.wv;
  243. model.tensors["layers." + std::to_string(i) + ".attention.wo.weight"] = layer.wo;
  244. model.tensors["layers." + std::to_string(i) + ".ffn_norm.weight"] = layer.ffn_norm;
  245. model.tensors["layers." + std::to_string(i) + ".feed_forward.w1.weight"] = layer.w1;
  246. model.tensors["layers." + std::to_string(i) + ".feed_forward.w2.weight"] = layer.w2;
  247. model.tensors["layers." + std::to_string(i) + ".feed_forward.w3.weight"] = layer.w3;
  248. }
  249. }
  250. // key + value memory
  251. {
  252. const auto & hparams = model.hparams;
  253. const int n_embd = hparams.n_embd;
  254. const int n_layer = hparams.n_layer;
  255. const int n_ctx = hparams.n_ctx;
  256. const int n_mem = n_layer*n_ctx;
  257. const int n_elements = n_embd*n_mem;
  258. model.memory_k = ggml_new_tensor_1d(ctx, memory_type, n_elements);
  259. model.memory_v = ggml_new_tensor_1d(ctx, memory_type, n_elements);
  260. const size_t memory_size = ggml_nbytes(model.memory_k) + ggml_nbytes(model.memory_v);
  261. fprintf(stderr, "%s: memory_size = %8.2f MB, n_mem = %d\n", __func__, memory_size/1024.0/1024.0, n_mem);
  262. }
  263. const size_t file_offset = fin.tellg();
  264. fin.close();
  265. std::vector<uint8_t> tmp;
  266. for (int i = 0; i < n_parts; ++i) {
  267. const int part_id = i;
  268. //const int part_id = n_parts - i - 1;
  269. std::string fname_part = fname;
  270. if (i > 0) {
  271. fname_part += "." + std::to_string(i);
  272. }
  273. fprintf(stderr, "%s: loading model part %d/%d from '%s'\n", __func__, i+1, n_parts, fname_part.c_str());
  274. fin = std::ifstream(fname_part, std::ios::binary);
  275. fin.rdbuf()->pubsetbuf(f_buf.data(), f_buf.size());
  276. fin.seekg(file_offset);
  277. // load weights
  278. {
  279. int n_tensors = 0;
  280. size_t total_size = 0;
  281. fprintf(stderr, "%s: ", __func__);
  282. while (true) {
  283. int32_t n_dims;
  284. int32_t length;
  285. int32_t ftype;
  286. fin.read(reinterpret_cast<char *>(&n_dims), sizeof(n_dims));
  287. fin.read(reinterpret_cast<char *>(&length), sizeof(length));
  288. fin.read(reinterpret_cast<char *>(&ftype), sizeof(ftype));
  289. if (fin.eof()) {
  290. break;
  291. }
  292. int32_t nelements = 1;
  293. int32_t ne[2] = { 1, 1 };
  294. for (int i = 0; i < n_dims; ++i) {
  295. fin.read(reinterpret_cast<char *>(&ne[i]), sizeof(ne[i]));
  296. nelements *= ne[i];
  297. }
  298. std::string name(length, 0);
  299. fin.read(&name[0], length);
  300. if (model.tensors.find(name.data()) == model.tensors.end()) {
  301. fprintf(stderr, "%s: unknown tensor '%s' in model file\n", __func__, name.data());
  302. return false;
  303. }
  304. // split_type = 0: split by columns
  305. // split_type = 1: split by rows
  306. int split_type = 0;
  307. // split_type = 0:
  308. // regex:
  309. // - tok_embeddings.*
  310. // - layers.*.attention.wo.weight
  311. // - layers.*.feed_forward.w2.weight
  312. // split_type = 1:
  313. // regex:
  314. // - output.*
  315. // - layers.*.attention.wq.weight
  316. // - layers.*.attention.wk.weight
  317. // - layers.*.attention.wv.weight
  318. // - layers.*.feed_forward.w1.weight
  319. // - layers.*.feed_forward.w3.weight
  320. if (name.find("tok_embeddings") != std::string::npos) {
  321. split_type = 0;
  322. } else if (name.find("layers") != std::string::npos) {
  323. if (name.find("attention.wo.weight") != std::string::npos) {
  324. split_type = 0;
  325. } else if (name.find("feed_forward.w2.weight") != std::string::npos) {
  326. split_type = 0;
  327. } else {
  328. split_type = 1;
  329. }
  330. } else if (name.find("output") != std::string::npos) {
  331. split_type = 1;
  332. }
  333. auto tensor = model.tensors[name.data()];
  334. if (n_dims == 1) {
  335. if (ggml_nelements(tensor) != nelements) {
  336. fprintf(stderr, "%s: tensor '%s' has wrong size in model file\n", __func__, name.data());
  337. return false;
  338. }
  339. } else {
  340. if (ggml_nelements(tensor)/n_parts != nelements) {
  341. fprintf(stderr, "%s: tensor '%s' has wrong size in model file\n", __func__, name.data());
  342. return false;
  343. }
  344. }
  345. if (n_dims == 1) {
  346. if (tensor->ne[0] != ne[0] || tensor->ne[1] != ne[1]) {
  347. fprintf(stderr, "%s: tensor '%s' has wrong shape in model file: got [%d, %d], expected [%d, %d]\n",
  348. __func__, name.data(), tensor->ne[0], tensor->ne[1], ne[0], ne[1]);
  349. return false;
  350. }
  351. } else {
  352. if (split_type == 0) {
  353. if (tensor->ne[0]/n_parts != ne[0] || tensor->ne[1] != ne[1]) {
  354. fprintf(stderr, "%s: tensor '%s' has wrong shape in model file: got [%d, %d], expected [%d, %d]\n",
  355. __func__, name.data(), tensor->ne[0]/n_parts, tensor->ne[1], ne[0], ne[1]);
  356. return false;
  357. }
  358. } else {
  359. if (tensor->ne[0] != ne[0] || tensor->ne[1]/n_parts != ne[1]) {
  360. fprintf(stderr, "%s: tensor '%s' has wrong shape in model file: got [%d, %d], expected [%d, %d]\n",
  361. __func__, name.data(), tensor->ne[0], tensor->ne[1]/n_parts, ne[0], ne[1]);
  362. return false;
  363. }
  364. }
  365. }
  366. if (0) {
  367. static const char * ftype_str[] = { "f32", "f16", "q4_0", "q4_1", };
  368. fprintf(stderr, "%24s - [%5d, %5d], type = %6s, split = %d\n", name.data(), ne[0], ne[1], ftype_str[ftype], split_type);
  369. }
  370. size_t bpe = 0;
  371. switch (ftype) {
  372. case 0: bpe = ggml_type_size(GGML_TYPE_F32); break;
  373. case 1: bpe = ggml_type_size(GGML_TYPE_F16); break;
  374. case 2: bpe = ggml_type_size(GGML_TYPE_Q4_0); assert(ne[0] % 64 == 0); break;
  375. case 3: bpe = ggml_type_size(GGML_TYPE_Q4_1); assert(ne[0] % 64 == 0); break;
  376. default:
  377. {
  378. fprintf(stderr, "%s: unknown ftype %d in model file\n", __func__, ftype);
  379. return false;
  380. }
  381. };
  382. if (n_dims == 1 || n_parts == 1) {
  383. if ((nelements*bpe)/ggml_blck_size(tensor->type) != ggml_nbytes(tensor)) {
  384. fprintf(stderr, "%s: tensor '%s' has wrong size in model file: got %zu, expected %zu\n",
  385. __func__, name.data(), ggml_nbytes(tensor), nelements*bpe);
  386. return false;
  387. }
  388. if (part_id == 0) {
  389. fin.read(reinterpret_cast<char *>(tensor->data), ggml_nbytes(tensor));
  390. } else {
  391. fin.seekg(ggml_nbytes(tensor), std::ios::cur);
  392. }
  393. total_size += ggml_nbytes(tensor);
  394. } else {
  395. if ((nelements*bpe)/ggml_blck_size(tensor->type) != ggml_nbytes(tensor)/n_parts) {
  396. fprintf(stderr, "%s: tensor '%s' has wrong size in model file: got %zu, expected %zu\n",
  397. __func__, name.data(), ggml_nbytes(tensor)/n_parts, nelements*bpe);
  398. return false;
  399. }
  400. if (split_type == 0) {
  401. const int np0 = ne[0];
  402. const size_t row_size = (tensor->ne[0]/ggml_blck_size(tensor->type))*ggml_type_size(tensor->type);
  403. assert(row_size == tensor->nb[1]);
  404. for (int i1 = 0; i1 < ne[1]; ++i1) {
  405. const size_t offset_row = i1*row_size;
  406. const size_t offset = offset_row + ((part_id*np0)/ggml_blck_size(tensor->type))*ggml_type_size(tensor->type);
  407. fin.read(reinterpret_cast<char *>(tensor->data) + offset, row_size/n_parts);
  408. }
  409. } else {
  410. const int np1 = ne[1];
  411. const size_t row_size = (tensor->ne[0]/ggml_blck_size(tensor->type))*ggml_type_size(tensor->type);
  412. for (int i1 = 0; i1 < ne[1]; ++i1) {
  413. const size_t offset_row = (i1 + part_id*np1)*row_size;
  414. fin.read(reinterpret_cast<char *>(tensor->data) + offset_row, row_size);
  415. }
  416. }
  417. total_size += ggml_nbytes(tensor)/n_parts;
  418. }
  419. //fprintf(stderr, "%42s - [%5d, %5d], type = %6s, %6.2f MB\n", name.data(), ne[0], ne[1], ftype == 0 ? "float" : "f16", ggml_nbytes(tensor)/1024.0/1024.0);
  420. if (++n_tensors % 8 == 0) {
  421. fprintf(stderr, ".");
  422. fflush(stderr);
  423. }
  424. }
  425. fprintf(stderr, " done\n");
  426. fprintf(stderr, "%s: model size = %8.2f MB / num tensors = %d\n", __func__, total_size/1024.0/1024.0, n_tensors);
  427. }
  428. fin.close();
  429. }
  430. return true;
  431. }
  432. // evaluate the transformer
  433. //
  434. // - model: the model
  435. // - n_threads: number of threads to use
  436. // - n_past: the context size so far
  437. // - embd_inp: the embeddings of the tokens in the context
  438. // - embd_w: the predicted logits for the next token
  439. //
  440. // The GPT-J model requires about 16MB of memory per input token.
  441. //
  442. bool llama_eval(
  443. const llama_model & model,
  444. const int n_threads,
  445. const int n_past,
  446. const std::vector<llama_vocab::id> & embd_inp,
  447. std::vector<float> & embd_w,
  448. size_t & mem_per_token) {
  449. const int N = embd_inp.size();
  450. const auto & hparams = model.hparams;
  451. const int n_embd = hparams.n_embd;
  452. const int n_layer = hparams.n_layer;
  453. const int n_ctx = hparams.n_ctx;
  454. const int n_head = hparams.n_head;
  455. const int n_vocab = hparams.n_vocab;
  456. const int n_rot = hparams.n_embd/hparams.n_head;
  457. // TODO: check if this size scales with n_ctx linearly and remove constant. somehow I feel it wasn't the case
  458. // static size_t buf_size = hparams.n_ctx*1024*1024;
  459. static size_t buf_size = 512u*1024*1024;
  460. static void * buf = malloc(buf_size);
  461. if (mem_per_token > 0 && mem_per_token*N > buf_size) {
  462. const size_t buf_size_new = 1.1*(mem_per_token*N); // add 10% to account for ggml object overhead
  463. //fprintf(stderr, "\n%s: reallocating buffer from %zu to %zu bytes\n", __func__, buf_size, buf_size_new);
  464. // reallocate
  465. buf_size = buf_size_new;
  466. buf = realloc(buf, buf_size);
  467. if (buf == nullptr) {
  468. fprintf(stderr, "%s: failed to allocate %zu bytes\n", __func__, buf_size);
  469. return false;
  470. }
  471. }
  472. struct ggml_init_params params = {
  473. /*.mem_size =*/ buf_size,
  474. /*.mem_buffer =*/ buf,
  475. };
  476. struct ggml_context * ctx0 = ggml_init(params);
  477. ggml_cgraph gf = {};
  478. gf.n_threads = n_threads;
  479. struct ggml_tensor * embd = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, N);
  480. memcpy(embd->data, embd_inp.data(), N*ggml_element_size(embd));
  481. struct ggml_tensor * inpL = ggml_get_rows(ctx0, model.tok_embeddings, embd);
  482. for (int il = 0; il < n_layer; ++il) {
  483. struct ggml_tensor * inpSA = inpL;
  484. struct ggml_tensor * cur;
  485. // norm
  486. {
  487. cur = ggml_rms_norm(ctx0, inpL);
  488. // cur = attention_norm*cur
  489. cur = ggml_mul(ctx0,
  490. ggml_repeat(ctx0, model.layers[il].attention_norm, cur),
  491. cur);
  492. }
  493. // self-attention
  494. {
  495. struct ggml_tensor * Qcur = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
  496. struct ggml_tensor * Kcur = ggml_mul_mat(ctx0, model.layers[il].wk, cur);
  497. struct ggml_tensor * Vcur = ggml_mul_mat(ctx0, model.layers[il].wv, cur);
  498. // store key and value to memory
  499. if (N >= 1) {
  500. struct ggml_tensor * k = ggml_view_1d(ctx0, model.memory_k, N*n_embd, (ggml_element_size(model.memory_k)*n_embd)*(il*n_ctx + n_past));
  501. struct ggml_tensor * v = ggml_view_1d(ctx0, model.memory_v, N*n_embd, (ggml_element_size(model.memory_v)*n_embd)*(il*n_ctx + n_past));
  502. ggml_build_forward_expand(&gf, ggml_cpy(ctx0, Kcur, k));
  503. ggml_build_forward_expand(&gf, ggml_cpy(ctx0, Vcur, v));
  504. }
  505. // Q = Qcur.contiguous().view(n_embd/n_head, n_head, N).permute(0, 2, 1, 3)
  506. struct ggml_tensor * Q =
  507. ggml_permute(ctx0,
  508. ggml_rope(ctx0,
  509. ggml_cpy(ctx0,
  510. Qcur,
  511. ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, n_embd/n_head, n_head, N)),
  512. n_past, n_rot, 0),
  513. 0, 2, 1, 3);
  514. // K = Kmem.view(n_embd/n_head, n_head, n_past + N).permute(0, 2, 1, 3)
  515. struct ggml_tensor * K =
  516. ggml_permute(ctx0,
  517. ggml_rope(ctx0,
  518. ggml_reshape_3d(ctx0,
  519. ggml_view_1d(ctx0, model.memory_k, (n_past + N)*n_embd, il*n_ctx*ggml_element_size(model.memory_k)*n_embd),
  520. n_embd/n_head, n_head, n_past + N),
  521. n_past, n_rot, 1),
  522. 0, 2, 1, 3);
  523. // K * Q
  524. struct ggml_tensor * KQ = ggml_mul_mat(ctx0, K, Q);
  525. // KQ_scaled = KQ / sqrt(n_embd/n_head)
  526. struct ggml_tensor * KQ_scaled =
  527. ggml_scale(ctx0,
  528. KQ,
  529. ggml_new_f32(ctx0, 1.0f/sqrt(float(n_embd)/n_head))
  530. );
  531. // KQ_masked = mask_past(KQ_scaled)
  532. struct ggml_tensor * KQ_masked = ggml_diag_mask_inf(ctx0, KQ_scaled, n_past);
  533. // KQ = soft_max(KQ_masked)
  534. struct ggml_tensor * KQ_soft_max = ggml_soft_max(ctx0, KQ_masked);
  535. // V_trans = Vmem.view(n_embd/n_head, n_head, n_past + N).permute(1, 2, 0, 3).contiguous()
  536. struct ggml_tensor * V_trans =
  537. ggml_permute(ctx0,
  538. ggml_reshape_3d(ctx0,
  539. ggml_view_1d(ctx0, model.memory_v, (n_past + N)*n_embd, il*n_ctx*ggml_element_size(model.memory_v)*n_embd),
  540. n_embd/n_head, n_head, n_past + N),
  541. 1, 2, 0, 3);
  542. // KQV = transpose(V) * KQ_soft_max
  543. struct ggml_tensor * KQV = ggml_mul_mat(ctx0, V_trans, KQ_soft_max);
  544. // KQV_merged = KQV.permute(0, 2, 1, 3)
  545. struct ggml_tensor * KQV_merged = ggml_permute(ctx0, KQV, 0, 2, 1, 3);
  546. // cur = KQV_merged.contiguous().view(n_embd, N)
  547. cur = ggml_cpy(ctx0,
  548. KQV_merged,
  549. ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd, N));
  550. // projection (no bias)
  551. cur = ggml_mul_mat(ctx0,
  552. model.layers[il].wo,
  553. cur);
  554. }
  555. struct ggml_tensor * inpFF = ggml_add(ctx0, cur, inpSA);
  556. // feed-forward network
  557. {
  558. // norm
  559. {
  560. cur = ggml_rms_norm(ctx0, inpFF);
  561. // cur = ffn_norm*cur
  562. cur = ggml_mul(ctx0,
  563. ggml_repeat(ctx0, model.layers[il].ffn_norm, cur),
  564. cur);
  565. }
  566. struct ggml_tensor * tmp = ggml_mul_mat(ctx0,
  567. model.layers[il].w3,
  568. cur);
  569. cur = ggml_mul_mat(ctx0,
  570. model.layers[il].w1,
  571. cur);
  572. // SILU activation
  573. cur = ggml_silu(ctx0, cur);
  574. cur = ggml_mul(ctx0, cur, tmp);
  575. cur = ggml_mul_mat(ctx0,
  576. model.layers[il].w2,
  577. cur);
  578. }
  579. cur = ggml_add(ctx0, cur, inpFF);
  580. // input for next layer
  581. inpL = cur;
  582. }
  583. // norm
  584. {
  585. inpL = ggml_rms_norm(ctx0, inpL);
  586. // inpL = norm*inpL
  587. inpL = ggml_mul(ctx0,
  588. ggml_repeat(ctx0, model.norm, inpL),
  589. inpL);
  590. }
  591. // lm_head
  592. {
  593. inpL = ggml_mul_mat(ctx0, model.output, inpL);
  594. }
  595. // logits -> probs
  596. //inpL = ggml_soft_max(ctx0, inpL);
  597. // run the computation
  598. ggml_build_forward_expand(&gf, inpL);
  599. ggml_graph_compute (ctx0, &gf);
  600. //if (n_past%100 == 0) {
  601. // ggml_graph_print (&gf);
  602. // ggml_graph_dump_dot(&gf, NULL, "gpt-2.dot");
  603. //}
  604. //embd_w.resize(n_vocab*N);
  605. //memcpy(embd_w.data(), ggml_get_data(inpL), sizeof(float)*n_vocab*N);
  606. // return result for just the last token
  607. embd_w.resize(n_vocab);
  608. memcpy(embd_w.data(), (float *) ggml_get_data(inpL) + (n_vocab*(N-1)), sizeof(float)*n_vocab);
  609. if (mem_per_token == 0) {
  610. mem_per_token = ggml_used_mem(ctx0)/N;
  611. }
  612. //fprintf(stderr, "used_mem = %zu\n", ggml_used_mem(ctx0));
  613. ggml_free(ctx0);
  614. return true;
  615. }
  616. static bool is_interacting = false;
  617. #if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32)
  618. void sigint_handler(int signo) {
  619. printf(ANSI_COLOR_RESET);
  620. printf("\n"); // this also force flush stdout.
  621. if (signo == SIGINT) {
  622. if (!is_interacting) {
  623. is_interacting=true;
  624. } else {
  625. _exit(130);
  626. }
  627. }
  628. }
  629. #endif
  630. const char * llama_print_system_info(void) {
  631. static std::string s;
  632. s = "";
  633. s += "AVX = " + std::to_string(ggml_cpu_has_avx()) + " | ";
  634. s += "AVX2 = " + std::to_string(ggml_cpu_has_avx2()) + " | ";
  635. s += "AVX512 = " + std::to_string(ggml_cpu_has_avx512()) + " | ";
  636. s += "FMA = " + std::to_string(ggml_cpu_has_fma()) + " | ";
  637. s += "NEON = " + std::to_string(ggml_cpu_has_neon()) + " | ";
  638. s += "ARM_FMA = " + std::to_string(ggml_cpu_has_arm_fma()) + " | ";
  639. s += "F16C = " + std::to_string(ggml_cpu_has_f16c()) + " | ";
  640. s += "FP16_VA = " + std::to_string(ggml_cpu_has_fp16_va()) + " | ";
  641. s += "WASM_SIMD = " + std::to_string(ggml_cpu_has_wasm_simd()) + " | ";
  642. s += "BLAS = " + std::to_string(ggml_cpu_has_blas()) + " | ";
  643. s += "SSE3 = " + std::to_string(ggml_cpu_has_sse3()) + " | ";
  644. s += "VSX = " + std::to_string(ggml_cpu_has_vsx()) + " | ";
  645. return s.c_str();
  646. }
  647. int main(int argc, char ** argv) {
  648. ggml_time_init();
  649. const int64_t t_main_start_us = ggml_time_us();
  650. gpt_params params;
  651. params.model = "models/llama-7B/ggml-model.bin";
  652. if (gpt_params_parse(argc, argv, params) == false) {
  653. return 1;
  654. }
  655. if (params.n_ctx > 2048) {
  656. fprintf(stderr, "%s: warning: model does not support context sizes greater than 2048 tokens (%d specified);"
  657. "expect poor results\n", __func__, params.n_ctx);
  658. }
  659. if (params.seed < 0) {
  660. params.seed = time(NULL);
  661. }
  662. fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
  663. std::mt19937 rng(params.seed);
  664. if (params.random_prompt) {
  665. params.prompt = gpt_random_prompt(rng);
  666. }
  667. // params.prompt = R"(// this function checks if the number n is prime
  668. //bool is_prime(int n) {)";
  669. int64_t t_load_us = 0;
  670. llama_vocab vocab;
  671. llama_model model;
  672. // load the model
  673. {
  674. const ggml_type memory_type = params.memory_f16 ? GGML_TYPE_F16 : GGML_TYPE_F32;
  675. const int64_t t_start_us = ggml_time_us();
  676. if (!llama_model_load(params.model, model, vocab, params.n_ctx, params.n_parts, memory_type)) {
  677. fprintf(stderr, "%s: failed to load model from '%s'\n", __func__, params.model.c_str());
  678. return 1;
  679. }
  680. t_load_us = ggml_time_us() - t_start_us;
  681. }
  682. // print system information
  683. {
  684. fprintf(stderr, "\n");
  685. fprintf(stderr, "system_info: n_threads = %d / %d | %s\n",
  686. params.n_threads, std::thread::hardware_concurrency(), llama_print_system_info());
  687. }
  688. int n_past = 0;
  689. int64_t t_sample_us = 0;
  690. int64_t t_predict_us = 0;
  691. std::vector<float> logits;
  692. // Add a space in front of the first character to match OG llama tokenizer behavior
  693. params.prompt.insert(0, 1, ' ');
  694. // tokenize the prompt
  695. std::vector<llama_vocab::id> embd_inp = ::llama_tokenize(vocab, params.prompt, true);
  696. params.n_predict = std::min(params.n_predict, model.hparams.n_ctx - (int) embd_inp.size());
  697. // prefix & suffix for instruct mode
  698. const std::vector<llama_vocab::id> inp_pfx = ::llama_tokenize(vocab, "\n\n### Instruction:\n\n", true);
  699. const std::vector<llama_vocab::id> inp_sfx = ::llama_tokenize(vocab, "\n\n### Response:\n\n", false);
  700. // in instruct mode, we inject a prefix and a suffix to each input by the user
  701. if (params.instruct) {
  702. params.interactive = true;
  703. params.antiprompt.push_back("### Instruction:\n\n");
  704. }
  705. // enable interactive mode if reverse prompt is specified
  706. if (params.antiprompt.size() != 0) {
  707. params.interactive = true;
  708. }
  709. fprintf(stderr, "\n");
  710. fprintf(stderr, "%s: prompt: '%s'\n", __func__, params.prompt.c_str());
  711. fprintf(stderr, "%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
  712. for (int i = 0; i < (int) embd_inp.size(); i++) {
  713. fprintf(stderr, "%6d -> '%s'\n", embd_inp[i], vocab.id_to_token.at(embd_inp[i]).c_str());
  714. }
  715. fprintf(stderr, "\n");
  716. if (params.interactive) {
  717. #if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
  718. struct sigaction sigint_action;
  719. sigint_action.sa_handler = sigint_handler;
  720. sigemptyset (&sigint_action.sa_mask);
  721. sigint_action.sa_flags = 0;
  722. sigaction(SIGINT, &sigint_action, NULL);
  723. #elif defined (_WIN32)
  724. signal(SIGINT, sigint_handler);
  725. #endif
  726. fprintf(stderr, "%s: interactive mode on.\n", __func__);
  727. if(params.antiprompt.size()) {
  728. for (auto antiprompt : params.antiprompt) {
  729. fprintf(stderr, "Reverse prompt: '%s'\n", antiprompt.c_str());
  730. }
  731. }
  732. }
  733. fprintf(stderr, "sampling parameters: temp = %f, top_k = %d, top_p = %f, repeat_last_n = %i, repeat_penalty = %f\n", params.temp, params.top_k, params.top_p, params.repeat_last_n, params.repeat_penalty);
  734. fprintf(stderr, "\n\n");
  735. std::vector<llama_vocab::id> embd;
  736. // determine the required inference memory per token:
  737. size_t mem_per_token = 0;
  738. llama_eval(model, params.n_threads, 0, { 0, 1, 2, 3 }, logits, mem_per_token);
  739. int last_n_size = params.repeat_last_n;
  740. std::vector<llama_vocab::id> last_n_tokens(last_n_size);
  741. std::fill(last_n_tokens.begin(), last_n_tokens.end(), 0);
  742. if (params.interactive) {
  743. fprintf(stderr, "== Running in interactive mode. ==\n"
  744. #if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32)
  745. " - Press Ctrl+C to interject at any time.\n"
  746. #endif
  747. " - Press Return to return control to LLaMa.\n"
  748. " - If you want to submit another line, end your input in '\\'.\n\n");
  749. is_interacting = true;
  750. }
  751. int input_consumed = 0;
  752. bool input_noecho = false;
  753. int remaining_tokens = params.n_predict;
  754. // set the color for the prompt which will be output initially
  755. if (params.use_color) {
  756. #if defined (_WIN32)
  757. // Enable ANSI colors on Windows 10+
  758. unsigned long dwMode = 0;
  759. void* hConOut = GetStdHandle((unsigned long)-11); // STD_OUTPUT_HANDLE (-11)
  760. if (hConOut && hConOut != (void*)-1 && GetConsoleMode(hConOut, &dwMode) && !(dwMode & 0x4)) {
  761. SetConsoleMode(hConOut, dwMode | 0x4); // ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
  762. }
  763. #endif
  764. printf(ANSI_COLOR_YELLOW);
  765. }
  766. while (remaining_tokens > 0 || params.interactive) {
  767. // predict
  768. if (embd.size() > 0) {
  769. const int64_t t_start_us = ggml_time_us();
  770. if (!llama_eval(model, params.n_threads, n_past, embd, logits, mem_per_token)) {
  771. fprintf(stderr, "Failed to predict\n");
  772. return 1;
  773. }
  774. t_predict_us += ggml_time_us() - t_start_us;
  775. }
  776. n_past += embd.size();
  777. embd.clear();
  778. if ((int) embd_inp.size() <= input_consumed) {
  779. // out of user input, sample next token
  780. const float top_k = params.top_k;
  781. const float top_p = params.top_p;
  782. const float temp = params.temp;
  783. const float repeat_penalty = params.repeat_penalty;
  784. const int n_vocab = model.hparams.n_vocab;
  785. llama_vocab::id id = 0;
  786. {
  787. const int64_t t_start_sample_us = ggml_time_us();
  788. if (params.ignore_eos) {
  789. // set the logit of the eos token to zero to avoid sampling it
  790. logits[logits.size() - n_vocab + EOS_TOKEN_ID] = 0;
  791. }
  792. id = llama_sample_top_p_top_k(vocab, logits.data() + (logits.size() - n_vocab), last_n_tokens, repeat_penalty, top_k, top_p, temp, rng);
  793. last_n_tokens.erase(last_n_tokens.begin());
  794. last_n_tokens.push_back(id);
  795. t_sample_us += ggml_time_us() - t_start_sample_us;
  796. }
  797. // add it to the context
  798. embd.push_back(id);
  799. // echo this to console
  800. input_noecho = false;
  801. // decrement remaining sampling budget
  802. --remaining_tokens;
  803. } else {
  804. // some user input remains from prompt or interaction, forward it to processing
  805. while ((int) embd_inp.size() > input_consumed) {
  806. embd.push_back(embd_inp[input_consumed]);
  807. last_n_tokens.erase(last_n_tokens.begin());
  808. last_n_tokens.push_back(embd_inp[input_consumed]);
  809. ++input_consumed;
  810. if ((int) embd.size() >= params.n_batch) {
  811. break;
  812. }
  813. }
  814. }
  815. // display text
  816. if (!input_noecho) {
  817. for (auto id : embd) {
  818. printf("%s", vocab.id_to_token[id].c_str());
  819. }
  820. fflush(stdout);
  821. }
  822. // reset color to default if we there is no pending user input
  823. if (!input_noecho && params.use_color && (int)embd_inp.size() == input_consumed) {
  824. printf(ANSI_COLOR_RESET);
  825. }
  826. // in interactive mode, and not currently processing queued inputs;
  827. // check if we should prompt the user for more
  828. if (params.interactive && (int) embd_inp.size() <= input_consumed) {
  829. // check for reverse prompt
  830. std::string last_output;
  831. for (auto id : last_n_tokens) {
  832. last_output += vocab.id_to_token[id];
  833. }
  834. // Check if each of the reverse prompts appears at the end of the output.
  835. for (std::string antiprompt : params.antiprompt) {
  836. if (last_output.find(antiprompt.c_str(), last_output.length() - antiprompt.length(), antiprompt.length()) != std::string::npos) {
  837. is_interacting = true;
  838. break;
  839. }
  840. }
  841. if (is_interacting) {
  842. if (params.instruct) {
  843. input_consumed = embd_inp.size();
  844. embd_inp.insert(embd_inp.end(), inp_pfx.begin(), inp_pfx.end());
  845. printf("\n> ");
  846. }
  847. // currently being interactive
  848. if (params.use_color) printf(ANSI_BOLD ANSI_COLOR_GREEN);
  849. std::string buffer;
  850. std::string line;
  851. bool another_line = true;
  852. do {
  853. std::getline(std::cin, line);
  854. if (line.empty() || line.back() != '\\') {
  855. another_line = false;
  856. } else {
  857. line.pop_back(); // Remove the continue character
  858. }
  859. buffer += line + '\n'; // Append the line to the result
  860. } while (another_line);
  861. if (params.use_color) printf(ANSI_COLOR_RESET);
  862. std::vector<llama_vocab::id> line_inp = ::llama_tokenize(vocab, buffer, false);
  863. embd_inp.insert(embd_inp.end(), line_inp.begin(), line_inp.end());
  864. if (params.instruct) {
  865. embd_inp.insert(embd_inp.end(), inp_sfx.begin(), inp_sfx.end());
  866. }
  867. remaining_tokens -= line_inp.size();
  868. input_noecho = true; // do not echo this again
  869. }
  870. is_interacting = false;
  871. }
  872. // end of text token
  873. if (embd.back() == EOS_TOKEN_ID) {
  874. if (params.interactive) {
  875. is_interacting = true;
  876. } else {
  877. fprintf(stderr, " [end of text]\n");
  878. break;
  879. }
  880. }
  881. // In interactive mode, respect the maximum number of tokens and drop back to user input when reached.
  882. if (params.interactive && remaining_tokens <= 0) {
  883. remaining_tokens = params.n_predict;
  884. is_interacting = true;
  885. }
  886. }
  887. #if defined (_WIN32)
  888. signal(SIGINT, SIG_DFL);
  889. #endif
  890. // report timing
  891. {
  892. const int64_t t_main_end_us = ggml_time_us();
  893. fprintf(stderr, "\n\n");
  894. fprintf(stderr, "%s: mem per token = %8zu bytes\n", __func__, mem_per_token);
  895. fprintf(stderr, "%s: load time = %8.2f ms\n", __func__, t_load_us/1000.0f);
  896. fprintf(stderr, "%s: sample time = %8.2f ms\n", __func__, t_sample_us/1000.0f);
  897. fprintf(stderr, "%s: predict time = %8.2f ms / %.2f ms per token\n", __func__, t_predict_us/1000.0f, t_predict_us/1000.0f/n_past);
  898. fprintf(stderr, "%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0f);
  899. }
  900. ggml_free(model.ctx);
  901. if (params.use_color) {
  902. printf(ANSI_COLOR_RESET);
  903. }
  904. return 0;
  905. }