Skip to content

Commit

Permalink
Update Float16VectorExample.java (#1203) (#1204)
Browse files Browse the repository at this point in the history
milvus-sdk-java has already implemented fp32 <-> fp16/bf16
conversion methods, this PR just updates the related example.

issue: milvus-io/milvus#37448

Signed-off-by: Yinzuo Jiang <jiangyinzuo@foxmail.com>
Signed-off-by: Yinzuo Jiang <yinzuo.jiang@zilliz.com>
  • Loading branch information
jiangyinzuo authored Dec 2, 2024
1 parent f41b2ef commit 355af1b
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions examples/main/java/io/milvus/v2/Float16VectorExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import java.nio.ByteBuffer;
import java.util.*;


public class Float16VectorExample {
private static final String COLLECTION_NAME = "java_sdk_example_float16_vector_v2";
private static final String ID_FIELD = "id";
Expand Down Expand Up @@ -89,8 +88,8 @@ private static void createCollection() {
.build());

List<IndexParam> indexes = new ArrayList<>();
Map<String,Object> extraParams = new HashMap<>();
extraParams.put("nlist",64);
Map<String, Object> extraParams = new HashMap<>();
extraParams.put("nlist", 64);
indexes.add(IndexParam.builder()
.fieldName(FP16_VECTOR_FIELD)
.indexType(IndexParam.IndexType.IVF_FLAT)
Expand Down Expand Up @@ -118,6 +117,8 @@ private static void prepareData(int count) {
for (long i = 0; i < count; i++) {
JsonObject row = new JsonObject();
row.addProperty(ID_FIELD, i);
// The method for converting float32 vector to float16 vector can be found in
// CommonUtils.
ByteBuffer buf1 = CommonUtils.generateFloat16Vector(VECTOR_DIM, false);
row.add(FP16_VECTOR_FIELD, gson.toJsonTree(buf1.array()));
ByteBuffer buf2 = CommonUtils.generateFloat16Vector(VECTOR_DIM, true);
Expand Down Expand Up @@ -164,9 +165,16 @@ private static void searchVectors(List<Long> taargetIDs, List<BaseVector> target
if (!vectorBuf.equals(targetVectors.get(i).getData())) {
throw new RuntimeException("The top1 output vector is incorrect");
}
// The method for converting float16 vector to float32 vector can be found in
// CommonUtils.
List<Float> decodedFpVector = CommonUtils.decodeFloat16Vector(vectorBuf,
BF16_VECTOR_FIELD.equals(vectorFieldName));
if (decodedFpVector.size() != VECTOR_DIM) {
throw new RuntimeException("The decoded vector dimension is incorrect");
}
System.out.println(results.get(0));
}
System.out.println("Search result of float16 vector is correct");
System.out.println("Search result of " + vectorFieldName + " is correct");
}

private static void search() {
Expand Down Expand Up @@ -208,7 +216,6 @@ private static void dropCollection() {
System.out.println("Collection dropped");
}


public static void main(String[] args) {
createCollection();
prepareData(10000);
Expand Down

0 comments on commit 355af1b

Please sign in to comment.