Skip to content

Commit 1fbc59f

Browse files
committed
Replace slope with cross product
1 parent c855094 commit 1fbc59f

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/llama-quant.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,22 +1230,27 @@ static std::unordered_map<std::string, ggml_type> target_bpw_type(
12301230
if (candidates.size() < 3) { return; } // need at least 3 points to do convex hull
12311231

12321232
// Convex hull (lower envelope)
1233-
auto slope = [](const candidate_types & a, const candidate_types & b) {
1234-
const double dx = b.bytes - a.bytes;
1235-
return dx <= 0.0 ? infinity : (b.error - a.error) / dx;
1236-
};
1237-
12381233
std::vector<candidate_types> hull; hull.reserve(candidates.size());
1239-
for (const auto & p : candidates) {
1234+
for (const auto & c : candidates) {
1235+
auto cross_product = [](const candidate_types & h0, const candidate_types & h1, const candidate_types & p) -> double {
1236+
const double dx1 = (double)h1.bytes - (double)h0.bytes;
1237+
const double dy1 = h1.error - h0.error;
1238+
const double dx2 = (double)p.bytes - (double)h0.bytes;
1239+
const double dy2 = p.error - h0.error;
1240+
return dx1 * dy2 - dx2 * dy1;
1241+
};
1242+
12401243
while (hull.size() >= 2) {
1241-
const double s1 = slope(hull[hull.size() - 2], hull[hull.size() - 1]);
1242-
const double s2 = slope(hull[hull.size() - 1], p);
1243-
if (s2 + epsilon < s1) hull.pop_back();
1244-
else { break; }
1244+
if (cross_product(hull[hull.size() - 2], hull[hull.size() - 1], c) <= epsilon) {
1245+
hull.pop_back();
1246+
} else {
1247+
break;
1248+
}
12451249
}
12461250

1247-
hull.push_back(p);
1251+
hull.push_back(c);
12481252
}
1253+
12491254
candidates.swap(hull);
12501255
};
12511256

0 commit comments

Comments
 (0)