Skip to content

Commit

Permalink
Merge branch 'refs/heads/main' into fix_106900
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosdelest committed Apr 12, 2024
2 parents c4c5da0 + 965ebab commit 24d1b54
Show file tree
Hide file tree
Showing 1,996 changed files with 44,915 additions and 13,665 deletions.
2 changes: 1 addition & 1 deletion .buildkite/pipelines/intake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ steps:
timeout_in_minutes: 300
matrix:
setup:
BWC_VERSION: ["7.17.20", "8.13.2", "8.14.0"]
BWC_VERSION: ["7.17.21", "8.13.3", "8.14.0"]
agents:
provider: gcp
image: family/elasticsearch-ubuntu-2004
Expand Down
12 changes: 6 additions & 6 deletions .buildkite/pipelines/periodic-packaging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ steps:
env:
BWC_VERSION: 7.16.3

- label: "{{matrix.image}} / 7.17.20 / packaging-tests-upgrade"
command: ./.ci/scripts/packaging-test.sh -Dbwc.checkout.align=true destructiveDistroUpgradeTest.v7.17.20
- label: "{{matrix.image}} / 7.17.21 / packaging-tests-upgrade"
command: ./.ci/scripts/packaging-test.sh -Dbwc.checkout.align=true destructiveDistroUpgradeTest.v7.17.21
timeout_in_minutes: 300
matrix:
setup:
Expand All @@ -319,7 +319,7 @@ steps:
machineType: custom-16-32768
buildDirectory: /dev/shm/bk
env:
BWC_VERSION: 7.17.20
BWC_VERSION: 7.17.21

- label: "{{matrix.image}} / 8.0.1 / packaging-tests-upgrade"
command: ./.ci/scripts/packaging-test.sh -Dbwc.checkout.align=true destructiveDistroUpgradeTest.v8.0.1
Expand Down Expand Up @@ -529,8 +529,8 @@ steps:
env:
BWC_VERSION: 8.12.2

- label: "{{matrix.image}} / 8.13.2 / packaging-tests-upgrade"
command: ./.ci/scripts/packaging-test.sh -Dbwc.checkout.align=true destructiveDistroUpgradeTest.v8.13.2
- label: "{{matrix.image}} / 8.13.3 / packaging-tests-upgrade"
command: ./.ci/scripts/packaging-test.sh -Dbwc.checkout.align=true destructiveDistroUpgradeTest.v8.13.3
timeout_in_minutes: 300
matrix:
setup:
Expand All @@ -543,7 +543,7 @@ steps:
machineType: custom-16-32768
buildDirectory: /dev/shm/bk
env:
BWC_VERSION: 8.13.2
BWC_VERSION: 8.13.3

- label: "{{matrix.image}} / 8.14.0 / packaging-tests-upgrade"
command: ./.ci/scripts/packaging-test.sh -Dbwc.checkout.align=true destructiveDistroUpgradeTest.v8.14.0
Expand Down
16 changes: 8 additions & 8 deletions .buildkite/pipelines/periodic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,16 @@ steps:
buildDirectory: /dev/shm/bk
env:
BWC_VERSION: 7.16.3
- label: 7.17.20 / bwc
command: .ci/scripts/run-gradle.sh -Dbwc.checkout.align=true v7.17.20#bwcTest
- label: 7.17.21 / bwc
command: .ci/scripts/run-gradle.sh -Dbwc.checkout.align=true v7.17.21#bwcTest
timeout_in_minutes: 300
agents:
provider: gcp
image: family/elasticsearch-ubuntu-2004
machineType: n1-standard-32
buildDirectory: /dev/shm/bk
env:
BWC_VERSION: 7.17.20
BWC_VERSION: 7.17.21
- label: 8.0.1 / bwc
command: .ci/scripts/run-gradle.sh -Dbwc.checkout.align=true v8.0.1#bwcTest
timeout_in_minutes: 300
Expand Down Expand Up @@ -312,16 +312,16 @@ steps:
buildDirectory: /dev/shm/bk
env:
BWC_VERSION: 8.12.2
- label: 8.13.2 / bwc
command: .ci/scripts/run-gradle.sh -Dbwc.checkout.align=true v8.13.2#bwcTest
- label: 8.13.3 / bwc
command: .ci/scripts/run-gradle.sh -Dbwc.checkout.align=true v8.13.3#bwcTest
timeout_in_minutes: 300
agents:
provider: gcp
image: family/elasticsearch-ubuntu-2004
machineType: n1-standard-32
buildDirectory: /dev/shm/bk
env:
BWC_VERSION: 8.13.2
BWC_VERSION: 8.13.3
- label: 8.14.0 / bwc
command: .ci/scripts/run-gradle.sh -Dbwc.checkout.align=true v8.14.0#bwcTest
timeout_in_minutes: 300
Expand Down Expand Up @@ -396,7 +396,7 @@ steps:
setup:
ES_RUNTIME_JAVA:
- openjdk17
BWC_VERSION: ["7.17.20", "8.13.2", "8.14.0"]
BWC_VERSION: ["7.17.21", "8.13.3", "8.14.0"]
agents:
provider: gcp
image: family/elasticsearch-ubuntu-2004
Expand Down Expand Up @@ -438,7 +438,7 @@ steps:
- graalvm-ce17
- openjdk17
- openjdk21
BWC_VERSION: ["7.17.20", "8.13.2", "8.14.0"]
BWC_VERSION: ["7.17.21", "8.13.3", "8.14.0"]
agents:
provider: gcp
image: family/elasticsearch-ubuntu-2004
Expand Down
4 changes: 2 additions & 2 deletions .ci/bwcVersions
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ BWC_VERSION:
- "7.14.2"
- "7.15.2"
- "7.16.3"
- "7.17.20"
- "7.17.21"
- "8.0.1"
- "8.1.3"
- "8.2.3"
Expand All @@ -30,5 +30,5 @@ BWC_VERSION:
- "8.10.4"
- "8.11.4"
- "8.12.2"
- "8.13.2"
- "8.13.3"
- "8.14.0"
4 changes: 2 additions & 2 deletions .ci/snapshotBwcVersions
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
BWC_VERSION:
- "7.17.20"
- "8.13.2"
- "7.17.21"
- "8.13.3"
- "8.14.0"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ testfixtures_shared/

# Generated
checkstyle_ide.xml
x-pack/plugin/esql/gen/
x-pack/plugin/esql/src/main/generated-src/generated/
13 changes: 13 additions & 0 deletions benchmarks/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ apply plugin: org.elasticsearch.gradle.internal.ElasticsearchJavaBasePlugin
apply plugin: 'java-library'
apply plugin: 'application'

var os = org.gradle.internal.os.OperatingSystem.current()

application {
mainClass = 'org.openjdk.jmh.Main'
}
Expand Down Expand Up @@ -39,6 +41,7 @@ dependencies {
api(project(':x-pack:plugin:ql'))
api(project(':x-pack:plugin:esql'))
api(project(':x-pack:plugin:esql:compute'))
implementation project(path: ':libs:elasticsearch-vec')
expression(project(path: ':modules:lang-expression', configuration: 'zip'))
painless(project(path: ':modules:lang-painless', configuration: 'zip'))
api "org.openjdk.jmh:jmh-core:$versions.jmh"
Expand Down Expand Up @@ -73,6 +76,16 @@ tasks.named("run").configure {
executable = "${BuildParams.runtimeJavaHome}/bin/java"
args << "-Dplugins.dir=${buildDir}/plugins" << "-Dtests.index=${buildDir}/index"
dependsOn "copyExpression", "copyPainless"
systemProperty 'java.library.path', file("../libs/native/libraries/build/platform/${platformName()}-${os.arch}")
}

String platformName() {
String name = System.getProperty("os.name");
if (name.startsWith("Mac")) {
return "darwin";
} else {
return name.toLowerCase(Locale.ROOT);
}
}

spotless {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.benchmark.vector;

import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.MMapDirectory;
import org.apache.lucene.util.quantization.ScalarQuantizedVectorSimilarity;
import org.elasticsearch.common.logging.LogConfigurator;
import org.elasticsearch.core.IOUtils;
import org.elasticsearch.vec.VectorScorer;
import org.elasticsearch.vec.VectorScorerFactory;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Warmup;

import java.io.IOException;
import java.nio.file.Files;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

import static org.elasticsearch.vec.VectorSimilarityType.DOT_PRODUCT;
import static org.elasticsearch.vec.VectorSimilarityType.EUCLIDEAN;

@Fork(value = 1, jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" })
@Warmup(iterations = 3, time = 3)
@Measurement(iterations = 5, time = 3)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Thread)
/**
* Benchmark that compares various scalar quantized vector similarity function
* implementations;: scalar, lucene's panama-ized, and Elasticsearch's native.
* Run with ./gradlew -p benchmarks run --args 'VectorScorerBenchmark'
*/
public class VectorScorerBenchmark {

static {
LogConfigurator.configureESLogging(); // native access requires logging to be initialized
}

@Param({ "96", "768", "1024" })
int dims;
int size = 2; // there are only two vectors to compare

Directory dir;
IndexInput in;
VectorScorerFactory factory;

byte[] vec1;
byte[] vec2;
float vec1Offset;
float vec2Offset;
float scoreCorrectionConstant;

ScalarQuantizedVectorSimilarity luceneDotScorer;
ScalarQuantizedVectorSimilarity luceneSqrScorer;
VectorScorer nativeDotScorer;
VectorScorer nativeSqrScorer;

@Setup
public void setup() throws IOException {
var optionalVectorScorerFactory = VectorScorerFactory.instance();
if (optionalVectorScorerFactory.isEmpty()) {
String msg = "JDK=["
+ Runtime.version()
+ "], os.name=["
+ System.getProperty("os.name")
+ "], os.arch=["
+ System.getProperty("os.arch")
+ "]";
throw new AssertionError("Vector scorer factory not present. Cannot run the benchmark. " + msg);
}
factory = optionalVectorScorerFactory.get();
scoreCorrectionConstant = 1f;
vec1 = new byte[dims];
vec2 = new byte[dims];

ThreadLocalRandom.current().nextBytes(vec1);
ThreadLocalRandom.current().nextBytes(vec2);
vec1Offset = ThreadLocalRandom.current().nextFloat();
vec2Offset = ThreadLocalRandom.current().nextFloat();

dir = new MMapDirectory(Files.createTempDirectory("nativeScalarQuantBench"));
try (IndexOutput out = dir.createOutput("vector.data", IOContext.DEFAULT)) {
out.writeBytes(vec1, 0, vec1.length);
out.writeInt(Float.floatToIntBits(vec1Offset));
out.writeBytes(vec2, 0, vec2.length);
out.writeInt(Float.floatToIntBits(vec2Offset));
}
in = dir.openInput("vector.data", IOContext.DEFAULT);

luceneDotScorer = ScalarQuantizedVectorSimilarity.fromVectorSimilarity(
VectorSimilarityFunction.DOT_PRODUCT,
scoreCorrectionConstant
);
luceneSqrScorer = ScalarQuantizedVectorSimilarity.fromVectorSimilarity(VectorSimilarityFunction.EUCLIDEAN, scoreCorrectionConstant);
nativeDotScorer = factory.getScalarQuantizedVectorScorer(dims, size, scoreCorrectionConstant, DOT_PRODUCT, in).get();
nativeSqrScorer = factory.getScalarQuantizedVectorScorer(dims, size, scoreCorrectionConstant, EUCLIDEAN, in).get();

// sanity
var f1 = dotProductLucene();
var f2 = dotProductNative();
var f3 = dotProductScalar();
if (f1 != f2) {
throw new AssertionError("lucene[" + f1 + "] != " + "native[" + f2 + "]");
}
if (f1 != f3) {
throw new AssertionError("lucene[" + f1 + "] != " + "scalar[" + f3 + "]");
}
// square distance
f1 = squareDistanceLucene();
f2 = squareDistanceNative();
f3 = squareDistanceScalar();
if (f1 != f2) {
throw new AssertionError("lucene[" + f1 + "] != " + "native[" + f2 + "]");
}
if (f1 != f3) {
throw new AssertionError("lucene[" + f1 + "] != " + "scalar[" + f3 + "]");
}
}

@TearDown
public void teardown() throws IOException {
IOUtils.close(dir, in);
}

@Benchmark
public float dotProductLucene() {
return luceneDotScorer.score(vec1, vec1Offset, vec2, vec2Offset);
}

@Benchmark
public float dotProductNative() throws IOException {
return nativeDotScorer.score(0, 1);
}

@Benchmark
public float dotProductScalar() {
int dotProduct = 0;
for (int i = 0; i < vec1.length; i++) {
dotProduct += vec1[i] * vec2[i];
}
float adjustedDistance = dotProduct * scoreCorrectionConstant + vec1Offset + vec2Offset;
return (1 + adjustedDistance) / 2;
}

// -- square distance

@Benchmark
public float squareDistanceLucene() {
return luceneSqrScorer.score(vec1, vec1Offset, vec2, vec2Offset);
}

@Benchmark
public float squareDistanceNative() throws IOException {
return nativeSqrScorer.score(0, 1);
}

@Benchmark
public float squareDistanceScalar() {
int squareDistance = 0;
for (int i = 0; i < vec1.length; i++) {
int diff = vec1[i] - vec2[i];
squareDistance += diff * diff;
}
float adjustedDistance = squareDistance * scoreCorrectionConstant;
return 1 / (1f + adjustedDistance);
}
}
Loading

0 comments on commit 24d1b54

Please sign in to comment.