Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[GGUF] Fix Coverity reports in scaling and weight requantization (#39102)

Maxim Vafin172fff31731c
diff --git a/src/frontends/gguf/src/quant/gguf.cpp b/src/frontends/gguf/src/quant/gguf.cpp
index e2fcaae6035..82131069f16 100644
--- a/src/frontends/gguf/src/quant/gguf.cpp
+++ b/src/frontends/gguf/src/quant/gguf.cpp
@@ -739,7 +739,20 @@ std::map<std::string, GGUFMetaData> decoder_config_from_meta(
// Mirrors llama.cpp: hparams.rope_freq_scale_train = 1/ropescale; ext_factor = (yarn?1:0).
{
const float ropescale = metadata_to_float_or(metadata, arch + ".rope.scaling.factor", 0.0f);
- config["rope_freq_scale"] = ropescale == 0.0f ? 1.0f : 1.0f / ropescale;
+ float freq_scale = 1.0f;
+ if (ropescale != 0.0f) {
+ OPENVINO_ASSERT(std::isfinite(ropescale) && ropescale > 0.0f,
+ "[GGUF] '",
+ arch,
+ ".rope.scaling.factor' must be finite and positive");
+ const double inverse = 1.0 / static_cast<double>(ropescale);
+ OPENVINO_ASSERT(inverse <= std::numeric_limits<float>::max(),
+ "[GGUF] '",
+ arch,
+ ".rope.scaling.factor' reciprocal exceeds the float range");
+ freq_scale = static_cast<float>(inverse);
+ }
+ config["rope_freq_scale"] = freq_scale;
const bool is_yarn = metadata.count(arch + ".rope.scaling.type") &&
std::get<std::string>(metadata.at(arch + ".rope.scaling.type")) == "yarn";
diff --git a/src/frontends/gguf/src/quant/gguf_quants.cpp b/src/frontends/gguf/src/quant/gguf_quants.cpp
index e8f2e864105..ee732d1f12a 100644
--- a/src/frontends/gguf/src/quant/gguf_quants.cpp
+++ b/src/frontends/gguf/src/quant/gguf_quants.cpp
@@ -98,14 +98,22 @@ void quantize_row_q8_0_c(const float* x, size_t cols, int8_t* out_weights, ov::f
for (size_t c = 0; c < cols; ++c) {
amax = std::max(amax, std::fabs(x[c]));
}
- const float d = amax / 127.0f;
- // A zero row has a zero scale and must remain zero. Keep the division in a
- // branch where its divisor is known to be non-zero.
- float id = 0.0f;
- if (d != 0.0f) {
- id = 1.0f / d;
+ if (amax == 0.0f) {
+ out_scale = ov::float16(0.0f);
+ std::fill_n(out_weights, cols, int8_t{0});
+ return;
}
+ const float d = amax / 127.0f;
out_scale = ov::float16(d);
+ if (d < std::numeric_limits<float>::min()) {
+ // Avoid an underflowed scale or overflowing reciprocal for tiny nonzero rows.
+ const double id = 127.0 / static_cast<double>(amax);
+ for (size_t c = 0; c < cols; ++c) {
+ out_weights[c] = static_cast<int8_t>(std::lround(static_cast<double>(x[c]) * id));
+ }
+ return;
+ }
+ const float id = 1.0f / d;
for (size_t c = 0; c < cols; ++c) {
out_weights[c] = static_cast<int8_t>(std::lround(x[c] * id));
}
diff --git a/src/frontends/gguf/tests/test_builder_api.cpp b/src/frontends/gguf/tests/test_builder_api.cpp
index 1b6943c8573..3f43f8a3a8a 100644
--- a/src/frontends/gguf/tests/test_builder_api.cpp
+++ b/src/frontends/gguf/tests/test_builder_api.cpp
@@ -282,6 +282,30 @@ TEST(GGUFBuilderAPI, DecoderOptionsAreResolvedBeforeRopePlans) {
EXPECT_THROW(other.configure_decoder(RopeMode::Neox, invalid), ov::Exception);
}
+TEST(GGUFBuilderAPI, RopeScalingDefaultsAndPositiveFactors) {
+ Environment env;
+ env.decoder();
+ EXPECT_FLOAT_EQ(std::get<float>(decoder_config_from_meta(env.metadata).at("rope_freq_scale")), 1.0f);
+ for (const float factor : {0.0f, 1.0f, 4.0f}) {
+ env.real("test.rope.scaling.factor", factor);
+ EXPECT_FLOAT_EQ(std::get<float>(decoder_config_from_meta(env.metadata).at("rope_freq_scale")),
+ factor == 0.0f ? 1.0f : 1.0f / factor);
+ }
+}
+
+TEST(GGUFBuilderAPI, RejectsInvalidRopeScalingFactors) {
+ for (const float factor : {-1.0f,
+ std::numeric_limits<float>::infinity(),
+ std::numeric_limits<float>::quiet_NaN(),
+ std::numeric_limits<float>::denorm_min()}) {
+ SCOPED_TRACE(factor);
+ Environment env;
+ env.decoder();
+ env.real("test.rope.scaling.factor", factor);
+ EXPECT_THROW(decoder_config_from_meta(env.metadata), ov::Exception);
+ }
+}
+
TEST(GGUFBuilderAPI, RecurrentStateDeclarationReachesMakeStateful) {
Environment env;
GgufGraphContext graph(env.context);
diff --git a/src/frontends/gguf/tests/test_dequant_vs_ggml.cpp b/src/frontends/gguf/tests/test_dequant_vs_ggml.cpp
index 6babd7a2db7..62222858789 100644
--- a/src/frontends/gguf/tests/test_dequant_vs_ggml.cpp
+++ b/src/frontends/gguf/tests/test_dequant_vs_ggml.cpp
@@ -19,6 +19,7 @@
#include <cmath>
#include <cstdint>
#include <cstring>
+#include <limits>
#include <string>
#include <tuple>
#include <vector>
@@ -35,6 +36,27 @@
using namespace ov::frontend::gguf;
using ov_gguf_test::load_npy;
+TEST(GGUFDequant, ChannelwiseQ8ZeroRowRemainsZero) {
+ const std::vector<float> row(32, 0.0f);
+ std::vector<int8_t> weights(row.size(), 1);
+ ov::float16 scale(1.0f);
+ quantize_row_q8_0_c(row.data(), row.size(), weights.data(), scale);
+ EXPECT_FLOAT_EQ(static_cast<float>(scale), 0.0f);
+ EXPECT_EQ(weights, std::vector<int8_t>(row.size(), 0));
+}
+
+TEST(GGUFDequant, ChannelwiseQ8TinyRowsHaveFiniteQuantizedWeights) {
+ for (const float maximum : {std::numeric_limits<float>::min(), std::numeric_limits<float>::denorm_min()}) {
+ SCOPED_TRACE(maximum);
+ const std::vector<float> row{maximum, -maximum, 0.0f};
+ std::vector<int8_t> weights(row.size());
+ ov::float16 scale;
+ quantize_row_q8_0_c(row.data(), row.size(), weights.data(), scale);
+ EXPECT_EQ(weights, (std::vector<int8_t>{127, -127, 0}));
+ EXPECT_FLOAT_EQ(static_cast<float>(scale), 0.0f);
+ }
+}
+
namespace {
std::vector<float> eval_as_f32(const std::shared_ptr<ov::Node>& node) {