forked from milvus-io/milvus-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloat16VectorExample.java
More file actions
232 lines (208 loc) · 9.88 KB
/
Float16VectorExample.java
File metadata and controls
232 lines (208 loc) · 9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* 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 io.milvus.v2;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.milvus.v1.CommonUtils;
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.common.ConsistencyLevel;
import io.milvus.v2.common.DataType;
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.collection.request.AddFieldReq;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
import io.milvus.v2.service.collection.request.DropCollectionReq;
import io.milvus.v2.service.collection.request.HasCollectionReq;
import io.milvus.v2.service.vector.request.InsertReq;
import io.milvus.v2.service.vector.request.QueryReq;
import io.milvus.v2.service.vector.request.SearchReq;
import io.milvus.v2.service.vector.request.data.BFloat16Vec;
import io.milvus.v2.service.vector.request.data.BaseVector;
import io.milvus.v2.service.vector.request.data.Float16Vec;
import io.milvus.v2.service.vector.response.InsertResp;
import io.milvus.v2.service.vector.response.QueryResp;
import io.milvus.v2.service.vector.response.SearchResp;
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";
private static final String FP16_VECTOR_FIELD = "fp16_vector";
private static final String BF16_VECTOR_FIELD = "bf16_vector";
private static final Integer VECTOR_DIM = 128;
private static final MilvusClientV2 client;
static {
client = new MilvusClientV2(ConnectConfig.builder()
.uri("http://localhost:19530")
.build());
}
private static void createCollection() {
// Drop the collection if you don't need the collection anymore
Boolean has = client.hasCollection(HasCollectionReq.builder()
.collectionName(COLLECTION_NAME)
.build());
if (has) {
dropCollection();
}
// Build a collection with two vector fields
CreateCollectionReq.CollectionSchema collectionSchema = CreateCollectionReq.CollectionSchema.builder()
.build();
collectionSchema.addField(AddFieldReq.builder()
.fieldName(ID_FIELD)
.dataType(DataType.Int64)
.isPrimaryKey(Boolean.TRUE)
.build());
collectionSchema.addField(AddFieldReq.builder()
.fieldName(FP16_VECTOR_FIELD)
.dataType(io.milvus.v2.common.DataType.Float16Vector)
.dimension(VECTOR_DIM)
.build());
collectionSchema.addField(AddFieldReq.builder()
.fieldName(BF16_VECTOR_FIELD)
.dataType(io.milvus.v2.common.DataType.BFloat16Vector)
.dimension(VECTOR_DIM)
.build());
List<IndexParam> indexes = new ArrayList<>();
Map<String, Object> extraParams = new HashMap<>();
extraParams.put("nlist", 64);
indexes.add(IndexParam.builder()
.fieldName(FP16_VECTOR_FIELD)
.indexType(IndexParam.IndexType.IVF_FLAT)
.metricType(IndexParam.MetricType.COSINE)
.extraParams(extraParams)
.build());
indexes.add(IndexParam.builder()
.fieldName(BF16_VECTOR_FIELD)
.indexType(IndexParam.IndexType.FLAT)
.metricType(IndexParam.MetricType.COSINE)
.build());
CreateCollectionReq requestCreate = CreateCollectionReq.builder()
.collectionName(COLLECTION_NAME)
.collectionSchema(collectionSchema)
.indexParams(indexes)
.consistencyLevel(ConsistencyLevel.BOUNDED)
.build();
client.createCollection(requestCreate);
}
private static void prepareData(int count) {
List<JsonObject> rows = new ArrayList<>();
Gson gson = new Gson();
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);
row.add(BF16_VECTOR_FIELD, gson.toJsonTree(buf2.array()));
rows.add(row);
}
InsertResp insertResp = client.insert(InsertReq.builder()
.collectionName(COLLECTION_NAME)
.data(rows)
.build());
System.out.println(insertResp.getInsertCnt() + " rows inserted");
}
private static void searchVectors(List<Long> taargetIDs, List<BaseVector> targetVectors, String vectorFieldName) {
int topK = 5;
SearchResp searchResp = client.search(SearchReq.builder()
.collectionName(COLLECTION_NAME)
.data(targetVectors)
.annsField(vectorFieldName)
.topK(topK)
.outputFields(Collections.singletonList(vectorFieldName))
.consistencyLevel(ConsistencyLevel.BOUNDED)
.build());
List<List<SearchResp.SearchResult>> searchResults = searchResp.getSearchResults();
if (searchResults.isEmpty()) {
throw new RuntimeException("The search result is empty");
}
for (int i = 0; i < searchResults.size(); i++) {
List<SearchResp.SearchResult> results = searchResults.get(i);
if (results.size() != topK) {
throw new RuntimeException(String.format("The search result should contains top%d items", topK));
}
SearchResp.SearchResult topResult = results.get(0);
long id = (long) topResult.getId();
if (id != taargetIDs.get(i)) {
throw new RuntimeException("The top1 id is incorrect");
}
Map<String, Object> entity = topResult.getEntity();
ByteBuffer vectorBuf = (ByteBuffer) entity.get(vectorFieldName);
ByteBuffer targetVectorBuf = (ByteBuffer)targetVectors.get(i).getData();
if (!vectorBuf.equals(targetVectorBuf)) {
throw new RuntimeException("The top1 output vector is incorrect");
}
List<Float> decodedTargetVector = CommonUtils.decodeFloat16Vector(targetVectorBuf,
BF16_VECTOR_FIELD.equals(vectorFieldName));
// 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("\nTarget vector: " + decodedTargetVector);
System.out.println("Top0 result: " + topResult);
System.out.println("Top0 result vector: " + decodedFpVector);
}
System.out.println("Search result of " + vectorFieldName + " is correct");
}
private static void search() {
// Retrieve some rows for search
List<Long> targetIDs = Arrays.asList(999L, 2024L);
QueryResp queryResp = client.query(QueryReq.builder()
.collectionName(COLLECTION_NAME)
.filter(ID_FIELD + " in " + targetIDs)
.outputFields(Arrays.asList(FP16_VECTOR_FIELD, BF16_VECTOR_FIELD))
.consistencyLevel(ConsistencyLevel.STRONG)
.build());
List<QueryResp.QueryResult> queryResults = queryResp.getQueryResults();
if (queryResults.isEmpty()) {
throw new RuntimeException("The query result is empty");
}
List<BaseVector> targetFP16Vectors = new ArrayList<>();
List<BaseVector> targetBF16Vectors = new ArrayList<>();
for (QueryResp.QueryResult queryResult : queryResults) {
Map<String, Object> entity = queryResult.getEntity();
ByteBuffer f16VectorBuf = (ByteBuffer) entity.get(FP16_VECTOR_FIELD);
targetFP16Vectors.add(new Float16Vec(f16VectorBuf));
ByteBuffer bf16VectorBuf = (ByteBuffer) entity.get(BF16_VECTOR_FIELD);
targetBF16Vectors.add(new BFloat16Vec(bf16VectorBuf));
}
// Search float16 vector
searchVectors(targetIDs, targetFP16Vectors, FP16_VECTOR_FIELD);
// Search bfloat16 vector
searchVectors(targetIDs, targetBF16Vectors, BF16_VECTOR_FIELD);
}
private static void dropCollection() {
client.dropCollection(DropCollectionReq.builder()
.collectionName(COLLECTION_NAME)
.build());
System.out.println("Collection dropped");
}
public static void main(String[] args) {
createCollection();
prepareData(10000);
search();
dropCollection();
client.close();
}
}