Skip to content

Commit c11184a

Browse files
committed
Generate model ID hash
1 parent 044fa78 commit c11184a

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

src/llama-quant.cpp

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,6 @@ static std::unordered_map<std::string, ggml_type> target_bpw_type(
674674
constexpr double infinity = std::numeric_limits<double>::infinity();
675675
constexpr uint32_t file_magic = 0x42505731; // BPW1
676676
const char * func = __func__;
677-
const std::string checkpoint_file = ml.arch_name + ".bpw_state";
678677

679678
auto tensor_bytes = [](const ggml_tensor * t, const ggml_type typ) -> size_t {
680679
const int64_t n_per_row = t->ne[0];
@@ -745,13 +744,34 @@ static std::unordered_map<std::string, ggml_type> target_bpw_type(
745744
size_t n_elements = 0;
746745
};
747746

747+
auto djb2_hash = [](const uint8_t * data, size_t n) -> uint64_t {
748+
uint64_t h = 5381;
749+
for (size_t i = 0; i < n; ++i) {
750+
h = (h << 5) + h + data[i];
751+
}
752+
return h ? h : 0xeabada55cafed00d;
753+
};
754+
755+
auto metadata_id = [&](const gguf_context * ctx) -> uint64_t {
756+
const size_t sz = gguf_get_meta_size(ctx);
757+
std::vector<uint8_t> buf(sz);
758+
gguf_get_meta_data(ctx, buf.data());
759+
return djb2_hash(buf.data(), buf.size());
760+
};
761+
762+
char hex[17];
763+
const uint64_t model_id = metadata_id(ml.meta.get());
764+
std::snprintf(hex, sizeof(hex), "%016" PRIx64, (uint64_t)model_id);
765+
const std::string checkpoint_file = ml.arch_name + "-" + std::string(hex) + ".bpw_state";
766+
748767
auto save_bpw_state = [&](const std::vector<tensor_info> & all_vec) {
749768
const std::string tmp = checkpoint_file + ".tmp";
750769
std::ofstream ofs(tmp, std::ios::binary | std::ios::trunc);
751770
if (!ofs) { return; } // best-effort
752771
const float target_bpw = params->target_bpw;
753772
const uint8_t bias_mode = params->no_bias ? 1 : 0;
754773
ofs.write((const char *)&file_magic, sizeof(file_magic));
774+
ofs.write((const char *)&model_id, sizeof(model_id));
755775
ofs.write((const char *)&target_bpw, sizeof(target_bpw));
756776
ofs.write((const char *)&bias_mode, sizeof(bias_mode));
757777
const uint64_t n = all_vec.size();
@@ -781,9 +801,9 @@ static std::unordered_map<std::string, ggml_type> target_bpw_type(
781801
}
782802

783803
ofs.close();
784-
std::remove(checkpoint_file.c_str()); // TODO: handle errors
804+
std::remove(checkpoint_file.c_str());
785805
std::rename(tmp.c_str(), checkpoint_file.c_str());
786-
LLAMA_LOG_INFO("%s: saved bpw progress for %lu tensors to %s\n", func, all_vec.size(), checkpoint_file.c_str());
806+
LLAMA_LOG_INFO("%s: saved progress for %lu tensors to %s\n", func, all_vec.size(), checkpoint_file.c_str());
787807
};
788808

789809
auto load_bpw_state = [&]() -> std::unordered_map<std::string, saved_info> {
@@ -792,22 +812,27 @@ static std::unordered_map<std::string, ggml_type> target_bpw_type(
792812
if (!ifs) { return out; }
793813

794814
uint32_t magic = 0;
795-
float target_bpw = 0.0f;
796-
uint8_t bias_mode = 0;
815+
uint64_t id = 0;
816+
float bpw = 0.0f;
817+
uint8_t bias = 0;
797818
ifs.read((char *)&magic, sizeof(magic));
798-
ifs.read((char *)&target_bpw, sizeof(target_bpw));
799-
ifs.read((char *)&bias_mode, sizeof(bias_mode));
819+
ifs.read((char *)&id, sizeof(id));
820+
ifs.read((char *)&bpw, sizeof(bpw));
821+
ifs.read((char *)&bias, sizeof(bias));
800822
if (magic != file_magic) {
801823
LLAMA_LOG_WARN("%s: invalid resume file, ignoring: %s\n", func, checkpoint_file.c_str());
802824
return out;
803-
}
804-
if (target_bpw != params->target_bpw) {
805-
LLAMA_LOG_WARN("%s: target bpw of %f does not match %f, ignoring: %s\n", func, params->target_bpw, target_bpw, checkpoint_file.c_str());
825+
} else if (id != model_id) {
826+
LLAMA_LOG_WARN("%s: model ID mismatch, ignoring: %s\n", func, checkpoint_file.c_str());
806827
return out;
807-
}
808-
if (bias_mode != (params->no_bias ? 1 : 0)) {
828+
} else if (bpw != params->target_bpw) {
829+
LLAMA_LOG_WARN("%s: target bpw of %f does not match %f, ignoring: %s\n", func, params->target_bpw, bpw, checkpoint_file.c_str());
830+
return out;
831+
} else if (bias != (params->no_bias ? 1 : 0)) {
809832
LLAMA_LOG_WARN("%s: bias mode does not match, ignoring: %s\n", func, checkpoint_file.c_str());
810833
return out;
834+
} else {
835+
LLAMA_LOG_INFO("%s: resuming tensor quantization\n", func);
811836
}
812837

813838
uint64_t n = 0;
@@ -859,7 +884,7 @@ static std::unordered_map<std::string, ggml_type> target_bpw_type(
859884

860885
auto check_signal_handler = [&](const std::vector<tensor_info> & all_vec) {
861886
if (bpw_stop.load(std::memory_order_relaxed)) {
862-
LLAMA_LOG_INFO("\n%s: saving bpw progress for %lu tensors to %s\n", func, all_vec.size(), checkpoint_file.c_str());
887+
LLAMA_LOG_INFO("\n%s: saving progress for %lu tensors to %s\n", func, all_vec.size(), checkpoint_file.c_str());
863888
save_bpw_state(all_vec);
864889
throw std::runtime_error("user interrupted the process");
865890
}

0 commit comments

Comments
 (0)