unity_model_loader.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 <vector>
  8. #include "model_loader.h"
  9. // TODO Merge with Ning implementation
  10. struct unity_hparams {
  11. int32_t model_dim;
  12. int32_t w2v2_encoder_config__model_dim;
  13. int32_t w2v2_encoder_config__max_seq_len;
  14. int32_t w2v2_encoder_config__feature_dim;
  15. int32_t w2v2_encoder_config__use_fbank;
  16. float w2v2_encoder_config__first_pass_dropout_p;
  17. int32_t w2v2_encoder_config__layer_norm_features;
  18. int32_t w2v2_encoder_config__feature_extractor_bias;
  19. int32_t w2v2_encoder_config__feature_extractor_layer_norm_convs;
  20. int32_t w2v2_encoder_config__feature_grad_scale;
  21. int32_t w2v2_encoder_config__num_fbank_channels;
  22. int32_t w2v2_encoder_config__fbank_stride;
  23. int32_t w2v2_encoder_config__sample_fbank_every_k;
  24. int32_t w2v2_encoder_config__pos_encoder_depth;
  25. int32_t w2v2_encoder_config__pos_conv_kernel_size;
  26. int32_t w2v2_encoder_config__num_pos_conv_groups;
  27. int32_t w2v2_encoder_config__use_conformer;
  28. int32_t w2v2_encoder_config__num_encoder_layers;
  29. int32_t w2v2_encoder_config__num_encoder_attn_heads;
  30. int32_t w2v2_encoder_config__ffn_inner_dim;
  31. float w2v2_encoder_config__dropout_p;
  32. float w2v2_encoder_config__attn_dropout_p;
  33. float w2v2_encoder_config__layer_drop_p;
  34. int32_t w2v2_encoder_config__norm_order;
  35. int32_t w2v2_encoder_config__depthwise_conv_kernel_size;
  36. int32_t nllb_config__model_dim;
  37. int32_t nllb_config__max_seq_len;
  38. int32_t nllb_config__vocabulary_size;
  39. int32_t nllb_config__pad_idx;
  40. int32_t nllb_config__num_encoder_layers;
  41. int32_t nllb_config__num_decoder_layers;
  42. int32_t nllb_config__num_encoder_attn_heads;
  43. int32_t nllb_config__num_decoder_attn_heads;
  44. int32_t nllb_config__ffn_inner_dim;
  45. float nllb_config__dropout_p;
  46. int32_t t2u_config__model_dim;
  47. int32_t t2u_config__unit_max_seq_len;
  48. int32_t t2u_config__unit_vocabulary_size;
  49. int32_t t2u_config__unit_pad_idx;
  50. int32_t t2u_config__num_encoder_layers;
  51. int32_t t2u_config__num_decoder_layers;
  52. int32_t t2u_config__num_encoder_attn_heads;
  53. int32_t t2u_config__num_decoder_attn_heads;
  54. int32_t t2u_config__ffn_inner_dim;
  55. float t2u_config__dropout_p;
  56. int32_t use_text_encoder;
  57. int32_t use_conformer_adaptor;
  58. int32_t num_adaptor_layers;
  59. int32_t adaptor_kernel_size;
  60. int32_t adaptor_stride;
  61. int32_t adaptor_layer_norm;
  62. float adaptor_dropout_p;
  63. };
  64. // Embedding
  65. std::size_t compute_embed_size(int32_t vocab_size, int32_t dim)
  66. {
  67. return vocab_size * dim * ggml_type_size(GGML_TYPE_F32);
  68. };
  69. // Attention Layer
  70. struct attention_layer {
  71. struct ggml_tensor* layer_norm_w; // model_dim
  72. struct ggml_tensor* layer_norm_b; // model_dim
  73. struct ggml_tensor* q_proj_w; // model_dim x model_dim
  74. struct ggml_tensor* q_proj_b; // model_dim
  75. struct ggml_tensor* k_proj_w; // model_dim x model_dim
  76. struct ggml_tensor* k_proj_b; // model_dim
  77. struct ggml_tensor* v_proj_w; // model_dim x model_dim
  78. struct ggml_tensor* v_proj_b; // model_dim
  79. struct ggml_tensor* output_proj_w; // model_dim x model_dim
  80. struct ggml_tensor* output_proj_b; // model_dim
  81. };
  82. std::size_t compute_attention_layer_size(int32_t dim)
  83. {
  84. return LayerNorm_size(dim)
  85. + 4 * Linear_size(dim, dim); // q, k, v, and out
  86. };
  87. void init_attention_layer(
  88. attention_layer *layer,
  89. fairseq2_model &model_ctx,
  90. const std::string &prefix)
  91. {
  92. auto hparams = (unity_hparams&)model_ctx.hparams;
  93. const auto dim = hparams.nllb_config__model_dim;
  94. auto ctx = model_ctx.ctx;
  95. auto &tensor_map = model_ctx.tensors;
  96. layer->layer_norm_w = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, dim);
  97. tensor_map[prefix + "_layer_norm.weight"] = layer->layer_norm_w;
  98. layer->layer_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, dim);
  99. tensor_map[prefix + "_layer_norm.bias"] = layer->layer_norm_b;
  100. layer->q_proj_w = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, dim, dim);
  101. tensor_map[prefix + ".q_proj.weight"] = layer->q_proj_w;
  102. layer->q_proj_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, dim);
  103. tensor_map[prefix + ".q_proj.bias"] = layer->q_proj_b;
  104. layer->k_proj_w = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, dim, dim);
  105. tensor_map[prefix + ".k_proj.weight"] = layer->k_proj_w;
  106. layer->k_proj_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, dim);
  107. tensor_map[prefix + ".k_proj.bias"] = layer->k_proj_b;
  108. layer->v_proj_w = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, dim, dim);
  109. tensor_map[prefix + ".v_proj.weight"] = layer->v_proj_w;
  110. layer->v_proj_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, dim);
  111. tensor_map[prefix + ".v_proj.bias"] = layer->v_proj_b;
  112. layer->output_proj_w = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, dim, dim);
  113. tensor_map[prefix + ".output_proj.weight"] = layer->output_proj_w;
  114. layer->output_proj_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, dim);
  115. tensor_map[prefix + ".output_proj.bias"] = layer->output_proj_b;
  116. }
  117. // Attention Head
  118. struct attention_head {
  119. struct attention_layer* self_attn; // model_dim
  120. struct attention_layer* encoder_decoder_attn; // model_dim
  121. struct StandardFeedForwardNetwork* ffn;
  122. };
  123. std::size_t compute_attention_head_size(int32_t dim, int32_t inner_dim)
  124. {
  125. return 2 * compute_attention_layer_size(dim) + StandardFeedForwardNetwork_size(dim, inner_dim);
  126. };
  127. void init_attention_head(
  128. attention_head *head,
  129. fairseq2_model &model_ctx,
  130. const std::string &prefix)
  131. {
  132. auto hparams = (unity_hparams&)model_ctx.hparams;
  133. init_attention_layer(head->self_attn, model_ctx, prefix + ".self_attn");
  134. init_attention_layer(head->encoder_decoder_attn, model_ctx, prefix + ".encoder_decoder_attn");
  135. StandardFeedForwardNetwork_init(head->ffn, model_ctx, prefix + ".ffn", hparams.nllb_config__model_dim, hparams.nllb_config__ffn_inner_dim);
  136. }
  137. // TODO: attention_head_compute_graph
  138. // Text Decoder
  139. struct text_decoder {
  140. struct ggml_tensor* frontend_embed_w; // vocab_size x model_dim
  141. std::vector<attention_head*> multi_head;
  142. struct ggml_tensor* layer_norm_w;
  143. struct ggml_tensor* layer_norm_b;
  144. };
  145. std::size_t compute_context_size(void* raw_hparams)
  146. {
  147. auto hparams = (unity_hparams&)raw_hparams;
  148. const auto vocab_size = hparams.nllb_config__vocabulary_size;
  149. const auto dim = hparams.nllb_config__model_dim;
  150. const auto inner_dim = hparams.nllb_config__ffn_inner_dim;
  151. const auto n_layers = hparams.nllb_config__num_decoder_layers;
  152. const auto overhead = (6 + 12 * n_layers) * 512; // TODO Find out what this is.
  153. return compute_embed_size(vocab_size, dim)
  154. + n_layers * compute_attention_head_size(dim, inner_dim)
  155. + LayerNorm_size(dim)
  156. + overhead;
  157. };
  158. void init_model_tensors(
  159. text_decoder &model,
  160. fairseq2_model &model_ctx,
  161. const std::string &prefix)
  162. {
  163. const auto ctx = model_ctx.ctx;
  164. auto hparams = (unity_hparams&)model_ctx.hparams;
  165. auto tensor_map = model_ctx.tensors;
  166. const auto vocab_size = hparams.nllb_config__vocabulary_size;
  167. const auto dim = hparams.nllb_config__model_dim;
  168. const auto n_layers = hparams.nllb_config__num_decoder_layers;
  169. // This can be simplified by adding syntax sugar
  170. // frontend
  171. model.frontend_embed_w = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, vocab_size, dim);
  172. tensor_map["text_decoder_frontend.embed.weight"] = model.frontend_embed_w;
  173. // layers
  174. model.multi_head.resize(n_layers);
  175. for (int i = 0; i < n_layers; ++i) {
  176. auto head = model.multi_head[i];
  177. auto prefix = "text_decoder.layers." + std::to_string(i);
  178. init_attention_head(head, model_ctx, prefix);
  179. }
  180. // layer_norm
  181. model.layer_norm_w = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, dim);
  182. tensor_map["text_decoder.layer_norm.weight"] = model.layer_norm_w;
  183. model.layer_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, dim);
  184. tensor_map["text_decoder.layer_norm.bias"] = model.layer_norm_b;
  185. };
  186. class unity_model_loader: public model_loader {
  187. public:
  188. fairseq2_model& alloc_model(ggml_context* ctx) {
  189. return alloc_fairseq2_model<unity_hparams>(ctx);
  190. };
  191. void load_hparams(fairseq2_model& model, std::ifstream &fin);
  192. std::size_t compute_context_size(void* raw_hparams);
  193. void init_model_tensors(fairseq2_model &model);
  194. };
  195. extern "C" fairseq2_model& load_unity_ggml_file(ggml_context* ctx, const char* fname) {
  196. return load_fairseq2_ggml_file<unity_model_loader>(ctx, fname);
  197. }