summaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/container/xml/EmbedderTestCase.java
blob: 05f848777d480b80d56018d3e3dc9df9a064822a (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.xml;

import com.yahoo.component.ComponentId;
import com.yahoo.config.model.application.provider.FilesApplicationPackage;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.deploy.TestProperties;
import com.yahoo.path.Path;
import com.yahoo.text.XML;
import com.yahoo.vespa.config.ConfigDefinitionKey;
import com.yahoo.vespa.config.ConfigPayloadBuilder;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.container.ApplicationContainerCluster;
import com.yahoo.vespa.model.container.component.Component;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.xml.sax.SAXException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

public class EmbedderTestCase {

    private static final String emptyPathFileName = "services.xml";
    private static final String BUNDLED_EMBEDDER_CLASS = "ai.vespa.embedding.BertBaseEmbedder";
    private static final String BUBNDLED_EMBEDDER_CONFIG = "embedding.bert-base-embedder";

    @Test
    void testApplicationEmbedder() throws IOException, SAXException {
        String embedder = "<embedder id='test' class='ai.vespa.test' bundle='bundle' def='def.name'>" +
                          "  <val>123</val>" +
                          "</embedder>";
        String component = "<component id='test' class='ai.vespa.test' bundle='bundle'>" +
                           "  <config name='def.name'>" +
                           "    <val>123</val>" +
                           "  </config>" +
                           "</component>";
        assertTransform(embedder, component);
    }

    @Test
    void testApplicationEmbedderWithoutConfig() throws IOException, SAXException {
        String embedder = "<embedder id='test' class='ai.vespa.test' bundle='bundle'>" +
                          "</embedder>";
        String component = "<component id='test' class='ai.vespa.test' bundle='bundle'>" +
                           "</component>";
        assertTransform(embedder, component);
    }

    @Test
    void testApplicationEmbedderWithoutConfigCannotSetConfig() throws IOException, SAXException {
        String embedder = "<embedder id='test' class='ai.vespa.test' bundle='bundle'>" +
                          "    <val>123</val>" +
                          "</embedder>";
        assertTransformThrows(embedder, "Embedder 'test' does not specify a 'def' parameter so it cannot contain config values", false);
    }

    @Test
    void testBundledEmbedder_selfhosted() throws IOException, SAXException {
        String embedder = "<embedder id='test' class='" + BUNDLED_EMBEDDER_CLASS + "'>" +
                          "  <transformerModel id='my_model_id' url='my-model-url' />" +
                          "  <tokenizerVocab id='my_vocab_id' url='my-vocab-url' />" +
                          "</embedder>";
        String component = "<component id='test' class='" + BUNDLED_EMBEDDER_CLASS + "' bundle='model-integration'>" +
                           "  <config name='" + BUBNDLED_EMBEDDER_CONFIG + "'>" +
                           "      <transformerModelUrl>my-model-url</transformerModelUrl>" +
                           "      <transformerModelPath>services.xml</transformerModelPath>" +
                           "      <tokenizerVocabUrl>my-vocab-url</tokenizerVocabUrl>" +
                           "      <tokenizerVocabPath>services.xml</tokenizerVocabPath>" +
                           "  </config>" +
                           "</component>";
        assertTransform(embedder, component, false);
    }

    @Test
    void testPathHasPriority_selfhosted() throws IOException, SAXException {
        String embedder = "<embedder id='test' class='" + BUNDLED_EMBEDDER_CLASS + "'>" +
                          "  <transformerModel id='my_model_id' url='my-model-url' path='files/model.onnx' />" +
                          "  <tokenizerVocab id='my_vocab_id' url='my-vocab-url' path='files/vocab.txt' />" +
                          "</embedder>";
        String component = "<component id='test' class='" + BUNDLED_EMBEDDER_CLASS + "' bundle='model-integration'>" +
                           "  <config name='" + BUBNDLED_EMBEDDER_CONFIG + "'>" +
                           "      <transformerModelUrl></transformerModelUrl>" +
                           "      <transformerModelPath>files/model.onnx</transformerModelPath>" +
                           "      <tokenizerVocabUrl></tokenizerVocabUrl>" +
                           "      <tokenizerVocabPath>files/vocab.txt</tokenizerVocabPath>" +
                           "  </config>" +
                           "</component>";
        assertTransform(embedder, component, false);
    }

    @Test
    void testBundledEmbedder_hosted() throws IOException, SAXException {
        String embedder = "<embedder id='test' class='" + BUNDLED_EMBEDDER_CLASS + "'>" +
                          "  <transformerModel id='minilm-l6-v2' />" +
                          "  <tokenizerVocab id='bert-base-uncased' />" +
                          "</embedder>";
        String component = "<component id='test' class='" + BUNDLED_EMBEDDER_CLASS + "' bundle='model-integration'>" +
                           "  <config name='" + BUBNDLED_EMBEDDER_CONFIG + "'>" +
                           "      <transformerModelUrl>https://data.vespa.oath.cloud/onnx_models/sentence_all_MiniLM_L6_v2.onnx</transformerModelUrl>" +
                           "      <transformerModelPath>services.xml</transformerModelPath>" +
                           "      <tokenizerVocabUrl>https://data.vespa.oath.cloud/onnx_models/bert-base-uncased-vocab.txt</tokenizerVocabUrl>" +
                           "      <tokenizerVocabPath>services.xml</tokenizerVocabPath>" +
                           "  </config>" +
                           "</component>";
        assertTransform(embedder, component, true);
    }

    @Test
    void testApplicationEmbedderWithBundledConfig_hosted() throws IOException, SAXException {
        String embedder = "<embedder id='test' class='ApplicationSpecificEmbedder' def='" + BUBNDLED_EMBEDDER_CONFIG + "'>" +
                          "  <transformerModel id='minilm-l6-v2' />" +
                          "  <tokenizerVocab id='bert-base-uncased' />" +
                          "</embedder>";
        String component = "<component id='test' class='ApplicationSpecificEmbedder' bundle='model-integration'>" +
                           "  <config name='" + BUBNDLED_EMBEDDER_CONFIG + "'>" +
                           "      <transformerModelUrl>https://data.vespa.oath.cloud/onnx_models/sentence_all_MiniLM_L6_v2.onnx</transformerModelUrl>" +
                           "      <transformerModelPath>services.xml</transformerModelPath>" +
                           "      <tokenizerVocabUrl>https://data.vespa.oath.cloud/onnx_models/bert-base-uncased-vocab.txt</tokenizerVocabUrl>" +
                           "      <tokenizerVocabPath>services.xml</tokenizerVocabPath>" +
                           "  </config>" +
                           "</component>";
        assertTransform(embedder, component, true);
    }

    @Test
    void testUnknownModelId_hosted() throws IOException, SAXException {
        String embedder = "<embedder id='test' class='" + BUNDLED_EMBEDDER_CLASS + "'>" +
                          "  <transformerModel id='my_model_id' />" +
                          "  <tokenizerVocab id='my_vocab_id' />" +
                          "</embedder>";
        assertTransformThrows(embedder,
                              "Unknown embedder model 'my_model_id'. " +
                              "Available models are [bert-base-uncased, minilm-l6-v2]",
                              true);
    }

    @Test
    void testApplicationPackageWithEmbedder_selfhosted() throws Exception  {
        Path applicationDir = Path.fromString("src/test/cfg/application/embed/");
        VespaModel model = loadModel(applicationDir, false);
        ApplicationContainerCluster containerCluster = model.getContainerClusters().get("container");

        Component<?, ?> transformer = containerCluster.getComponentsMap().get(new ComponentId("transformer"));
        ConfigPayloadBuilder config = transformer.getUserConfigs().get(new ConfigDefinitionKey("bert-base-embedder", "embedding"));
        assertEquals("application-url", config.getObject("transformerModelUrl").getValue());
        assertEquals(emptyPathFileName, config.getObject("transformerModelPath").getValue());
        assertEquals("", config.getObject("tokenizerVocabUrl").getValue());
        assertEquals("files/vocab.txt", config.getObject("tokenizerVocabPath").getValue());
        assertEquals("4", config.getObject("onnxIntraOpThreads").getValue());
    }

    @Test
    void testApplicationPackageWithEmbedder_hosted() throws Exception  {
        Path applicationDir = Path.fromString("src/test/cfg/application/embed/");
        VespaModel model = loadModel(applicationDir, true);
        ApplicationContainerCluster containerCluster = model.getContainerClusters().get("container");

        Component<?, ?> transformer = containerCluster.getComponentsMap().get(new ComponentId("transformer"));
        ConfigPayloadBuilder config = transformer.getUserConfigs().get(new ConfigDefinitionKey("bert-base-embedder", "embedding"));
        assertEquals("https://data.vespa.oath.cloud/onnx_models/sentence_all_MiniLM_L6_v2.onnx",
                     config.getObject("transformerModelUrl").getValue());
        assertEquals(emptyPathFileName, config.getObject("transformerModelPath").getValue());
        assertEquals("", config.getObject("tokenizerVocabUrl").getValue());
        assertEquals("files/vocab.txt", config.getObject("tokenizerVocabPath").getValue());
        assertEquals("4", config.getObject("onnxIntraOpThreads").getValue());
    }

    @Test
    void testApplicationPackageWithApplicationEmbedder_selfhosted() throws Exception  {
        Path applicationDir = Path.fromString("src/test/cfg/application/embed_generic/");
        VespaModel model = loadModel(applicationDir, false);
        ApplicationContainerCluster containerCluster = model.getContainerClusters().get("container");

        Component<?, ?> testComponent = containerCluster.getComponentsMap().get(new ComponentId("transformer"));
        ConfigPayloadBuilder config = testComponent.getUserConfigs().get(new ConfigDefinitionKey("sentence-embedder", "ai.vespa.example.paragraph"));
        assertEquals("application-url", config.getObject("modelUrl").getValue());
        assertEquals(emptyPathFileName, config.getObject("modelPath").getValue());
        assertEquals("files/vocab.txt", config.getObject("vocabPath").getValue());
        assertEquals("foo", config.getObject("myValue").getValue());
    }

    @Test
    void testApplicationPackageWithApplicationEmbedder_hosted() throws Exception  {
        Path applicationDir = Path.fromString("src/test/cfg/application/embed_generic/");
        VespaModel model = loadModel(applicationDir, true);
        ApplicationContainerCluster containerCluster = model.getContainerClusters().get("container");

        Component<?, ?> testComponent = containerCluster.getComponentsMap().get(new ComponentId("transformer"));
        ConfigPayloadBuilder config = testComponent.getUserConfigs().get(new ConfigDefinitionKey("sentence-embedder", "ai.vespa.example.paragraph"));
        assertEquals("https://data.vespa.oath.cloud/onnx_models/sentence_all_MiniLM_L6_v2.onnx",
                     config.getObject("modelUrl").getValue());
        assertEquals(emptyPathFileName, config.getObject("modelPath").getValue());
        assertEquals("files/vocab.txt", config.getObject("vocabPath").getValue());
        assertEquals("foo", config.getObject("myValue").getValue());
    }

    private VespaModel loadModel(Path path, boolean hosted) throws Exception {
        FilesApplicationPackage applicationPackage = FilesApplicationPackage.fromFile(path.toFile());
        TestProperties properties = new TestProperties().setHostedVespa(hosted);
        DeployState state = new DeployState.Builder().properties(properties).applicationPackage(applicationPackage).build();
        return new VespaModel(state);
    }

    private void assertTransform(String embedder, String component) throws IOException, SAXException {
        assertTransform(embedder, component, false);
    }

    private void assertTransform(String embedder, String expectedComponent, boolean hosted) throws IOException, SAXException {
        assertSpec(createElement(expectedComponent),
                   EmbedderConfigTransformer.transform(createEmptyDeployState(hosted), createElement(embedder)));
    }

    private void assertSpec(Element e1, Element e2) {
        assertEquals(e1.getTagName(), e2.getTagName());
        assertAttributes(e1, e2);
        assertAttributes(e2, e1);
        assertEquals(XML.getValue(e1).trim(), XML.getValue(e2).trim(), "Content of " + e1.getTagName() + "' is identical");
        assertChildren(e1, e2);
    }

    private void assertAttributes(Element e1, Element e2) {
        NamedNodeMap map = e1.getAttributes();
        for (int i = 0; i < map.getLength(); ++i) {
            String attr = map.item(i).getNodeName();
            assertEquals(e1.getAttribute(attr), e2.getAttribute(attr));
        }
    }

    private void assertChildren(Element e1, Element e2) {
        List<Element> list1 = XML.getChildren(e1);
        List<Element> list2 = XML.getChildren(e2);
        assertEquals(list1.size(), list2.size());
        for (int i = 0; i < list1.size(); ++i) {
            Element child1 = list1.get(i);
            Element child2 = list2.get(i);
            assertSpec(child1, child2);
        }
    }

    private void assertTransformThrows(String embedder, String expectedMessage, boolean hosted) throws IOException, SAXException {
        try {
            EmbedderConfigTransformer.transform(createEmptyDeployState(hosted), createElement(embedder));
            fail("Expected exception was not thrown: " + expectedMessage);
        } catch (IllegalArgumentException e) {
            assertEquals(expectedMessage, e.getMessage());
        }
    }

    private Element createElement(String xml) throws IOException, SAXException {
        Document doc = XML.getDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
        return (Element) doc.getFirstChild();
    }

    private DeployState createEmptyDeployState(boolean hosted) {
        TestProperties properties = new TestProperties().setHostedVespa(hosted);
        return new DeployState.Builder().properties(properties).build();
    }

}