summaryrefslogtreecommitdiffstats
path: root/model-integration/src/test/java/ai/vespa/embedding/ColBertEmbedderTest.java
blob: f3682e45efcc7c70a51ccf7aec2c1d464e419051 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.embedding;

import ai.vespa.modelintegration.evaluator.OnnxRuntime;
import com.yahoo.config.ModelReference;
import com.yahoo.embedding.ColBertEmbedderConfig;
import com.yahoo.language.process.Embedder;
import com.yahoo.tensor.IndexedTensor;
import com.yahoo.tensor.MixedTensor;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorAddress;
import com.yahoo.tensor.TensorType;
import org.junit.Test;

import java.util.List;
import java.util.Set;

import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;

public class ColBertEmbedderTest {

    @Test
    public void tesSkipTokens() {
        Set<Long> skipTokens = embedder.getSkipTokens();
        assertTrue(skipTokens.contains(999L));
        assertTrue(skipTokens.contains(1000L));
        assertTrue(skipTokens.contains(1001L));
        assertTrue(skipTokens.contains(1002L));
        assertTrue(skipTokens.contains(1003L));
        assertTrue(skipTokens.contains(1031L));
    }

    @Test
    public void testPacking() {
        assertPackedRight(
                "" +
                        "tensor<float>(d1[6],d2[8]):" +
                        "[" +
                        "[0, 0, 0, 0, 0, 0, 0, 1]," +
                        "[0, 0, 0, 0, 0, 1, 0, 1]," +
                        "[0, 0, 0, 0, 0, 0, 1, 1]," +
                        "[0, 1, 1, 1, 1, 1, 1, 1]," +
                        "[1, 0, 0, 0, 0, 0, 0, 0]," +
                        "[1, 1, 1, 1, 1, 1, 1, 1]" +
                        "]",
                    TensorType.fromSpec("tensor<int8>(dt{},x[1])"),
                "tensor<int8>(dt{},x[1]):{0:1.0, 1:5.0, 2:3.0, 3:127.0, 4:-128.0, 5:-1.0}", 6
        );
        assertPackedRight(
                "" +
                        "tensor<float>(d1[2],d2[16]):" +
                        "[" +
                        "[0, 0, 0, 0, 0, 0, 0, 1,   1, 0, 0, 0, 0, 0, 0, 0]," +
                        "[0, 0, 0, 0, 0, 1, 0, 1,   0, 0, 0, 0, 0, 0, 0, 1]" +
                        "]",
                TensorType.fromSpec("tensor<int8>(dt{},x[2])"),
                "tensor<int8>(dt{},x[2]):{0:[1.0, -128.0], 1:[5.0, 1.0]}",2
        );
    }

    @Test
    public void testEmbedder() {
        assertEmbed("tensor<float>(dt{},x[128])", "this is a document", indexingContext);
        assertEmbed("tensor<int8>(dt{},x[16])", "this is a document", indexingContext);
        assertEmbed("tensor<float>(qt{},x[128])", "this is a query", queryContext);

        assertThrows(IllegalArgumentException.class, () -> {
            //throws because int8 is not supported for query context
            assertEmbed("tensor<int8>(qt{},x[16])", "this is a query", queryContext);
        });
        assertThrows(IllegalArgumentException.class, () -> {
            //throws because 16 is less than model output (128) and we want float
            assertEmbed("tensor<float>(qt{},x[16])", "this is a query", queryContext);
        });

        assertThrows(IllegalArgumentException.class, () -> {
            //throws because 128/8 does not fit into 15
            assertEmbed("tensor<int8>(qt{},x[15])", "this is a query", indexingContext);
        });
    }

    @Test
    public void testInputTensorsWordPiece() {
        //wordPiece tokenizer("this is a query !") -> [2023, 2003, 1037, 23032, 999]
        List<Long> tokens = List.of(2023L, 2003L, 1037L, 23032L, 999L);
        ColBertEmbedder.TransformerInput input = embedder.buildTransformerInput(tokens,10,true);
        assertEquals(10,input.inputIds().size());
        assertEquals(10,input.attentionMask().size());
        assertEquals(List.of(101L, 1L, 2023L, 2003L, 1037L, 23032L, 999L, 102L, 103L, 103L),input.inputIds());
        assertEquals(List.of(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L),input.attentionMask());

        input = embedder.buildTransformerInput(tokens,10,false);
        assertEquals(7,input.inputIds().size());
        assertEquals(7,input.attentionMask().size());
        assertEquals(List.of(101L, 2L, 2023L, 2003L, 1037L, 23032L, 102L),input.inputIds());
        assertEquals(List.of(1L, 1L, 1L, 1L, 1L, 1L, 1L),input.attentionMask());
    }

    @Test
    public void testInputTensorsSentencePiece() {
        //Sentencepiece tokenizer("this is a query !") -> [903, 83, 10, 41, 1294, 711]
        // ! is mapped to 711 and is a punctuation character
        List<Long> tokens = List.of(903L, 83L, 10L, 41L, 1294L, 711L);
        ColBertEmbedder.TransformerInput input = multiLingualEmbedder.buildTransformerInput(tokens,10,true);
        assertEquals(10,input.inputIds().size());
        assertEquals(10,input.attentionMask().size());
        assertEquals(List.of(0L, 3L, 903L, 83L, 10L, 41L, 1294L, 711L, 2L, 250001L),input.inputIds());
        assertEquals(List.of(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L),input.attentionMask());

        //NO padding for document side and 711 (punctuation) is now filtered out
        input = multiLingualEmbedder.buildTransformerInput(tokens,10,false);
        assertEquals(8,input.inputIds().size());
        assertEquals(8,input.attentionMask().size());
        assertEquals(List.of(0L, 4L, 903L, 83L, 10L, 41L, 1294L, 2L),input.inputIds());
        assertEquals(List.of(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),input.attentionMask());

        input = multiLingualEmbedder.buildTransformerInput(List.of(711L), 5, true);
        assertEquals(List.of(0L, 3L, 711L,2L, 250001L),input.inputIds());
        assertEquals(List.of(1L, 1L, 1L, 1L, 0L),input.attentionMask());

        input = multiLingualEmbedder.buildTransformerInput(List.of(711L), 5, false);
        assertEquals(List.of(0L, 4L, 2L),input.inputIds());
        assertEquals(List.of(1L, 1L, 1L),input.attentionMask());
    }

    @Test
    public void testLenghtLimits() {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < 1024; i++) {
            sb.append("annoyance");
            sb.append(" ");
        }
        String text = sb.toString();
        Tensor fullFloat = assertEmbed("tensor<float>(dt{},x[128])", text, indexingContext);
        assertEquals(511*128,fullFloat.size());

        Tensor query = assertEmbed("tensor<float>(dt{},x[128])", text, queryContext);
        assertEquals(32*128,query.size());

        Tensor binaryRep = assertEmbed("tensor<int8>(dt{},x[16])", text, indexingContext);
        assertEquals(511*16,binaryRep.size());

        Tensor shortDoc = assertEmbed("tensor<int8>(dt{},x[16])", "annoyance", indexingContext);
        // 3 tokens, 16 bytes each = 48 bytes
        //CLS [unused1] sequence
        assertEquals(3*16,shortDoc.size());;
    }

    static Tensor assertEmbed(String tensorSpec, String text, Embedder.Context context) {
        TensorType destType = TensorType.fromSpec(tensorSpec);
        Tensor result = embedder.embed(text, context, destType);
        assertEquals(destType,result.type());
        MixedTensor mixedTensor = (MixedTensor) result;
        if(context == queryContext) {
            assertEquals(32*mixedTensor.denseSubspaceSize(),mixedTensor.size());
        }
        return result;
    }

    static void assertPackedRight(String numbers, TensorType destination, String expected, int size) {
        var in = (IndexedTensor) Tensor.from(numbers);
        Tensor packed = ColBertEmbedder.toBitTensor(in, destination, size);
        assertEquals(expected, packed.toString());
        Tensor unpacked = ColBertEmbedder.expandBitTensor(packed);
        assertEquals(in.shape()[1], unpacked.type().indexedSubtype().dimensions().get(0).size().get().longValue());
        for (int dOuter = 0; dOuter < size; dOuter++) {
            for (int dInner = 0; dInner < in.shape()[1]; dInner++) {
                var addr = TensorAddress.of(dOuter, dInner);
                double oldVal = in.get(addr);
                if (oldVal > 0) {
                    assertEquals(unpacked.get(addr), 1.0, 0.0);
                } else {
                    assertEquals(unpacked.get(addr), 0.0, 0.0);
                }
            }
        }
    }

    static final ColBertEmbedder embedder;

    static final ColBertEmbedder multiLingualEmbedder;
    static final Embedder.Context indexingContext;
    static final Embedder.Context queryContext;
    static {
        indexingContext = new Embedder.Context("schema.indexing");
        queryContext = new Embedder.Context("query(qt)");
        embedder = getEmbedder();
        multiLingualEmbedder = getMultiLingualEmbedder();
    }
    private static ColBertEmbedder getEmbedder() {
        String vocabPath = "src/test/models/onnx/transformer/real_tokenizer.json";
        String modelPath = "src/test/models/onnx/transformer/colbert-dummy-v2.onnx";
        assumeTrue(OnnxRuntime.isRuntimeAvailable(modelPath));
        ColBertEmbedderConfig.Builder builder = new ColBertEmbedderConfig.Builder();
        builder.tokenizerPath(ModelReference.valueOf(vocabPath));
        builder.transformerModel(ModelReference.valueOf(modelPath));
        builder.transformerGpuDevice(-1);
        return  new ColBertEmbedder(new OnnxRuntime(), Embedder.Runtime.testInstance(), builder.build());
    }

    private static ColBertEmbedder getMultiLingualEmbedder() {
        String vocabPath = "src/test/models/onnx/transformer/sentence_piece_tokenizer.json";
        String modelPath = "src/test/models/onnx/transformer/colbert-dummy-v2.onnx";
        assumeTrue(OnnxRuntime.isRuntimeAvailable(modelPath));
        ColBertEmbedderConfig.Builder builder = new ColBertEmbedderConfig.Builder();
        builder.tokenizerPath(ModelReference.valueOf(vocabPath));
        builder.transformerModel(ModelReference.valueOf(modelPath));
        builder.transformerGpuDevice(-1);

        builder.transformerStartSequenceToken(0);
        builder.transformerPadToken(1);
        builder.transformerEndSequenceToken(2);
        builder.transformerMaskToken(250001);
        builder.queryTokenId(3);
        builder.documentTokenId(4);

        return  new ColBertEmbedder(new OnnxRuntime(), Embedder.Runtime.testInstance(), builder.build());
    }
}