model_loader.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright (c) Meta Platforms, Inc. and affiliates.
  2. // All rights reserved.
  3. //
  4. // This source code is licensed under the license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. #pragma once
  7. #include "ggml/ggml.h"
  8. #include "ggml/ggml-alloc.h"
  9. #include "common.h"
  10. #include "common-ggml.h"
  11. #include "fairseq2.h"
  12. #include <iostream>
  13. #include <stdexcept>
  14. class model_loader {
  15. public:
  16. virtual ~model_loader() {};
  17. virtual void load_hparams(fairseq2_model& model, std::ifstream &fin) = 0;
  18. virtual std::size_t compute_context_size(void *raw_hparams) = 0;
  19. virtual void tensors_alloc(fairseq2_model& model) = 0;
  20. void load_model_weights(fairseq2_model &model, std::ifstream &fin);
  21. private:
  22. ggml_tensor * next_tensor(std::ifstream &fin, fairseq2_model &model);
  23. std::string get_name(std::ifstream &fin);
  24. };
  25. ggml_tensor* load_tensor_value(std::ifstream &fin, ggml_context* ctx);
  26. std::ifstream open_ggml_file(const char* fname);
  27. template<typename T>
  28. void load_fairseq2_ggml_file(fairseq2_model& model, const char* fname) {
  29. T loader;
  30. auto fin = open_ggml_file(fname);
  31. loader.load_hparams(model, fin);
  32. std::size_t ctx_size = loader.compute_context_size(model.hparams);
  33. struct ggml_init_params params = {
  34. /*.mem_size =*/ ctx_size,
  35. /*.mem_buffer =*/ NULL,
  36. /*.no_alloc =*/ false,
  37. };
  38. model.ctx = ggml_init(params);
  39. loader.load_model_weights(model, fin);;
  40. }