CommonUtils.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package io.milvus.v1;
  20. import io.milvus.common.utils.Float16Utils;
  21. import io.milvus.param.R;
  22. import org.tensorflow.Tensor;
  23. import org.tensorflow.ndarray.Shape;
  24. import org.tensorflow.ndarray.buffer.ByteDataBuffer;
  25. import org.tensorflow.ndarray.buffer.DataBuffers;
  26. import org.tensorflow.types.TBfloat16;
  27. import org.tensorflow.types.TFloat16;
  28. import java.nio.ByteBuffer;
  29. import java.util.*;
  30. public class CommonUtils {
  31. public static void handleResponseStatus(R<?> r) {
  32. if (r.getStatus() != R.Status.Success.getCode()) {
  33. throw new RuntimeException(r.getMessage());
  34. }
  35. }
  36. public static List<Float> generateFloatVector(int dimension) {
  37. Random ran = new Random();
  38. List<Float> vector = new ArrayList<>();
  39. for (int i = 0; i < dimension; ++i) {
  40. vector.add(ran.nextFloat());
  41. }
  42. return vector;
  43. }
  44. public static List<Float> generateFloatVector(int dimension, Float initValue) {
  45. List<Float> vector = new ArrayList<>();
  46. for (int i = 0; i < dimension; ++i) {
  47. vector.add(initValue);
  48. }
  49. return vector;
  50. }
  51. public static List<List<Float>> generateFloatVectors(int dimension, int count) {
  52. List<List<Float>> vectors = new ArrayList<>();
  53. for (int n = 0; n < count; ++n) {
  54. List<Float> vector = generateFloatVector(dimension);
  55. vectors.add(vector);
  56. }
  57. return vectors;
  58. }
  59. public static List<List<Float>> generateFixFloatVectors(int dimension, int count) {
  60. List<List<Float>> vectors = new ArrayList<>();
  61. for (int n = 0; n < count; ++n) {
  62. List<Float> vector = generateFloatVector(dimension, (float)n);
  63. vectors.add(vector);
  64. }
  65. return vectors;
  66. }
  67. /////////////////////////////////////////////////////////////////////////////////////////////////////
  68. public static ByteBuffer generateBinaryVector(int dimension) {
  69. Random ran = new Random();
  70. int byteCount = dimension / 8;
  71. // binary vector doesn't care endian since each byte is independent
  72. ByteBuffer vector = ByteBuffer.allocate(byteCount);
  73. for (int i = 0; i < byteCount; ++i) {
  74. vector.put((byte) ran.nextInt(Byte.MAX_VALUE));
  75. }
  76. return vector;
  77. }
  78. public static List<ByteBuffer> generateBinaryVectors(int dimension, int count) {
  79. List<ByteBuffer> vectors = new ArrayList<>();
  80. for (int n = 0; n < count; ++n) {
  81. ByteBuffer vector = generateBinaryVector(dimension);
  82. vectors.add(vector);
  83. }
  84. return vectors;
  85. }
  86. /////////////////////////////////////////////////////////////////////////////////////////////////////
  87. public static TBfloat16 genTensorflowBF16Vector(int dimension) {
  88. Random ran = new Random();
  89. float[] array = new float[dimension];
  90. for (int n = 0; n < dimension; ++n) {
  91. array[n] = ran.nextFloat();
  92. }
  93. return TBfloat16.vectorOf(array);
  94. }
  95. public static List<TBfloat16> genTensorflowBF16Vectors(int dimension, int count) {
  96. List<TBfloat16> vectors = new ArrayList<>();
  97. for (int n = 0; n < count; ++n) {
  98. TBfloat16 vector = genTensorflowBF16Vector(dimension);
  99. vectors.add(vector);
  100. }
  101. return vectors;
  102. }
  103. public static ByteBuffer encodeTensorBF16Vector(TBfloat16 vector) {
  104. ByteDataBuffer tensorBuf = vector.asRawTensor().data();
  105. ByteBuffer buf = ByteBuffer.allocate((int)tensorBuf.size());
  106. for (long i = 0; i < tensorBuf.size(); i++) {
  107. buf.put(tensorBuf.getByte(i));
  108. }
  109. return buf;
  110. }
  111. public static List<ByteBuffer> encodeTensorBF16Vectors(List<TBfloat16> vectors) {
  112. List<ByteBuffer> buffers = new ArrayList<>();
  113. for (TBfloat16 tf : vectors) {
  114. ByteBuffer bf = encodeTensorBF16Vector(tf);
  115. buffers.add(bf);
  116. }
  117. return buffers;
  118. }
  119. public static TBfloat16 decodeTensorBF16Vector(ByteBuffer buf) {
  120. if (buf.limit()%2 != 0) {
  121. return null;
  122. }
  123. int dim = buf.limit()/2;
  124. ByteDataBuffer bf = DataBuffers.of(buf.array());
  125. return Tensor.of(TBfloat16.class, Shape.of(dim), bf);
  126. }
  127. public static TFloat16 genTensorflowFP16Vector(int dimension) {
  128. Random ran = new Random();
  129. float[] array = new float[dimension];
  130. for (int n = 0; n < dimension; ++n) {
  131. array[n] = ran.nextFloat();
  132. }
  133. return TFloat16.vectorOf(array);
  134. }
  135. public static List<TFloat16> genTensorflowFP16Vectors(int dimension, int count) {
  136. List<TFloat16> vectors = new ArrayList<>();
  137. for (int n = 0; n < count; ++n) {
  138. TFloat16 vector = genTensorflowFP16Vector(dimension);
  139. vectors.add(vector);
  140. }
  141. return vectors;
  142. }
  143. public static ByteBuffer encodeTensorFP16Vector(TFloat16 vector) {
  144. ByteDataBuffer tensorBuf = vector.asRawTensor().data();
  145. ByteBuffer buf = ByteBuffer.allocate((int)tensorBuf.size());
  146. for (long i = 0; i < tensorBuf.size(); i++) {
  147. buf.put(tensorBuf.getByte(i));
  148. }
  149. return buf;
  150. }
  151. public static List<ByteBuffer> encodeTensorFP16Vectors(List<TFloat16> vectors) {
  152. List<ByteBuffer> buffers = new ArrayList<>();
  153. for (TFloat16 tf : vectors) {
  154. ByteBuffer bf = encodeTensorFP16Vector(tf);
  155. buffers.add(bf);
  156. }
  157. return buffers;
  158. }
  159. public static TFloat16 decodeTensorFP16Vector(ByteBuffer buf) {
  160. if (buf.limit()%2 != 0) {
  161. return null;
  162. }
  163. int dim = buf.limit()/2;
  164. ByteDataBuffer bf = DataBuffers.of(buf.array());
  165. return Tensor.of(TFloat16.class, Shape.of(dim), bf);
  166. }
  167. /////////////////////////////////////////////////////////////////////////////////////////////////////
  168. public static ByteBuffer encodeFloat16Vector(List<Float> originVector, boolean bfloat16) {
  169. if (bfloat16) {
  170. return Float16Utils.f32VectorToBf16Buffer(originVector);
  171. } else {
  172. return Float16Utils.f32VectorToFp16Buffer(originVector);
  173. }
  174. }
  175. public static List<Float> decodeFloat16Vector(ByteBuffer buf, boolean bfloat16) {
  176. if (bfloat16) {
  177. return Float16Utils.bf16BufferToVector(buf);
  178. } else {
  179. return Float16Utils.fp16BufferToVector(buf);
  180. }
  181. }
  182. public static List<ByteBuffer> encodeFloat16Vectors(List<List<Float>> originVectors, boolean bfloat16) {
  183. List<ByteBuffer> vectors = new ArrayList<>();
  184. for (List<Float> originVector : originVectors) {
  185. if (bfloat16) {
  186. vectors.add(Float16Utils.f32VectorToBf16Buffer(originVector));
  187. } else {
  188. vectors.add(Float16Utils.f32VectorToFp16Buffer(originVector));
  189. }
  190. }
  191. return vectors;
  192. }
  193. public static ByteBuffer generateFloat16Vector(int dimension, boolean bfloat16) {
  194. List<Float> originalVector = generateFloatVector(dimension);
  195. return encodeFloat16Vector(originalVector, bfloat16);
  196. }
  197. public static List<ByteBuffer> generateFloat16Vectors(int dimension, int count, boolean bfloat16) {
  198. List<ByteBuffer> vectors = new ArrayList<>();
  199. for (int i = 0; i < count; i++) {
  200. ByteBuffer buf = generateFloat16Vector(dimension, bfloat16);
  201. vectors.add((buf));
  202. }
  203. return vectors;
  204. }
  205. /////////////////////////////////////////////////////////////////////////////////////////////////////
  206. public static SortedMap<Long, Float> generateSparseVector() {
  207. Random ran = new Random();
  208. SortedMap<Long, Float> sparse = new TreeMap<>();
  209. int dim = ran.nextInt(10) + 10;
  210. for (int i = 0; i < dim; ++i) {
  211. sparse.put((long)ran.nextInt(1000000), ran.nextFloat());
  212. }
  213. return sparse;
  214. }
  215. public static List<SortedMap<Long, Float>> generateSparseVectors(int count) {
  216. List<SortedMap<Long, Float>> vectors = new ArrayList<>();
  217. for (int n = 0; n < count; ++n) {
  218. SortedMap<Long, Float> sparse = generateSparseVector();
  219. vectors.add(sparse);
  220. }
  221. return vectors;
  222. }
  223. }