|
| 1 | +#include "example/tiny_mixtral/checkpoint_loader.h" |
| 2 | + |
| 3 | +#include <cstdint> |
| 4 | +#include <fstream> |
| 5 | +#include <memory> |
| 6 | +#include <string> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +#include "glog/logging.h" |
| 10 | + |
| 11 | +#include "infini_train/include/datatype.h" |
| 12 | +#include "infini_train/include/nn/modules/transformer/transformer.h" |
| 13 | +#include "infini_train/include/tensor.h" |
| 14 | + |
| 15 | +#include "example/common/utils.h" |
| 16 | +#include "example/tiny_mixtral/config.h" |
| 17 | + |
| 18 | +namespace nn = infini_train::nn; |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +constexpr int32_t kTinyMixtralLLMCMagic = 20260513; |
| 23 | +constexpr int32_t kTinyMixtralLLMCVersion = 2; |
| 24 | +constexpr int64_t kLLMCHeaderEntries = 256; |
| 25 | + |
| 26 | +} // namespace |
| 27 | + |
| 28 | +namespace tiny_mixtral { |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +template <typename T> |
| 33 | +void CompareCheckpointValue(const std::string &name, const T &checkpoint_value, |
| 34 | + const T &runtime_value) { |
| 35 | + CHECK_EQ(checkpoint_value, runtime_value) |
| 36 | + << name << " value from checkpoint (" << checkpoint_value << ") is not equal to runtime config value (" |
| 37 | + << runtime_value << ")"; |
| 38 | +} |
| 39 | + |
| 40 | +} // namespace |
| 41 | + |
| 42 | +nn::TransformerConfig ConfigFromLLMC(const std::string &filepath) { |
| 43 | + std::ifstream ifs(filepath, std::ios::binary); |
| 44 | + CHECK(ifs) << "Failed to open tiny Mixtral LLMC file: " << filepath; |
| 45 | + const auto header = infini_train::ReadSeveralBytesFromIfstream(kLLMCHeaderEntries * sizeof(int32_t), &ifs); |
| 46 | + CHECK(ifs) << "Failed to read tiny Mixtral LLMC header: " << filepath; |
| 47 | + CHECK_EQ(infini_train::BytesToType<int32_t>(header, 0 * sizeof(int32_t)), kTinyMixtralLLMCMagic); |
| 48 | + CHECK_EQ(infini_train::BytesToType<int32_t>(header, 1 * sizeof(int32_t)), kTinyMixtralLLMCVersion); |
| 49 | + |
| 50 | + auto config = TinyMixtralConfig(); |
| 51 | + config.block_size = infini_train::BytesToType<int32_t>(header, 2 * sizeof(int32_t)); |
| 52 | + config.vocab_size = infini_train::BytesToType<int32_t>(header, 3 * sizeof(int32_t)); |
| 53 | + config.original_vocab_size = config.vocab_size; |
| 54 | + config.n_layer = infini_train::BytesToType<int32_t>(header, 4 * sizeof(int32_t)); |
| 55 | + config.n_head = infini_train::BytesToType<int32_t>(header, 5 * sizeof(int32_t)); |
| 56 | + config.n_kv_head = infini_train::BytesToType<int32_t>(header, 6 * sizeof(int32_t)); |
| 57 | + config.n_embd = infini_train::BytesToType<int32_t>(header, 7 * sizeof(int32_t)); |
| 58 | + config.ffn_expansion_ratio = infini_train::BytesToType<float>(header, 9 * sizeof(int32_t)); |
| 59 | + config.ffn_dim_multiplier = infini_train::BytesToType<float>(header, 10 * sizeof(int32_t)); |
| 60 | + config.multiple_of = infini_train::BytesToType<int32_t>(header, 11 * sizeof(int32_t)); |
| 61 | + config.norm_eps = infini_train::BytesToType<float>(header, 12 * sizeof(int32_t)); |
| 62 | + config.rope_theta = infini_train::BytesToType<float>(header, 13 * sizeof(int32_t)); |
| 63 | + config.use_scaled_rope = infini_train::BytesToType<int32_t>(header, 14 * sizeof(int32_t)) != 0; |
| 64 | + |
| 65 | + nn::MoEConfig moe_config; |
| 66 | + moe_config.num_experts = infini_train::BytesToType<int32_t>(header, 8 * sizeof(int32_t)); |
| 67 | + moe_config.expert_parallel_size = 1; |
| 68 | + moe_config.router_topk = infini_train::BytesToType<int32_t>(header, 15 * sizeof(int32_t)); |
| 69 | + moe_config.moe_ffn_hidden_size = infini_train::BytesToType<int32_t>(header, 16 * sizeof(int32_t)); |
| 70 | + moe_config.dispatcher_type = nn::MoEConfig::DispatcherType::kAllGather; |
| 71 | + moe_config.expert_impl = nn::MoEConfig::ExpertImpl::kSequential; |
| 72 | + config.moe_config = moe_config; |
| 73 | + SanitizeTinyMixtralConfig(config); |
| 74 | + return config; |
| 75 | +} |
| 76 | + |
| 77 | +void CheckLLMCConfig(const std::string &filepath, const nn::TransformerConfig &expected_config) { |
| 78 | + SanitizeTinyMixtralConfig(expected_config); |
| 79 | + const auto checkpoint_config = ConfigFromLLMC(filepath); |
| 80 | + CompareCheckpointValue("block_size", checkpoint_config.block_size, expected_config.block_size); |
| 81 | + CompareCheckpointValue("vocab_size", checkpoint_config.vocab_size, expected_config.vocab_size); |
| 82 | + CompareCheckpointValue("original_vocab_size", checkpoint_config.original_vocab_size, |
| 83 | + expected_config.original_vocab_size); |
| 84 | + CompareCheckpointValue("n_layer", checkpoint_config.n_layer, expected_config.n_layer); |
| 85 | + CompareCheckpointValue("n_head", checkpoint_config.n_head, expected_config.n_head); |
| 86 | + CompareCheckpointValue("n_kv_head", checkpoint_config.n_kv_head, expected_config.n_kv_head); |
| 87 | + CompareCheckpointValue("n_embd", checkpoint_config.n_embd, expected_config.n_embd); |
| 88 | + CompareCheckpointValue("ffn_expansion_ratio", checkpoint_config.ffn_expansion_ratio, |
| 89 | + expected_config.ffn_expansion_ratio); |
| 90 | + CHECK(checkpoint_config.ffn_dim_multiplier.has_value()) << "checkpoint ffn_dim_multiplier is missing"; |
| 91 | + CHECK(expected_config.ffn_dim_multiplier.has_value()) << "runtime ffn_dim_multiplier is missing"; |
| 92 | + CompareCheckpointValue("ffn_dim_multiplier", checkpoint_config.ffn_dim_multiplier.value(), |
| 93 | + expected_config.ffn_dim_multiplier.value()); |
| 94 | + CompareCheckpointValue("multiple_of", checkpoint_config.multiple_of, expected_config.multiple_of); |
| 95 | + CompareCheckpointValue("norm_eps", checkpoint_config.norm_eps, expected_config.norm_eps); |
| 96 | + CompareCheckpointValue("rope_theta", checkpoint_config.rope_theta, expected_config.rope_theta); |
| 97 | + CompareCheckpointValue("use_scaled_rope", checkpoint_config.use_scaled_rope, expected_config.use_scaled_rope); |
| 98 | + |
| 99 | + CHECK(expected_config.moe_config.has_value()) << "tiny Mixtral runtime config requires MoE config"; |
| 100 | + const auto &checkpoint_moe = checkpoint_config.moe_config.value(); |
| 101 | + const auto &expected_moe = expected_config.moe_config.value(); |
| 102 | + CompareCheckpointValue("num_experts", checkpoint_moe.num_experts, expected_moe.num_experts); |
| 103 | + CompareCheckpointValue("router_topk", checkpoint_moe.router_topk, expected_moe.router_topk); |
| 104 | + CompareCheckpointValue("moe_ffn_hidden_size", checkpoint_moe.moe_ffn_hidden_size, |
| 105 | + expected_moe.moe_ffn_hidden_size); |
| 106 | +} |
| 107 | + |
| 108 | +std::shared_ptr<nn::TransformerModel> LoadFromLLMC(const std::string &filepath, |
| 109 | + const nn::TransformerConfig &expected_config) { |
| 110 | + CheckLLMCConfig(filepath, expected_config); |
| 111 | + auto model = std::make_shared<nn::TransformerModel>(expected_config); |
| 112 | + |
| 113 | + std::ifstream ifs(filepath, std::ios::binary); |
| 114 | + CHECK(ifs) << "Failed to open tiny Mixtral LLMC file: " << filepath; |
| 115 | + const auto header = infini_train::ReadSeveralBytesFromIfstream(kLLMCHeaderEntries * sizeof(int32_t), &ifs); |
| 116 | + CHECK(ifs) << "Failed to read tiny Mixtral LLMC header: " << filepath; |
| 117 | + CHECK_EQ(infini_train::BytesToType<int32_t>(header, 0 * sizeof(int32_t)), kTinyMixtralLLMCMagic); |
| 118 | + CHECK_EQ(infini_train::BytesToType<int32_t>(header, 1 * sizeof(int32_t)), kTinyMixtralLLMCVersion); |
| 119 | + |
| 120 | + const auto &config = expected_config; |
| 121 | + auto state = model->StateDict(); |
| 122 | + auto read_tensor_by_state_key = [&](const std::string &name) { |
| 123 | + CHECK(state.contains(name)) << "Model state_dict does not contain " << name; |
| 124 | + std::shared_ptr<infini_train::Tensor> tensor = state.at(name); |
| 125 | + CHECK(tensor->Dtype() == infini_train::DataType::kFLOAT32) |
| 126 | + << "Only float32 tiny Mixtral LLMC files are supported: " << name; |
| 127 | + infini_train::ReadMatrixAllFloat(ifs, static_cast<float *>(tensor->DataPtr()), tensor->NumElements(), 1); |
| 128 | + CHECK(ifs) << "Failed to read tensor " << name; |
| 129 | + }; |
| 130 | + |
| 131 | + auto read_projection_into_packed_qkv = [&](const std::string &packed_qkv_name, int64_t row_offset, |
| 132 | + int64_t num_rows, const std::string &projection_name) { |
| 133 | + CHECK(state.contains(packed_qkv_name)) << "Model state_dict does not contain " << packed_qkv_name; |
| 134 | + std::shared_ptr<infini_train::Tensor> tensor = state.at(packed_qkv_name); |
| 135 | + CHECK(tensor->Dtype() == infini_train::DataType::kFLOAT32) |
| 136 | + << "Only float32 tiny Mixtral LLMC files are supported: " << projection_name; |
| 137 | + CHECK_EQ(tensor->Dims().size(), 2); |
| 138 | + CHECK_GE(row_offset, 0); |
| 139 | + CHECK_GT(num_rows, 0); |
| 140 | + CHECK_LE(row_offset + num_rows, tensor->Dims()[0]); |
| 141 | + const int64_t cols = tensor->Dims()[1]; |
| 142 | + auto *data = static_cast<float *>(tensor->DataPtr()) + row_offset * cols; |
| 143 | + infini_train::ReadMatrixAllFloat(ifs, data, num_rows, cols); |
| 144 | + CHECK(ifs) << "Failed to read tensor rows " << projection_name; |
| 145 | + }; |
| 146 | + |
| 147 | + const auto &moe_config = config.moe_config.value(); |
| 148 | + read_tensor_by_state_key("transformer.wte.weight"); |
| 149 | + for (int64_t layer = 0; layer < config.n_layer; ++layer) { |
| 150 | + const std::string prefix = "transformer.h." + std::to_string(layer); |
| 151 | + read_tensor_by_state_key(prefix + ".ln_1.weight"); |
| 152 | + const auto c_attn_name = prefix + ".attn.c_attn.weight"; |
| 153 | + const int64_t head_dim = config.n_embd / config.n_head; |
| 154 | + const int64_t q_rows = config.n_head * head_dim; |
| 155 | + const int64_t kv_rows = config.n_kv_head * head_dim; |
| 156 | + read_projection_into_packed_qkv(c_attn_name, 0, q_rows, c_attn_name + ".q_proj"); |
| 157 | + read_projection_into_packed_qkv(c_attn_name, q_rows, kv_rows, c_attn_name + ".k_proj"); |
| 158 | + read_projection_into_packed_qkv(c_attn_name, q_rows + kv_rows, kv_rows, c_attn_name + ".v_proj"); |
| 159 | + read_tensor_by_state_key(prefix + ".attn.c_proj.weight"); |
| 160 | + read_tensor_by_state_key(prefix + ".ln_2.weight"); |
| 161 | + read_tensor_by_state_key(prefix + ".mlp.router.weight"); |
| 162 | + for (int64_t expert = 0; expert < moe_config.num_experts; ++expert) { |
| 163 | + const std::string expert_prefix = prefix + ".mlp.experts.expert_" + std::to_string(expert); |
| 164 | + read_tensor_by_state_key(expert_prefix + ".c_fc2.weight"); // Mixtral w1/gate_proj |
| 165 | + read_tensor_by_state_key(expert_prefix + ".c_fc.weight"); // Mixtral w3/up_proj |
| 166 | + read_tensor_by_state_key(expert_prefix + ".c_proj.weight"); // Mixtral w2/down_proj |
| 167 | + } |
| 168 | + } |
| 169 | + read_tensor_by_state_key("transformer.ln_f.weight"); |
| 170 | + read_tensor_by_state_key("lm_head.weight"); |
| 171 | + |
| 172 | + CHECK_EQ(ifs.peek(), std::ifstream::traits_type::eof()) << "Unexpected trailing bytes in tiny Mixtral LLMC file"; |
| 173 | + return model; |
| 174 | +} |
| 175 | + |
| 176 | +} // namespace tiny_mixtral |
0 commit comments