|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.inference.embedding; |
| 9 | + |
| 10 | +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
| 11 | + |
| 12 | +import org.elasticsearch.compute.data.Block; |
| 13 | +import org.elasticsearch.compute.data.FloatBlock; |
| 14 | +import org.elasticsearch.compute.data.Page; |
| 15 | +import org.elasticsearch.compute.test.ComputeTestCase; |
| 16 | +import org.elasticsearch.compute.test.RandomBlock; |
| 17 | +import org.elasticsearch.core.Releasables; |
| 18 | +import org.elasticsearch.xpack.core.inference.results.TextEmbeddingBitResults; |
| 19 | +import org.elasticsearch.xpack.core.inference.results.TextEmbeddingByteResults; |
| 20 | +import org.elasticsearch.xpack.core.inference.results.TextEmbeddingFloatResults; |
| 21 | +import org.elasticsearch.xpack.core.inference.results.TextEmbeddingResults; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import static org.hamcrest.Matchers.equalTo; |
| 27 | + |
| 28 | +public class DenseEmbeddingOperatorOutputBuilderTests extends ComputeTestCase { |
| 29 | + |
| 30 | + private final static List<Integer> DIMENSIONS = List.of(1, 32, 128, 512, 2048, 5096); |
| 31 | + private final static List<Integer> INPUT_SIZES = List.of(10, 100, 1_000, 10_000); |
| 32 | + private final static List<Integer> BATCH_SIZES = List.of(1, 10, 100, 1000); |
| 33 | + private final static List<Class<? extends TextEmbeddingResults<?>>> EMBEDDING_TYPES = List.of( |
| 34 | + TextEmbeddingBitResults.class, |
| 35 | + TextEmbeddingByteResults.class, |
| 36 | + TextEmbeddingFloatResults.class |
| 37 | + ); |
| 38 | + |
| 39 | + private final static String TEST_PARAMS_FORMATING = "dims=%d, input_size=%d, batch_size=%d, embedding_type=%s"; |
| 40 | + |
| 41 | + private final int dimensions; |
| 42 | + private final int inputPageSize; |
| 43 | + private final int batchSize; |
| 44 | + private final Class<? extends TextEmbeddingResults<?>> embeddingType; |
| 45 | + |
| 46 | + @ParametersFactory(argumentFormatting = TEST_PARAMS_FORMATING) |
| 47 | + public static Iterable<Object[]> parameters() { |
| 48 | + List<Object[]> params = new ArrayList<>(); |
| 49 | + params.add(new Object[] {}); |
| 50 | + |
| 51 | + for (List<?> axis : List.of(DIMENSIONS, INPUT_SIZES, BATCH_SIZES, EMBEDDING_TYPES)) { |
| 52 | + |
| 53 | + List<Object[]> newParams = new ArrayList<>(); |
| 54 | + for (Object[] combination : params) { |
| 55 | + for (Object element : axis) { |
| 56 | + Object[] newCombination = new Object[combination.length + 1]; |
| 57 | + System.arraycopy(combination, 0, newCombination, 0, combination.length); |
| 58 | + newCombination[newCombination.length - 1] = element; |
| 59 | + newParams.add(newCombination); |
| 60 | + } |
| 61 | + } |
| 62 | + params = newParams; |
| 63 | + } |
| 64 | + |
| 65 | + return params; |
| 66 | + } |
| 67 | + |
| 68 | + public DenseEmbeddingOperatorOutputBuilderTests( |
| 69 | + int dimensions, |
| 70 | + int inputPageSize, |
| 71 | + int batchSize, |
| 72 | + Class<? extends TextEmbeddingBitResults> embeddingType |
| 73 | + ) { |
| 74 | + this.dimensions = dimensions; |
| 75 | + this.inputPageSize = inputPageSize; |
| 76 | + this.batchSize = batchSize; |
| 77 | + this.embeddingType = embeddingType; |
| 78 | + } |
| 79 | + |
| 80 | + public void testOutput() { |
| 81 | + final Page inputPage = randomInputPage(inputPageSize, between(1, 20)); |
| 82 | + try ( |
| 83 | + DenseEmbeddingOperatorOutputBuilder outputBuilder = new DenseEmbeddingOperatorOutputBuilder( |
| 84 | + blockFactory().newFloatBlockBuilder(inputPageSize * dimensions), |
| 85 | + inputPage, |
| 86 | + dimensions |
| 87 | + ) |
| 88 | + ) { |
| 89 | + for (int currentPos = 0; currentPos < inputPage.getPositionCount(); currentPos += batchSize) { |
| 90 | + outputBuilder.addInferenceResponse( |
| 91 | + DenseEmbeddingUtils.inferenceResponse( |
| 92 | + currentPos, |
| 93 | + Math.min(inputPage.getPositionCount() - currentPos, batchSize), |
| 94 | + embeddingType, |
| 95 | + dimensions |
| 96 | + ) |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + final Page outputPage = outputBuilder.buildOutput(); |
| 101 | + try { |
| 102 | + assertThat(outputPage.getPositionCount(), equalTo(inputPage.getPositionCount())); |
| 103 | + assertThat(outputPage.getBlockCount(), equalTo(inputPage.getBlockCount() + 1)); |
| 104 | + assertOutputContent(outputPage.getBlock(inputPage.getBlockCount())); |
| 105 | + } finally { |
| 106 | + outputPage.releaseBlocks(); |
| 107 | + } |
| 108 | + |
| 109 | + } finally { |
| 110 | + inputPage.releaseBlocks(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void assertOutputContent(FloatBlock block) { |
| 115 | + |
| 116 | + for (int currentPos = 0; currentPos < block.getPositionCount(); currentPos++) { |
| 117 | + assertThat(block.isNull(currentPos), equalTo(false)); |
| 118 | + assertThat(block.getValueCount(currentPos), equalTo(dimensions)); |
| 119 | + float[] expectedEmbeddingValues = new float[dimensions]; |
| 120 | + if (embeddingType.equals(TextEmbeddingByteResults.class) || embeddingType.equals(TextEmbeddingBitResults.class)) { |
| 121 | + byte[] bytesValues = embeddingType.equals(TextEmbeddingBitResults.class) |
| 122 | + ? DenseEmbeddingUtils.toBitArray(currentPos, dimensions) |
| 123 | + : DenseEmbeddingUtils.toByteArray(currentPos, dimensions); |
| 124 | + assertThat(bytesValues.length, equalTo(dimensions)); |
| 125 | + for (int i = 0; i < bytesValues.length; i++) { |
| 126 | + expectedEmbeddingValues[i] = bytesValues[i]; |
| 127 | + } |
| 128 | + } else if (embeddingType.equals(TextEmbeddingFloatResults.class)) { |
| 129 | + expectedEmbeddingValues = DenseEmbeddingUtils.toFloatArray(currentPos, dimensions); |
| 130 | + } |
| 131 | + |
| 132 | + for (int valueIndex = 0; valueIndex < block.getValueCount(currentPos); valueIndex++) { |
| 133 | + assertThat(block.getFloat(block.getFirstValueIndex(currentPos) + valueIndex), equalTo(expectedEmbeddingValues[valueIndex])); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private Page randomInputPage(int positionCount, int columnCount) { |
| 139 | + final Block[] blocks = new Block[columnCount]; |
| 140 | + try { |
| 141 | + for (int i = 0; i < columnCount; i++) { |
| 142 | + blocks[i] = RandomBlock.randomBlock( |
| 143 | + blockFactory(), |
| 144 | + RandomBlock.randomElementType(), |
| 145 | + positionCount, |
| 146 | + randomBoolean(), |
| 147 | + 0, |
| 148 | + 0, |
| 149 | + randomInt(10), |
| 150 | + randomInt(10) |
| 151 | + ).block(); |
| 152 | + } |
| 153 | + |
| 154 | + return new Page(blocks); |
| 155 | + } catch (Exception e) { |
| 156 | + Releasables.close(blocks); |
| 157 | + throw (e); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments