model_loader.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include <string>
  2. #include "model_loader.h"
  3. #define DEBUG_MODEL_LOAD 0
  4. std::ifstream open_ggml_file(const char* fname) {
  5. printf("%s: loading model from '%s'\n", __func__, fname);
  6. auto fin = std::ifstream(std::string(fname), std::ios::binary);
  7. if (!fin) {
  8. fprintf(stderr, "%s: failed to open '%s'\n", __func__, fname);
  9. throw std::invalid_argument("failed to open file."); // TODO Merge error message.
  10. }
  11. std::uint32_t magic;
  12. fin.read((char*)&magic, 4);
  13. if (magic != GGML_FILE_MAGIC) {
  14. fprintf(stderr, "%s: invalid model file '%s' (bad header %d)\n", __func__, fname, magic);
  15. throw std::invalid_argument("failed to open file."); // TODO Merge error message.
  16. }
  17. return fin;
  18. }
  19. void register_prefix(fairseq2_model &model, const std::string& name) {
  20. std::size_t i = name.find_last_of('.');
  21. while(i != std::string::npos && i > 0) {
  22. std::string prefix = name.substr(0, i);
  23. auto prev_tensor = model.tensors.find(prefix);
  24. if (prev_tensor != model.tensors.end()) {
  25. GGML_ASSERT(prev_tensor->second == nullptr);
  26. }
  27. model.tensors[prefix] = nullptr;
  28. i = name.find_last_of('.', i - 1);
  29. }
  30. }
  31. std::int64_t
  32. model_loader::load_model_weights(fairseq2_model &model, std::ifstream &fin)
  33. {
  34. std::int64_t num_tensor = 0;
  35. std::int64_t f32_ctx_size = 0;
  36. std::int64_t f16_ctx_size = 0;
  37. fin.read((char*) &num_tensor, sizeof(num_tensor));
  38. fin.read((char*) &f32_ctx_size, sizeof(f32_ctx_size));
  39. fin.read((char*) &f16_ctx_size, sizeof(f16_ctx_size));
  40. // TODO: it might be intersting to allow the caller to not upcast the weights to float32.
  41. bool as_float32 = true;
  42. struct ggml_init_params params = {
  43. /*.mem_size =*/ as_float32 ? f32_ctx_size : f16_ctx_size,
  44. /*.mem_buffer =*/ NULL,
  45. /*.no_alloc =*/ false,
  46. };
  47. model.tensors_ctx = ggml_init(params);
  48. size_t model_size = 0;
  49. for (int i = 0; i < num_tensor; ++i) {
  50. std::string name = get_name(fin);
  51. if (name.length() == 0)
  52. break;
  53. auto tensor = load_tensor_value(fin, model.tensors_ctx, as_float32);
  54. if (tensor == nullptr) {
  55. // Abort in case of error, the input stream is corrupted at this point.
  56. printf("Error while reading tensor %s\n", name.c_str() );
  57. throw std::invalid_argument("Error while reading tensor from file.");
  58. }
  59. register_prefix(model, name);
  60. ggml_set_name(tensor, name.c_str());
  61. model.tensors[name] = tensor;
  62. if (DEBUG_MODEL_LOAD) {
  63. printf("%s [%5ld, %5ld], type = %6s, %6.2f MB, %9zu bytes\n", name.c_str(), tensor->ne[0], tensor->ne[1], ggml_type_name(tensor->type), ggml_nbytes(tensor)/1024.0/1024.0, ggml_nbytes(tensor));
  64. }
  65. model_size += ggml_nbytes(tensor);
  66. }
  67. double mb = 1024.0 * 1024.0;
  68. printf("%s: model size: %8.2f MB, memory used: %8.2f MB, memory reserved: %8.2f MB\n",
  69. __func__,
  70. model_size / mb,
  71. ggml_used_mem(model.tensors_ctx) / mb,
  72. ggml_get_mem_size(model.tensors_ctx) / mb
  73. );
  74. return ggml_get_mem_size(model.tensors_ctx);
  75. }
  76. void assert_endianness() {
  77. union {
  78. unsigned int i;
  79. char c[4];
  80. } un;
  81. un.i = 0x12345678;
  82. if (un.c[0] == 0x78 && un.c[3] == 0x12) {
  83. printf("little-endian\n");
  84. }
  85. else if (un.c[0] == 0x12 && un.c[3] == 0x78) {
  86. printf("big-endian\n");
  87. GGML_ASSERT(false); // model_loader.cpp assumes the system is little-endian
  88. }
  89. else {
  90. printf("unknown-endian\n");
  91. GGML_ASSERT(false); // model_loader.cpp assumes the system is little-endian
  92. }
  93. }
  94. void model_loader::load_hparams(std::unordered_map<std::string, std::int64_t>& hparams, std::ifstream &fin)
  95. {
  96. std::int64_t num_params = 0;
  97. fin.read(reinterpret_cast<char*>(&num_params), sizeof num_params);
  98. GGML_ASSERT(fin.gcount() == 8);
  99. hparams.reserve(num_params);
  100. std::int64_t value;
  101. for (int i = 0; i < num_params; ++i) {
  102. std::string name = get_name(fin);
  103. if (name.length() == 0)
  104. break;
  105. fin.read((char*) &value, sizeof(value));
  106. hparams[name] = value;
  107. }
  108. }
  109. void model_loader::load_vocab(llama_vocab& vocab, std::ifstream &fin)
  110. {
  111. // vocab.special_bos_id = 1;
  112. // vocab.special_eos_id = 2;
  113. // vocab.special_unk_id = 0;
  114. // vocab.special_sep_id = -1;
  115. // vocab.special_pad_id = -1;
  116. std::int64_t vocab_size = 0;
  117. fin.read(reinterpret_cast<char*>(&vocab_size), sizeof(vocab_size));
  118. GGML_ASSERT(fin.gcount() == 8);
  119. vocab.token_to_id.reserve(vocab_size);
  120. vocab.id_to_token.reserve(vocab_size);
  121. std::string packed_vocab = get_name(fin);
  122. std::int64_t ctx_size = vocab_size * sizeof(float) + vocab_size + 2 * ggml_tensor_overhead();
  123. ctx_size *= 2;
  124. ggml_context* ctx = ggml_init(ggml_init_params{ctx_size, nullptr, false});
  125. ggml_tensor* lengths_tensor = load_tensor_value(fin, ctx, true);
  126. std::int8_t* lengths = (std::int8_t*)lengths_tensor->data;
  127. ggml_tensor* scores_tensor = load_tensor_value(fin, ctx, true);
  128. float* scores = ggml_get_data_f32(scores_tensor);
  129. int64_t offset = 0;
  130. for (int i = 0; i < vocab_size; ++i) {
  131. // TODO: we should use string view instead of copying each word in a new string
  132. std::string word = packed_vocab.substr(offset, lengths[i]);
  133. vocab.token_to_id[word] = i;
  134. vocab.id_to_token.push_back({word, scores[i], LLAMA_TOKEN_TYPE_NORMAL});
  135. offset += lengths[i] + 1;
  136. }
  137. // Since we copied lengths and scores, we don't need the context anymore.
  138. ggml_free(ctx);
  139. // vocab.linefeed_id = llama_byte_to_token(vocab, '\n');
  140. // TODO: special tokens stuff ?
  141. }
  142. ggml_tensor* load_tensor_value(std::ifstream &fin, ggml_context* ctx, bool as_float32)
  143. {
  144. int32_t n_dims = 0;
  145. int32_t raw_type = 0;
  146. fin.read(reinterpret_cast<char *>(&n_dims), sizeof(n_dims));
  147. fin.read(reinterpret_cast<char *>(&raw_type), sizeof(raw_type));
  148. ggml_type type = ggml_type(raw_type);
  149. if (n_dims <= 0 || n_dims > GGML_MAX_DIMS || raw_type < 0 || raw_type > GGML_TYPE_COUNT) {
  150. return nullptr;
  151. }
  152. int64_t ne[4] = {1, 1, 1, 1};
  153. for (int i = 0; i < n_dims; ++i) {
  154. fin.read(reinterpret_cast<char *>(&ne[i]), sizeof(ne[i]));
  155. }
  156. ggml_tensor* tensor;
  157. if (as_float32 && type == GGML_TYPE_F16) {
  158. // read quantized weights from disk, and convert them to f32.
  159. tensor = ggml_new_tensor(ctx, GGML_TYPE_F32, n_dims, ne);
  160. ggml_fp16_t buf[128];
  161. int num_el = ggml_nelements(tensor);
  162. for (int i = 0; i < num_el; i += 128) {
  163. int block_size = std::min(128, num_el - i);
  164. fin.read(reinterpret_cast<char *>(&buf), ggml_type_size(type) * block_size);
  165. ggml_fp16_to_fp32_row((const ggml_fp16_t*)&buf, (float*)tensor->data + i, block_size);
  166. }
  167. } else {
  168. tensor = ggml_new_tensor(ctx, type, n_dims, ne);
  169. fin.read(reinterpret_cast<char *>(tensor->data), ggml_nbytes(tensor));
  170. }
  171. return tensor;
  172. }
  173. std::string
  174. model_loader::get_name(std::ifstream& fin)
  175. {
  176. std::uint32_t length = 0;
  177. fin.read(reinterpret_cast<char *>(&length), sizeof(length));
  178. if (length == 0)
  179. return "";
  180. std::string name(length, 0);
  181. fin.read(&name[0], length);
  182. return name;
  183. }
  184. extern "C" int load_fairseq2_ggml_file(fairseq2_model& model, const char* fname) {
  185. model_loader loader;
  186. assert_endianness();
  187. auto fin = open_ggml_file(fname);
  188. loader.load_hparams(model.hparams, fin);
  189. loader.load_hparams(model.layer_config, fin);
  190. loader.load_vocab(model.vocab, fin);
  191. loader.load_model_weights(model, fin);
  192. return 0;
  193. }