-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add bfloat16 to bbq_disk and bbq_hnsw #136179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c72eda6
2f8e975
5ef4cf1
69f52cb
2e815fe
9c81e33
90ca761
1de5bd8
e54d4c6
d6491a2
74bb130
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.index.codec.vectors; | ||
|
||
import org.apache.lucene.util.BitUtil; | ||
|
||
import java.nio.ByteOrder; | ||
import java.nio.ShortBuffer; | ||
|
||
public final class BFloat16 { | ||
|
||
public static final int BYTES = Short.BYTES; | ||
|
||
public static short floatToBFloat16(float f) { | ||
// this rounds towards 0 | ||
// zero - zero exp, zero fraction | ||
// denormal - zero exp, non-zero fraction | ||
// infinity - all-1 exp, zero fraction | ||
// NaN - all-1 exp, non-zero fraction | ||
// the Float.NaN constant is 0x7fc0_0000, so this won't turn the most common NaN values into | ||
// infinities | ||
return (short) (Float.floatToIntBits(f) >>> 16); | ||
} | ||
|
||
public static float bFloat16ToFloat(short bf) { | ||
return Float.intBitsToFloat(bf << 16); | ||
} | ||
|
||
public static void floatToBFloat16(float[] floats, ShortBuffer bFloats) { | ||
assert bFloats.remaining() == floats.length; | ||
assert bFloats.order() == ByteOrder.LITTLE_ENDIAN; | ||
for (float v : floats) { | ||
bFloats.put(floatToBFloat16(v)); | ||
} | ||
} | ||
|
||
public static void bFloat16ToFloat(byte[] bfBytes, float[] floats) { | ||
assert floats.length * 2 == bfBytes.length; | ||
for (int i = 0; i < floats.length; i++) { | ||
floats[i] = bFloat16ToFloat((short) BitUtil.VH_LE_SHORT.get(bfBytes, i * 2)); | ||
} | ||
} | ||
|
||
public static void bFloat16ToFloat(ShortBuffer bFloats, float[] floats) { | ||
assert floats.length == bFloats.remaining(); | ||
assert bFloats.order() == ByteOrder.LITTLE_ENDIAN; | ||
for (int i = 0; i < floats.length; i++) { | ||
floats[i] = bFloat16ToFloat(bFloats.get()); | ||
} | ||
} | ||
|
||
private BFloat16() {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* @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. | ||
* | ||
* Modifications copyright (C) 2025 Elasticsearch B.V. | ||
*/ | ||
package org.elasticsearch.index.codec.vectors.es93; | ||
|
||
import org.apache.lucene.codecs.hnsw.FlatVectorsReader; | ||
import org.apache.lucene.codecs.hnsw.FlatVectorsScorer; | ||
import org.apache.lucene.codecs.hnsw.FlatVectorsWriter; | ||
import org.apache.lucene.index.SegmentReadState; | ||
import org.apache.lucene.index.SegmentWriteState; | ||
import org.elasticsearch.index.codec.vectors.DirectIOCapableFlatVectorsFormat; | ||
|
||
import java.io.IOException; | ||
|
||
public final class ES93BFloat16FlatVectorsFormat extends DirectIOCapableFlatVectorsFormat { | ||
|
||
static final String NAME = "ES93BFloat16FlatVectorsFormat"; | ||
static final String META_CODEC_NAME = "ES93BFloat16FlatVectorsFormatMeta"; | ||
static final String VECTOR_DATA_CODEC_NAME = "ES93BFloat16FlatVectorsFormatData"; | ||
static final String META_EXTENSION = "vemf"; | ||
static final String VECTOR_DATA_EXTENSION = "vec"; | ||
|
||
public static final int VERSION_START = 0; | ||
public static final int VERSION_CURRENT = VERSION_START; | ||
|
||
static final int DIRECT_MONOTONIC_BLOCK_SHIFT = 16; | ||
private final FlatVectorsScorer vectorsScorer; | ||
|
||
public ES93BFloat16FlatVectorsFormat(FlatVectorsScorer vectorsScorer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should allow injectable scorers here. We cannot use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use the default vector scorers at the moment, as they work on |
||
super(NAME); | ||
this.vectorsScorer = vectorsScorer; | ||
} | ||
|
||
@Override | ||
protected FlatVectorsScorer flatVectorsScorer() { | ||
return vectorsScorer; | ||
} | ||
|
||
@Override | ||
public FlatVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException { | ||
return new ES93BFloat16FlatVectorsWriter(state, vectorsScorer); | ||
} | ||
|
||
@Override | ||
protected FlatVectorsReader createReader(SegmentReadState state) throws IOException { | ||
return new ES93BFloat16FlatVectorsReader(state, vectorsScorer); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.