From c6a396c6a4fbc66831f0f83ce4c84c5afee97d90 Mon Sep 17 00:00:00 2001 From: Alex Klibisz Date: Mon, 27 Feb 2023 10:08:06 -0800 Subject: [PATCH] Some internal cleanup from previous PR (#498) --- .../vectors/VectorOpsJmhBenchmark.scala | 8 ++++---- .../vectors/DefaultFloatVectorOps.java | 20 ++++++++----------- .../elastiknn/vectors/FloatVectorOps.java | 6 +++--- .../vectors/PanamaFloatVectorOps.java | 8 ++++---- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/elastiknn-models-benchmarks/src/main/scala/com/klibisz/elastiknn/vectors/VectorOpsJmhBenchmark.scala b/elastiknn-models-benchmarks/src/main/scala/com/klibisz/elastiknn/vectors/VectorOpsJmhBenchmark.scala index 1ee7502d3..dfc38ee49 100644 --- a/elastiknn-models-benchmarks/src/main/scala/com/klibisz/elastiknn/vectors/VectorOpsJmhBenchmark.scala +++ b/elastiknn-models-benchmarks/src/main/scala/com/klibisz/elastiknn/vectors/VectorOpsJmhBenchmark.scala @@ -37,16 +37,16 @@ class VectorOpsJmhBenchmark { @Fork(value = 1) @Warmup(time = 5, iterations = 6) @Measurement(time = 5, iterations = 6) - def dotProductPanama(state: BenchmarkState): Unit = - state.panama.dotProduct(state.v1, state.v2) + def dotProductDefault(state: BenchmarkState): Double = + state.default.dotProduct(state.v1, state.v2) @Benchmark @BenchmarkMode(Array(Mode.Throughput)) @Fork(value = 1) @Warmup(time = 5, iterations = 6) @Measurement(time = 5, iterations = 6) - def dotProductDefault(state: BenchmarkState): Double = - state.default.cosineSimilarity(state.v1, state.v2) + def dotProductPanama(state: BenchmarkState): Unit = + state.panama.dotProduct(state.v1, state.v2) @Benchmark @BenchmarkMode(Array(Mode.Throughput)) diff --git a/elastiknn-models/src/main/java/com/klibisz/elastiknn/vectors/DefaultFloatVectorOps.java b/elastiknn-models/src/main/java/com/klibisz/elastiknn/vectors/DefaultFloatVectorOps.java index af3d3a72a..95809a482 100644 --- a/elastiknn-models/src/main/java/com/klibisz/elastiknn/vectors/DefaultFloatVectorOps.java +++ b/elastiknn-models/src/main/java/com/klibisz/elastiknn/vectors/DefaultFloatVectorOps.java @@ -19,25 +19,21 @@ public double cosineSimilarity(float[] v1, float[] v2) { else return -1; } + public double dotProduct(float[] v1, float[] v2) { + float dotProd = 0f; + for (int i = 0; i < v1.length; i++) dotProd += v1[i] * v2[i]; + return dotProd; + } + public double l1Distance(float[] v1, float[] v2) { double sumAbsDiff = 0.0; - for (int i = 0; i < v1.length; i++) { - sumAbsDiff += Math.abs(v1[i] - v2[i]); - } + for (int i = 0; i < v1.length; i++) sumAbsDiff += Math.abs(v1[i] - v2[i]); return sumAbsDiff; } - public double dotProduct(float[] v1, float[] v2) { - float dp = 0f; - for (int i = 0; i < v1.length; i++) dp += v1[i] * v2[i]; - return dp; - } - public double l2Distance(float[] v1, float[] v2) { double sumSqrDiff = 0.0; - for (int i = 0; i < v1.length; i++) { - sumSqrDiff += Math.pow(v1[i] - v2[i], 2); - } + for (int i = 0; i < v1.length; i++) sumSqrDiff += Math.pow(v1[i] - v2[i], 2); return Math.sqrt(sumSqrDiff); } } diff --git a/elastiknn-models/src/main/java/com/klibisz/elastiknn/vectors/FloatVectorOps.java b/elastiknn-models/src/main/java/com/klibisz/elastiknn/vectors/FloatVectorOps.java index f8d2a060b..485bca4f6 100644 --- a/elastiknn-models/src/main/java/com/klibisz/elastiknn/vectors/FloatVectorOps.java +++ b/elastiknn-models/src/main/java/com/klibisz/elastiknn/vectors/FloatVectorOps.java @@ -2,11 +2,11 @@ public interface FloatVectorOps { - double dotProduct(final float[] v1, final float[] v2); + double dotProduct(float[] v1, float[] v2); - double l2Distance(final float[] v1, final float[] v2); + double l2Distance(float[] v1, float[] v2); double l1Distance(float[] v1, float[] v2); - public double cosineSimilarity(float[] v1, float[] v2); + double cosineSimilarity(float[] v1, float[] v2); } diff --git a/elastiknn-models/src/main/java/com/klibisz/elastiknn/vectors/PanamaFloatVectorOps.java b/elastiknn-models/src/main/java/com/klibisz/elastiknn/vectors/PanamaFloatVectorOps.java index c9b091b8e..dbe37fc5d 100644 --- a/elastiknn-models/src/main/java/com/klibisz/elastiknn/vectors/PanamaFloatVectorOps.java +++ b/elastiknn-models/src/main/java/com/klibisz/elastiknn/vectors/PanamaFloatVectorOps.java @@ -36,19 +36,19 @@ public double cosineSimilarity(float[] v1, float[] v2) { } public double dotProduct(float[] v1, float[] v2) { - double dp = 0f; + double dotProd = 0f; int i = 0; int bound = species.loopBound(v1.length); FloatVector pv1, pv2; for (; i < bound; i += species.length()) { pv1 = FloatVector.fromArray(species, v1, i); pv2 = FloatVector.fromArray(species, v2, i); - dp += pv1.mul(pv2).reduceLanes(VectorOperators.ADD); + dotProd += pv1.mul(pv2).reduceLanes(VectorOperators.ADD); } for (; i < v1.length; i++) { - dp = Math.fma(v1[i], v2[i], dp); + dotProd = Math.fma(v1[i], v2[i], dotProd); } - return dp; + return dotProd; }