kaivalnp commented on code in PR #16790:
URL: https://github.com/apache/lucene/pull/16790#discussion_r4516973200
##########
lucene/core/src/java/org/apache/lucene/util/VectorUtil.java:
##########
@@ -598,6 +598,62 @@ public static float recalculateOffset(
vector, oldAlpha, oldMinQuantile, scale, alpha, minQuantile,
maxQuantile);
}
+ /**
+ * Subtracts {@code centroid} from {@code vector} in place, for the
optimized scalar quantizer,
+ * and writes into {@code stats} the dot product of the original vector with
{@code centroid},
+ * then the min, max, sum of squares and sum of the result, and the sum of
its squared differences
+ * from its mean.
+ *
+ * @lucene.experimental
+ */
+ public static void osqCenter(float[] vector, float[] centroid, float[]
stats) {
+ if (vector.length != centroid.length) {
+ throw new IllegalArgumentException(
+ "vector dimensions differ: " + vector.length + "!=" +
centroid.length);
+ }
+ if (stats.length != 6) {
+ throw new IllegalArgumentException("stats must have 6 entries: " +
stats.length);
+ }
+ IMPL.osqCenter(vector, centroid, stats);
+ }
+
+ /**
+ * Computes the sums that the optimized scalar quantizer's interval search
needs for one interval.
+ * Each value {@code x} of {@code vector}, limited to {@code [lower,
upper]}, is rounded half up
+ * to one of {@code points} levels {@code k}, which dequantizes to {@code
x'}. Writes the sums of
+ * k, k * k, x * k, x * (x - x') and (x - x') * (x - x') into {@code stats}.
+ *
+ * @lucene.experimental
+ */
+ public static void osqGridStats(
+ float[] vector, float lower, float upper, int points, double[] stats) {
+ if (points < 2 || points > 256) {
+ throw new IllegalArgumentException("points must be in [2, 256]: " +
points);
+ }
+ if (stats.length != 5) {
+ throw new IllegalArgumentException("stats must have 5 entries: " +
stats.length);
+ }
+ assert lower <= upper : lower + " > " + upper;
Review Comment:
I asked an AI for an example of when the quantize function can trip this
assertion:
```java
float[] v = {0.1f, 0.1f, 0.10000001f, 0.10000001f}; // two values 1 ulp apart
new OptimizedScalarQuantizer(VectorSimilarityFunction.EUCLIDEAN)
.scalarQuantize(v, new byte[4], (byte) 2, new float[4]);
```
Unfortunately looks like this can be common, should we remove the assertion?
Sorry for the churn :(
##########
lucene/core/src/java25/org/apache/lucene/internal/vectorization/PanamaVectorUtilSupport.java:
##########
@@ -1584,6 +1584,151 @@ public float recalculateScalarQuantizationOffset(
return correction;
}
+ // reduceLanes(ADD) leaves the order of the additions open, and it adds in a
different order
+ // before C2 compiles it than after, so the codes would depend on JIT state.
+ // Adding the lanes in order keeps them the same.
+ private static float sumLanes(FloatVector v) {
+ float sum = 0;
+ for (int i = 0; i < v.length(); i++) {
+ sum += v.lane(i);
+ }
+ return sum;
+ }
Review Comment:
Based on [existing
usage](https://github.com/apache/lucene/blob/e0a3a951e7ef2e7b4e04b6b9966468099e642056/lucene/core/src/java25/org/apache/lucene/internal/vectorization/PanamaVectorUtilSupport.java#L237-L239),
this personally feels ok to use.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]