This commit is contained in:
Sigbjørn Skjæret 2024-08-27 10:30:30 +01:00 committed by GitHub
commit ed1455ca77
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 11 deletions

View file

@ -250,8 +250,7 @@ void IMatrixCollector::save_imatrix(int ncall) const {
} }
if (n_zeros > 0) { if (n_zeros > 0) {
fprintf(stderr, "%s: entry '%40s' has partial data (%.2f%%) - skipping\n", __func__, kv.first.c_str(), 100.0f * (n_all - n_zeros) / n_all); fprintf(stderr, "%s: entry '%40s' has partial data (%.2f%%)\n", __func__, kv.first.c_str(), 100.0f * (n_all - n_zeros) / n_all);
continue;
} }
n_entries++; n_entries++;
@ -275,7 +274,7 @@ void IMatrixCollector::save_imatrix(int ncall) const {
if (nval > 0) { if (nval > 0) {
std::vector<float> tmp(nval); std::vector<float> tmp(nval);
for (int i = 0; i < nval; i++) { for (int i = 0; i < nval; i++) {
tmp[i] = (stat.values[i] / static_cast<float>(stat.counts[i])) * static_cast<float>(stat.ncall); tmp[i] = stat.counts[i] ? (stat.values[i] / static_cast<float>(stat.counts[i])) * static_cast<float>(stat.ncall) : 0.0f;
} }
out.write((const char*)tmp.data(), nval*sizeof(float)); out.write((const char*)tmp.data(), nval*sizeof(float));
} }
@ -344,9 +343,11 @@ bool IMatrixCollector::load_imatrix(const char * fname) {
// Recreate the state as expected by save_imatrix(), and corerct for weighted sum. // Recreate the state as expected by save_imatrix(), and corerct for weighted sum.
for (int i = 0; i < nval; i++) { for (int i = 0; i < nval; i++) {
if (std::isnormal(tmp[i])) {
e.values[i] += tmp[i]; e.values[i] += tmp[i];
e.counts[i] += ncall; e.counts[i] += ncall;
} }
}
e.ncall += ncall; e.ncall += ncall;
} }

View file

@ -16767,19 +16767,25 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const s
if (params->only_copy) { if (params->only_copy) {
ftype = model.ftype; ftype = model.ftype;
} }
const std::unordered_map<std::string, std::vector<float>> * imatrix_data = nullptr; std::unordered_map<std::string, std::vector<float>> * imatrix_data = nullptr;
if (params->imatrix) { if (params->imatrix) {
imatrix_data = static_cast<const std::unordered_map<std::string, std::vector<float>>*>(params->imatrix); imatrix_data = static_cast<std::unordered_map<std::string, std::vector<float>>*>(params->imatrix);
if (imatrix_data) { if (imatrix_data) {
LLAMA_LOG_INFO("================================ Have weights data with %d entries\n",int(imatrix_data->size())); LLAMA_LOG_INFO("================================ Have weights data with %d entries\n",int(imatrix_data->size()));
qs.has_imatrix = true; qs.has_imatrix = true;
// check imatrix for nans or infs // check imatrix for nans or infs
for (const auto & kv : *imatrix_data) { for (auto it = imatrix_data->begin(); it != imatrix_data->end();) {
for (float f : kv.second) { bool remove_entry = false;
if (!std::isfinite(f)) {
throw std::runtime_error(format("imatrix contains non-finite value %f\n", f)); for (float f : it->second) {
if (!std::isnormal(f)) {
LLAMA_LOG_WARN("imatrix entry \"%s\" contains non-normal value %f, skipping!\n", it->first.c_str(), f);
remove_entry = true;
break;
} }
} }
it = remove_entry ? imatrix_data->erase(it) : ++it;
} }
} }
} }