Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hparam comparison uses memcpy for floating point comparison, which is in general a bad idea #3446

Merged
merged 5 commits into from
Oct 6, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,28 @@ static void replace_all(std::string & s, const std::string & search, const std::
}
s = std::move(result);
}

static bool is_float_eq(float a, float b, float abs_tol) {
// Check for non-negative tolerance
if (abs_tol < 0.0) {
throw std::invalid_argument("Tolerance must be non-negative");
}

// Exact equality check
if (a == b) {
return true;
}

// Check for infinities
if (std::isinf(a) || std::isinf(b)) {
return false;
}

// Regular comparison using the provided absolute tolerance
double diff = std::fabs(b - a);
return (diff <= abs_tol);
}

#ifdef GGML_USE_CPU_HBM
#include <hbwmalloc.h>
#endif
Expand Down Expand Up @@ -948,7 +970,24 @@ struct llama_hparams {
float rope_freq_scale_train;

bool operator!=(const llama_hparams & other) const {
l3utterfly marked this conversation as resolved.
Show resolved Hide resolved
return static_cast<bool>(memcmp(this, &other, sizeof(llama_hparams))); // NOLINT
if (this->vocab_only != other.vocab_only) return true;
if (this->n_vocab != other.n_vocab) return true;
if (this->n_ctx_train != other.n_ctx_train) return true;
if (this->n_embd != other.n_embd) return true;
if (this->n_head != other.n_head) return true;
if (this->n_head_kv != other.n_head_kv) return true;
if (this->n_layer != other.n_layer) return true;
if (this->n_rot != other.n_rot) return true;
if (this->n_ff != other.n_ff) return true;

const float EPSILON = 1e-9;

if (!is_float_eq(this->f_norm_eps, other.f_norm_eps, EPSILON)) return true;
if (!is_float_eq(this->f_norm_rms_eps, other.f_norm_rms_eps, EPSILON)) return true;
if (!is_float_eq(this->rope_freq_base_train, other.rope_freq_base_train, EPSILON)) return true;
if (!is_float_eq(this->rope_freq_scale_train, other.rope_freq_scale_train, EPSILON)) return true;

return false;
}

uint32_t n_gqa() const {
Expand Down