CommonUtils.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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;
  20. import io.milvus.param.R;
  21. import org.tensorflow.ndarray.buffer.ByteDataBuffer;
  22. import org.tensorflow.types.TBfloat16;
  23. import org.tensorflow.types.TFloat16;
  24. import java.nio.ByteBuffer;
  25. import java.util.*;
  26. public class CommonUtils {
  27. public static void handleResponseStatus(R<?> r) {
  28. if (r.getStatus() != R.Status.Success.getCode()) {
  29. throw new RuntimeException(r.getMessage());
  30. }
  31. }
  32. public static List<Float> generateFloatVector(int dimension) {
  33. Random ran = new Random();
  34. List<Float> vector = new ArrayList<>();
  35. for (int i = 0; i < dimension; ++i) {
  36. vector.add(ran.nextFloat());
  37. }
  38. return vector;
  39. }
  40. public static List<Float> generateFloatVector(int dimension, Float initValue) {
  41. List<Float> vector = new ArrayList<>();
  42. for (int i = 0; i < dimension; ++i) {
  43. vector.add(initValue);
  44. }
  45. return vector;
  46. }
  47. public static List<List<Float>> generateFloatVectors(int dimension, int count) {
  48. List<List<Float>> vectors = new ArrayList<>();
  49. for (int n = 0; n < count; ++n) {
  50. List<Float> vector = generateFloatVector(dimension);
  51. vectors.add(vector);
  52. }
  53. return vectors;
  54. }
  55. public static List<List<Float>> generateFixFloatVectors(int dimension, int count) {
  56. List<List<Float>> vectors = new ArrayList<>();
  57. for (int n = 0; n < count; ++n) {
  58. List<Float> vector = generateFloatVector(dimension, (float)n);
  59. vectors.add(vector);
  60. }
  61. return vectors;
  62. }
  63. public static ByteBuffer generateBinaryVector(int dimension) {
  64. Random ran = new Random();
  65. int byteCount = dimension / 8;
  66. ByteBuffer vector = ByteBuffer.allocate(byteCount);
  67. for (int i = 0; i < byteCount; ++i) {
  68. vector.put((byte) ran.nextInt(Byte.MAX_VALUE));
  69. }
  70. return vector;
  71. }
  72. public static List<ByteBuffer> generateBinaryVectors(int dimension, int count) {
  73. List<ByteBuffer> vectors = new ArrayList<>();
  74. for (int n = 0; n < count; ++n) {
  75. ByteBuffer vector = generateBinaryVector(dimension);
  76. vectors.add(vector);
  77. }
  78. return vectors;
  79. }
  80. public static ByteBuffer generateFloat16Vector(int dimension, boolean bfloat16) {
  81. Random ran = new Random();
  82. int byteCount = dimension*2;
  83. ByteBuffer vector = ByteBuffer.allocate(byteCount);
  84. for (int i = 0; i < dimension; ++i) {
  85. ByteDataBuffer bf;
  86. if (bfloat16) {
  87. TFloat16 tt = TFloat16.scalarOf((float)ran.nextInt(dimension));
  88. bf = tt.asRawTensor().data();
  89. } else {
  90. TBfloat16 tt = TBfloat16.scalarOf((float)ran.nextInt(dimension));
  91. bf = tt.asRawTensor().data();
  92. }
  93. vector.put(bf.getByte(0));
  94. vector.put(bf.getByte(1));
  95. }
  96. return vector;
  97. }
  98. public static List<ByteBuffer> generateFloat16Vectors(int dimension, int count, boolean bfloat16) {
  99. List<ByteBuffer> vectors = new ArrayList<>();
  100. for (int n = 0; n < count; ++n) {
  101. ByteBuffer vector = generateFloat16Vector(dimension, bfloat16);
  102. vectors.add(vector);
  103. }
  104. return vectors;
  105. }
  106. public static SortedMap<Long, Float> generateSparseVector() {
  107. Random ran = new Random();
  108. SortedMap<Long, Float> sparse = new TreeMap<>();
  109. int dim = ran.nextInt(10) + 1;
  110. for (int i = 0; i < dim; ++i) {
  111. sparse.put((long)ran.nextInt(1000000), ran.nextFloat());
  112. }
  113. return sparse;
  114. }
  115. public static List<SortedMap<Long, Float>> generateSparseVectors(int count) {
  116. List<SortedMap<Long, Float>> vectors = new ArrayList<>();
  117. for (int n = 0; n < count; ++n) {
  118. SortedMap<Long, Float> sparse = generateSparseVector();
  119. vectors.add(sparse);
  120. }
  121. return vectors;
  122. }
  123. }