mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-04-19 04:45:07 -04:00
Add an optimised int8 vector distance function for aarch64. (#106133)
This commit adds an optimised int8 vector distance implementation for aarch64. Additional platforms like, say, x64, will be added as a follow-up. The vector distance implementation outperforms Lucene's Pamana Vector implementation for binary comparisons by approx 5x (depending on the number of dimensions). It does so by means of compiler intrinsics built into a separate native library and link by Panama's FFI. Comparisons are performed on off-heap mmap'ed vector data. The implementation is currently only used during merging of scalar quantized segments, through a custom format ES814HnswScalarQuantizedVectorsFormat, but its usage will likely be expanded over time. Co-authored-by: Benjamin Trent <ben.w.trent@gmail.com> Co-authored-by: Lorenzo Dematté <lorenzo.dematte@elastic.co> Co-authored-by: Mark Vieira <portugee@gmail.com> Co-authored-by: Ryan Ernst <ryan@iernst.net>
This commit is contained in:
parent
fb1bc58664
commit
6b52d7837b
63 changed files with 4812 additions and 12 deletions
|
@ -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'
|
||||
}
|
||||
|
@ -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"
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -63,6 +63,7 @@ public class InternalDistributionModuleCheckTaskProvider {
|
|||
"org.elasticsearch.securesm",
|
||||
"org.elasticsearch.server",
|
||||
"org.elasticsearch.tdigest",
|
||||
"org.elasticsearch.vec",
|
||||
"org.elasticsearch.xcontent"
|
||||
);
|
||||
|
||||
|
|
110
dev-tools/publish_zstd_binaries.sh
Executable file
110
dev-tools/publish_zstd_binaries.sh
Executable file
|
@ -0,0 +1,110 @@
|
|||
#!/usr/bin/env bash
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
set -e
|
||||
|
||||
if [ "$#" -ne 1 ]; then
|
||||
printf 'Usage: %s <version>\n' "$(basename "$0")"
|
||||
exit 0;
|
||||
fi
|
||||
|
||||
if [ $(docker buildx inspect --bootstrap | grep -c 'Platforms:.*linux/arm64') -ne 1 ]; then
|
||||
echo 'Error: No Docker support for linux/arm64 detected'
|
||||
echo 'For more information see https://docs.docker.com/build/building/multi-platform'
|
||||
exit 1;
|
||||
fi
|
||||
|
||||
if [ -z "$ARTIFACTORY_API_KEY" ]; then
|
||||
echo 'Error: The ARTIFACTORY_API_KEY environment variable must be set.'
|
||||
exit 1;
|
||||
fi
|
||||
|
||||
VERSION="$1"
|
||||
ARTIFACTORY_REPOSITORY="${ARTIFACTORY_REPOSITORY:-https://artifactory.elastic.dev/artifactory/elasticsearch-native/}"
|
||||
TEMP=$(mktemp -d)
|
||||
|
||||
fetch_homebrew_artifact() {
|
||||
DIGEST=$(curl -sS --retry 3 -H "Accept: application/vnd.oci.image.index.v1+json" -H "Authorization: Bearer QQ==" \
|
||||
--location "https://ghcr.io/v2/homebrew/core/zstd/manifests/$VERSION" | jq -r \
|
||||
".manifests[] | select(.platform.os == \"darwin\" and .platform.architecture == \"$1\" and .platform.\"os.version\" == \"macOS 13\") | .annotations.\"sh.brew.bottle.digest\"")
|
||||
|
||||
OUTPUT_FILE="$TEMP/zstd-$VERSION-darwin-$1.tar.gz"
|
||||
curl -sS --retry 3 -H "Authorization: Bearer QQ==" --output "$OUTPUT_FILE" --location "https://ghcr.io/v2/homebrew/core/zstd/blobs/sha256:$DIGEST"
|
||||
echo $OUTPUT_FILE
|
||||
}
|
||||
|
||||
download_license() {
|
||||
curl -sS --retry 3 --location https://raw.githubusercontent.com/facebook/zstd/v${VERSION}/LICENSE --output $1
|
||||
}
|
||||
|
||||
echo 'Downloading MacOS zstd binaries...'
|
||||
DARWIN_ARM_BREW=$(fetch_homebrew_artifact 'arm64')
|
||||
DARWIN_X86_BREW=$(fetch_homebrew_artifact 'amd64')
|
||||
|
||||
build_darwin_jar() {
|
||||
ARTIFACT="$TEMP/zstd-$VERSION-darwin-$2.jar"
|
||||
TAR_DIR="$TEMP/darwin-$2"
|
||||
mkdir $TAR_DIR
|
||||
tar zxf $1 --strip-components=2 --include="*/LICENSE" --include="*/libzstd.$VERSION.dylib" -C $TAR_DIR && rm $1
|
||||
mv $TAR_DIR/lib/libzstd.$VERSION.dylib $TAR_DIR/libzstd.dylib && rm -rf $TAR_DIR/lib
|
||||
FILE_COUNT=$(ls -1 $TAR_DIR | wc -l | xargs)
|
||||
if [ "$FILE_COUNT" -ne 2 ]; then
|
||||
>&2 echo "ERROR: Expected 2 files in $TAR_DIR but found $FILE_COUNT"
|
||||
exit 1
|
||||
fi
|
||||
(cd $TAR_DIR/../ && zip -rq - $(basename $TAR_DIR)) > $ARTIFACT && rm -rf $TAR_DIR
|
||||
echo $ARTIFACT
|
||||
}
|
||||
|
||||
echo 'Building MacOS jars...'
|
||||
DARWIN_ARM_JAR=$(build_darwin_jar $DARWIN_ARM_BREW "aarch64")
|
||||
DARWIN_X86_JAR=$(build_darwin_jar $DARWIN_X86_BREW "x86-64")
|
||||
|
||||
build_linux_jar() {
|
||||
ARTIFACT="$TEMP/zstd-$VERSION-linux-$2.jar"
|
||||
OUTPUT_DIR="$TEMP/linux-$2"
|
||||
mkdir $OUTPUT_DIR
|
||||
DOCKER_IMAGE=$(docker build --build-arg="ZSTD_VERSION=1.5.5" --file zstd.Dockerfile --platform $1 --quiet .)
|
||||
docker run --platform $1 $DOCKER_IMAGE > $OUTPUT_DIR/libzstd.so
|
||||
download_license $OUTPUT_DIR/LICENSE
|
||||
(cd $OUTPUT_DIR/../ && zip -rq - $(basename $OUTPUT_DIR)) > $ARTIFACT && rm -rf $OUTPUT_DIR
|
||||
echo $ARTIFACT
|
||||
}
|
||||
|
||||
echo 'Building Linux jars...'
|
||||
LINUX_ARM_JAR=$(build_linux_jar "linux/amd64" "x86-64")
|
||||
LINUX_X86_JAR=$(build_linux_jar "linux/arm64" "aarch64")
|
||||
|
||||
build_windows_jar() {
|
||||
ARTIFACT="$TEMP/zstd-$VERSION-windows-x86-64.jar"
|
||||
OUTPUT_DIR="$TEMP/win32-x86-64"
|
||||
mkdir $OUTPUT_DIR
|
||||
curl -sS --retry 3 --location https://github.com/facebook/zstd/releases/download/v${VERSION}/zstd-v${VERSION}-win64.zip --output $OUTPUT_DIR/zstd.zip
|
||||
unzip -jq $OUTPUT_DIR/zstd.zip zstd-v${VERSION}-win64/dll/libzstd.dll -d $OUTPUT_DIR && rm $OUTPUT_DIR/zstd.zip
|
||||
mv $OUTPUT_DIR/libzstd.dll $OUTPUT_DIR/zstd.dll
|
||||
download_license $OUTPUT_DIR/LICENSE
|
||||
(cd $OUTPUT_DIR/../ && zip -rq - $(basename $OUTPUT_DIR)) > $ARTIFACT && rm -rf $OUTPUT_DIR
|
||||
echo $ARTIFACT
|
||||
}
|
||||
|
||||
echo 'Building Windows jar...'
|
||||
WINDOWS_X86_JAR=$(build_windows_jar)
|
||||
|
||||
upload_artifact() {
|
||||
curl -sS -X PUT -H "X-JFrog-Art-Api: ${ARTIFACTORY_API_KEY}" --data-binary "@$1" --location "${ARTIFACTORY_REPOSITORY}/org/elasticsearch/zstd/${VERSION}/$(basename $1)"
|
||||
}
|
||||
|
||||
echo 'Uploading artifacts...'
|
||||
upload_artifact ${DARWIN_ARM_JAR}
|
||||
upload_artifact ${DARWIN_X86_JAR}
|
||||
upload_artifact ${LINUX_ARM_JAR}
|
||||
upload_artifact ${LINUX_X86_JAR}
|
||||
upload_artifact ${WINDOWS_X86_JAR}
|
||||
|
||||
rm -rf $TEMP
|
11
dev-tools/zstd.Dockerfile
Normal file
11
dev-tools/zstd.Dockerfile
Normal file
|
@ -0,0 +1,11 @@
|
|||
FROM centos:7
|
||||
ARG ZSTD_VERSION
|
||||
|
||||
RUN yum install -y git gcc gcc-c++ make
|
||||
RUN git clone --depth 1 --branch v${ZSTD_VERSION} https://github.com/facebook/zstd.git
|
||||
WORKDIR zstd
|
||||
RUN make lib-release && strip --strip-unneeded lib/libzstd.so.${ZSTD_VERSION}
|
||||
|
||||
ENV ZSTD_VERSION=${ZSTD_VERSION}
|
||||
|
||||
CMD cat lib/libzstd.so.${ZSTD_VERSION}
|
|
@ -73,6 +73,7 @@ final class SystemJvmOptions {
|
|||
* explore alternatives. See org.elasticsearch.xpack.searchablesnapshots.preallocate.Preallocate.
|
||||
*/
|
||||
"--add-opens=java.base/java.io=org.elasticsearch.preallocate",
|
||||
"--add-opens=org.apache.lucene.core/org.apache.lucene.store=org.elasticsearch.vec",
|
||||
maybeEnableNativeAccess(),
|
||||
maybeOverrideDockerCgroup(distroType),
|
||||
maybeSetActiveProcessorCount(nodeSettings),
|
||||
|
|
5
docs/changelog/106133.yaml
Normal file
5
docs/changelog/106133.yaml
Normal file
|
@ -0,0 +1,5 @@
|
|||
pr: 106133
|
||||
summary: Add an optimised vector distance function for aarch64
|
||||
area: Search
|
||||
type: enhancement
|
||||
issues: []
|
|
@ -9,12 +9,15 @@
|
|||
package org.elasticsearch.nativeaccess.jna;
|
||||
|
||||
import org.elasticsearch.nativeaccess.lib.JavaLibrary;
|
||||
import org.elasticsearch.nativeaccess.lib.NativeLibrary;
|
||||
import org.elasticsearch.nativeaccess.lib.NativeLibraryProvider;
|
||||
import org.elasticsearch.nativeaccess.lib.PosixCLibrary;
|
||||
import org.elasticsearch.nativeaccess.lib.SystemdLibrary;
|
||||
import org.elasticsearch.nativeaccess.lib.VectorLibrary;
|
||||
import org.elasticsearch.nativeaccess.lib.ZstdLibrary;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class JnaNativeLibraryProvider extends NativeLibraryProvider {
|
||||
|
||||
|
@ -29,8 +32,14 @@ public class JnaNativeLibraryProvider extends NativeLibraryProvider {
|
|||
SystemdLibrary.class,
|
||||
JnaSystemdLibrary::new,
|
||||
ZstdLibrary.class,
|
||||
JnaZstdLibrary::new
|
||||
JnaZstdLibrary::new,
|
||||
VectorLibrary.class,
|
||||
notImplemented()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private static Supplier<NativeLibrary> notImplemented() {
|
||||
return () -> { throw new AssertionError(); };
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,12 +18,13 @@ configurations {
|
|||
}
|
||||
|
||||
var zstdVersion = "1.5.5"
|
||||
var vecVersion = "1.0.1"
|
||||
|
||||
repositories {
|
||||
exclusiveContent {
|
||||
forRepository {
|
||||
maven {
|
||||
url "https://artifactory.elastic.dev/artifactory/elasticsearch-zstd"
|
||||
url "https://artifactory.elastic.dev/artifactory/elasticsearch-native"
|
||||
metadataSources {
|
||||
artifact()
|
||||
}
|
||||
|
@ -31,6 +32,7 @@ repositories {
|
|||
}
|
||||
filter {
|
||||
includeModule("org.elasticsearch", "zstd")
|
||||
includeModule("org.elasticsearch", "vec")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -40,16 +42,21 @@ dependencies {
|
|||
transformSpec.getFrom().attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.JAR_TYPE);
|
||||
transformSpec.getTo().attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.DIRECTORY_TYPE);
|
||||
});
|
||||
registerTransform(UnzipTransform, transformSpec -> {
|
||||
transformSpec.getFrom().attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.ZIP_TYPE);
|
||||
transformSpec.getTo().attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.DIRECTORY_TYPE);
|
||||
});
|
||||
libs "org.elasticsearch:zstd:${zstdVersion}:darwin-aarch64"
|
||||
libs "org.elasticsearch:zstd:${zstdVersion}:darwin-x86-64"
|
||||
libs "org.elasticsearch:zstd:${zstdVersion}:linux-aarch64"
|
||||
libs "org.elasticsearch:zstd:${zstdVersion}:linux-x86-64"
|
||||
libs "org.elasticsearch:zstd:${zstdVersion}:windows-x86-64"
|
||||
libs "org.elasticsearch:vec:${vecVersion}@zip" // temporarily comment this out, if testing a locally built native lib
|
||||
}
|
||||
|
||||
def extractLibs = tasks.register('extractLibs', Copy) {
|
||||
from configurations.libs
|
||||
into layout.buildDirectory.dir('platform')
|
||||
from configurations.libs
|
||||
// TODO: fix architecture in uploaded libs
|
||||
filesMatching("*-x86-64/*") {
|
||||
it.path = it.path.replace("x86-64", "x64")
|
||||
|
@ -57,6 +64,7 @@ def extractLibs = tasks.register('extractLibs', Copy) {
|
|||
filesMatching("win32*/*") {
|
||||
it.path = it.path.replace("win32", "windows")
|
||||
}
|
||||
includeEmptyDirs = false
|
||||
filePermissions {
|
||||
unix("644")
|
||||
}
|
||||
|
|
|
@ -14,7 +14,12 @@ module org.elasticsearch.nativeaccess {
|
|||
requires org.elasticsearch.base;
|
||||
requires org.elasticsearch.logging;
|
||||
|
||||
exports org.elasticsearch.nativeaccess to org.elasticsearch.nativeaccess.jna, org.elasticsearch.server, org.elasticsearch.systemd;
|
||||
exports org.elasticsearch.nativeaccess
|
||||
to
|
||||
org.elasticsearch.nativeaccess.jna,
|
||||
org.elasticsearch.server,
|
||||
org.elasticsearch.systemd,
|
||||
org.elasticsearch.vec;
|
||||
// allows jna to implement a library provider, and ProviderLocator to load it
|
||||
exports org.elasticsearch.nativeaccess.lib to org.elasticsearch.nativeaccess.jna, org.elasticsearch.base;
|
||||
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
|
||||
package org.elasticsearch.nativeaccess;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Provides access to native functionality needed by Elastisearch.
|
||||
*/
|
||||
|
@ -35,6 +37,11 @@ public interface NativeAccess {
|
|||
*/
|
||||
Zstd getZstd();
|
||||
|
||||
/*
|
||||
* Returns the vector similarity functions, or an empty optional.
|
||||
*/
|
||||
Optional<VectorSimilarityFunctions> getVectorSimilarityFunctions();
|
||||
|
||||
/**
|
||||
* Creates a new {@link CloseableByteBuffer}. The buffer must be used within the same thread
|
||||
* that it is created.
|
||||
|
|
|
@ -11,6 +11,8 @@ package org.elasticsearch.nativeaccess;
|
|||
import org.elasticsearch.logging.LogManager;
|
||||
import org.elasticsearch.logging.Logger;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
class NoopNativeAccess implements NativeAccess {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(NativeAccess.class);
|
||||
|
@ -40,4 +42,10 @@ class NoopNativeAccess implements NativeAccess {
|
|||
logger.warn("cannot allocate buffer because native access is not available");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<VectorSimilarityFunctions> getVectorSimilarityFunctions() {
|
||||
logger.warn("cannot get vector distance because native access is not available");
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,18 +10,54 @@ package org.elasticsearch.nativeaccess;
|
|||
|
||||
import org.elasticsearch.nativeaccess.lib.NativeLibraryProvider;
|
||||
import org.elasticsearch.nativeaccess.lib.PosixCLibrary;
|
||||
import org.elasticsearch.nativeaccess.lib.VectorLibrary;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
abstract class PosixNativeAccess extends AbstractNativeAccess {
|
||||
|
||||
protected final PosixCLibrary libc;
|
||||
protected final VectorSimilarityFunctions vectorDistance;
|
||||
|
||||
PosixNativeAccess(String name, NativeLibraryProvider libraryProvider) {
|
||||
super(name, libraryProvider);
|
||||
this.libc = libraryProvider.getLibrary(PosixCLibrary.class);
|
||||
this.vectorDistance = vectorSimilarityFunctionsOrNull(libraryProvider);
|
||||
}
|
||||
|
||||
static VectorSimilarityFunctions vectorSimilarityFunctionsOrNull(NativeLibraryProvider libraryProvider) {
|
||||
if (isNativeVectorLibSupported()) {
|
||||
var lib = new VectorSimilarityFunctions(libraryProvider.getLibrary(VectorLibrary.class));
|
||||
logger.info("Using native vector library; to disable start with -D" + ENABLE_JDK_VECTOR_LIBRARY + "=false");
|
||||
return lib;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean definitelyRunningAsRoot() {
|
||||
return libc.geteuid() == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<VectorSimilarityFunctions> getVectorSimilarityFunctions() {
|
||||
return Optional.ofNullable(vectorDistance);
|
||||
}
|
||||
|
||||
static boolean isNativeVectorLibSupported() {
|
||||
return Runtime.version().feature() >= 21 && isMacOrLinuxAarch64() && checkEnableSystemProperty();
|
||||
}
|
||||
|
||||
/** Returns true iff the OS is Mac or Linux, and the architecture is aarch64. */
|
||||
static boolean isMacOrLinuxAarch64() {
|
||||
String name = System.getProperty("os.name");
|
||||
return (name.startsWith("Mac") || name.startsWith("Linux")) && System.getProperty("os.arch").equals("aarch64");
|
||||
}
|
||||
|
||||
/** -Dorg.elasticsearch.nativeaccess.enableVectorLibrary=false to disable.*/
|
||||
static final String ENABLE_JDK_VECTOR_LIBRARY = "org.elasticsearch.nativeaccess.enableVectorLibrary";
|
||||
|
||||
static boolean checkEnableSystemProperty() {
|
||||
return Optional.ofNullable(System.getProperty(ENABLE_JDK_VECTOR_LIBRARY)).map(Boolean::valueOf).orElse(Boolean.TRUE);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* 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.nativeaccess;
|
||||
|
||||
import org.elasticsearch.nativeaccess.lib.VectorLibrary;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
|
||||
/**
|
||||
* Utility class providing vector similarity functions.
|
||||
*
|
||||
* <p> MethodHandles are returned to avoid a static reference to MemorySegment,
|
||||
* which is not in the currently lowest compile version, JDK 17. Code consuming
|
||||
* the method handles will, by definition, require access to MemorySegment.
|
||||
*/
|
||||
public final class VectorSimilarityFunctions implements VectorLibrary {
|
||||
|
||||
private final VectorLibrary vectorLibrary;
|
||||
|
||||
VectorSimilarityFunctions(VectorLibrary vectorLibrary) {
|
||||
this.vectorLibrary = vectorLibrary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Produces a method handle returning the dot product of byte (signed int8) vectors.
|
||||
*
|
||||
* <p> The type of the method handle will have {@code int} as return type, The type of
|
||||
* its first and second arguments will be {@code MemorySegment}, whose contents is the
|
||||
* vector data bytes. The third argument is the length of the vector data.
|
||||
*/
|
||||
public MethodHandle dotProductHandle() {
|
||||
return vectorLibrary.dotProductHandle();
|
||||
}
|
||||
|
||||
/**
|
||||
* Produces a method handle returning the square distance of byte (signed int8) vectors.
|
||||
*
|
||||
* <p> The type of the method handle will have {@code int} as return type, The type of
|
||||
* its first and second arguments will be {@code MemorySegment}, whose contents is the
|
||||
* vector data bytes. The third argument is the length of the vector data.
|
||||
*/
|
||||
public MethodHandle squareDistanceHandle() {
|
||||
return vectorLibrary.squareDistanceHandle();
|
||||
}
|
||||
}
|
|
@ -10,6 +10,8 @@ package org.elasticsearch.nativeaccess;
|
|||
|
||||
import org.elasticsearch.nativeaccess.lib.NativeLibraryProvider;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
class WindowsNativeAccess extends AbstractNativeAccess {
|
||||
|
||||
WindowsNativeAccess(NativeLibraryProvider libraryProvider) {
|
||||
|
@ -20,4 +22,9 @@ class WindowsNativeAccess extends AbstractNativeAccess {
|
|||
public boolean definitelyRunningAsRoot() {
|
||||
return false; // don't know
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<VectorSimilarityFunctions> getVectorSimilarityFunctions() {
|
||||
return Optional.empty(); // not supported yet
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,4 +9,4 @@
|
|||
package org.elasticsearch.nativeaccess.lib;
|
||||
|
||||
/** A marker interface for libraries that can be loaded by {@link org.elasticsearch.nativeaccess.lib.NativeLibraryProvider} */
|
||||
public sealed interface NativeLibrary permits JavaLibrary, PosixCLibrary, SystemdLibrary, ZstdLibrary {}
|
||||
public sealed interface NativeLibrary permits JavaLibrary, PosixCLibrary, SystemdLibrary, VectorLibrary, ZstdLibrary {}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
* 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.nativeaccess.lib;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
|
||||
/**
|
||||
* A VectorLibrary is just an adaptation of the factory for a NativeLibrary.
|
||||
* It is needed so the NativeLibraryProvider can be the single point of construction
|
||||
* for native implementations.
|
||||
*/
|
||||
public non-sealed interface VectorLibrary extends NativeLibrary {
|
||||
|
||||
MethodHandle dotProductHandle();
|
||||
|
||||
MethodHandle squareDistanceHandle();
|
||||
}
|
|
@ -12,6 +12,7 @@ import org.elasticsearch.nativeaccess.lib.JavaLibrary;
|
|||
import org.elasticsearch.nativeaccess.lib.NativeLibraryProvider;
|
||||
import org.elasticsearch.nativeaccess.lib.PosixCLibrary;
|
||||
import org.elasticsearch.nativeaccess.lib.SystemdLibrary;
|
||||
import org.elasticsearch.nativeaccess.lib.VectorLibrary;
|
||||
import org.elasticsearch.nativeaccess.lib.ZstdLibrary;
|
||||
|
||||
import java.util.Map;
|
||||
|
@ -29,7 +30,9 @@ public class JdkNativeLibraryProvider extends NativeLibraryProvider {
|
|||
SystemdLibrary.class,
|
||||
JdkSystemdLibrary::new,
|
||||
ZstdLibrary.class,
|
||||
JdkZstdLibrary::new
|
||||
JdkZstdLibrary::new,
|
||||
VectorLibrary.class,
|
||||
JdkVectorLibrary::new
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,164 @@
|
|||
/*
|
||||
* 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.nativeaccess.jdk;
|
||||
|
||||
import org.elasticsearch.nativeaccess.lib.VectorLibrary;
|
||||
|
||||
import java.lang.foreign.FunctionDescriptor;
|
||||
import java.lang.foreign.MemorySegment;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
|
||||
import static java.lang.foreign.ValueLayout.ADDRESS;
|
||||
import static java.lang.foreign.ValueLayout.JAVA_BYTE;
|
||||
import static java.lang.foreign.ValueLayout.JAVA_INT;
|
||||
import static org.elasticsearch.nativeaccess.jdk.LinkerHelper.downcallHandle;
|
||||
|
||||
public final class JdkVectorLibrary implements VectorLibrary {
|
||||
|
||||
static {
|
||||
System.loadLibrary("vec");
|
||||
}
|
||||
|
||||
public JdkVectorLibrary() {}
|
||||
|
||||
static final MethodHandle dot8stride$mh = downcallHandle("dot8s_stride", FunctionDescriptor.of(JAVA_INT));
|
||||
static final MethodHandle sqr8stride$mh = downcallHandle("sqr8s_stride", FunctionDescriptor.of(JAVA_INT));
|
||||
|
||||
static final MethodHandle dot8s$mh = downcallHandle("dot8s", FunctionDescriptor.of(JAVA_INT, ADDRESS, ADDRESS, JAVA_INT));
|
||||
static final MethodHandle sqr8s$mh = downcallHandle("sqr8s", FunctionDescriptor.of(JAVA_INT, ADDRESS, ADDRESS, JAVA_INT));
|
||||
|
||||
// Stride of the native implementation - consumes this number of bytes per loop invocation.
|
||||
// There must be at least this number of bytes/elements available when going native
|
||||
static final int DOT_STRIDE = 32;
|
||||
static final int SQR_STRIDE = 16;
|
||||
|
||||
static {
|
||||
assert DOT_STRIDE > 0 && (DOT_STRIDE & (DOT_STRIDE - 1)) == 0 : "Not a power of two";
|
||||
assert dot8Stride() == DOT_STRIDE : dot8Stride() + " != " + DOT_STRIDE;
|
||||
assert SQR_STRIDE > 0 && (SQR_STRIDE & (SQR_STRIDE - 1)) == 0 : "Not a power of two";
|
||||
assert sqr8Stride() == SQR_STRIDE : sqr8Stride() + " != " + SQR_STRIDE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the dot product of given byte vectors.
|
||||
* @param a address of the first vector
|
||||
* @param b address of the second vector
|
||||
* @param length the vector dimensions
|
||||
*/
|
||||
static int dotProduct(MemorySegment a, MemorySegment b, int length) {
|
||||
assert length >= 0;
|
||||
if (a.byteSize() != b.byteSize()) {
|
||||
throw new IllegalArgumentException("dimensions differ: " + a.byteSize() + "!=" + b.byteSize());
|
||||
}
|
||||
if (length > a.byteSize()) {
|
||||
throw new IllegalArgumentException("length: " + length + ", greater than vector dimensions: " + a.byteSize());
|
||||
}
|
||||
int i = 0;
|
||||
int res = 0;
|
||||
if (length >= DOT_STRIDE) {
|
||||
i += length & ~(DOT_STRIDE - 1);
|
||||
res = dot8s(a, b, i);
|
||||
}
|
||||
|
||||
// tail
|
||||
for (; i < length; i++) {
|
||||
res += a.get(JAVA_BYTE, i) * b.get(JAVA_BYTE, i);
|
||||
}
|
||||
assert i == length;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the square distance of given byte vectors.
|
||||
* @param a address of the first vector
|
||||
* @param b address of the second vector
|
||||
* @param length the vector dimensions
|
||||
*/
|
||||
static int squareDistance(MemorySegment a, MemorySegment b, int length) {
|
||||
assert length >= 0;
|
||||
if (a.byteSize() != b.byteSize()) {
|
||||
throw new IllegalArgumentException("dimensions differ: " + a.byteSize() + "!=" + b.byteSize());
|
||||
}
|
||||
if (length > a.byteSize()) {
|
||||
throw new IllegalArgumentException("length: " + length + ", greater than vector dimensions: " + a.byteSize());
|
||||
}
|
||||
int i = 0;
|
||||
int res = 0;
|
||||
if (length >= SQR_STRIDE) {
|
||||
i += length & ~(SQR_STRIDE - 1);
|
||||
res = sqr8s(a, b, i);
|
||||
}
|
||||
|
||||
// tail
|
||||
for (; i < length; i++) {
|
||||
int dist = a.get(JAVA_BYTE, i) - b.get(JAVA_BYTE, i);
|
||||
res += dist * dist;
|
||||
}
|
||||
assert i == length;
|
||||
return res;
|
||||
}
|
||||
|
||||
private static int dot8Stride() {
|
||||
try {
|
||||
return (int) dot8stride$mh.invokeExact();
|
||||
} catch (Throwable t) {
|
||||
throw new AssertionError(t);
|
||||
}
|
||||
}
|
||||
|
||||
private static int sqr8Stride() {
|
||||
try {
|
||||
return (int) sqr8stride$mh.invokeExact();
|
||||
} catch (Throwable t) {
|
||||
throw new AssertionError(t);
|
||||
}
|
||||
}
|
||||
|
||||
private static int dot8s(MemorySegment a, MemorySegment b, int length) {
|
||||
try {
|
||||
return (int) dot8s$mh.invokeExact(a, b, length);
|
||||
} catch (Throwable t) {
|
||||
throw new AssertionError(t);
|
||||
}
|
||||
}
|
||||
|
||||
private static int sqr8s(MemorySegment a, MemorySegment b, int length) {
|
||||
try {
|
||||
return (int) sqr8s$mh.invokeExact(a, b, length);
|
||||
} catch (Throwable t) {
|
||||
throw new AssertionError(t);
|
||||
}
|
||||
}
|
||||
|
||||
static final MethodHandle DOT_HANDLE;
|
||||
static final MethodHandle SQR_HANDLE;
|
||||
|
||||
static {
|
||||
try {
|
||||
var lookup = MethodHandles.lookup();
|
||||
var mt = MethodType.methodType(int.class, MemorySegment.class, MemorySegment.class, int.class);
|
||||
DOT_HANDLE = lookup.findStatic(JdkVectorLibrary.class, "dotProduct", mt);
|
||||
SQR_HANDLE = lookup.findStatic(JdkVectorLibrary.class, "squareDistance", mt);
|
||||
} catch (NoSuchMethodException | IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MethodHandle dotProductHandle() {
|
||||
return DOT_HANDLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MethodHandle squareDistanceHandle() {
|
||||
return SQR_HANDLE;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* 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.nativeaccess;
|
||||
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.elasticsearch.test.hamcrest.OptionalMatchers.isPresent;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
public class VectorSimilarityFunctionsTests extends ESTestCase {
|
||||
|
||||
final Optional<VectorSimilarityFunctions> vectorSimilarityFunctions;
|
||||
|
||||
public VectorSimilarityFunctionsTests() {
|
||||
logger.info(platformMsg());
|
||||
vectorSimilarityFunctions = NativeAccess.instance().getVectorSimilarityFunctions();
|
||||
}
|
||||
|
||||
public void testSupported() {
|
||||
supported();
|
||||
}
|
||||
|
||||
protected VectorSimilarityFunctions getVectorDistance() {
|
||||
return vectorSimilarityFunctions.get();
|
||||
}
|
||||
|
||||
public boolean supported() {
|
||||
var jdkVersion = Runtime.version().feature();
|
||||
var arch = System.getProperty("os.arch");
|
||||
var osName = System.getProperty("os.name");
|
||||
|
||||
if (jdkVersion >= 21 && arch.equals("aarch64") && (osName.startsWith("Mac") || osName.equals("Linux"))) {
|
||||
assertThat(vectorSimilarityFunctions, isPresent());
|
||||
return true;
|
||||
} else {
|
||||
assertThat(vectorSimilarityFunctions, not(isPresent()));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static String notSupportedMsg() {
|
||||
return "Not supported on [" + platformMsg() + "]";
|
||||
}
|
||||
|
||||
public static String platformMsg() {
|
||||
var jdkVersion = Runtime.version().feature();
|
||||
var arch = System.getProperty("os.arch");
|
||||
var osName = System.getProperty("os.name");
|
||||
return "JDK=" + jdkVersion + ", os=" + osName + ", arch=" + arch;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* 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.nativeaccess;
|
||||
|
||||
import org.apache.lucene.tests.util.LuceneTestCase;
|
||||
import org.elasticsearch.core.SuppressForbidden;
|
||||
import org.elasticsearch.test.ESTestCase.WithoutSecurityManager;
|
||||
import org.elasticsearch.test.compiler.InMemoryJavaCompiler;
|
||||
import org.elasticsearch.test.jar.JarUtils;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.elasticsearch.nativeaccess.PosixNativeAccess.ENABLE_JDK_VECTOR_LIBRARY;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@WithoutSecurityManager
|
||||
public class VectorSystemPropertyTests extends LuceneTestCase {
|
||||
|
||||
static Path jarPath;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() throws Exception {
|
||||
var classBytes = InMemoryJavaCompiler.compile("p.Test", TEST_SOURCE);
|
||||
Map<String, byte[]> jarEntries = new HashMap<>();
|
||||
jarEntries.put("p/Test.class", classBytes);
|
||||
Path topLevelDir = createTempDir();
|
||||
jarPath = topLevelDir.resolve("test.jar");
|
||||
JarUtils.createJarWithEntries(jarPath, jarEntries);
|
||||
}
|
||||
|
||||
@SuppressForbidden(reason = "pathSeparator")
|
||||
public void testSystemPropertyDisabled() throws Exception {
|
||||
var process = new ProcessBuilder(
|
||||
getJavaExecutable(),
|
||||
"-D" + ENABLE_JDK_VECTOR_LIBRARY + "=false",
|
||||
"-Xms4m",
|
||||
"-cp",
|
||||
jarPath + File.pathSeparator + System.getProperty("java.class.path"),
|
||||
"-Djava.library.path=" + System.getProperty("java.library.path"),
|
||||
"p.Test"
|
||||
).start();
|
||||
String output = new String(process.getInputStream().readAllBytes(), UTF_8);
|
||||
String error = new String(process.getErrorStream().readAllBytes(), UTF_8);
|
||||
// System.out.println(output);
|
||||
// System.out.println(error);
|
||||
process.waitFor(30, TimeUnit.SECONDS);
|
||||
assertThat(output, containsString("getVectorSimilarityFunctions=[Optional.empty]"));
|
||||
assertThat(process.exitValue(), equalTo(0));
|
||||
}
|
||||
|
||||
static String getJavaExecutable() {
|
||||
return Path.of(System.getProperty("java.home")).toAbsolutePath().resolve("bin").resolve("java").toString();
|
||||
}
|
||||
|
||||
static final String TEST_SOURCE = """
|
||||
package p;
|
||||
import org.elasticsearch.nativeaccess.NativeAccess;
|
||||
import org.elasticsearch.common.logging.LogConfigurator;
|
||||
|
||||
public class Test {
|
||||
static {
|
||||
LogConfigurator.loadLog4jPlugins();
|
||||
LogConfigurator.configureESLogging(); // native access requires logging to be initialized
|
||||
}
|
||||
|
||||
public static void main(String... args) {
|
||||
var na = NativeAccess.instance().getVectorSimilarityFunctions();
|
||||
System.out.println("getVectorSimilarityFunctions=[" + NativeAccess.instance().getVectorSimilarityFunctions() + "]");
|
||||
}
|
||||
}
|
||||
""";
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
/*
|
||||
* 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.nativeaccess.jdk;
|
||||
|
||||
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
|
||||
|
||||
import org.elasticsearch.nativeaccess.VectorSimilarityFunctionsTests;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
import java.lang.foreign.Arena;
|
||||
import java.lang.foreign.MemorySegment;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
public class JDKVectorLibraryTests extends VectorSimilarityFunctionsTests {
|
||||
|
||||
static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
|
||||
|
||||
static final int[] VECTOR_DIMS = { 1, 4, 6, 8, 13, 16, 25, 31, 32, 33, 64, 100, 128, 207, 256, 300, 512, 702, 1023, 1024, 1025 };
|
||||
|
||||
final int size;
|
||||
|
||||
static Arena arena;
|
||||
|
||||
public JDKVectorLibraryTests(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
arena = Arena.ofConfined();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() {
|
||||
arena.close();
|
||||
}
|
||||
|
||||
@ParametersFactory
|
||||
public static Iterable<Object[]> parametersFactory() {
|
||||
return () -> IntStream.of(VECTOR_DIMS).boxed().map(i -> new Object[] { i }).iterator();
|
||||
}
|
||||
|
||||
public void testBinaryVectors() {
|
||||
assumeTrue(notSupportedMsg(), supported());
|
||||
final int dims = size;
|
||||
final int numVecs = randomIntBetween(2, 101);
|
||||
var values = new byte[numVecs][dims];
|
||||
var segment = arena.allocate((long) dims * numVecs);
|
||||
for (int i = 0; i < numVecs; i++) {
|
||||
random().nextBytes(values[i]);
|
||||
MemorySegment.copy(MemorySegment.ofArray(values[i]), 0L, segment, (long) i * dims, dims);
|
||||
}
|
||||
|
||||
final int loopTimes = 1000;
|
||||
for (int i = 0; i < loopTimes; i++) {
|
||||
int first = randomInt(numVecs - 1);
|
||||
int second = randomInt(numVecs - 1);
|
||||
// dot product
|
||||
int implDot = dotProduct(segment.asSlice((long) first * dims, dims), segment.asSlice((long) second * dims, dims), dims);
|
||||
int otherDot = dotProductScalar(values[first], values[second]);
|
||||
assertEquals(otherDot, implDot);
|
||||
|
||||
int squareDist = squareDistance(segment.asSlice((long) first * dims, dims), segment.asSlice((long) second * dims, dims), dims);
|
||||
int otherSq = squareDistanceScalar(values[first], values[second]);
|
||||
assertEquals(otherSq, squareDist);
|
||||
}
|
||||
}
|
||||
|
||||
public void testIllegalDims() {
|
||||
assumeTrue(notSupportedMsg(), supported());
|
||||
var segment = arena.allocate((long) size * 3);
|
||||
var e = expectThrows(IAE, () -> dotProduct(segment.asSlice(0L, size), segment.asSlice(size, size + 1), size));
|
||||
assertThat(e.getMessage(), containsString("dimensions differ"));
|
||||
|
||||
e = expectThrows(IAE, () -> dotProduct(segment.asSlice(0L, size), segment.asSlice(size, size), size + 1));
|
||||
assertThat(e.getMessage(), containsString("greater than vector dimensions"));
|
||||
}
|
||||
|
||||
int dotProduct(MemorySegment a, MemorySegment b, int length) {
|
||||
try {
|
||||
return (int) getVectorDistance().dotProductHandle().invokeExact(a, b, length);
|
||||
} catch (Throwable e) {
|
||||
if (e instanceof Error err) {
|
||||
throw err;
|
||||
} else if (e instanceof RuntimeException re) {
|
||||
throw re;
|
||||
} else {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int squareDistance(MemorySegment a, MemorySegment b, int length) {
|
||||
try {
|
||||
return (int) getVectorDistance().squareDistanceHandle().invokeExact(a, b, length);
|
||||
} catch (Throwable e) {
|
||||
if (e instanceof Error err) {
|
||||
throw err;
|
||||
} else if (e instanceof RuntimeException re) {
|
||||
throw re;
|
||||
} else {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Computes the dot product of the given vectors a and b. */
|
||||
static int dotProductScalar(byte[] a, byte[] b) {
|
||||
int res = 0;
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
res += a[i] * b[i];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/** Computes the square distance of the given vectors a and b. */
|
||||
static int squareDistanceScalar(byte[] a, byte[] b) {
|
||||
// Note: this will not overflow if dim < 2^18, since max(byte * byte) = 2^14.
|
||||
int squareSum = 0;
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
int diff = a[i] - b[i];
|
||||
squareSum += diff * diff;
|
||||
}
|
||||
return squareSum;
|
||||
}
|
||||
}
|
30
libs/vec/build.gradle
Normal file
30
libs/vec/build.gradle
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import org.elasticsearch.gradle.internal.precommit.CheckForbiddenApisTask
|
||||
|
||||
apply plugin: 'elasticsearch.publish'
|
||||
apply plugin: 'elasticsearch.build'
|
||||
apply plugin: 'elasticsearch.mrjar'
|
||||
|
||||
dependencies {
|
||||
implementation project(':libs:elasticsearch-native')
|
||||
implementation project(':libs:elasticsearch-logging')
|
||||
implementation "org.apache.lucene:lucene-core:${versions.lucene}"
|
||||
|
||||
testImplementation(project(":test:framework")) {
|
||||
exclude group: 'org.elasticsearch', module: 'elasticsearch-native'
|
||||
}
|
||||
}
|
||||
|
||||
tasks.withType(CheckForbiddenApisTask).configureEach {
|
||||
replaceSignatureFiles 'jdk-signatures'
|
||||
}
|
||||
|
||||
// hack for now, fix the jarhell check for MRJar
|
||||
tasks.named("jarHell").configure { enabled = false }
|
12
libs/vec/includes.txt
Normal file
12
libs/vec/includes.txt
Normal file
|
@ -0,0 +1,12 @@
|
|||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
### Extracted from: vec.h
|
||||
|
||||
--include-function dot8s # header: native/unix/vec.h
|
||||
--include-function stride # header: native/unix/vec.h
|
475
libs/vec/licenses/lucene-core-LICENSE.txt
Normal file
475
libs/vec/licenses/lucene-core-LICENSE.txt
Normal file
|
@ -0,0 +1,475 @@
|
|||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
|
||||
|
||||
Some code in core/src/java/org/apache/lucene/util/UnicodeUtil.java was
|
||||
derived from unicode conversion examples available at
|
||||
http://www.unicode.org/Public/PROGRAMS/CVTUTF. Here is the copyright
|
||||
from those sources:
|
||||
|
||||
/*
|
||||
* Copyright 2001-2004 Unicode, Inc.
|
||||
*
|
||||
* Disclaimer
|
||||
*
|
||||
* This source code is provided as is by Unicode, Inc. No claims are
|
||||
* made as to fitness for any particular purpose. No warranties of any
|
||||
* kind are expressed or implied. The recipient agrees to determine
|
||||
* applicability of information provided. If this file has been
|
||||
* purchased on magnetic or optical media from Unicode, Inc., the
|
||||
* sole remedy for any claim will be exchange of defective media
|
||||
* within 90 days of receipt.
|
||||
*
|
||||
* Limitations on Rights to Redistribute This Code
|
||||
*
|
||||
* Unicode, Inc. hereby grants the right to freely use the information
|
||||
* supplied in this file in the creation of products supporting the
|
||||
* Unicode Standard, and to make copies of this file in any form
|
||||
* for internal or external distribution as long as this notice
|
||||
* remains attached.
|
||||
*/
|
||||
|
||||
|
||||
Some code in core/src/java/org/apache/lucene/util/ArrayUtil.java was
|
||||
derived from Python 2.4.2 sources available at
|
||||
http://www.python.org. Full license is here:
|
||||
|
||||
http://www.python.org/download/releases/2.4.2/license/
|
||||
|
||||
Some code in core/src/java/org/apache/lucene/util/UnicodeUtil.java was
|
||||
derived from Python 3.1.2 sources available at
|
||||
http://www.python.org. Full license is here:
|
||||
|
||||
http://www.python.org/download/releases/3.1.2/license/
|
||||
|
||||
Some code in core/src/java/org/apache/lucene/util/automaton was
|
||||
derived from Brics automaton sources available at
|
||||
www.brics.dk/automaton/. Here is the copyright from those sources:
|
||||
|
||||
/*
|
||||
* Copyright (c) 2001-2009 Anders Moeller
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
The levenshtein automata tables in core/src/java/org/apache/lucene/util/automaton
|
||||
were automatically generated with the moman/finenight FSA package.
|
||||
Here is the copyright for those sources:
|
||||
|
||||
# Copyright (c) 2010, Jean-Philippe Barrette-LaPierre, <jpb@rrette.com>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person
|
||||
# obtaining a copy of this software and associated documentation
|
||||
# files (the "Software"), to deal in the Software without
|
||||
# restriction, including without limitation the rights to use,
|
||||
# copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the
|
||||
# Software is furnished to do so, subject to the following
|
||||
# conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be
|
||||
# included in all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
# OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Some code in core/src/java/org/apache/lucene/util/UnicodeUtil.java was
|
||||
derived from ICU (http://www.icu-project.org)
|
||||
The full license is available here:
|
||||
http://source.icu-project.org/repos/icu/icu/trunk/license.html
|
||||
|
||||
/*
|
||||
* Copyright (C) 1999-2010, International Business Machines
|
||||
* Corporation and others. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* provided that the above copyright notice(s) and this permission notice appear
|
||||
* in all copies of the Software and that both the above copyright notice(s) and
|
||||
* this permission notice appear in supporting documentation.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE
|
||||
* LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR
|
||||
* ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
||||
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* Except as contained in this notice, the name of a copyright holder shall not
|
||||
* be used in advertising or otherwise to promote the sale, use or other
|
||||
* dealings in this Software without prior written authorization of the
|
||||
* copyright holder.
|
||||
*/
|
||||
|
||||
The following license applies to the Snowball stemmers:
|
||||
|
||||
Copyright (c) 2001, Dr Martin Porter
|
||||
Copyright (c) 2002, Richard Boulton
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the copyright holders nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The following license applies to the KStemmer:
|
||||
|
||||
Copyright © 2003,
|
||||
Center for Intelligent Information Retrieval,
|
||||
University of Massachusetts, Amherst.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
3. The names "Center for Intelligent Information Retrieval" and
|
||||
"University of Massachusetts" must not be used to endorse or promote products
|
||||
derived from this software without prior written permission. To obtain
|
||||
permission, contact info@ciir.cs.umass.edu.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF MASSACHUSETTS AND OTHER CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGE.
|
||||
|
||||
The following license applies to the Morfologik project:
|
||||
|
||||
Copyright (c) 2006 Dawid Weiss
|
||||
Copyright (c) 2007-2011 Dawid Weiss, Marcin Miłkowski
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of Morfologik nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
---
|
||||
|
||||
The dictionary comes from Morfologik project. Morfologik uses data from
|
||||
Polish ispell/myspell dictionary hosted at http://www.sjp.pl/slownik/en/ and
|
||||
is licenced on the terms of (inter alia) LGPL and Creative Commons
|
||||
ShareAlike. The part-of-speech tags were added in Morfologik project and
|
||||
are not found in the data from sjp.pl. The tagset is similar to IPI PAN
|
||||
tagset.
|
||||
|
||||
---
|
||||
|
||||
The following license applies to the Morfeusz project,
|
||||
used by org.apache.lucene.analysis.morfologik.
|
||||
|
||||
BSD-licensed dictionary of Polish (SGJP)
|
||||
http://sgjp.pl/morfeusz/
|
||||
|
||||
Copyright © 2011 Zygmunt Saloni, Włodzimierz Gruszczyński,
|
||||
Marcin Woliński, Robert Wołosz
|
||||
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS “AS IS” AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
||||
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
192
libs/vec/licenses/lucene-core-NOTICE.txt
Normal file
192
libs/vec/licenses/lucene-core-NOTICE.txt
Normal file
|
@ -0,0 +1,192 @@
|
|||
Apache Lucene
|
||||
Copyright 2014 The Apache Software Foundation
|
||||
|
||||
This product includes software developed at
|
||||
The Apache Software Foundation (http://www.apache.org/).
|
||||
|
||||
Includes software from other Apache Software Foundation projects,
|
||||
including, but not limited to:
|
||||
- Apache Ant
|
||||
- Apache Jakarta Regexp
|
||||
- Apache Commons
|
||||
- Apache Xerces
|
||||
|
||||
ICU4J, (under analysis/icu) is licensed under an MIT styles license
|
||||
and Copyright (c) 1995-2008 International Business Machines Corporation and others
|
||||
|
||||
Some data files (under analysis/icu/src/data) are derived from Unicode data such
|
||||
as the Unicode Character Database. See http://unicode.org/copyright.html for more
|
||||
details.
|
||||
|
||||
Brics Automaton (under core/src/java/org/apache/lucene/util/automaton) is
|
||||
BSD-licensed, created by Anders Møller. See http://www.brics.dk/automaton/
|
||||
|
||||
The levenshtein automata tables (under core/src/java/org/apache/lucene/util/automaton) were
|
||||
automatically generated with the moman/finenight FSA library, created by
|
||||
Jean-Philippe Barrette-LaPierre. This library is available under an MIT license,
|
||||
see http://sites.google.com/site/rrettesite/moman and
|
||||
http://bitbucket.org/jpbarrette/moman/overview/
|
||||
|
||||
The class org.apache.lucene.util.WeakIdentityMap was derived from
|
||||
the Apache CXF project and is Apache License 2.0.
|
||||
|
||||
The Google Code Prettify is Apache License 2.0.
|
||||
See http://code.google.com/p/google-code-prettify/
|
||||
|
||||
JUnit (junit-4.10) is licensed under the Common Public License v. 1.0
|
||||
See http://junit.sourceforge.net/cpl-v10.html
|
||||
|
||||
This product includes code (JaspellTernarySearchTrie) from Java Spelling Checkin
|
||||
g Package (jaspell): http://jaspell.sourceforge.net/
|
||||
License: The BSD License (http://www.opensource.org/licenses/bsd-license.php)
|
||||
|
||||
The snowball stemmers in
|
||||
analysis/common/src/java/net/sf/snowball
|
||||
were developed by Martin Porter and Richard Boulton.
|
||||
The snowball stopword lists in
|
||||
analysis/common/src/resources/org/apache/lucene/analysis/snowball
|
||||
were developed by Martin Porter and Richard Boulton.
|
||||
The full snowball package is available from
|
||||
http://snowball.tartarus.org/
|
||||
|
||||
The KStem stemmer in
|
||||
analysis/common/src/org/apache/lucene/analysis/en
|
||||
was developed by Bob Krovetz and Sergio Guzman-Lara (CIIR-UMass Amherst)
|
||||
under the BSD-license.
|
||||
|
||||
The Arabic,Persian,Romanian,Bulgarian, Hindi and Bengali analyzers (common) come with a default
|
||||
stopword list that is BSD-licensed created by Jacques Savoy. These files reside in:
|
||||
analysis/common/src/resources/org/apache/lucene/analysis/ar/stopwords.txt,
|
||||
analysis/common/src/resources/org/apache/lucene/analysis/fa/stopwords.txt,
|
||||
analysis/common/src/resources/org/apache/lucene/analysis/ro/stopwords.txt,
|
||||
analysis/common/src/resources/org/apache/lucene/analysis/bg/stopwords.txt,
|
||||
analysis/common/src/resources/org/apache/lucene/analysis/hi/stopwords.txt,
|
||||
analysis/common/src/resources/org/apache/lucene/analysis/bn/stopwords.txt
|
||||
See http://members.unine.ch/jacques.savoy/clef/index.html.
|
||||
|
||||
The German,Spanish,Finnish,French,Hungarian,Italian,Portuguese,Russian and Swedish light stemmers
|
||||
(common) are based on BSD-licensed reference implementations created by Jacques Savoy and
|
||||
Ljiljana Dolamic. These files reside in:
|
||||
analysis/common/src/java/org/apache/lucene/analysis/de/GermanLightStemmer.java
|
||||
analysis/common/src/java/org/apache/lucene/analysis/de/GermanMinimalStemmer.java
|
||||
analysis/common/src/java/org/apache/lucene/analysis/es/SpanishLightStemmer.java
|
||||
analysis/common/src/java/org/apache/lucene/analysis/fi/FinnishLightStemmer.java
|
||||
analysis/common/src/java/org/apache/lucene/analysis/fr/FrenchLightStemmer.java
|
||||
analysis/common/src/java/org/apache/lucene/analysis/fr/FrenchMinimalStemmer.java
|
||||
analysis/common/src/java/org/apache/lucene/analysis/hu/HungarianLightStemmer.java
|
||||
analysis/common/src/java/org/apache/lucene/analysis/it/ItalianLightStemmer.java
|
||||
analysis/common/src/java/org/apache/lucene/analysis/pt/PortugueseLightStemmer.java
|
||||
analysis/common/src/java/org/apache/lucene/analysis/ru/RussianLightStemmer.java
|
||||
analysis/common/src/java/org/apache/lucene/analysis/sv/SwedishLightStemmer.java
|
||||
|
||||
The Stempel analyzer (stempel) includes BSD-licensed software developed
|
||||
by the Egothor project http://egothor.sf.net/, created by Leo Galambos, Martin Kvapil,
|
||||
and Edmond Nolan.
|
||||
|
||||
The Polish analyzer (stempel) comes with a default
|
||||
stopword list that is BSD-licensed created by the Carrot2 project. The file resides
|
||||
in stempel/src/resources/org/apache/lucene/analysis/pl/stopwords.txt.
|
||||
See http://project.carrot2.org/license.html.
|
||||
|
||||
The SmartChineseAnalyzer source code (smartcn) was
|
||||
provided by Xiaoping Gao and copyright 2009 by www.imdict.net.
|
||||
|
||||
WordBreakTestUnicode_*.java (under modules/analysis/common/src/test/)
|
||||
is derived from Unicode data such as the Unicode Character Database.
|
||||
See http://unicode.org/copyright.html for more details.
|
||||
|
||||
The Morfologik analyzer (morfologik) includes BSD-licensed software
|
||||
developed by Dawid Weiss and Marcin Miłkowski (http://morfologik.blogspot.com/).
|
||||
|
||||
Morfologik uses data from Polish ispell/myspell dictionary
|
||||
(http://www.sjp.pl/slownik/en/) licenced on the terms of (inter alia)
|
||||
LGPL and Creative Commons ShareAlike.
|
||||
|
||||
Morfologic includes data from BSD-licensed dictionary of Polish (SGJP)
|
||||
(http://sgjp.pl/morfeusz/)
|
||||
|
||||
Servlet-api.jar and javax.servlet-*.jar are under the CDDL license, the original
|
||||
source code for this can be found at http://www.eclipse.org/jetty/downloads.php
|
||||
|
||||
===========================================================================
|
||||
Kuromoji Japanese Morphological Analyzer - Apache Lucene Integration
|
||||
===========================================================================
|
||||
|
||||
This software includes a binary and/or source version of data from
|
||||
|
||||
mecab-ipadic-2.7.0-20070801
|
||||
|
||||
which can be obtained from
|
||||
|
||||
http://atilika.com/releases/mecab-ipadic/mecab-ipadic-2.7.0-20070801.tar.gz
|
||||
|
||||
or
|
||||
|
||||
http://jaist.dl.sourceforge.net/project/mecab/mecab-ipadic/2.7.0-20070801/mecab-ipadic-2.7.0-20070801.tar.gz
|
||||
|
||||
===========================================================================
|
||||
mecab-ipadic-2.7.0-20070801 Notice
|
||||
===========================================================================
|
||||
|
||||
Nara Institute of Science and Technology (NAIST),
|
||||
the copyright holders, disclaims all warranties with regard to this
|
||||
software, including all implied warranties of merchantability and
|
||||
fitness, in no event shall NAIST be liable for
|
||||
any special, indirect or consequential damages or any damages
|
||||
whatsoever resulting from loss of use, data or profits, whether in an
|
||||
action of contract, negligence or other tortuous action, arising out
|
||||
of or in connection with the use or performance of this software.
|
||||
|
||||
A large portion of the dictionary entries
|
||||
originate from ICOT Free Software. The following conditions for ICOT
|
||||
Free Software applies to the current dictionary as well.
|
||||
|
||||
Each User may also freely distribute the Program, whether in its
|
||||
original form or modified, to any third party or parties, PROVIDED
|
||||
that the provisions of Section 3 ("NO WARRANTY") will ALWAYS appear
|
||||
on, or be attached to, the Program, which is distributed substantially
|
||||
in the same form as set out herein and that such intended
|
||||
distribution, if actually made, will neither violate or otherwise
|
||||
contravene any of the laws and regulations of the countries having
|
||||
jurisdiction over the User or the intended distribution itself.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
The program was produced on an experimental basis in the course of the
|
||||
research and development conducted during the project and is provided
|
||||
to users as so produced on an experimental basis. Accordingly, the
|
||||
program is provided without any warranty whatsoever, whether express,
|
||||
implied, statutory or otherwise. The term "warranty" used herein
|
||||
includes, but is not limited to, any warranty of the quality,
|
||||
performance, merchantability and fitness for a particular purpose of
|
||||
the program and the nonexistence of any infringement or violation of
|
||||
any right of any third party.
|
||||
|
||||
Each user of the program will agree and understand, and be deemed to
|
||||
have agreed and understood, that there is no warranty whatsoever for
|
||||
the program and, accordingly, the entire risk arising from or
|
||||
otherwise connected with the program is assumed by the user.
|
||||
|
||||
Therefore, neither ICOT, the copyright holder, or any other
|
||||
organization that participated in or was otherwise related to the
|
||||
development of the program and their respective officials, directors,
|
||||
officers and other employees shall be held liable for any and all
|
||||
damages, including, without limitation, general, special, incidental
|
||||
and consequential damages, arising out of or otherwise in connection
|
||||
with the use or inability to use the program or any product, material
|
||||
or result produced or otherwise obtained by using the program,
|
||||
regardless of whether they have been advised of, or otherwise had
|
||||
knowledge of, the possibility of such damages at any time during the
|
||||
project or thereafter. Each user will be deemed to have agreed to the
|
||||
foregoing by his or her commencement of use of the program. The term
|
||||
"use" as used herein includes, but is not limited to, the use,
|
||||
modification, copying and distribution of the program and the
|
||||
production of secondary products from the program.
|
||||
|
||||
In the case where the program, whether in its original form or
|
||||
modified, was distributed or delivered to or received by a user from
|
||||
any person, organization or entity other than ICOT, unless it makes or
|
||||
grants independently of ICOT any specific warranty to the user in
|
||||
writing, such person, organization or entity, will also be exempted
|
||||
from and not be held liable to the user for any such damages as noted
|
||||
above as far as the program is concerned.
|
9
libs/vec/native/Dockerfile
Normal file
9
libs/vec/native/Dockerfile
Normal file
|
@ -0,0 +1,9 @@
|
|||
FROM debian:latest
|
||||
|
||||
RUN apt update
|
||||
RUN apt install -y gcc g++ openjdk-17-jdk
|
||||
COPY . /workspace
|
||||
WORKDIR /workspace
|
||||
RUN ./gradlew --quiet --console=plain clean vecSharedLibrary
|
||||
|
||||
CMD cat build/libs/vec/shared/libvec.so
|
49
libs/vec/native/build.gradle
Normal file
49
libs/vec/native/build.gradle
Normal file
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
apply plugin: 'c'
|
||||
|
||||
var os = org.gradle.internal.os.OperatingSystem.current()
|
||||
|
||||
// To update this library run publish_vec_binaries.sh
|
||||
// Or
|
||||
// For local development, build the docker image with:
|
||||
// docker build --platform linux/arm64 --progress=plain .
|
||||
// Grab the image id from the console output, then, e.g.
|
||||
// docker run 9c9f36564c148b275aeecc42749e7b4580ded79dcf51ff6ccc008c8861e7a979 > build/libs/vec/shared/libvec.so
|
||||
//
|
||||
// Look at the disassemble:
|
||||
// objdump --disassemble-symbols=_dot8s build/libs/vec/shared/libvec.dylib
|
||||
// Note: symbol decoration may differ on Linux, i.e. the leading underscore is not present
|
||||
//
|
||||
// gcc -shared -fpic -o libvec.so -I src/vec/headers/ src/vec/c/vec.c -O3
|
||||
|
||||
group = 'org.elasticsearch'
|
||||
|
||||
model {
|
||||
toolChains {
|
||||
gcc(Gcc) {
|
||||
target("aarch64") {
|
||||
cCompiler.executable = "/usr/bin/gcc"
|
||||
}
|
||||
}
|
||||
clang(Clang)
|
||||
}
|
||||
platforms {
|
||||
aarch64 {
|
||||
architecture "aarch64"
|
||||
}
|
||||
}
|
||||
components {
|
||||
vec(NativeLibrarySpec) {
|
||||
targetPlatform "aarch64"
|
||||
binaries.withType(SharedLibraryBinarySpec) {
|
||||
cCompiler.args "-O3", "-std=c99", "-march=armv8-a"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
BIN
libs/vec/native/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
libs/vec/native/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
7
libs/vec/native/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
7
libs/vec/native/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
|
||||
networkTimeout=10000
|
||||
validateDistributionUrl=true
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
249
libs/vec/native/gradlew
vendored
Executable file
249
libs/vec/native/gradlew
vendored
Executable file
|
@ -0,0 +1,249 @@
|
|||
#!/bin/sh
|
||||
|
||||
#
|
||||
# Copyright © 2015-2021 the original authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
#
|
||||
# Gradle start up script for POSIX generated by Gradle.
|
||||
#
|
||||
# Important for running:
|
||||
#
|
||||
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
|
||||
# noncompliant, but you have some other compliant shell such as ksh or
|
||||
# bash, then to run this script, type that shell name before the whole
|
||||
# command line, like:
|
||||
#
|
||||
# ksh Gradle
|
||||
#
|
||||
# Busybox and similar reduced shells will NOT work, because this script
|
||||
# requires all of these POSIX shell features:
|
||||
# * functions;
|
||||
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
|
||||
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
|
||||
# * compound commands having a testable exit status, especially «case»;
|
||||
# * various built-in commands including «command», «set», and «ulimit».
|
||||
#
|
||||
# Important for patching:
|
||||
#
|
||||
# (2) This script targets any POSIX shell, so it avoids extensions provided
|
||||
# by Bash, Ksh, etc; in particular arrays are avoided.
|
||||
#
|
||||
# The "traditional" practice of packing multiple parameters into a
|
||||
# space-separated string is a well documented source of bugs and security
|
||||
# problems, so this is (mostly) avoided, by progressively accumulating
|
||||
# options in "$@", and eventually passing that to Java.
|
||||
#
|
||||
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
|
||||
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
|
||||
# see the in-line comments for details.
|
||||
#
|
||||
# There are tweaks for specific operating systems such as AIX, CygWin,
|
||||
# Darwin, MinGW, and NonStop.
|
||||
#
|
||||
# (3) This script is generated from the Groovy template
|
||||
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||
# within the Gradle project.
|
||||
#
|
||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
|
||||
# Resolve links: $0 may be a link
|
||||
app_path=$0
|
||||
|
||||
# Need this for daisy-chained symlinks.
|
||||
while
|
||||
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
|
||||
[ -h "$app_path" ]
|
||||
do
|
||||
ls=$( ls -ld "$app_path" )
|
||||
link=${ls#*' -> '}
|
||||
case $link in #(
|
||||
/*) app_path=$link ;; #(
|
||||
*) app_path=$APP_HOME$link ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# This is normally unused
|
||||
# shellcheck disable=SC2034
|
||||
APP_BASE_NAME=${0##*/}
|
||||
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD=maximum
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
} >&2
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
} >&2
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "$( uname )" in #(
|
||||
CYGWIN* ) cygwin=true ;; #(
|
||||
Darwin* ) darwin=true ;; #(
|
||||
MSYS* | MINGW* ) msys=true ;; #(
|
||||
NONSTOP* ) nonstop=true ;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD=$JAVA_HOME/jre/sh/java
|
||||
else
|
||||
JAVACMD=$JAVA_HOME/bin/java
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD=java
|
||||
if ! command -v java >/dev/null 2>&1
|
||||
then
|
||||
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||
case $MAX_FD in #(
|
||||
max*)
|
||||
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
MAX_FD=$( ulimit -H -n ) ||
|
||||
warn "Could not query maximum file descriptor limit"
|
||||
esac
|
||||
case $MAX_FD in #(
|
||||
'' | soft) :;; #(
|
||||
*)
|
||||
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
ulimit -n "$MAX_FD" ||
|
||||
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||
esac
|
||||
fi
|
||||
|
||||
# Collect all arguments for the java command, stacking in reverse order:
|
||||
# * args from the command line
|
||||
# * the main class name
|
||||
# * -classpath
|
||||
# * -D...appname settings
|
||||
# * --module-path (only if needed)
|
||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
|
||||
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if "$cygwin" || "$msys" ; then
|
||||
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
|
||||
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
|
||||
|
||||
JAVACMD=$( cygpath --unix "$JAVACMD" )
|
||||
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
for arg do
|
||||
if
|
||||
case $arg in #(
|
||||
-*) false ;; # don't mess with options #(
|
||||
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
|
||||
[ -e "$t" ] ;; #(
|
||||
*) false ;;
|
||||
esac
|
||||
then
|
||||
arg=$( cygpath --path --ignore --mixed "$arg" )
|
||||
fi
|
||||
# Roll the args list around exactly as many times as the number of
|
||||
# args, so each arg winds up back in the position where it started, but
|
||||
# possibly modified.
|
||||
#
|
||||
# NB: a `for` loop captures its iteration list before it begins, so
|
||||
# changing the positional parameters here affects neither the number of
|
||||
# iterations, nor the values presented in `arg`.
|
||||
shift # remove old arg
|
||||
set -- "$@" "$arg" # push replacement arg
|
||||
done
|
||||
fi
|
||||
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Collect all arguments for the java command:
|
||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||
# and any embedded shellness will be escaped.
|
||||
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||
# treated as '${Hostname}' itself on the command line.
|
||||
|
||||
set -- \
|
||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||
-classpath "$CLASSPATH" \
|
||||
org.gradle.wrapper.GradleWrapperMain \
|
||||
"$@"
|
||||
|
||||
# Stop when "xargs" is not available.
|
||||
if ! command -v xargs >/dev/null 2>&1
|
||||
then
|
||||
die "xargs is not available"
|
||||
fi
|
||||
|
||||
# Use "xargs" to parse quoted args.
|
||||
#
|
||||
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
||||
#
|
||||
# In Bash we could simply go:
|
||||
#
|
||||
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
|
||||
# set -- "${ARGS[@]}" "$@"
|
||||
#
|
||||
# but POSIX shell has neither arrays nor command substitution, so instead we
|
||||
# post-process each arg (as a line of input to sed) to backslash-escape any
|
||||
# character that might be a shell metacharacter, then use eval to reverse
|
||||
# that process (while maintaining the separation between arguments), and wrap
|
||||
# the whole thing up as a single "set" statement.
|
||||
#
|
||||
# This will of course break if any of these variables contains a newline or
|
||||
# an unmatched quote.
|
||||
#
|
||||
|
||||
eval "set -- $(
|
||||
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
|
||||
xargs -n1 |
|
||||
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
|
||||
tr '\n' ' '
|
||||
)" '"$@"'
|
||||
|
||||
exec "$JAVACMD" "$@"
|
92
libs/vec/native/gradlew.bat
vendored
Normal file
92
libs/vec/native/gradlew.bat
vendored
Normal file
|
@ -0,0 +1,92 @@
|
|||
@rem
|
||||
@rem Copyright 2015 the original author or authors.
|
||||
@rem
|
||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@rem you may not use this file except in compliance with the License.
|
||||
@rem You may obtain a copy of the License at
|
||||
@rem
|
||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||
@rem
|
||||
@rem Unless required by applicable law or agreed to in writing, software
|
||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%"=="" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%"=="" set DIRNAME=.
|
||||
@rem This is normally unused
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if %ERRORLEVEL% equ 0 goto execute
|
||||
|
||||
echo. 1>&2
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
|
||||
echo. 1>&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||
echo location of your Java installation. 1>&2
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto execute
|
||||
|
||||
echo. 1>&2
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
|
||||
echo. 1>&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||
echo location of your Java installation. 1>&2
|
||||
|
||||
goto fail
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if %ERRORLEVEL% equ 0 goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
set EXIT_CODE=%ERRORLEVEL%
|
||||
if %EXIT_CODE% equ 0 set EXIT_CODE=1
|
||||
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
|
||||
exit /b %EXIT_CODE%
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
46
libs/vec/native/publish_vec_binaries.sh
Executable file
46
libs/vec/native/publish_vec_binaries.sh
Executable file
|
@ -0,0 +1,46 @@
|
|||
#!/usr/bin/env bash
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
set -e
|
||||
|
||||
if [ "$(uname -sm)" != "Darwin arm64" ]; then
|
||||
echo 'This script must be run on an aarch64 MacOS system.'
|
||||
exit 1;
|
||||
fi
|
||||
|
||||
if [ -z "$ARTIFACTORY_API_KEY" ]; then
|
||||
echo 'Error: The ARTIFACTORY_API_KEY environment variable must be set.'
|
||||
exit 1;
|
||||
fi
|
||||
|
||||
VERSION="1.0.1"
|
||||
ARTIFACTORY_REPOSITORY="${ARTIFACTORY_REPOSITORY:-https://artifactory.elastic.dev/artifactory/elasticsearch-native/}"
|
||||
TEMP=$(mktemp -d)
|
||||
|
||||
if curl -sS -I --fail --location "${ARTIFACTORY_REPOSITORY}/org/elasticsearch/vec/${VERSION}/vec-${VERSION}.zip" > /dev/null 2>&1; then
|
||||
echo "Error: Artifacts already exist for version '${VERSION}'. Bump version before republishing."
|
||||
exit 1;
|
||||
fi
|
||||
|
||||
echo 'Building Darwin binary...'
|
||||
./gradlew --quiet --console=plain vecSharedLibrary
|
||||
|
||||
echo 'Building Linux binary...'
|
||||
DOCKER_IMAGE=$(docker build --platform linux/arm64 --quiet .)
|
||||
docker run $DOCKER_IMAGE > build/libs/vec/shared/libvec.so
|
||||
|
||||
mkdir -p $TEMP/darwin-aarch64
|
||||
mkdir -p $TEMP/linux-aarch64
|
||||
cp build/libs/vec/shared/libvec.dylib $TEMP/darwin-aarch64/
|
||||
cp build/libs/vec/shared/libvec.so $TEMP/linux-aarch64/
|
||||
|
||||
echo 'Uploading to Artifactory...'
|
||||
(cd $TEMP && zip -rq - .) | curl -sS -X PUT -H "X-JFrog-Art-Api: ${ARTIFACTORY_API_KEY}" --data-binary @- --location "${ARTIFACTORY_REPOSITORY}/org/elasticsearch/vec/${VERSION}/vec-${VERSION}.zip"
|
||||
|
||||
rm -rf $TEMP
|
9
libs/vec/native/settings.gradle
Normal file
9
libs/vec/native/settings.gradle
Normal file
|
@ -0,0 +1,9 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
rootProject.name = 'vec'
|
86
libs/vec/native/src/vec/c/vec.c
Normal file
86
libs/vec/native/src/vec/c/vec.c
Normal file
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <arm_neon.h>
|
||||
#include "vec.h"
|
||||
|
||||
#ifndef DOT8_STRIDE_BYTES_LEN
|
||||
#define DOT8_STRIDE_BYTES_LEN 32
|
||||
#endif
|
||||
|
||||
#ifndef SQR8S_STRIDE_BYTES_LEN
|
||||
#define SQR8S_STRIDE_BYTES_LEN 16
|
||||
#endif
|
||||
|
||||
EXPORT int dot8s_stride() {
|
||||
return DOT8_STRIDE_BYTES_LEN;
|
||||
}
|
||||
|
||||
EXPORT int sqr8s_stride() {
|
||||
return SQR8S_STRIDE_BYTES_LEN;
|
||||
}
|
||||
|
||||
EXPORT int32_t dot8s(int8_t* a, int8_t* b, size_t dims) {
|
||||
// We have contention in the instruction pipeline on the accumulation
|
||||
// registers if we use too few.
|
||||
int32x4_t acc1 = vdupq_n_s32(0);
|
||||
int32x4_t acc2 = vdupq_n_s32(0);
|
||||
int32x4_t acc3 = vdupq_n_s32(0);
|
||||
int32x4_t acc4 = vdupq_n_s32(0);
|
||||
|
||||
// Some unrolling gives around 50% performance improvement.
|
||||
for (int i = 0; i < dims; i += DOT8_STRIDE_BYTES_LEN) {
|
||||
// Read into 16 x 8 bit vectors.
|
||||
int8x16_t va1 = vld1q_s8(a + i);
|
||||
int8x16_t vb1 = vld1q_s8(b + i);
|
||||
int8x16_t va2 = vld1q_s8(a + i + 16);
|
||||
int8x16_t vb2 = vld1q_s8(b + i + 16);
|
||||
|
||||
int16x8_t tmp1 = vmull_s8(vget_low_s8(va1), vget_low_s8(vb1));
|
||||
int16x8_t tmp2 = vmull_s8(vget_high_s8(va1), vget_high_s8(vb1));
|
||||
int16x8_t tmp3 = vmull_s8(vget_low_s8(va2), vget_low_s8(vb2));
|
||||
int16x8_t tmp4 = vmull_s8(vget_high_s8(va2), vget_high_s8(vb2));
|
||||
|
||||
// Accumulate 4 x 32 bit vectors (adding adjacent 16 bit lanes).
|
||||
acc1 = vpadalq_s16(acc1, tmp1);
|
||||
acc2 = vpadalq_s16(acc2, tmp2);
|
||||
acc3 = vpadalq_s16(acc3, tmp3);
|
||||
acc4 = vpadalq_s16(acc4, tmp4);
|
||||
}
|
||||
|
||||
// reduce
|
||||
int32x4_t acc5 = vaddq_s32(acc1, acc2);
|
||||
int32x4_t acc6 = vaddq_s32(acc3, acc4);
|
||||
return vaddvq_s32(vaddq_s32(acc5, acc6));
|
||||
}
|
||||
|
||||
EXPORT int32_t sqr8s(int8_t *a, int8_t *b, size_t dims) {
|
||||
int32x4_t acc1 = vdupq_n_s32(0);
|
||||
int32x4_t acc2 = vdupq_n_s32(0);
|
||||
int32x4_t acc3 = vdupq_n_s32(0);
|
||||
int32x4_t acc4 = vdupq_n_s32(0);
|
||||
|
||||
for (int i = 0; i < dims; i += SQR8S_STRIDE_BYTES_LEN) {
|
||||
int8x16_t va1 = vld1q_s8(a + i);
|
||||
int8x16_t vb1 = vld1q_s8(b + i);
|
||||
|
||||
int16x8_t tmp1 = vsubl_s8(vget_low_s8(va1), vget_low_s8(vb1));
|
||||
int16x8_t tmp2 = vsubl_s8(vget_high_s8(va1), vget_high_s8(vb1));
|
||||
|
||||
acc1 = vmlal_s16(acc1, vget_low_s16(tmp1), vget_low_s16(tmp1));
|
||||
acc2 = vmlal_s16(acc2, vget_high_s16(tmp1), vget_high_s16(tmp1));
|
||||
acc3 = vmlal_s16(acc3, vget_low_s16(tmp2), vget_low_s16(tmp2));
|
||||
acc4 = vmlal_s16(acc4, vget_high_s16(tmp2), vget_high_s16(tmp2));
|
||||
}
|
||||
|
||||
// reduce
|
||||
int32x4_t acc5 = vaddq_s32(acc1, acc2);
|
||||
int32x4_t acc6 = vaddq_s32(acc3, acc4);
|
||||
return vaddvq_s32(vaddq_s32(acc5, acc6));
|
||||
}
|
17
libs/vec/native/src/vec/headers/vec.h
Normal file
17
libs/vec/native/src/vec/headers/vec.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#define EXPORT __attribute__((externally_visible,visibility("default")))
|
||||
|
||||
EXPORT int dot8s_stride();
|
||||
|
||||
EXPORT int sqr8s_stride();
|
||||
|
||||
EXPORT int32_t dot8s(int8_t* a, int8_t* b, size_t dims);
|
||||
|
||||
EXPORT int32_t sqr8s(int8_t *a, int8_t *b, size_t length);
|
14
libs/vec/src/main/java/module-info.java
Normal file
14
libs/vec/src/main/java/module-info.java
Normal file
|
@ -0,0 +1,14 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
module org.elasticsearch.vec {
|
||||
requires org.elasticsearch.nativeaccess;
|
||||
requires org.apache.lucene.core;
|
||||
|
||||
exports org.elasticsearch.vec to org.elasticsearch.server;
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* 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.vec;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/** A scorer of vectors. */
|
||||
public interface VectorScorer {
|
||||
|
||||
/** Computes the score of the vectors at the given ordinals. */
|
||||
float score(int firstOrd, int secondOrd) throws IOException;
|
||||
|
||||
/** The per-vector dimension size. */
|
||||
int dims();
|
||||
|
||||
/** The maximum ordinal of vector this scorer can score. */
|
||||
int maxOrd();
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* 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.vec;
|
||||
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/** A factory of quantized vector scorers. */
|
||||
public interface VectorScorerFactory {
|
||||
|
||||
static Optional<VectorScorerFactory> instance() {
|
||||
return Optional.ofNullable(VectorScorerFactoryImpl.INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an optional containing a scalar quantized vector scorer for the
|
||||
* given parameters, or an empty optional if a scorer is not supported.
|
||||
*
|
||||
* @param dims the vector dimensions
|
||||
* @param maxOrd the ordinal of the largest vector accessible
|
||||
* @param scoreCorrectionConstant the score correction constant
|
||||
* @param similarityType the similarity type
|
||||
* @param indexInput the index input containing the vector data;
|
||||
* offset of the first vector is 0,
|
||||
* the length must be (maxOrd + Float#BYTES) * dims
|
||||
* @return an optional containing the vector scorer, or empty
|
||||
*/
|
||||
Optional<VectorScorer> getScalarQuantizedVectorScorer(
|
||||
int dims,
|
||||
int maxOrd,
|
||||
float scoreCorrectionConstant,
|
||||
VectorSimilarityType similarityType,
|
||||
IndexInput indexInput
|
||||
);
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* 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.vec;
|
||||
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
class VectorScorerFactoryImpl implements VectorScorerFactory {
|
||||
|
||||
static final VectorScorerFactoryImpl INSTANCE = null;
|
||||
|
||||
@Override
|
||||
public Optional<VectorScorer> getScalarQuantizedVectorScorer(
|
||||
int dims,
|
||||
int maxOrd,
|
||||
float scoreCorrectionConstant,
|
||||
VectorSimilarityType similarityType,
|
||||
IndexInput input
|
||||
) {
|
||||
throw new UnsupportedOperationException("should not reach here");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* 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.vec;
|
||||
|
||||
import org.apache.lucene.util.hnsw.RandomVectorScorer;
|
||||
import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/** An adapter between VectorScorer and RandomVectorScorerSupplier. */
|
||||
public final class VectorScorerSupplierAdapter implements RandomVectorScorerSupplier {
|
||||
|
||||
private final VectorScorer scorer;
|
||||
|
||||
public VectorScorerSupplierAdapter(VectorScorer scorer) {
|
||||
this.scorer = scorer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RandomVectorScorer scorer(int ord) throws IOException {
|
||||
return new RandomVectorScorer() {
|
||||
final int firstOrd = ord;
|
||||
|
||||
@Override
|
||||
public float score(int otherOrd) throws IOException {
|
||||
return scorer.score(firstOrd, otherOrd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int maxOrd() {
|
||||
return scorer.maxOrd();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public RandomVectorScorerSupplier copy() throws IOException {
|
||||
return this; // no need to copy, thread-safe
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* 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.vec;
|
||||
|
||||
import org.apache.lucene.index.VectorSimilarityFunction;
|
||||
|
||||
/** Vector similarity type. */
|
||||
public enum VectorSimilarityType {
|
||||
|
||||
COSINE,
|
||||
|
||||
DOT_PRODUCT,
|
||||
|
||||
EUCLIDEAN,
|
||||
|
||||
MAXIMUM_INNER_PRODUCT;
|
||||
|
||||
/** Converts from the given vector similarity type to this similarity type. */
|
||||
public static VectorSimilarityType of(VectorSimilarityFunction func) {
|
||||
return switch (func) {
|
||||
case EUCLIDEAN -> VectorSimilarityType.EUCLIDEAN;
|
||||
case COSINE -> VectorSimilarityType.COSINE;
|
||||
case DOT_PRODUCT -> VectorSimilarityType.DOT_PRODUCT;
|
||||
case MAXIMUM_INNER_PRODUCT -> VectorSimilarityType.MAXIMUM_INNER_PRODUCT;
|
||||
};
|
||||
}
|
||||
|
||||
/** Converts from this vector similarity type to VectorSimilarityFunction. */
|
||||
public static VectorSimilarityFunction of(VectorSimilarityType func) {
|
||||
return switch (func) {
|
||||
case EUCLIDEAN -> VectorSimilarityFunction.EUCLIDEAN;
|
||||
case COSINE -> VectorSimilarityFunction.COSINE;
|
||||
case DOT_PRODUCT -> VectorSimilarityFunction.DOT_PRODUCT;
|
||||
case MAXIMUM_INNER_PRODUCT -> VectorSimilarityFunction.MAXIMUM_INNER_PRODUCT;
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* 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.vec;
|
||||
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
import org.elasticsearch.nativeaccess.NativeAccess;
|
||||
import org.elasticsearch.vec.internal.DotProduct;
|
||||
import org.elasticsearch.vec.internal.Euclidean;
|
||||
import org.elasticsearch.vec.internal.IndexInputUtils;
|
||||
import org.elasticsearch.vec.internal.MaximumInnerProduct;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
class VectorScorerFactoryImpl implements VectorScorerFactory {
|
||||
|
||||
static final VectorScorerFactoryImpl INSTANCE;
|
||||
|
||||
private VectorScorerFactoryImpl() {}
|
||||
|
||||
static {
|
||||
INSTANCE = NativeAccess.instance().getVectorSimilarityFunctions().map(ignore -> new VectorScorerFactoryImpl()).orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<VectorScorer> getScalarQuantizedVectorScorer(
|
||||
int dims,
|
||||
int maxOrd,
|
||||
float scoreCorrectionConstant,
|
||||
VectorSimilarityType similarityType,
|
||||
IndexInput input
|
||||
) {
|
||||
input = IndexInputUtils.unwrapAndCheckInputOrNull(input);
|
||||
if (input == null) {
|
||||
return Optional.empty(); // the input type is not MemorySegment based
|
||||
}
|
||||
return Optional.of(switch (similarityType) {
|
||||
case COSINE, DOT_PRODUCT -> new DotProduct(dims, maxOrd, scoreCorrectionConstant, input);
|
||||
case EUCLIDEAN -> new Euclidean(dims, maxOrd, scoreCorrectionConstant, input);
|
||||
case MAXIMUM_INNER_PRODUCT -> new MaximumInnerProduct(dims, maxOrd, scoreCorrectionConstant, input);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
* 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.vec.internal;
|
||||
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
import org.apache.lucene.util.quantization.ScalarQuantizedVectorSimilarity;
|
||||
import org.elasticsearch.nativeaccess.NativeAccess;
|
||||
import org.elasticsearch.nativeaccess.VectorSimilarityFunctions;
|
||||
import org.elasticsearch.vec.VectorScorer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.foreign.MemorySegment;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
|
||||
abstract sealed class AbstractScalarQuantizedVectorScorer implements VectorScorer permits DotProduct, Euclidean, MaximumInnerProduct {
|
||||
|
||||
static final VectorSimilarityFunctions DISTANCE_FUNCS = NativeAccess.instance()
|
||||
.getVectorSimilarityFunctions()
|
||||
.orElseThrow(AssertionError::new);
|
||||
|
||||
protected final int dims;
|
||||
protected final int maxOrd;
|
||||
protected final float scoreCorrectionConstant;
|
||||
protected final IndexInput input;
|
||||
protected final MemorySegment segment;
|
||||
protected final MemorySegment[] segments;
|
||||
protected final long offset;
|
||||
protected final int chunkSizePower;
|
||||
protected final long chunkSizeMask;
|
||||
|
||||
private final ScalarQuantizedVectorSimilarity fallbackScorer;
|
||||
|
||||
protected AbstractScalarQuantizedVectorScorer(
|
||||
int dims,
|
||||
int maxOrd,
|
||||
float scoreCorrectionConstant,
|
||||
IndexInput input,
|
||||
ScalarQuantizedVectorSimilarity fallbackScorer
|
||||
) {
|
||||
this.dims = dims;
|
||||
this.maxOrd = maxOrd;
|
||||
this.scoreCorrectionConstant = scoreCorrectionConstant;
|
||||
this.input = input;
|
||||
this.fallbackScorer = fallbackScorer;
|
||||
|
||||
this.segments = IndexInputUtils.segmentArray(input);
|
||||
if (segments.length == 1) {
|
||||
segment = segments[0];
|
||||
offset = 0L;
|
||||
} else {
|
||||
segment = null;
|
||||
offset = IndexInputUtils.offset(input);
|
||||
}
|
||||
this.chunkSizePower = IndexInputUtils.chunkSizePower(input);
|
||||
this.chunkSizeMask = IndexInputUtils.chunkSizeMask(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int dims() {
|
||||
return dims;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int maxOrd() {
|
||||
return maxOrd;
|
||||
}
|
||||
|
||||
protected final void checkOrdinal(int ord) {
|
||||
if (ord < 0 || ord > maxOrd) {
|
||||
throw new IllegalArgumentException("illegal ordinal: " + ord);
|
||||
}
|
||||
}
|
||||
|
||||
protected final float fallbackScore(int firstByteOffset, int secondByteOffset) throws IOException {
|
||||
input.seek(firstByteOffset);
|
||||
byte[] a = new byte[dims];
|
||||
input.readBytes(a, 0, a.length);
|
||||
float aOffsetValue = Float.intBitsToFloat(input.readInt());
|
||||
|
||||
input.seek(secondByteOffset);
|
||||
byte[] b = new byte[dims];
|
||||
input.readBytes(b, 0, a.length);
|
||||
float bOffsetValue = Float.intBitsToFloat(input.readInt());
|
||||
|
||||
return fallbackScorer.score(a, aOffsetValue, b, bOffsetValue);
|
||||
}
|
||||
|
||||
protected final MemorySegment segmentSlice(long pos, int length) {
|
||||
if (segment != null) {
|
||||
// single
|
||||
if (checkIndex(pos, segment.byteSize() + 1)) {
|
||||
return segment.asSlice(pos, length);
|
||||
}
|
||||
} else {
|
||||
// multi
|
||||
pos = pos + this.offset;
|
||||
final int si = (int) (pos >> chunkSizePower);
|
||||
final MemorySegment seg = segments[si];
|
||||
long offset = pos & chunkSizeMask;
|
||||
if (checkIndex(offset + length, seg.byteSize() + 1)) {
|
||||
return seg.asSlice(offset, length);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static boolean checkIndex(long index, long length) {
|
||||
return index >= 0 && index < length;
|
||||
}
|
||||
|
||||
static final MethodHandle DOT_PRODUCT = DISTANCE_FUNCS.dotProductHandle();
|
||||
static final MethodHandle SQUARE_DISTANCE = DISTANCE_FUNCS.squareDistanceHandle();
|
||||
|
||||
static int dotProduct(MemorySegment a, MemorySegment b, int length) {
|
||||
// assert assertSegments(a, b, length);
|
||||
try {
|
||||
return (int) DOT_PRODUCT.invokeExact(a, b, length);
|
||||
} catch (Throwable e) {
|
||||
if (e instanceof Error err) {
|
||||
throw err;
|
||||
} else if (e instanceof RuntimeException re) {
|
||||
throw re;
|
||||
} else {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int squareDistance(MemorySegment a, MemorySegment b, int length) {
|
||||
// assert assertSegments(a, b, length);
|
||||
try {
|
||||
return (int) SQUARE_DISTANCE.invokeExact(a, b, length);
|
||||
} catch (Throwable e) {
|
||||
if (e instanceof Error err) {
|
||||
throw err;
|
||||
} else if (e instanceof RuntimeException re) {
|
||||
throw re;
|
||||
} else {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static boolean assertSegments(MemorySegment a, MemorySegment b, int length) {
|
||||
return a.isNative() && a.byteSize() >= length && b.isNative() && b.byteSize() >= length;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* 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.vec.internal;
|
||||
|
||||
import org.apache.lucene.index.VectorSimilarityFunction;
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
import org.apache.lucene.util.quantization.ScalarQuantizedVectorSimilarity;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.foreign.MemorySegment;
|
||||
|
||||
// Scalar Quantized vectors are inherently byte sized, so dims is equal to the length in bytes.
|
||||
public final class DotProduct extends AbstractScalarQuantizedVectorScorer {
|
||||
|
||||
public DotProduct(int dims, int maxOrd, float scoreCorrectionConstant, IndexInput input) {
|
||||
super(
|
||||
dims,
|
||||
maxOrd,
|
||||
scoreCorrectionConstant,
|
||||
input,
|
||||
ScalarQuantizedVectorSimilarity.fromVectorSimilarity(VectorSimilarityFunction.DOT_PRODUCT, scoreCorrectionConstant)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float score(int firstOrd, int secondOrd) throws IOException {
|
||||
checkOrdinal(firstOrd);
|
||||
checkOrdinal(secondOrd);
|
||||
|
||||
final int length = dims;
|
||||
int firstByteOffset = firstOrd * (length + Float.BYTES);
|
||||
int secondByteOffset = secondOrd * (length + Float.BYTES);
|
||||
|
||||
MemorySegment firstSeg = segmentSlice(firstByteOffset, length);
|
||||
input.seek(firstByteOffset + length);
|
||||
float firstOffset = Float.intBitsToFloat(input.readInt());
|
||||
|
||||
MemorySegment secondSeg = segmentSlice(secondByteOffset, length);
|
||||
input.seek(secondByteOffset + length);
|
||||
float secondOffset = Float.intBitsToFloat(input.readInt());
|
||||
|
||||
if (firstSeg != null && secondSeg != null) {
|
||||
int dotProduct = dotProduct(firstSeg, secondSeg, length);
|
||||
float adjustedDistance = dotProduct * scoreCorrectionConstant + firstOffset + secondOffset;
|
||||
return (1 + adjustedDistance) / 2;
|
||||
} else {
|
||||
return fallbackScore(firstByteOffset, secondByteOffset);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* 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.vec.internal;
|
||||
|
||||
import org.apache.lucene.index.VectorSimilarityFunction;
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
import org.apache.lucene.util.quantization.ScalarQuantizedVectorSimilarity;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.foreign.MemorySegment;
|
||||
|
||||
// Scalar Quantized vectors are inherently bytes.
|
||||
public final class Euclidean extends AbstractScalarQuantizedVectorScorer {
|
||||
|
||||
public Euclidean(int dims, int maxOrd, float scoreCorrectionConstant, IndexInput input) {
|
||||
super(
|
||||
dims,
|
||||
maxOrd,
|
||||
scoreCorrectionConstant,
|
||||
input,
|
||||
ScalarQuantizedVectorSimilarity.fromVectorSimilarity(VectorSimilarityFunction.EUCLIDEAN, scoreCorrectionConstant)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float score(int firstOrd, int secondOrd) throws IOException {
|
||||
checkOrdinal(firstOrd);
|
||||
checkOrdinal(secondOrd);
|
||||
|
||||
final int length = dims;
|
||||
int firstByteOffset = firstOrd * (length + Float.BYTES);
|
||||
int secondByteOffset = secondOrd * (length + Float.BYTES);
|
||||
|
||||
MemorySegment firstSeg = segmentSlice(firstByteOffset, length);
|
||||
MemorySegment secondSeg = segmentSlice(secondByteOffset, length);
|
||||
|
||||
if (firstSeg != null && secondSeg != null) {
|
||||
int squareDistance = squareDistance(firstSeg, secondSeg, length);
|
||||
float adjustedDistance = squareDistance * scoreCorrectionConstant;
|
||||
return 1 / (1f + adjustedDistance);
|
||||
} else {
|
||||
return fallbackScore(firstByteOffset, secondByteOffset);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* 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.vec.internal;
|
||||
|
||||
import org.apache.lucene.store.FilterIndexInput;
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
|
||||
import java.lang.foreign.MemorySegment;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.VarHandle;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.security.PrivilegedActionException;
|
||||
import java.security.PrivilegedExceptionAction;
|
||||
|
||||
public final class IndexInputUtils {
|
||||
|
||||
static final Class<?> MSINDEX_CLS, MS_MSINDEX_CLS;
|
||||
static final VarHandle SEGMENTS_ARRAY, CHUNK_SIZE_POWER, CHUNK_SIZE_MASK, MULTI_OFFSET;
|
||||
|
||||
static {
|
||||
try {
|
||||
MSINDEX_CLS = Class.forName("org.apache.lucene.store.MemorySegmentIndexInput");
|
||||
MS_MSINDEX_CLS = Class.forName("org.apache.lucene.store.MemorySegmentIndexInput$MultiSegmentImpl");
|
||||
var lookup = privilegedPrivateLookupIn(MSINDEX_CLS, MethodHandles.lookup());
|
||||
SEGMENTS_ARRAY = privilegedFindVarHandle(lookup, MSINDEX_CLS, "segments", MemorySegment[].class);
|
||||
CHUNK_SIZE_POWER = privilegedFindVarHandle(lookup, MSINDEX_CLS, "chunkSizePower", int.class);
|
||||
CHUNK_SIZE_MASK = privilegedFindVarHandle(lookup, MSINDEX_CLS, "chunkSizeMask", long.class);
|
||||
MULTI_OFFSET = privilegedFindVarHandle(lookup, MS_MSINDEX_CLS, "offset", long.class);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new AssertionError(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new AssertionError("should not happen, check opens", e);
|
||||
} catch (PrivilegedActionException e) {
|
||||
throw new AssertionError("should not happen", e);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
static VarHandle privilegedFindVarHandle(MethodHandles.Lookup lookup, Class<?> cls, String name, Class<?> type)
|
||||
throws PrivilegedActionException {
|
||||
PrivilegedExceptionAction<VarHandle> pa = () -> lookup.findVarHandle(cls, name, type);
|
||||
return AccessController.doPrivileged(pa);
|
||||
}
|
||||
|
||||
private IndexInputUtils() {}
|
||||
|
||||
/** Unwraps and returns the input if it's a MemorySegment backed input. Otherwise, null. */
|
||||
public static IndexInput unwrapAndCheckInputOrNull(IndexInput input) {
|
||||
input = FilterIndexInput.unwrap(input);
|
||||
if (MSINDEX_CLS.isAssignableFrom(input.getClass())) {
|
||||
return input;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static MemorySegment[] segmentArray(IndexInput input) {
|
||||
return (MemorySegment[]) SEGMENTS_ARRAY.get(input);
|
||||
}
|
||||
|
||||
static long chunkSizeMask(IndexInput input) {
|
||||
return (long) CHUNK_SIZE_MASK.get(input);
|
||||
}
|
||||
|
||||
static int chunkSizePower(IndexInput input) {
|
||||
return (int) CHUNK_SIZE_POWER.get(input);
|
||||
}
|
||||
|
||||
static long offset(IndexInput input) {
|
||||
return (long) MULTI_OFFSET.get(input);
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
static MethodHandles.Lookup privilegedPrivateLookupIn(Class<?> cls, MethodHandles.Lookup lookup) throws IllegalAccessException {
|
||||
PrivilegedAction<MethodHandles.Lookup> pa = () -> {
|
||||
try {
|
||||
return MethodHandles.privateLookupIn(cls, lookup);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new AssertionError("should not happen, check opens", e);
|
||||
}
|
||||
};
|
||||
return AccessController.doPrivileged(pa);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* 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.vec.internal;
|
||||
|
||||
import org.apache.lucene.index.VectorSimilarityFunction;
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
import org.apache.lucene.util.quantization.ScalarQuantizedVectorSimilarity;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.foreign.MemorySegment;
|
||||
|
||||
// Scalar Quantized vectors are inherently bytes.
|
||||
public final class MaximumInnerProduct extends AbstractScalarQuantizedVectorScorer {
|
||||
|
||||
public MaximumInnerProduct(int dims, int maxOrd, float scoreCorrectionConstant, IndexInput input) {
|
||||
super(
|
||||
dims,
|
||||
maxOrd,
|
||||
scoreCorrectionConstant,
|
||||
input,
|
||||
ScalarQuantizedVectorSimilarity.fromVectorSimilarity(VectorSimilarityFunction.MAXIMUM_INNER_PRODUCT, scoreCorrectionConstant)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float score(int firstOrd, int secondOrd) throws IOException {
|
||||
checkOrdinal(firstOrd);
|
||||
checkOrdinal(secondOrd);
|
||||
|
||||
final int length = dims;
|
||||
int firstByteOffset = firstOrd * (length + Float.BYTES);
|
||||
int secondByteOffset = secondOrd * (length + Float.BYTES);
|
||||
|
||||
MemorySegment firstSeg = segmentSlice(firstByteOffset, length);
|
||||
input.seek(firstByteOffset + length);
|
||||
float firstOffset = Float.intBitsToFloat(input.readInt());
|
||||
|
||||
MemorySegment secondSeg = segmentSlice(secondByteOffset, length);
|
||||
input.seek(secondByteOffset + length);
|
||||
float secondOffset = Float.intBitsToFloat(input.readInt());
|
||||
|
||||
if (firstSeg != null && secondSeg != null) {
|
||||
int dotProduct = dotProduct(firstSeg, secondSeg, length);
|
||||
float adjustedDistance = dotProduct * scoreCorrectionConstant + firstOffset + secondOffset;
|
||||
return scaleMaxInnerProductScore(adjustedDistance);
|
||||
} else {
|
||||
return fallbackScore(firstByteOffset, secondByteOffset);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a scaled score preventing negative scores for maximum-inner-product
|
||||
* @param rawSimilarity the raw similarity between two vectors
|
||||
*/
|
||||
static float scaleMaxInnerProductScore(float rawSimilarity) {
|
||||
if (rawSimilarity < 0) {
|
||||
return 1 / (1 + -1 * rawSimilarity);
|
||||
}
|
||||
return rawSimilarity + 1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* 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.vec;
|
||||
|
||||
import org.apache.lucene.util.quantization.ScalarQuantizedVectorSimilarity;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.elasticsearch.test.hamcrest.OptionalMatchers.isPresent;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
public abstract class AbstractVectorTestCase extends ESTestCase {
|
||||
|
||||
static Optional<VectorScorerFactory> factory;
|
||||
|
||||
@BeforeClass
|
||||
public static void getVectorScorerFactory() {
|
||||
factory = VectorScorerFactory.instance();
|
||||
}
|
||||
|
||||
protected AbstractVectorTestCase() {
|
||||
logger.info(platformMsg());
|
||||
}
|
||||
|
||||
public static boolean supported() {
|
||||
var jdkVersion = Runtime.version().feature();
|
||||
var arch = System.getProperty("os.arch");
|
||||
var osName = System.getProperty("os.name");
|
||||
|
||||
if (jdkVersion >= 21 && arch.equals("aarch64") && (osName.startsWith("Mac") || osName.equals("Linux"))) {
|
||||
assertThat(factory, isPresent());
|
||||
return true;
|
||||
} else {
|
||||
assertThat(factory, not(isPresent()));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static String notSupportedMsg() {
|
||||
return "Not supported on [" + platformMsg() + "]";
|
||||
}
|
||||
|
||||
public static String platformMsg() {
|
||||
var jdkVersion = Runtime.version().feature();
|
||||
var arch = System.getProperty("os.arch");
|
||||
var osName = System.getProperty("os.name");
|
||||
return "JDK=" + jdkVersion + ", os=" + osName + ", arch=" + arch;
|
||||
}
|
||||
|
||||
/** Computes the score using the Lucene implementation. */
|
||||
public static float luceneScore(
|
||||
VectorSimilarityType similarityFunc,
|
||||
byte[] a,
|
||||
byte[] b,
|
||||
float correction,
|
||||
float aOffsetValue,
|
||||
float bOffsetValue
|
||||
) {
|
||||
var scorer = ScalarQuantizedVectorSimilarity.fromVectorSimilarity(VectorSimilarityType.of(similarityFunc), correction);
|
||||
return scorer.score(a, aOffsetValue, b, bOffsetValue);
|
||||
}
|
||||
|
||||
/** Converts a float value to a byte array. */
|
||||
public static byte[] floatToByteArray(float value) {
|
||||
return ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putFloat(value).array();
|
||||
}
|
||||
|
||||
/** Concatenates byte arrays. */
|
||||
public static byte[] concat(byte[]... arrays) throws IOException {
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||
for (var ba : arrays) {
|
||||
baos.write(ba);
|
||||
}
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,239 @@
|
|||
/*
|
||||
* 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.vec;
|
||||
|
||||
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.elasticsearch.test.ESTestCase;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.elasticsearch.vec.VectorSimilarityType.COSINE;
|
||||
import static org.elasticsearch.vec.VectorSimilarityType.DOT_PRODUCT;
|
||||
import static org.elasticsearch.vec.VectorSimilarityType.EUCLIDEAN;
|
||||
import static org.elasticsearch.vec.VectorSimilarityType.MAXIMUM_INNER_PRODUCT;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
// @com.carrotsearch.randomizedtesting.annotations.Repeat(iterations = 100)
|
||||
public class VectorScorerFactoryTests extends AbstractVectorTestCase {
|
||||
|
||||
// Tests that the provider instance is present or not on expected platforms/architectures
|
||||
public void testSupport() {
|
||||
supported();
|
||||
}
|
||||
|
||||
public void testSimple() throws IOException {
|
||||
testSimpleImpl(MMapDirectory.DEFAULT_MAX_CHUNK_SIZE);
|
||||
}
|
||||
|
||||
public void testSimpleMaxChunkSizeSmall() throws IOException {
|
||||
long maxChunkSize = randomLongBetween(4, 16);
|
||||
logger.info("maxChunkSize=" + maxChunkSize);
|
||||
testSimpleImpl(maxChunkSize);
|
||||
}
|
||||
|
||||
void testSimpleImpl(long maxChunkSize) throws IOException {
|
||||
assumeTrue(notSupportedMsg(), supported());
|
||||
var factory = AbstractVectorTestCase.factory.get();
|
||||
|
||||
try (Directory dir = new MMapDirectory(createTempDir(getTestName()), maxChunkSize)) {
|
||||
for (int dims : List.of(31, 32, 33)) {
|
||||
// dimensions that cross the scalar / native boundary (stride)
|
||||
byte[] vec1 = new byte[dims];
|
||||
byte[] vec2 = new byte[dims];
|
||||
String fileName = getTestName() + "-" + dims;
|
||||
try (IndexOutput out = dir.createOutput(fileName, IOContext.DEFAULT)) {
|
||||
for (int i = 0; i < dims; i++) {
|
||||
vec1[i] = (byte) i;
|
||||
vec2[i] = (byte) (dims - i);
|
||||
}
|
||||
var oneFactor = floatToByteArray(1f);
|
||||
byte[] bytes = concat(vec1, oneFactor, vec2, oneFactor);
|
||||
out.writeBytes(bytes, 0, bytes.length);
|
||||
}
|
||||
try (IndexInput in = dir.openInput(fileName, IOContext.DEFAULT)) {
|
||||
// dot product
|
||||
float expected = luceneScore(DOT_PRODUCT, vec1, vec2, 1, 1, 1);
|
||||
var scorer = factory.getScalarQuantizedVectorScorer(dims, 2, 1, DOT_PRODUCT, in).get();
|
||||
assertThat(scorer.score(0, 1), equalTo(expected));
|
||||
assertThat((new VectorScorerSupplierAdapter(scorer)).scorer(0).score(1), equalTo(expected));
|
||||
// max inner product
|
||||
expected = luceneScore(MAXIMUM_INNER_PRODUCT, vec1, vec2, 1, 1, 1);
|
||||
scorer = factory.getScalarQuantizedVectorScorer(dims, 2, 1, MAXIMUM_INNER_PRODUCT, in).get();
|
||||
assertThat(scorer.score(0, 1), equalTo(expected));
|
||||
assertThat((new VectorScorerSupplierAdapter(scorer)).scorer(0).score(1), equalTo(expected));
|
||||
// cosine
|
||||
expected = luceneScore(COSINE, vec1, vec2, 1, 1, 1);
|
||||
scorer = factory.getScalarQuantizedVectorScorer(dims, 2, 1, COSINE, in).get();
|
||||
assertThat(scorer.score(0, 1), equalTo(expected));
|
||||
assertThat((new VectorScorerSupplierAdapter(scorer)).scorer(0).score(1), equalTo(expected));
|
||||
// euclidean
|
||||
expected = luceneScore(EUCLIDEAN, vec1, vec2, 1, 1, 1);
|
||||
scorer = factory.getScalarQuantizedVectorScorer(dims, 2, 1, EUCLIDEAN, in).get();
|
||||
assertThat(scorer.score(0, 1), equalTo(expected));
|
||||
assertThat((new VectorScorerSupplierAdapter(scorer)).scorer(0).score(1), equalTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testRandom() throws IOException {
|
||||
assumeTrue(notSupportedMsg(), supported());
|
||||
testRandom(MMapDirectory.DEFAULT_MAX_CHUNK_SIZE, ESTestCase::randomByteArrayOfLength);
|
||||
}
|
||||
|
||||
public void testRandomMaxChunkSizeSmall() throws IOException {
|
||||
assumeTrue(notSupportedMsg(), supported());
|
||||
long maxChunkSize = randomLongBetween(32, 128);
|
||||
logger.info("maxChunkSize=" + maxChunkSize);
|
||||
testRandom(maxChunkSize, ESTestCase::randomByteArrayOfLength);
|
||||
}
|
||||
|
||||
public void testRandomMax() throws IOException {
|
||||
assumeTrue(notSupportedMsg(), supported());
|
||||
testRandom(MMapDirectory.DEFAULT_MAX_CHUNK_SIZE, BYTE_ARRAY_MAX_FUNC);
|
||||
}
|
||||
|
||||
public void testRandomMin() throws IOException {
|
||||
assumeTrue(notSupportedMsg(), supported());
|
||||
testRandom(MMapDirectory.DEFAULT_MAX_CHUNK_SIZE, BYTE_ARRAY_MIN_FUNC);
|
||||
}
|
||||
|
||||
void testRandom(long maxChunkSize, Function<Integer, byte[]> byteArraySupplier) throws IOException {
|
||||
var factory = AbstractVectorTestCase.factory.get();
|
||||
|
||||
try (Directory dir = new MMapDirectory(createTempDir(getTestName()), maxChunkSize)) {
|
||||
for (int times = 0; times < TIMES; times++) {
|
||||
final int dims = randomIntBetween(1, 4096);
|
||||
final int size = randomIntBetween(2, 100);
|
||||
final float correction = randomFloat();
|
||||
final byte[][] vectors = new byte[size][];
|
||||
final float[] offsets = new float[size];
|
||||
|
||||
String fileName = getTestName() + "-" + times + "-" + dims;
|
||||
logger.info("Testing " + fileName);
|
||||
try (IndexOutput out = dir.createOutput(fileName, IOContext.DEFAULT)) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
var vec = byteArraySupplier.apply(dims);
|
||||
var off = randomFloat();
|
||||
out.writeBytes(vec, 0, vec.length);
|
||||
out.writeInt(Float.floatToIntBits(off));
|
||||
vectors[i] = vec;
|
||||
offsets[i] = off;
|
||||
}
|
||||
}
|
||||
try (IndexInput in = dir.openInput(fileName, IOContext.DEFAULT)) {
|
||||
int idx0 = randomIntBetween(0, size - 1);
|
||||
int idx1 = randomIntBetween(0, size - 1); // may be the same as idx0 - which is ok.
|
||||
// dot product
|
||||
float expected = luceneScore(DOT_PRODUCT, vectors[idx0], vectors[idx1], correction, offsets[idx0], offsets[idx1]);
|
||||
var scorer = factory.getScalarQuantizedVectorScorer(dims, size, correction, DOT_PRODUCT, in).get();
|
||||
assertThat(scorer.score(idx0, idx1), equalTo(expected));
|
||||
assertThat((new VectorScorerSupplierAdapter(scorer)).scorer(idx0).score(idx1), equalTo(expected));
|
||||
// max inner product
|
||||
expected = luceneScore(MAXIMUM_INNER_PRODUCT, vectors[idx0], vectors[idx1], correction, offsets[idx0], offsets[idx1]);
|
||||
scorer = factory.getScalarQuantizedVectorScorer(dims, size, correction, MAXIMUM_INNER_PRODUCT, in).get();
|
||||
assertThat(scorer.score(idx0, idx1), equalTo(expected));
|
||||
assertThat((new VectorScorerSupplierAdapter(scorer)).scorer(idx0).score(idx1), equalTo(expected));
|
||||
// cosine
|
||||
expected = luceneScore(COSINE, vectors[idx0], vectors[idx1], correction, offsets[idx0], offsets[idx1]);
|
||||
scorer = factory.getScalarQuantizedVectorScorer(dims, size, correction, COSINE, in).get();
|
||||
assertThat(scorer.score(idx0, idx1), equalTo(expected));
|
||||
assertThat((new VectorScorerSupplierAdapter(scorer)).scorer(idx0).score(idx1), equalTo(expected));
|
||||
// euclidean
|
||||
expected = luceneScore(EUCLIDEAN, vectors[idx0], vectors[idx1], correction, offsets[idx0], offsets[idx1]);
|
||||
scorer = factory.getScalarQuantizedVectorScorer(dims, size, correction, EUCLIDEAN, in).get();
|
||||
assertThat(scorer.score(idx0, idx1), equalTo(expected));
|
||||
assertThat((new VectorScorerSupplierAdapter(scorer)).scorer(idx0).score(idx1), equalTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testRandomSlice() throws IOException {
|
||||
assumeTrue(notSupportedMsg(), supported());
|
||||
testRandomSliceImpl(30, 64, 1, ESTestCase::randomByteArrayOfLength);
|
||||
}
|
||||
|
||||
void testRandomSliceImpl(int dims, long maxChunkSize, int initialPadding, Function<Integer, byte[]> byteArraySupplier)
|
||||
throws IOException {
|
||||
var factory = AbstractVectorTestCase.factory.get();
|
||||
|
||||
try (Directory dir = new MMapDirectory(createTempDir(getTestName()), maxChunkSize)) {
|
||||
for (int times = 0; times < TIMES; times++) {
|
||||
final int size = randomIntBetween(2, 100);
|
||||
final float correction = randomFloat();
|
||||
final byte[][] vectors = new byte[size][];
|
||||
final float[] offsets = new float[size];
|
||||
|
||||
String fileName = getTestName() + "-" + times + "-" + dims;
|
||||
logger.info("Testing " + fileName);
|
||||
try (IndexOutput out = dir.createOutput(fileName, IOContext.DEFAULT)) {
|
||||
byte[] ba = new byte[initialPadding];
|
||||
out.writeBytes(ba, 0, ba.length);
|
||||
for (int i = 0; i < size; i++) {
|
||||
var vec = byteArraySupplier.apply(dims);
|
||||
var off = randomFloat();
|
||||
out.writeBytes(vec, 0, vec.length);
|
||||
out.writeInt(Float.floatToIntBits(off));
|
||||
vectors[i] = vec;
|
||||
offsets[i] = off;
|
||||
}
|
||||
}
|
||||
try (
|
||||
var outter = dir.openInput(fileName, IOContext.DEFAULT);
|
||||
var in = outter.slice("slice", initialPadding, outter.length() - initialPadding)
|
||||
) {
|
||||
int idx0 = randomIntBetween(0, size - 1);
|
||||
int idx1 = randomIntBetween(0, size - 1); // may be the same as idx0 - which is ok.
|
||||
// dot product
|
||||
float expected = luceneScore(DOT_PRODUCT, vectors[idx0], vectors[idx1], correction, offsets[idx0], offsets[idx1]);
|
||||
var scorer = factory.getScalarQuantizedVectorScorer(dims, size, correction, DOT_PRODUCT, in).get();
|
||||
assertThat(scorer.score(idx0, idx1), equalTo(expected));
|
||||
assertThat((new VectorScorerSupplierAdapter(scorer)).scorer(idx0).score(idx1), equalTo(expected));
|
||||
// max inner product
|
||||
expected = luceneScore(MAXIMUM_INNER_PRODUCT, vectors[idx0], vectors[idx1], correction, offsets[idx0], offsets[idx1]);
|
||||
scorer = factory.getScalarQuantizedVectorScorer(dims, size, correction, MAXIMUM_INNER_PRODUCT, in).get();
|
||||
assertThat(scorer.score(idx0, idx1), equalTo(expected));
|
||||
assertThat((new VectorScorerSupplierAdapter(scorer)).scorer(idx0).score(idx1), equalTo(expected));
|
||||
// cosine
|
||||
expected = luceneScore(COSINE, vectors[idx0], vectors[idx1], correction, offsets[idx0], offsets[idx1]);
|
||||
scorer = factory.getScalarQuantizedVectorScorer(dims, size, correction, COSINE, in).get();
|
||||
assertThat(scorer.score(idx0, idx1), equalTo(expected));
|
||||
assertThat((new VectorScorerSupplierAdapter(scorer)).scorer(idx0).score(idx1), equalTo(expected));
|
||||
// euclidean
|
||||
expected = luceneScore(EUCLIDEAN, vectors[idx0], vectors[idx1], correction, offsets[idx0], offsets[idx1]);
|
||||
scorer = factory.getScalarQuantizedVectorScorer(dims, size, correction, EUCLIDEAN, in).get();
|
||||
assertThat(scorer.score(idx0, idx1), equalTo(expected));
|
||||
assertThat((new VectorScorerSupplierAdapter(scorer)).scorer(idx0).score(idx1), equalTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Function<Integer, byte[]> BYTE_ARRAY_MAX_FUNC = size -> {
|
||||
byte[] ba = new byte[size];
|
||||
Arrays.fill(ba, Byte.MAX_VALUE);
|
||||
return ba;
|
||||
};
|
||||
|
||||
static Function<Integer, byte[]> BYTE_ARRAY_MIN_FUNC = size -> {
|
||||
byte[] ba = new byte[size];
|
||||
Arrays.fill(ba, Byte.MIN_VALUE);
|
||||
return ba;
|
||||
};
|
||||
|
||||
static final int TIMES = 100; // a loop iteration times
|
||||
}
|
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
* 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.vec.internal;
|
||||
|
||||
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.elasticsearch.test.ESTestCase;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.foreign.MemorySegment;
|
||||
import java.lang.foreign.ValueLayout;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
|
||||
public class IndexInputUtilsTests extends ESTestCase {
|
||||
|
||||
public void testSingleSegment() throws IOException {
|
||||
try (Directory dir = new MMapDirectory(createTempDir(getTestName()))) {
|
||||
for (int times = 0; times < TIMES; times++) {
|
||||
String fileName = getTestName() + times;
|
||||
int size = randomIntBetween(10, 127);
|
||||
try (IndexOutput out = dir.createOutput(fileName, IOContext.DEFAULT)) {
|
||||
byte[] ba = new byte[size];
|
||||
IntStream.range(0, size).forEach(i -> ba[i] = (byte) i);
|
||||
out.writeBytes(ba, 0, ba.length);
|
||||
}
|
||||
try (IndexInput in = dir.openInput(fileName, IOContext.DEFAULT)) {
|
||||
var input = IndexInputUtils.unwrapAndCheckInputOrNull(in);
|
||||
assertNotNull(input);
|
||||
{
|
||||
var segArray = IndexInputUtils.segmentArray(input);
|
||||
assertThat(segArray.length, equalTo(1));
|
||||
assertThat(segArray[0].byteSize(), equalTo((long) size));
|
||||
|
||||
// Out of Bounds - cannot retrieve the segment
|
||||
assertNull(segmentSlice(input, 0, size + 1));
|
||||
assertNull(segmentSlice(input, size - 1, 2));
|
||||
|
||||
var fullSeg = segmentSlice(input, 0, size);
|
||||
assertNotNull(fullSeg);
|
||||
for (int i = 0; i < size; i++) {
|
||||
assertThat(fullSeg.get(ValueLayout.JAVA_BYTE, i), equalTo((byte) i));
|
||||
}
|
||||
|
||||
var partialSeg = segmentSlice(input, 1, size - 1);
|
||||
assertNotNull(partialSeg);
|
||||
for (int i = 0; i < size - 2; i++) {
|
||||
assertThat(partialSeg.get(ValueLayout.JAVA_BYTE, i), equalTo((byte) (i + 1)));
|
||||
}
|
||||
}
|
||||
// IndexInput::slice
|
||||
{
|
||||
var slice = input.slice("partial slice", 1, size - 2);
|
||||
var sliceSgArray = IndexInputUtils.segmentArray(slice);
|
||||
assertThat(sliceSgArray.length, equalTo(1));
|
||||
assertThat(sliceSgArray[0].byteSize(), equalTo((long) size - 2));
|
||||
|
||||
var fullSeg = segmentSlice(slice, 0, size - 2);
|
||||
assertNotNull(fullSeg);
|
||||
for (int i = 0; i < size - 2; i++) {
|
||||
assertThat(fullSeg.get(ValueLayout.JAVA_BYTE, i), equalTo((byte) (i + 1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testMultiSegment() throws IOException {
|
||||
try (Directory dir = new MMapDirectory(createTempDir(getTestName()), 32L)) {
|
||||
for (int times = 0; times < TIMES; times++) {
|
||||
String fileName = getTestName() + times;
|
||||
int size = randomIntBetween(65, 1511);
|
||||
int expectedNumSegs = size / 32 + 1;
|
||||
try (IndexOutput out = dir.createOutput(fileName, IOContext.DEFAULT)) {
|
||||
byte[] ba = new byte[size];
|
||||
IntStream.range(0, size).forEach(i -> ba[i] = (byte) i);
|
||||
out.writeBytes(ba, 0, ba.length);
|
||||
}
|
||||
try (IndexInput in = dir.openInput(fileName, IOContext.DEFAULT)) {
|
||||
var input = IndexInputUtils.unwrapAndCheckInputOrNull(in);
|
||||
assertNotNull(input);
|
||||
|
||||
var fullSegArray = IndexInputUtils.segmentArray(input);
|
||||
assertThat(fullSegArray.length, equalTo(expectedNumSegs));
|
||||
assertThat(Arrays.stream(fullSegArray).mapToLong(MemorySegment::byteSize).sum(), equalTo((long) size));
|
||||
assertThat(IndexInputUtils.offset(input), equalTo(0L));
|
||||
|
||||
var partialSlice = input.slice("partial slice", 1, size - 1);
|
||||
assertThat(IndexInputUtils.offset(partialSlice), equalTo(1L));
|
||||
var msseg1 = segmentSlice(partialSlice, 0, 24);
|
||||
for (int i = 0; i < 24; i++) {
|
||||
assertThat(msseg1.get(ValueLayout.JAVA_BYTE, i), equalTo((byte) (i + 1)));
|
||||
}
|
||||
|
||||
var fullMSSlice = input.slice("start at full MemorySegment slice", 32, size - 32);
|
||||
var segArray2 = IndexInputUtils.segmentArray(fullMSSlice);
|
||||
assertThat(Arrays.stream(segArray2).mapToLong(MemorySegment::byteSize).sum(), equalTo((long) size - 32));
|
||||
assertThat(IndexInputUtils.offset(fullMSSlice), equalTo(0L));
|
||||
var msseg2 = segmentSlice(fullMSSlice, 0, 32);
|
||||
for (int i = 0; i < 32; i++) {
|
||||
assertThat(msseg2.get(ValueLayout.JAVA_BYTE, i), equalTo((byte) (i + 32)));
|
||||
}
|
||||
|
||||
// slice of a slice
|
||||
var sliceSlice = partialSlice.slice("slice of a slice", 1, partialSlice.length() - 1);
|
||||
var segSliceSliceArray = IndexInputUtils.segmentArray(sliceSlice);
|
||||
assertThat(Arrays.stream(segSliceSliceArray).mapToLong(MemorySegment::byteSize).sum(), equalTo((long) size));
|
||||
assertThat(IndexInputUtils.offset(sliceSlice), equalTo(2L));
|
||||
var msseg3 = segmentSlice(sliceSlice, 0, 28);
|
||||
for (int i = 0; i < 28; i++) {
|
||||
assertThat(msseg3.get(ValueLayout.JAVA_BYTE, i), equalTo((byte) (i + 2)));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static MemorySegment segmentSlice(IndexInput input, long pos, int length) {
|
||||
if (IndexInputUtils.MS_MSINDEX_CLS.isAssignableFrom(input.getClass())) {
|
||||
pos += IndexInputUtils.offset(input);
|
||||
}
|
||||
final int si = (int) (pos >> IndexInputUtils.chunkSizePower(input));
|
||||
final MemorySegment seg = IndexInputUtils.segmentArray(input)[si];
|
||||
long offset = pos & IndexInputUtils.chunkSizeMask(input);
|
||||
if (checkIndex(offset + length, seg.byteSize() + 1)) {
|
||||
return seg.asSlice(offset, length);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static boolean checkIndex(long index, long length) {
|
||||
return index >= 0 && index < length;
|
||||
}
|
||||
|
||||
static final int TIMES = 100; // a loop iteration times
|
||||
|
||||
}
|
|
@ -34,6 +34,10 @@ setup:
|
|||
vector: [230.0, 300.33, -34.8988, 15.555, -200.0]
|
||||
another_vector: [130.0, 115.0, -1.02, 15.555, -100.0]
|
||||
|
||||
# Flush in order to provoke a merge later
|
||||
- do:
|
||||
indices.flush: { }
|
||||
|
||||
- do:
|
||||
index:
|
||||
index: hnsw_byte_quantized
|
||||
|
@ -43,6 +47,10 @@ setup:
|
|||
vector: [-0.5, 100.0, -13, 14.8, -156.0]
|
||||
another_vector: [-0.5, 50.0, -1, 1, 120]
|
||||
|
||||
# Flush in order to provoke a merge later
|
||||
- do:
|
||||
indices.flush: { }
|
||||
|
||||
- do:
|
||||
index:
|
||||
index: hnsw_byte_quantized
|
||||
|
@ -52,6 +60,11 @@ setup:
|
|||
vector: [0.5, 111.3, -13.0, 14.8, -156.0]
|
||||
another_vector: [-0.5, 11.0, 0, 12, 111.0]
|
||||
|
||||
- do:
|
||||
indices.forcemerge:
|
||||
index: hnsw_byte_quantized
|
||||
max_num_segments: 1
|
||||
|
||||
- do:
|
||||
indices.refresh: {}
|
||||
|
||||
|
@ -251,6 +264,10 @@ setup:
|
|||
name: cow.jpg
|
||||
vector: [1, 2, 3, 4, 5]
|
||||
|
||||
# Flush in order to provoke a merge later
|
||||
- do:
|
||||
indices.flush: { }
|
||||
|
||||
- do:
|
||||
index:
|
||||
index: mip
|
||||
|
@ -259,6 +276,10 @@ setup:
|
|||
name: moose.jpg
|
||||
vector: [1, 1, 1, 1, 1]
|
||||
|
||||
# Flush in order to provoke a merge later
|
||||
- do:
|
||||
indices.flush: { }
|
||||
|
||||
- do:
|
||||
index:
|
||||
index: mip
|
||||
|
@ -371,3 +392,149 @@ setup:
|
|||
index: false
|
||||
index_options:
|
||||
type: int8_hnsw
|
||||
---
|
||||
"Test create, merge, and search cosine":
|
||||
- skip:
|
||||
version: ' - 8.11.99'
|
||||
reason: 'kNN float to byte quantization added in 8.12'
|
||||
- do:
|
||||
indices.create:
|
||||
index: hnsw_byte_quantized_merge_cosine
|
||||
|
||||
- do:
|
||||
indices.put_mapping:
|
||||
index: hnsw_byte_quantized_merge_cosine
|
||||
body:
|
||||
properties:
|
||||
embedding:
|
||||
type: dense_vector
|
||||
element_type: float
|
||||
similarity: cosine
|
||||
index_options:
|
||||
type: int8_hnsw
|
||||
|
||||
- do:
|
||||
index:
|
||||
index: hnsw_byte_quantized_merge_cosine
|
||||
id: "1"
|
||||
body:
|
||||
embedding: [1.0, 1.0, 1.0, 1.0]
|
||||
|
||||
# Flush in order to provoke a merge later
|
||||
- do:
|
||||
indices.flush: { }
|
||||
|
||||
- do:
|
||||
index:
|
||||
index: hnsw_byte_quantized_merge_cosine
|
||||
id: "2"
|
||||
body:
|
||||
embedding: [1.0, 1.0, 1.0, 2.0]
|
||||
|
||||
# Flush in order to provoke a merge later
|
||||
- do:
|
||||
indices.flush: { }
|
||||
|
||||
- do:
|
||||
index:
|
||||
index: hnsw_byte_quantized_merge_cosine
|
||||
id: "3"
|
||||
body:
|
||||
embedding: [1.0, 1.0, 1.0, 3.0]
|
||||
|
||||
- do:
|
||||
indices.forcemerge:
|
||||
index: hnsw_byte_quantized_merge_cosine
|
||||
max_num_segments: 1
|
||||
|
||||
- do:
|
||||
indices.refresh: {}
|
||||
|
||||
- do:
|
||||
search:
|
||||
index: hnsw_byte_quantized_merge_cosine
|
||||
body:
|
||||
size: 3
|
||||
query:
|
||||
knn:
|
||||
field: embedding
|
||||
query_vector: [1.0, 1.0, 1.0, 1.0]
|
||||
num_candidates: 10
|
||||
|
||||
- length: { hits.hits: 3 }
|
||||
- match: { hits.hits.0._id: "1"}
|
||||
- match: { hits.hits.1._id: "2"}
|
||||
- match: { hits.hits.2._id: "3"}
|
||||
---
|
||||
"Test create, merge, and search dot_product":
|
||||
- skip:
|
||||
version: ' - 8.11.99'
|
||||
reason: 'kNN float to byte quantization added in 8.12'
|
||||
- do:
|
||||
indices.create:
|
||||
index: hnsw_byte_quantized_merge_dot_product
|
||||
|
||||
- do:
|
||||
indices.put_mapping:
|
||||
index: hnsw_byte_quantized_merge_dot_product
|
||||
body:
|
||||
properties:
|
||||
embedding:
|
||||
type: dense_vector
|
||||
element_type: float
|
||||
similarity: dot_product
|
||||
index_options:
|
||||
type: int8_hnsw
|
||||
|
||||
- do:
|
||||
index:
|
||||
index: hnsw_byte_quantized_merge_dot_product
|
||||
id: "1"
|
||||
body:
|
||||
embedding: [0.6, 0.8]
|
||||
|
||||
# Flush in order to provoke a merge later
|
||||
- do:
|
||||
indices.flush: { }
|
||||
|
||||
- do:
|
||||
index:
|
||||
index: hnsw_byte_quantized_merge_dot_product
|
||||
id: "2"
|
||||
body:
|
||||
embedding: [0.8, 0.6]
|
||||
|
||||
# Flush in order to provoke a merge later
|
||||
- do:
|
||||
indices.flush: { }
|
||||
|
||||
- do:
|
||||
index:
|
||||
index: hnsw_byte_quantized_merge_dot_product
|
||||
id: "3"
|
||||
body:
|
||||
embedding: [-0.6, -0.8]
|
||||
|
||||
- do:
|
||||
indices.forcemerge:
|
||||
index: hnsw_byte_quantized_merge_dot_product
|
||||
max_num_segments: 1
|
||||
|
||||
- do:
|
||||
indices.refresh: {}
|
||||
|
||||
- do:
|
||||
search:
|
||||
index: hnsw_byte_quantized_merge_dot_product
|
||||
body:
|
||||
size: 3
|
||||
query:
|
||||
knn:
|
||||
field: embedding
|
||||
query_vector: [0.6, 0.8]
|
||||
num_candidates: 10
|
||||
|
||||
- length: { hits.hits: 3 }
|
||||
- match: { hits.hits.0._id: "1"}
|
||||
- match: { hits.hits.1._id: "2"}
|
||||
- match: { hits.hits.2._id: "3"}
|
||||
|
|
|
@ -37,6 +37,7 @@ dependencies {
|
|||
api project(":libs:elasticsearch-plugin-analysis-api")
|
||||
api project(':libs:elasticsearch-grok')
|
||||
api project(":libs:elasticsearch-tdigest")
|
||||
implementation project(":libs:elasticsearch-vec")
|
||||
|
||||
implementation project(':libs:elasticsearch-plugin-classloader')
|
||||
// no compile dependency by server, but server defines security policy for this codebase so it i>
|
||||
|
|
|
@ -32,6 +32,7 @@ module org.elasticsearch.server {
|
|||
requires org.elasticsearch.plugin.analysis;
|
||||
requires org.elasticsearch.grok;
|
||||
requires org.elasticsearch.tdigest;
|
||||
requires org.elasticsearch.vec;
|
||||
|
||||
requires com.sun.jna;
|
||||
requires hppc;
|
||||
|
@ -437,7 +438,8 @@ module org.elasticsearch.server {
|
|||
provides org.apache.lucene.codecs.KnnVectorsFormat
|
||||
with
|
||||
org.elasticsearch.index.codec.vectors.ES813FlatVectorFormat,
|
||||
org.elasticsearch.index.codec.vectors.ES813Int8FlatVectorFormat;
|
||||
org.elasticsearch.index.codec.vectors.ES813Int8FlatVectorFormat,
|
||||
org.elasticsearch.index.codec.vectors.ES814HnswScalarQuantizedVectorsFormat;
|
||||
provides org.apache.lucene.codecs.Codec with Elasticsearch814Codec;
|
||||
|
||||
provides org.apache.logging.log4j.core.util.ContextDataProvider with org.elasticsearch.common.logging.DynamicContextDataProvider;
|
||||
|
|
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* 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.index.codec.vectors;
|
||||
|
||||
import org.apache.lucene.codecs.KnnVectorsFormat;
|
||||
import org.apache.lucene.codecs.KnnVectorsReader;
|
||||
import org.apache.lucene.codecs.KnnVectorsWriter;
|
||||
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsReader;
|
||||
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsWriter;
|
||||
import org.apache.lucene.index.SegmentReadState;
|
||||
import org.apache.lucene.index.SegmentWriteState;
|
||||
import org.apache.lucene.search.TaskExecutor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.DEFAULT_BEAM_WIDTH;
|
||||
import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.DEFAULT_MAX_CONN;
|
||||
import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.DEFAULT_NUM_MERGE_WORKER;
|
||||
|
||||
public final class ES814HnswScalarQuantizedVectorsFormat extends KnnVectorsFormat {
|
||||
|
||||
static final String NAME = "ES814HnswScalarQuantizedVectorsFormat";
|
||||
|
||||
static final int MAXIMUM_MAX_CONN = 512;
|
||||
static final int MAXIMUM_BEAM_WIDTH = 3200;
|
||||
|
||||
private final int maxConn;
|
||||
|
||||
private final int beamWidth;
|
||||
|
||||
/** The format for storing, reading, merging vectors on disk */
|
||||
private final ES814ScalarQuantizedVectorsFormat flatVectorsFormat;
|
||||
|
||||
private final int numMergeWorkers;
|
||||
private final TaskExecutor mergeExec;
|
||||
|
||||
public ES814HnswScalarQuantizedVectorsFormat() {
|
||||
this(DEFAULT_MAX_CONN, DEFAULT_BEAM_WIDTH, DEFAULT_NUM_MERGE_WORKER, null, null);
|
||||
}
|
||||
|
||||
public ES814HnswScalarQuantizedVectorsFormat(
|
||||
int maxConn,
|
||||
int beamWidth,
|
||||
int numMergeWorkers,
|
||||
Float confidenceInterval,
|
||||
ExecutorService mergeExec
|
||||
) {
|
||||
super(NAME);
|
||||
if (maxConn <= 0 || maxConn > MAXIMUM_MAX_CONN) {
|
||||
throw new IllegalArgumentException(
|
||||
"maxConn must be positive and less than or equal to " + MAXIMUM_MAX_CONN + "; maxConn=" + maxConn
|
||||
);
|
||||
}
|
||||
if (beamWidth <= 0 || beamWidth > MAXIMUM_BEAM_WIDTH) {
|
||||
throw new IllegalArgumentException(
|
||||
"beamWidth must be positive and less than or equal to " + MAXIMUM_BEAM_WIDTH + "; beamWidth=" + beamWidth
|
||||
);
|
||||
}
|
||||
this.maxConn = maxConn;
|
||||
this.beamWidth = beamWidth;
|
||||
if (numMergeWorkers > 1 && mergeExec == null) {
|
||||
throw new IllegalArgumentException("No executor service passed in when " + numMergeWorkers + " merge workers are requested");
|
||||
}
|
||||
if (numMergeWorkers == 1 && mergeExec != null) {
|
||||
throw new IllegalArgumentException("No executor service is needed as we'll use single thread to merge");
|
||||
}
|
||||
this.numMergeWorkers = numMergeWorkers;
|
||||
if (mergeExec != null) {
|
||||
this.mergeExec = new TaskExecutor(mergeExec);
|
||||
} else {
|
||||
this.mergeExec = null;
|
||||
}
|
||||
this.flatVectorsFormat = new ES814ScalarQuantizedVectorsFormat(confidenceInterval);
|
||||
}
|
||||
|
||||
@Override
|
||||
public KnnVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException {
|
||||
return new Lucene99HnswVectorsWriter(state, maxConn, beamWidth, flatVectorsFormat.fieldsWriter(state), numMergeWorkers, mergeExec);
|
||||
}
|
||||
|
||||
@Override
|
||||
public KnnVectorsReader fieldsReader(SegmentReadState state) throws IOException {
|
||||
return new Lucene99HnswVectorsReader(state, flatVectorsFormat.fieldsReader(state));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxDimensions(String fieldName) {
|
||||
return 1024;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ES814HnswScalarQuantizedVectorsFormat(name=ES814HnswScalarQuantizedVectorsFormat, maxConn="
|
||||
+ maxConn
|
||||
+ ", beamWidth="
|
||||
+ beamWidth
|
||||
+ ", flatVectorFormat="
|
||||
+ flatVectorsFormat
|
||||
+ ")";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
* 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.index.codec.vectors;
|
||||
|
||||
import org.apache.lucene.codecs.FlatVectorsFormat;
|
||||
import org.apache.lucene.codecs.FlatVectorsReader;
|
||||
import org.apache.lucene.codecs.FlatVectorsWriter;
|
||||
import org.apache.lucene.codecs.lucene99.Lucene99FlatVectorsFormat;
|
||||
import org.apache.lucene.codecs.lucene99.Lucene99ScalarQuantizedVectorsReader;
|
||||
import org.apache.lucene.index.ByteVectorValues;
|
||||
import org.apache.lucene.index.FloatVectorValues;
|
||||
import org.apache.lucene.index.SegmentReadState;
|
||||
import org.apache.lucene.index.SegmentWriteState;
|
||||
import org.apache.lucene.util.hnsw.RandomVectorScorer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ES814ScalarQuantizedVectorsFormat extends FlatVectorsFormat {
|
||||
public static final String QUANTIZED_VECTOR_COMPONENT = "QVEC";
|
||||
|
||||
static final String NAME = "ES814ScalarQuantizedVectorsFormat";
|
||||
|
||||
static final int VERSION_START = 0;
|
||||
static final int VERSION_CURRENT = VERSION_START;
|
||||
static final String META_CODEC_NAME = "Lucene99ScalarQuantizedVectorsFormatMeta";
|
||||
static final String VECTOR_DATA_CODEC_NAME = "Lucene99ScalarQuantizedVectorsFormatData";
|
||||
static final String META_EXTENSION = "vemq";
|
||||
static final String VECTOR_DATA_EXTENSION = "veq";
|
||||
|
||||
private static final FlatVectorsFormat rawVectorFormat = new Lucene99FlatVectorsFormat();
|
||||
|
||||
/** The minimum confidence interval */
|
||||
private static final float MINIMUM_CONFIDENCE_INTERVAL = 0.9f;
|
||||
|
||||
/** The maximum confidence interval */
|
||||
private static final float MAXIMUM_CONFIDENCE_INTERVAL = 1f;
|
||||
|
||||
/**
|
||||
* Controls the confidence interval used to scalar quantize the vectors the default value is
|
||||
* calculated as `1-1/(vector_dimensions + 1)`
|
||||
*/
|
||||
public final Float confidenceInterval;
|
||||
|
||||
public ES814ScalarQuantizedVectorsFormat(Float confidenceInterval) {
|
||||
if (confidenceInterval != null
|
||||
&& (confidenceInterval < MINIMUM_CONFIDENCE_INTERVAL || confidenceInterval > MAXIMUM_CONFIDENCE_INTERVAL)) {
|
||||
throw new IllegalArgumentException(
|
||||
"confidenceInterval must be between "
|
||||
+ MINIMUM_CONFIDENCE_INTERVAL
|
||||
+ " and "
|
||||
+ MAXIMUM_CONFIDENCE_INTERVAL
|
||||
+ "; confidenceInterval="
|
||||
+ confidenceInterval
|
||||
);
|
||||
}
|
||||
this.confidenceInterval = confidenceInterval;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return NAME + "(name=" + NAME + ", confidenceInterval=" + confidenceInterval + ", rawVectorFormat=" + rawVectorFormat + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public FlatVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException {
|
||||
return new ES814ScalarQuantizedVectorsWriter(state, confidenceInterval, rawVectorFormat.fieldsWriter(state));
|
||||
}
|
||||
|
||||
@Override
|
||||
public FlatVectorsReader fieldsReader(SegmentReadState state) throws IOException {
|
||||
return new ES814ScalarQuantizedVectorsReader(new Lucene99ScalarQuantizedVectorsReader(state, rawVectorFormat.fieldsReader(state)));
|
||||
}
|
||||
|
||||
static class ES814ScalarQuantizedVectorsReader extends FlatVectorsReader {
|
||||
|
||||
final FlatVectorsReader reader;
|
||||
|
||||
ES814ScalarQuantizedVectorsReader(FlatVectorsReader reader) {
|
||||
this.reader = reader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RandomVectorScorer getRandomVectorScorer(String field, float[] target) throws IOException {
|
||||
return reader.getRandomVectorScorer(field, target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RandomVectorScorer getRandomVectorScorer(String field, byte[] target) throws IOException {
|
||||
return reader.getRandomVectorScorer(field, target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkIntegrity() throws IOException {
|
||||
reader.checkIntegrity();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FloatVectorValues getFloatVectorValues(String field) throws IOException {
|
||||
return reader.getFloatVectorValues(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteVectorValues getByteVectorValues(String field) throws IOException {
|
||||
return reader.getByteVectorValues(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
reader.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long ramBytesUsed() {
|
||||
return reader.ramBytesUsed();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,927 @@
|
|||
/*
|
||||
* @notice
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.index.codec.vectors;
|
||||
|
||||
import org.apache.lucene.codecs.CodecUtil;
|
||||
import org.apache.lucene.codecs.FlatFieldVectorsWriter;
|
||||
import org.apache.lucene.codecs.FlatVectorsWriter;
|
||||
import org.apache.lucene.codecs.KnnFieldVectorsWriter;
|
||||
import org.apache.lucene.codecs.KnnVectorsReader;
|
||||
import org.apache.lucene.codecs.lucene95.OrdToDocDISIReaderConfiguration;
|
||||
import org.apache.lucene.codecs.lucene99.OffHeapQuantizedByteVectorValues;
|
||||
import org.apache.lucene.codecs.perfield.PerFieldKnnVectorsFormat;
|
||||
import org.apache.lucene.index.DocIDMerger;
|
||||
import org.apache.lucene.index.DocsWithFieldSet;
|
||||
import org.apache.lucene.index.FieldInfo;
|
||||
import org.apache.lucene.index.FloatVectorValues;
|
||||
import org.apache.lucene.index.IndexFileNames;
|
||||
import org.apache.lucene.index.MergeState;
|
||||
import org.apache.lucene.index.SegmentWriteState;
|
||||
import org.apache.lucene.index.Sorter;
|
||||
import org.apache.lucene.index.VectorEncoding;
|
||||
import org.apache.lucene.index.VectorSimilarityFunction;
|
||||
import org.apache.lucene.search.DocIdSetIterator;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.IndexInput;
|
||||
import org.apache.lucene.store.IndexOutput;
|
||||
import org.apache.lucene.util.InfoStream;
|
||||
import org.apache.lucene.util.RamUsageEstimator;
|
||||
import org.apache.lucene.util.VectorUtil;
|
||||
import org.apache.lucene.util.hnsw.CloseableRandomVectorScorerSupplier;
|
||||
import org.apache.lucene.util.hnsw.RandomVectorScorer;
|
||||
import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier;
|
||||
import org.apache.lucene.util.quantization.QuantizedByteVectorValues;
|
||||
import org.apache.lucene.util.quantization.QuantizedVectorsReader;
|
||||
import org.apache.lucene.util.quantization.ScalarQuantizedRandomVectorScorerSupplier;
|
||||
import org.apache.lucene.util.quantization.ScalarQuantizer;
|
||||
import org.elasticsearch.core.IOUtils;
|
||||
import org.elasticsearch.core.SuppressForbidden;
|
||||
import org.elasticsearch.vec.VectorScorerFactory;
|
||||
import org.elasticsearch.vec.VectorScorerSupplierAdapter;
|
||||
import org.elasticsearch.vec.VectorSimilarityType;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.apache.lucene.codecs.lucene99.Lucene99ScalarQuantizedVectorsFormat.QUANTIZED_VECTOR_COMPONENT;
|
||||
import static org.apache.lucene.codecs.lucene99.Lucene99ScalarQuantizedVectorsFormat.calculateDefaultConfidenceInterval;
|
||||
import static org.apache.lucene.codecs.lucene99.Lucene99ScalarQuantizedVectorsWriter.mergeAndRecalculateQuantiles;
|
||||
import static org.apache.lucene.codecs.lucene99.Lucene99ScalarQuantizedVectorsWriter.writeQuantizedVectorData;
|
||||
import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
|
||||
import static org.apache.lucene.util.RamUsageEstimator.shallowSizeOfInstance;
|
||||
|
||||
/**
|
||||
* Writes quantized vector values and metadata to index segments.
|
||||
* Amended copy of Lucene99ScalarQuantizedVectorsWriter
|
||||
*/
|
||||
public final class ES814ScalarQuantizedVectorsWriter extends FlatVectorsWriter {
|
||||
|
||||
static final int DIRECT_MONOTONIC_BLOCK_SHIFT = 16;
|
||||
|
||||
private static final long SHALLOW_RAM_BYTES_USED = shallowSizeOfInstance(ES814ScalarQuantizedVectorsWriter.class);
|
||||
|
||||
// Used for determining if a new quantization state requires a re-quantization
|
||||
// for a given segment.
|
||||
// This ensures that in expectation 4/5 of the vector would be unchanged by requantization.
|
||||
// Furthermore, only those values where the value is within 1/5 of the centre of a quantization
|
||||
// bin will be changed. In these cases the error introduced by snapping one way or another
|
||||
// is small compared to the error introduced by quantization in the first place. Furthermore,
|
||||
// empirical testing showed that the relative error by not requantizing is small (compared to
|
||||
// the quantization error) and the condition is sensitive enough to detect all adversarial cases,
|
||||
// such as merging clustered data.
|
||||
private static final float REQUANTIZATION_LIMIT = 0.2f;
|
||||
private final SegmentWriteState segmentWriteState;
|
||||
|
||||
private final List<FieldWriter> fields = new ArrayList<>();
|
||||
private final IndexOutput meta, quantizedVectorData;
|
||||
private final Float confidenceInterval;
|
||||
private final FlatVectorsWriter rawVectorDelegate;
|
||||
private boolean finished;
|
||||
|
||||
ES814ScalarQuantizedVectorsWriter(SegmentWriteState state, Float confidenceInterval, FlatVectorsWriter rawVectorDelegate)
|
||||
throws IOException {
|
||||
this.confidenceInterval = confidenceInterval;
|
||||
segmentWriteState = state;
|
||||
String metaFileName = IndexFileNames.segmentFileName(
|
||||
state.segmentInfo.name,
|
||||
state.segmentSuffix,
|
||||
ES814ScalarQuantizedVectorsFormat.META_EXTENSION
|
||||
);
|
||||
|
||||
String quantizedVectorDataFileName = IndexFileNames.segmentFileName(
|
||||
state.segmentInfo.name,
|
||||
state.segmentSuffix,
|
||||
ES814ScalarQuantizedVectorsFormat.VECTOR_DATA_EXTENSION
|
||||
);
|
||||
this.rawVectorDelegate = rawVectorDelegate;
|
||||
boolean success = false;
|
||||
try {
|
||||
meta = state.directory.createOutput(metaFileName, state.context);
|
||||
quantizedVectorData = state.directory.createOutput(quantizedVectorDataFileName, state.context);
|
||||
|
||||
CodecUtil.writeIndexHeader(
|
||||
meta,
|
||||
ES814ScalarQuantizedVectorsFormat.META_CODEC_NAME,
|
||||
ES814ScalarQuantizedVectorsFormat.VERSION_CURRENT,
|
||||
state.segmentInfo.getId(),
|
||||
state.segmentSuffix
|
||||
);
|
||||
CodecUtil.writeIndexHeader(
|
||||
quantizedVectorData,
|
||||
ES814ScalarQuantizedVectorsFormat.VECTOR_DATA_CODEC_NAME,
|
||||
ES814ScalarQuantizedVectorsFormat.VERSION_CURRENT,
|
||||
state.segmentInfo.getId(),
|
||||
state.segmentSuffix
|
||||
);
|
||||
success = true;
|
||||
} finally {
|
||||
if (success == false) {
|
||||
IOUtils.closeWhileHandlingException(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public FlatFieldVectorsWriter<?> addField(FieldInfo fieldInfo, KnnFieldVectorsWriter<?> indexWriter) throws IOException {
|
||||
if (fieldInfo.getVectorEncoding().equals(VectorEncoding.FLOAT32)) {
|
||||
float confidenceInterval = this.confidenceInterval == null
|
||||
? calculateDefaultConfidenceInterval(fieldInfo.getVectorDimension())
|
||||
: this.confidenceInterval;
|
||||
FieldWriter quantizedWriter = new FieldWriter(confidenceInterval, fieldInfo, segmentWriteState.infoStream, indexWriter);
|
||||
fields.add(quantizedWriter);
|
||||
indexWriter = quantizedWriter;
|
||||
}
|
||||
return rawVectorDelegate.addField(fieldInfo, indexWriter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mergeOneField(FieldInfo fieldInfo, MergeState mergeState) throws IOException {
|
||||
rawVectorDelegate.mergeOneField(fieldInfo, mergeState);
|
||||
// Since we know we will not be searching for additional indexing, we can just write the
|
||||
// the vectors directly to the new segment.
|
||||
// No need to use temporary file as we don't have to re-open for reading
|
||||
if (fieldInfo.getVectorEncoding().equals(VectorEncoding.FLOAT32)) {
|
||||
ScalarQuantizer mergedQuantizationState = mergeQuantiles(fieldInfo, mergeState);
|
||||
MergedQuantizedVectorValues byteVectorValues = MergedQuantizedVectorValues.mergeQuantizedByteVectorValues(
|
||||
fieldInfo,
|
||||
mergeState,
|
||||
mergedQuantizationState
|
||||
);
|
||||
long vectorDataOffset = quantizedVectorData.alignFilePointer(Float.BYTES);
|
||||
DocsWithFieldSet docsWithField = writeQuantizedVectorData(quantizedVectorData, byteVectorValues);
|
||||
long vectorDataLength = quantizedVectorData.getFilePointer() - vectorDataOffset;
|
||||
float confidenceInterval = this.confidenceInterval == null
|
||||
? calculateDefaultConfidenceInterval(fieldInfo.getVectorDimension())
|
||||
: this.confidenceInterval;
|
||||
writeMeta(
|
||||
fieldInfo,
|
||||
segmentWriteState.segmentInfo.maxDoc(),
|
||||
vectorDataOffset,
|
||||
vectorDataLength,
|
||||
confidenceInterval,
|
||||
mergedQuantizationState.getLowerQuantile(),
|
||||
mergedQuantizationState.getUpperQuantile(),
|
||||
docsWithField
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(FieldInfo fieldInfo, MergeState mergeState) throws IOException {
|
||||
if (fieldInfo.getVectorEncoding().equals(VectorEncoding.FLOAT32)) {
|
||||
// Simply merge the underlying delegate, which just copies the raw vector data to a new
|
||||
// segment file
|
||||
rawVectorDelegate.mergeOneField(fieldInfo, mergeState);
|
||||
ScalarQuantizer mergedQuantizationState = mergeQuantiles(fieldInfo, mergeState);
|
||||
return mergeOneFieldToIndex(segmentWriteState, fieldInfo, mergeState, mergedQuantizationState);
|
||||
}
|
||||
// We only merge the delegate, since the field type isn't float32, quantization wasn't
|
||||
// supported, so bypass it.
|
||||
return rawVectorDelegate.mergeOneFieldToIndex(fieldInfo, mergeState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush(int maxDoc, Sorter.DocMap sortMap) throws IOException {
|
||||
rawVectorDelegate.flush(maxDoc, sortMap);
|
||||
for (FieldWriter field : fields) {
|
||||
field.finish();
|
||||
if (sortMap == null) {
|
||||
writeField(field, maxDoc);
|
||||
} else {
|
||||
writeSortingField(field, maxDoc, sortMap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() throws IOException {
|
||||
if (finished) {
|
||||
throw new IllegalStateException("already finished");
|
||||
}
|
||||
finished = true;
|
||||
rawVectorDelegate.finish();
|
||||
if (meta != null) {
|
||||
// write end of fields marker
|
||||
meta.writeInt(-1);
|
||||
CodecUtil.writeFooter(meta);
|
||||
}
|
||||
if (quantizedVectorData != null) {
|
||||
CodecUtil.writeFooter(quantizedVectorData);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long ramBytesUsed() {
|
||||
long total = SHALLOW_RAM_BYTES_USED;
|
||||
for (FieldWriter field : fields) {
|
||||
total += field.ramBytesUsed();
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
private void writeField(FieldWriter fieldData, int maxDoc) throws IOException {
|
||||
// write vector values
|
||||
long vectorDataOffset = quantizedVectorData.alignFilePointer(Float.BYTES);
|
||||
writeQuantizedVectors(fieldData);
|
||||
long vectorDataLength = quantizedVectorData.getFilePointer() - vectorDataOffset;
|
||||
|
||||
writeMeta(
|
||||
fieldData.fieldInfo,
|
||||
maxDoc,
|
||||
vectorDataOffset,
|
||||
vectorDataLength,
|
||||
confidenceInterval,
|
||||
fieldData.minQuantile,
|
||||
fieldData.maxQuantile,
|
||||
fieldData.docsWithField
|
||||
);
|
||||
}
|
||||
|
||||
private void writeMeta(
|
||||
FieldInfo field,
|
||||
int maxDoc,
|
||||
long vectorDataOffset,
|
||||
long vectorDataLength,
|
||||
Float confidenceInterval,
|
||||
Float lowerQuantile,
|
||||
Float upperQuantile,
|
||||
DocsWithFieldSet docsWithField
|
||||
) throws IOException {
|
||||
meta.writeInt(field.number);
|
||||
meta.writeInt(field.getVectorEncoding().ordinal());
|
||||
meta.writeInt(field.getVectorSimilarityFunction().ordinal());
|
||||
meta.writeVLong(vectorDataOffset);
|
||||
meta.writeVLong(vectorDataLength);
|
||||
meta.writeVInt(field.getVectorDimension());
|
||||
int count = docsWithField.cardinality();
|
||||
meta.writeInt(count);
|
||||
if (count > 0) {
|
||||
assert Float.isFinite(lowerQuantile) && Float.isFinite(upperQuantile);
|
||||
meta.writeInt(
|
||||
Float.floatToIntBits(
|
||||
confidenceInterval != null ? confidenceInterval : calculateDefaultConfidenceInterval(field.getVectorDimension())
|
||||
)
|
||||
);
|
||||
meta.writeInt(Float.floatToIntBits(lowerQuantile));
|
||||
meta.writeInt(Float.floatToIntBits(upperQuantile));
|
||||
}
|
||||
// write docIDs
|
||||
OrdToDocDISIReaderConfiguration.writeStoredMeta(
|
||||
DIRECT_MONOTONIC_BLOCK_SHIFT,
|
||||
meta,
|
||||
quantizedVectorData,
|
||||
count,
|
||||
maxDoc,
|
||||
docsWithField
|
||||
);
|
||||
}
|
||||
|
||||
private void writeQuantizedVectors(FieldWriter fieldData) throws IOException {
|
||||
ScalarQuantizer scalarQuantizer = fieldData.createQuantizer();
|
||||
byte[] vector = new byte[fieldData.fieldInfo.getVectorDimension()];
|
||||
final ByteBuffer offsetBuffer = ByteBuffer.allocate(Float.BYTES).order(ByteOrder.LITTLE_ENDIAN);
|
||||
float[] copy = fieldData.normalize ? new float[fieldData.fieldInfo.getVectorDimension()] : null;
|
||||
for (float[] v : fieldData.floatVectors) {
|
||||
if (fieldData.normalize) {
|
||||
System.arraycopy(v, 0, copy, 0, copy.length);
|
||||
VectorUtil.l2normalize(copy);
|
||||
v = copy;
|
||||
}
|
||||
|
||||
float offsetCorrection = scalarQuantizer.quantize(v, vector, fieldData.fieldInfo.getVectorSimilarityFunction());
|
||||
quantizedVectorData.writeBytes(vector, vector.length);
|
||||
offsetBuffer.putFloat(offsetCorrection);
|
||||
quantizedVectorData.writeBytes(offsetBuffer.array(), offsetBuffer.array().length);
|
||||
offsetBuffer.rewind();
|
||||
}
|
||||
}
|
||||
|
||||
private void writeSortingField(FieldWriter fieldData, int maxDoc, Sorter.DocMap sortMap) throws IOException {
|
||||
final int[] docIdOffsets = new int[sortMap.size()];
|
||||
int offset = 1; // 0 means no vector for this (field, document)
|
||||
DocIdSetIterator iterator = fieldData.docsWithField.iterator();
|
||||
for (int docID = iterator.nextDoc(); docID != NO_MORE_DOCS; docID = iterator.nextDoc()) {
|
||||
int newDocID = sortMap.oldToNew(docID);
|
||||
docIdOffsets[newDocID] = offset++;
|
||||
}
|
||||
DocsWithFieldSet newDocsWithField = new DocsWithFieldSet();
|
||||
final int[] ordMap = new int[offset - 1]; // new ord to old ord
|
||||
int ord = 0;
|
||||
int doc = 0;
|
||||
for (int docIdOffset : docIdOffsets) {
|
||||
if (docIdOffset != 0) {
|
||||
ordMap[ord] = docIdOffset - 1;
|
||||
newDocsWithField.add(doc);
|
||||
ord++;
|
||||
}
|
||||
doc++;
|
||||
}
|
||||
|
||||
// write vector values
|
||||
long vectorDataOffset = quantizedVectorData.alignFilePointer(Float.BYTES);
|
||||
writeSortedQuantizedVectors(fieldData, ordMap);
|
||||
long quantizedVectorLength = quantizedVectorData.getFilePointer() - vectorDataOffset;
|
||||
writeMeta(
|
||||
fieldData.fieldInfo,
|
||||
maxDoc,
|
||||
vectorDataOffset,
|
||||
quantizedVectorLength,
|
||||
confidenceInterval,
|
||||
fieldData.minQuantile,
|
||||
fieldData.maxQuantile,
|
||||
newDocsWithField
|
||||
);
|
||||
}
|
||||
|
||||
private void writeSortedQuantizedVectors(FieldWriter fieldData, int[] ordMap) throws IOException {
|
||||
ScalarQuantizer scalarQuantizer = fieldData.createQuantizer();
|
||||
byte[] vector = new byte[fieldData.fieldInfo.getVectorDimension()];
|
||||
final ByteBuffer offsetBuffer = ByteBuffer.allocate(Float.BYTES).order(ByteOrder.LITTLE_ENDIAN);
|
||||
float[] copy = fieldData.normalize ? new float[fieldData.fieldInfo.getVectorDimension()] : null;
|
||||
for (int ordinal : ordMap) {
|
||||
float[] v = fieldData.floatVectors.get(ordinal);
|
||||
if (fieldData.normalize) {
|
||||
System.arraycopy(v, 0, copy, 0, copy.length);
|
||||
VectorUtil.l2normalize(copy);
|
||||
v = copy;
|
||||
}
|
||||
float offsetCorrection = scalarQuantizer.quantize(v, vector, fieldData.fieldInfo.getVectorSimilarityFunction());
|
||||
quantizedVectorData.writeBytes(vector, vector.length);
|
||||
offsetBuffer.putFloat(offsetCorrection);
|
||||
quantizedVectorData.writeBytes(offsetBuffer.array(), offsetBuffer.array().length);
|
||||
offsetBuffer.rewind();
|
||||
}
|
||||
}
|
||||
|
||||
private ScalarQuantizer mergeQuantiles(FieldInfo fieldInfo, MergeState mergeState) throws IOException {
|
||||
assert fieldInfo.getVectorEncoding() == VectorEncoding.FLOAT32;
|
||||
float confidenceInterval = this.confidenceInterval == null
|
||||
? calculateDefaultConfidenceInterval(fieldInfo.getVectorDimension())
|
||||
: this.confidenceInterval;
|
||||
return mergeAndRecalculateQuantiles(mergeState, fieldInfo, confidenceInterval);
|
||||
}
|
||||
|
||||
private ScalarQuantizedCloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
|
||||
SegmentWriteState segmentWriteState,
|
||||
FieldInfo fieldInfo,
|
||||
MergeState mergeState,
|
||||
ScalarQuantizer mergedQuantizationState
|
||||
) throws IOException {
|
||||
long vectorDataOffset = quantizedVectorData.alignFilePointer(Float.BYTES);
|
||||
IndexOutput tempQuantizedVectorData = segmentWriteState.directory.createTempOutput(
|
||||
quantizedVectorData.getName(),
|
||||
"temp",
|
||||
segmentWriteState.context
|
||||
);
|
||||
IndexInput quantizationDataInput = null;
|
||||
boolean success = false;
|
||||
try {
|
||||
MergedQuantizedVectorValues byteVectorValues = MergedQuantizedVectorValues.mergeQuantizedByteVectorValues(
|
||||
fieldInfo,
|
||||
mergeState,
|
||||
mergedQuantizationState
|
||||
);
|
||||
DocsWithFieldSet docsWithField = writeQuantizedVectorData(tempQuantizedVectorData, byteVectorValues);
|
||||
CodecUtil.writeFooter(tempQuantizedVectorData);
|
||||
IOUtils.close(tempQuantizedVectorData);
|
||||
quantizationDataInput = segmentWriteState.directory.openInput(tempQuantizedVectorData.getName(), segmentWriteState.context);
|
||||
quantizedVectorData.copyBytes(quantizationDataInput, quantizationDataInput.length() - CodecUtil.footerLength());
|
||||
long vectorDataLength = quantizedVectorData.getFilePointer() - vectorDataOffset;
|
||||
CodecUtil.retrieveChecksum(quantizationDataInput);
|
||||
float confidenceInterval = this.confidenceInterval == null
|
||||
? calculateDefaultConfidenceInterval(fieldInfo.getVectorDimension())
|
||||
: this.confidenceInterval;
|
||||
writeMeta(
|
||||
fieldInfo,
|
||||
segmentWriteState.segmentInfo.maxDoc(),
|
||||
vectorDataOffset,
|
||||
vectorDataLength,
|
||||
confidenceInterval,
|
||||
mergedQuantizationState.getLowerQuantile(),
|
||||
mergedQuantizationState.getUpperQuantile(),
|
||||
docsWithField
|
||||
);
|
||||
success = true;
|
||||
final IndexInput finalQuantizationDataInput = quantizationDataInput;
|
||||
|
||||
// retrieve a scorer
|
||||
RandomVectorScorerSupplier scorerSupplier = null;
|
||||
Optional<VectorScorerFactory> factory = VectorScorerFactory.instance();
|
||||
if (factory.isPresent()) {
|
||||
var scorer = factory.get()
|
||||
.getScalarQuantizedVectorScorer(
|
||||
byteVectorValues.dimension(),
|
||||
docsWithField.cardinality(),
|
||||
mergedQuantizationState.getConstantMultiplier(),
|
||||
VectorSimilarityType.of(fieldInfo.getVectorSimilarityFunction()),
|
||||
quantizationDataInput
|
||||
)
|
||||
.map(VectorScorerSupplierAdapter::new);
|
||||
if (scorer.isPresent()) {
|
||||
scorerSupplier = scorer.get();
|
||||
}
|
||||
}
|
||||
if (scorerSupplier == null) {
|
||||
scorerSupplier = new ScalarQuantizedRandomVectorScorerSupplier(
|
||||
fieldInfo.getVectorSimilarityFunction(),
|
||||
mergedQuantizationState,
|
||||
new OffHeapQuantizedByteVectorValues.DenseOffHeapVectorValues(
|
||||
fieldInfo.getVectorDimension(),
|
||||
docsWithField.cardinality(),
|
||||
quantizationDataInput
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return new ScalarQuantizedCloseableRandomVectorScorerSupplier(() -> {
|
||||
IOUtils.close(finalQuantizationDataInput);
|
||||
segmentWriteState.directory.deleteFile(tempQuantizedVectorData.getName());
|
||||
}, docsWithField.cardinality(), scorerSupplier);
|
||||
} finally {
|
||||
if (success == false) {
|
||||
IOUtils.closeWhileHandlingException(tempQuantizedVectorData, quantizationDataInput);
|
||||
deleteFilesIgnoringExceptions(segmentWriteState.directory, tempQuantizedVectorData.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressForbidden(reason = "closing using Lucene's variant")
|
||||
private static void deleteFilesIgnoringExceptions(Directory dir, String... files) {
|
||||
org.apache.lucene.util.IOUtils.deleteFilesIgnoringExceptions(dir, files);
|
||||
}
|
||||
|
||||
private static QuantizedVectorsReader getQuantizedKnnVectorsReader(KnnVectorsReader vectorsReader, String fieldName) {
|
||||
if (vectorsReader instanceof PerFieldKnnVectorsFormat.FieldsReader) {
|
||||
vectorsReader = ((PerFieldKnnVectorsFormat.FieldsReader) vectorsReader).getFieldReader(fieldName);
|
||||
}
|
||||
if (vectorsReader instanceof QuantizedVectorsReader) {
|
||||
return (QuantizedVectorsReader) vectorsReader;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the quantiles of the new quantization state are too far from the quantiles of
|
||||
* the existing quantization state. This would imply that floating point values would slightly
|
||||
* shift quantization buckets.
|
||||
*
|
||||
* @param existingQuantiles The existing quantiles for a segment
|
||||
* @param newQuantiles The new quantiles for a segment, could be merged, or fully re-calculated
|
||||
* @return true if the floating point values should be requantized
|
||||
*/
|
||||
static boolean shouldRequantize(ScalarQuantizer existingQuantiles, ScalarQuantizer newQuantiles) {
|
||||
float tol = REQUANTIZATION_LIMIT * (newQuantiles.getUpperQuantile() - newQuantiles.getLowerQuantile()) / 128f;
|
||||
if (Math.abs(existingQuantiles.getUpperQuantile() - newQuantiles.getUpperQuantile()) > tol) {
|
||||
return true;
|
||||
}
|
||||
return Math.abs(existingQuantiles.getLowerQuantile() - newQuantiles.getLowerQuantile()) > tol;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
IOUtils.close(meta, quantizedVectorData, rawVectorDelegate);
|
||||
}
|
||||
|
||||
static class FieldWriter extends FlatFieldVectorsWriter<float[]> {
|
||||
private static final long SHALLOW_SIZE = shallowSizeOfInstance(FieldWriter.class);
|
||||
private final List<float[]> floatVectors;
|
||||
private final FieldInfo fieldInfo;
|
||||
private final float confidenceInterval;
|
||||
private final InfoStream infoStream;
|
||||
private final boolean normalize;
|
||||
private float minQuantile = Float.POSITIVE_INFINITY;
|
||||
private float maxQuantile = Float.NEGATIVE_INFINITY;
|
||||
private boolean finished;
|
||||
private final DocsWithFieldSet docsWithField;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
FieldWriter(float confidenceInterval, FieldInfo fieldInfo, InfoStream infoStream, KnnFieldVectorsWriter<?> indexWriter) {
|
||||
super((KnnFieldVectorsWriter<float[]>) indexWriter);
|
||||
this.confidenceInterval = confidenceInterval;
|
||||
this.fieldInfo = fieldInfo;
|
||||
this.normalize = fieldInfo.getVectorSimilarityFunction() == VectorSimilarityFunction.COSINE;
|
||||
this.floatVectors = new ArrayList<>();
|
||||
this.infoStream = infoStream;
|
||||
this.docsWithField = new DocsWithFieldSet();
|
||||
}
|
||||
|
||||
void finish() throws IOException {
|
||||
if (finished) {
|
||||
return;
|
||||
}
|
||||
if (floatVectors.size() == 0) {
|
||||
finished = true;
|
||||
return;
|
||||
}
|
||||
ScalarQuantizer quantizer = ScalarQuantizer.fromVectors(
|
||||
new FloatVectorWrapper(floatVectors, fieldInfo.getVectorSimilarityFunction() == VectorSimilarityFunction.COSINE),
|
||||
confidenceInterval,
|
||||
floatVectors.size()
|
||||
);
|
||||
minQuantile = quantizer.getLowerQuantile();
|
||||
maxQuantile = quantizer.getUpperQuantile();
|
||||
if (infoStream.isEnabled(QUANTIZED_VECTOR_COMPONENT)) {
|
||||
infoStream.message(
|
||||
QUANTIZED_VECTOR_COMPONENT,
|
||||
"quantized field="
|
||||
+ " confidenceInterval="
|
||||
+ confidenceInterval
|
||||
+ " minQuantile="
|
||||
+ minQuantile
|
||||
+ " maxQuantile="
|
||||
+ maxQuantile
|
||||
);
|
||||
}
|
||||
finished = true;
|
||||
}
|
||||
|
||||
ScalarQuantizer createQuantizer() {
|
||||
assert finished;
|
||||
return new ScalarQuantizer(minQuantile, maxQuantile, confidenceInterval);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long ramBytesUsed() {
|
||||
long size = SHALLOW_SIZE;
|
||||
if (indexingDelegate != null) {
|
||||
size += indexingDelegate.ramBytesUsed();
|
||||
}
|
||||
if (floatVectors.size() == 0) return size;
|
||||
return size + (long) floatVectors.size() * RamUsageEstimator.NUM_BYTES_OBJECT_REF;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addValue(int docID, float[] vectorValue) throws IOException {
|
||||
docsWithField.add(docID);
|
||||
floatVectors.add(vectorValue);
|
||||
if (indexingDelegate != null) {
|
||||
indexingDelegate.addValue(docID, vectorValue);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public float[] copyValue(float[] vectorValue) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
static class FloatVectorWrapper extends FloatVectorValues {
|
||||
private final List<float[]> vectorList;
|
||||
private final float[] copy;
|
||||
private final boolean normalize;
|
||||
protected int curDoc = -1;
|
||||
|
||||
FloatVectorWrapper(List<float[]> vectorList, boolean normalize) {
|
||||
this.vectorList = vectorList;
|
||||
this.copy = new float[vectorList.get(0).length];
|
||||
this.normalize = normalize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int dimension() {
|
||||
return vectorList.get(0).length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return vectorList.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float[] vectorValue() throws IOException {
|
||||
if (curDoc == -1 || curDoc >= vectorList.size()) {
|
||||
throw new IOException("Current doc not set or too many iterations");
|
||||
}
|
||||
if (normalize) {
|
||||
System.arraycopy(vectorList.get(curDoc), 0, copy, 0, copy.length);
|
||||
VectorUtil.l2normalize(copy);
|
||||
return copy;
|
||||
}
|
||||
return vectorList.get(curDoc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int docID() {
|
||||
if (curDoc >= vectorList.size()) {
|
||||
return NO_MORE_DOCS;
|
||||
}
|
||||
return curDoc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextDoc() throws IOException {
|
||||
curDoc++;
|
||||
return docID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int advance(int target) throws IOException {
|
||||
curDoc = target;
|
||||
return docID();
|
||||
}
|
||||
}
|
||||
|
||||
private static class QuantizedByteVectorValueSub extends DocIDMerger.Sub {
|
||||
private final QuantizedByteVectorValues values;
|
||||
|
||||
QuantizedByteVectorValueSub(MergeState.DocMap docMap, QuantizedByteVectorValues values) {
|
||||
super(docMap);
|
||||
this.values = values;
|
||||
assert values.docID() == -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextDoc() throws IOException {
|
||||
return values.nextDoc();
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns a merged view over all the segment's {@link QuantizedByteVectorValues}. */
|
||||
static class MergedQuantizedVectorValues extends QuantizedByteVectorValues {
|
||||
public static MergedQuantizedVectorValues mergeQuantizedByteVectorValues(
|
||||
FieldInfo fieldInfo,
|
||||
MergeState mergeState,
|
||||
ScalarQuantizer scalarQuantizer
|
||||
) throws IOException {
|
||||
assert fieldInfo != null && fieldInfo.hasVectorValues();
|
||||
|
||||
List<QuantizedByteVectorValueSub> subs = new ArrayList<>();
|
||||
for (int i = 0; i < mergeState.knnVectorsReaders.length; i++) {
|
||||
if (mergeState.knnVectorsReaders[i] != null
|
||||
&& mergeState.knnVectorsReaders[i].getFloatVectorValues(fieldInfo.name) != null) {
|
||||
QuantizedVectorsReader reader = getQuantizedKnnVectorsReader(mergeState.knnVectorsReaders[i], fieldInfo.name);
|
||||
assert scalarQuantizer != null;
|
||||
final QuantizedByteVectorValueSub sub;
|
||||
// Either our quantization parameters are way different than the merged ones
|
||||
// Or we have never been quantized.
|
||||
if (reader == null
|
||||
|| reader.getQuantizationState(fieldInfo.name) == null
|
||||
|| shouldRequantize(reader.getQuantizationState(fieldInfo.name), scalarQuantizer)) {
|
||||
sub = new QuantizedByteVectorValueSub(
|
||||
mergeState.docMaps[i],
|
||||
new QuantizedFloatVectorValues(
|
||||
mergeState.knnVectorsReaders[i].getFloatVectorValues(fieldInfo.name),
|
||||
fieldInfo.getVectorSimilarityFunction(),
|
||||
scalarQuantizer
|
||||
)
|
||||
);
|
||||
} else {
|
||||
sub = new QuantizedByteVectorValueSub(
|
||||
mergeState.docMaps[i],
|
||||
new OffsetCorrectedQuantizedByteVectorValues(
|
||||
reader.getQuantizedVectorValues(fieldInfo.name),
|
||||
fieldInfo.getVectorSimilarityFunction(),
|
||||
scalarQuantizer,
|
||||
reader.getQuantizationState(fieldInfo.name)
|
||||
)
|
||||
);
|
||||
}
|
||||
subs.add(sub);
|
||||
}
|
||||
}
|
||||
return new MergedQuantizedVectorValues(subs, mergeState);
|
||||
}
|
||||
|
||||
private final List<QuantizedByteVectorValueSub> subs;
|
||||
private final DocIDMerger<QuantizedByteVectorValueSub> docIdMerger;
|
||||
private final int size;
|
||||
|
||||
private int docId;
|
||||
private QuantizedByteVectorValueSub current;
|
||||
|
||||
private MergedQuantizedVectorValues(List<QuantizedByteVectorValueSub> subs, MergeState mergeState) throws IOException {
|
||||
this.subs = subs;
|
||||
docIdMerger = DocIDMerger.of(subs, mergeState.needsIndexSort);
|
||||
int totalSize = 0;
|
||||
for (QuantizedByteVectorValueSub sub : subs) {
|
||||
totalSize += sub.values.size();
|
||||
}
|
||||
size = totalSize;
|
||||
docId = -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] vectorValue() throws IOException {
|
||||
return current.values.vectorValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int docID() {
|
||||
return docId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextDoc() throws IOException {
|
||||
current = docIdMerger.next();
|
||||
if (current == null) {
|
||||
docId = NO_MORE_DOCS;
|
||||
} else {
|
||||
docId = current.mappedDocID;
|
||||
}
|
||||
return docId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int advance(int target) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int dimension() {
|
||||
return subs.get(0).values.dimension();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getScoreCorrectionConstant() throws IOException {
|
||||
return current.values.getScoreCorrectionConstant();
|
||||
}
|
||||
}
|
||||
|
||||
private static class QuantizedFloatVectorValues extends QuantizedByteVectorValues {
|
||||
private final FloatVectorValues values;
|
||||
private final ScalarQuantizer quantizer;
|
||||
private final byte[] quantizedVector;
|
||||
private final float[] normalizedVector;
|
||||
private float offsetValue = 0f;
|
||||
|
||||
private final VectorSimilarityFunction vectorSimilarityFunction;
|
||||
|
||||
QuantizedFloatVectorValues(FloatVectorValues values, VectorSimilarityFunction vectorSimilarityFunction, ScalarQuantizer quantizer) {
|
||||
this.values = values;
|
||||
this.quantizer = quantizer;
|
||||
this.quantizedVector = new byte[values.dimension()];
|
||||
this.vectorSimilarityFunction = vectorSimilarityFunction;
|
||||
if (vectorSimilarityFunction == VectorSimilarityFunction.COSINE) {
|
||||
this.normalizedVector = new float[values.dimension()];
|
||||
} else {
|
||||
this.normalizedVector = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getScoreCorrectionConstant() {
|
||||
return offsetValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int dimension() {
|
||||
return values.dimension();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return values.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] vectorValue() throws IOException {
|
||||
return quantizedVector;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int docID() {
|
||||
return values.docID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextDoc() throws IOException {
|
||||
int doc = values.nextDoc();
|
||||
if (doc != NO_MORE_DOCS) {
|
||||
quantize();
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int advance(int target) throws IOException {
|
||||
int doc = values.advance(target);
|
||||
if (doc != NO_MORE_DOCS) {
|
||||
quantize();
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
private void quantize() throws IOException {
|
||||
if (vectorSimilarityFunction == VectorSimilarityFunction.COSINE) {
|
||||
System.arraycopy(values.vectorValue(), 0, normalizedVector, 0, normalizedVector.length);
|
||||
VectorUtil.l2normalize(normalizedVector);
|
||||
offsetValue = quantizer.quantize(normalizedVector, quantizedVector, vectorSimilarityFunction);
|
||||
} else {
|
||||
offsetValue = quantizer.quantize(values.vectorValue(), quantizedVector, vectorSimilarityFunction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static final class ScalarQuantizedCloseableRandomVectorScorerSupplier implements CloseableRandomVectorScorerSupplier {
|
||||
|
||||
private final RandomVectorScorerSupplier supplier;
|
||||
private final Closeable onClose;
|
||||
private final int numVectors;
|
||||
|
||||
ScalarQuantizedCloseableRandomVectorScorerSupplier(Closeable onClose, int numVectors, RandomVectorScorerSupplier supplier) {
|
||||
this.onClose = onClose;
|
||||
this.supplier = supplier;
|
||||
this.numVectors = numVectors;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RandomVectorScorer scorer(int ord) throws IOException {
|
||||
return supplier.scorer(ord);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RandomVectorScorerSupplier copy() throws IOException {
|
||||
return supplier.copy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
onClose.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int totalVectorCount() {
|
||||
return numVectors;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class OffsetCorrectedQuantizedByteVectorValues extends QuantizedByteVectorValues {
|
||||
|
||||
private final QuantizedByteVectorValues in;
|
||||
private final VectorSimilarityFunction vectorSimilarityFunction;
|
||||
private final ScalarQuantizer scalarQuantizer, oldScalarQuantizer;
|
||||
|
||||
private OffsetCorrectedQuantizedByteVectorValues(
|
||||
QuantizedByteVectorValues in,
|
||||
VectorSimilarityFunction vectorSimilarityFunction,
|
||||
ScalarQuantizer scalarQuantizer,
|
||||
ScalarQuantizer oldScalarQuantizer
|
||||
) {
|
||||
this.in = in;
|
||||
this.vectorSimilarityFunction = vectorSimilarityFunction;
|
||||
this.scalarQuantizer = scalarQuantizer;
|
||||
this.oldScalarQuantizer = oldScalarQuantizer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getScoreCorrectionConstant() throws IOException {
|
||||
return scalarQuantizer.recalculateCorrectiveOffset(in.vectorValue(), oldScalarQuantizer, vectorSimilarityFunction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int dimension() {
|
||||
return in.dimension();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return in.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] vectorValue() throws IOException {
|
||||
return in.vectorValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int docID() {
|
||||
return in.docID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextDoc() throws IOException {
|
||||
return in.nextDoc();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int advance(int target) throws IOException {
|
||||
return in.advance(target);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,7 +11,6 @@ package org.elasticsearch.index.mapper.vectors;
|
|||
import org.apache.lucene.codecs.KnnVectorsFormat;
|
||||
import org.apache.lucene.codecs.KnnVectorsReader;
|
||||
import org.apache.lucene.codecs.KnnVectorsWriter;
|
||||
import org.apache.lucene.codecs.lucene99.Lucene99HnswScalarQuantizedVectorsFormat;
|
||||
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat;
|
||||
import org.apache.lucene.document.BinaryDocValuesField;
|
||||
import org.apache.lucene.document.Field;
|
||||
|
@ -49,6 +48,7 @@ import org.elasticsearch.index.IndexVersion;
|
|||
import org.elasticsearch.index.IndexVersions;
|
||||
import org.elasticsearch.index.codec.vectors.ES813FlatVectorFormat;
|
||||
import org.elasticsearch.index.codec.vectors.ES813Int8FlatVectorFormat;
|
||||
import org.elasticsearch.index.codec.vectors.ES814HnswScalarQuantizedVectorsFormat;
|
||||
import org.elasticsearch.index.fielddata.FieldDataContext;
|
||||
import org.elasticsearch.index.fielddata.IndexFieldData;
|
||||
import org.elasticsearch.index.mapper.ArraySourceValueFetcher;
|
||||
|
@ -996,7 +996,7 @@ public class DenseVectorFieldMapper extends FieldMapper {
|
|||
|
||||
@Override
|
||||
public KnnVectorsFormat getVectorsFormat() {
|
||||
return new Lucene99HnswScalarQuantizedVectorsFormat(m, efConstruction, 1, confidenceInterval, null);
|
||||
return new ES814HnswScalarQuantizedVectorsFormat(m, efConstruction, 1, confidenceInterval, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
org.elasticsearch.index.codec.vectors.ES813FlatVectorFormat
|
||||
org.elasticsearch.index.codec.vectors.ES813Int8FlatVectorFormat
|
||||
org.elasticsearch.index.codec.vectors.ES814HnswScalarQuantizedVectorsFormat
|
||||
|
|
|
@ -83,6 +83,12 @@ grant codeBase "${codebase.elasticsearch-preallocate}" {
|
|||
permission java.lang.reflect.ReflectPermission "newProxyInPackage.org.elasticsearch.preallocate";
|
||||
};
|
||||
|
||||
grant codeBase "${codebase.elasticsearch-vec}" {
|
||||
// for access MemorySegmentIndexInput internals
|
||||
permission java.lang.RuntimePermission "accessDeclaredMembers";
|
||||
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
|
||||
};
|
||||
|
||||
//// Everything else:
|
||||
|
||||
grant {
|
||||
|
|
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
* 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.index.codec.vectors;
|
||||
|
||||
import org.apache.lucene.codecs.Codec;
|
||||
import org.apache.lucene.codecs.KnnVectorsFormat;
|
||||
import org.apache.lucene.codecs.lucene99.Lucene99Codec;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.KnnFloatVectorField;
|
||||
import org.apache.lucene.index.DirectoryReader;
|
||||
import org.apache.lucene.index.FloatVectorValues;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.IndexWriter;
|
||||
import org.apache.lucene.index.LeafReader;
|
||||
import org.apache.lucene.index.VectorSimilarityFunction;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.MMapDirectory;
|
||||
import org.apache.lucene.tests.index.BaseKnnVectorsFormatTestCase;
|
||||
import org.elasticsearch.common.logging.LogConfigurator;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
|
||||
|
||||
// @com.carrotsearch.randomizedtesting.annotations.Repeat(iterations = 50) // tests.directory sys property?
|
||||
public class ES814HnswScalarQuantizedVectorsFormatTests extends BaseKnnVectorsFormatTestCase {
|
||||
|
||||
static {
|
||||
LogConfigurator.configureESLogging(); // native access requires logging to be initialized
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Codec getCodec() {
|
||||
return new Lucene99Codec() {
|
||||
@Override
|
||||
public KnnVectorsFormat getKnnVectorsFormatForField(String field) {
|
||||
return new ES814HnswScalarQuantizedVectorsFormat();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// The following test scenarios are similar to their superclass namesakes,
|
||||
// but here we ensure that the Directory implementation is a FSDirectory
|
||||
// which helps test the native code vector distance implementation
|
||||
|
||||
public void testAddIndexesDirectory0FS() throws Exception {
|
||||
Path root = createTempDir();
|
||||
String fieldName = "field";
|
||||
Document doc = new Document();
|
||||
doc.add(new KnnFloatVectorField(fieldName, new float[4], VectorSimilarityFunction.DOT_PRODUCT));
|
||||
try (Directory dir = new MMapDirectory(root.resolve("dir1")); Directory dir2 = new MMapDirectory(root.resolve("dir2"))) {
|
||||
try (IndexWriter w = new IndexWriter(dir, newIndexWriterConfig())) {
|
||||
w.addDocument(doc);
|
||||
}
|
||||
try (IndexWriter w2 = new IndexWriter(dir2, newIndexWriterConfig())) {
|
||||
w2.addIndexes(dir);
|
||||
w2.forceMerge(1);
|
||||
try (IndexReader reader = DirectoryReader.open(w2)) {
|
||||
LeafReader r = getOnlyLeafReader(reader);
|
||||
FloatVectorValues vectorValues = r.getFloatVectorValues(fieldName);
|
||||
assertEquals(0, vectorValues.nextDoc());
|
||||
assertEquals(0, vectorValues.vectorValue()[0], 0);
|
||||
assertEquals(NO_MORE_DOCS, vectorValues.nextDoc());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testAddIndexesDirectory01FSCosine() throws Exception {
|
||||
testAddIndexesDirectory01FS(VectorSimilarityFunction.COSINE);
|
||||
}
|
||||
|
||||
public void testAddIndexesDirectory01FSDot() throws Exception {
|
||||
testAddIndexesDirectory01FS(VectorSimilarityFunction.DOT_PRODUCT);
|
||||
}
|
||||
|
||||
public void testAddIndexesDirectory01FSEuclidean() throws Exception {
|
||||
testAddIndexesDirectory01FS(VectorSimilarityFunction.EUCLIDEAN);
|
||||
}
|
||||
|
||||
public void testAddIndexesDirectory01FSMaxIP() throws Exception {
|
||||
testAddIndexesDirectory01FS(VectorSimilarityFunction.MAXIMUM_INNER_PRODUCT);
|
||||
}
|
||||
|
||||
private void testAddIndexesDirectory01FS(VectorSimilarityFunction similarityFunction) throws Exception {
|
||||
Path root = createTempDir();
|
||||
String fieldName = "field";
|
||||
float[] vector = new float[] { 1f };
|
||||
Document doc = new Document();
|
||||
doc.add(new KnnFloatVectorField(fieldName, vector, similarityFunction));
|
||||
try (Directory dir = new MMapDirectory(root.resolve("dir1")); Directory dir2 = new MMapDirectory(root.resolve("dir2"))) {
|
||||
try (IndexWriter w = new IndexWriter(dir, newIndexWriterConfig())) {
|
||||
w.addDocument(doc);
|
||||
}
|
||||
try (IndexWriter w2 = new IndexWriter(dir2, newIndexWriterConfig())) {
|
||||
vector[0] = 2f;
|
||||
w2.addDocument(doc);
|
||||
w2.addIndexes(dir);
|
||||
w2.forceMerge(1);
|
||||
try (IndexReader reader = DirectoryReader.open(w2)) {
|
||||
LeafReader r = getOnlyLeafReader(reader);
|
||||
FloatVectorValues vectorValues = r.getFloatVectorValues(fieldName);
|
||||
assertEquals(0, vectorValues.nextDoc());
|
||||
// The merge order is randomized, we might get 1 first, or 2
|
||||
float value = vectorValues.vectorValue()[0];
|
||||
assertTrue(value == 1 || value == 2);
|
||||
assertEquals(1, vectorValues.nextDoc());
|
||||
value += vectorValues.vectorValue()[0];
|
||||
assertEquals(3f, value, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1148,12 +1148,12 @@ public class DenseVectorFieldMapperTests extends MapperTestCase {
|
|||
Codec codec = codecService.codec("default");
|
||||
assertThat(codec, instanceOf(PerFieldMapperCodec.class));
|
||||
KnnVectorsFormat knnVectorsFormat = ((PerFieldMapperCodec) codec).getKnnVectorsFormatForField("field");
|
||||
String expectedString = "Lucene99HnswScalarQuantizedVectorsFormat(name=Lucene99HnswScalarQuantizedVectorsFormat, maxConn="
|
||||
String expectedString = "ES814HnswScalarQuantizedVectorsFormat(name=ES814HnswScalarQuantizedVectorsFormat, maxConn="
|
||||
+ m
|
||||
+ ", beamWidth="
|
||||
+ efConstruction
|
||||
+ ", flatVectorFormat=Lucene99ScalarQuantizedVectorsFormat("
|
||||
+ "name=Lucene99ScalarQuantizedVectorsFormat, confidenceInterval="
|
||||
+ ", flatVectorFormat=ES814ScalarQuantizedVectorsFormat("
|
||||
+ "name=ES814ScalarQuantizedVectorsFormat, confidenceInterval="
|
||||
+ (setConfidenceInterval ? confidenceInterval : null)
|
||||
+ ", rawVectorFormat=Lucene99FlatVectorsFormat()"
|
||||
+ "))";
|
||||
|
|
|
@ -220,6 +220,7 @@ public class BootstrapForTesting {
|
|||
addClassCodebase(codebases, "elasticsearch-core", "org.elasticsearch.core.Booleans");
|
||||
addClassCodebase(codebases, "elasticsearch-cli", "org.elasticsearch.cli.Command");
|
||||
addClassCodebase(codebases, "elasticsearch-preallocate", "org.elasticsearch.preallocate.Preallocate");
|
||||
addClassCodebase(codebases, "elasticsearch-vec", "org.elasticsearch.vec.VectorScorer");
|
||||
addClassCodebase(codebases, "framework", "org.elasticsearch.test.ESTestCase");
|
||||
return codebases;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue