model_loader.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. int load_model_weights(fairseq2_model &model, std::ifstream &fin);
  20. private:
  21. ggml_tensor * next_tensor(std::ifstream &fin, fairseq2_model &model);
  22. std::string get_name(std::ifstream &fin);
  23. };
  24. ggml_tensor* load_tensor_value(std::ifstream &fin, ggml_context* ctx);
  25. std::ifstream open_ggml_file(const char* fname);
  26. template<typename T>
  27. int load_fairseq2_ggml_file(fairseq2_model& model, const char* fname) {
  28. T loader;
  29. auto fin = open_ggml_file(fname);
  30. loader.load_hparams(model, fin);
  31. std::size_t ctx_size = loader.compute_context_size(model.hparams);
  32. struct ggml_init_params params = {
  33. /*.mem_size =*/ ctx_size,
  34. /*.mem_buffer =*/ NULL,
  35. /*.no_alloc =*/ false,
  36. };
  37. model.tensors_ctx = ggml_init(params);
  38. return loader.load_model_weights(model, fin);
  39. }