aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/container/xml/EmbedderTestCase.java
blob: 0dae86473c865eaf4d775d34bbb9cc5561d1431b (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
// 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 com.yahoo.vespa.model.container.xml.embedder.EmbedderConfig;
import org.junit.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.Assert.assertEquals;
import static org.junit.Assert.fail;

public class EmbedderTestCase {

    private static final String PREDEFINED_EMBEDDER_CLASS = "ai.vespa.embedding.BertBaseEmbedder";
    private static final String PREDEFINED_EMBEDDER_CONFIG = "embedding.bert-base-embedder";

    @Test
    public void testGenericEmbedConfig() 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
    public void testGenericEmbedConfigRequiresBundleAndDef() throws IOException, SAXException {
        assertTransformThrows("<embedder id=\"test\" class=\"ai.vespa.test\"></embedder>",
                "Embedder configuration requires a bundle name");
        assertTransformThrows("<embedder id=\"test\" class=\"ai.vespa.test\" bundle=\"bundle\"></embedder>",
                "Embedder configuration requires a config definition name");
    }

    @Test
    public void testPredefinedEmbedConfigSelfHosted() throws IOException, SAXException {
        assertTransformThrows("<embedder id=\"test\" class=\"" + PREDEFINED_EMBEDDER_CLASS + "\"></embedder>",
                "Embedder '" + PREDEFINED_EMBEDDER_CLASS + "' requires options for [vocab, model]");
        assertTransformThrows("<embedder id=\"test\" class=\"" + PREDEFINED_EMBEDDER_CLASS + "\">" +
                "  <model />" +
                "  <vocab />" +
                "</embedder>",
                "Model option requires either a 'path' or a 'url' attribute");
        assertTransformThrows("<embedder id=\"test\" class=\"" + PREDEFINED_EMBEDDER_CLASS + "\">" +
                "  <model id=\"my_model_id\" />" +
                "  <vocab id=\"my_vocab_id\" />" +
                "</embedder>",
                "Model option 'id' is not valid here");

        String embedder = "<embedder id=\"test\" class=\"" + PREDEFINED_EMBEDDER_CLASS + "\">" +
                "  <model id=\"my_model_id\" url=\"my-model-url\" />" +
                "  <vocab id=\"my_vocab_id\" url=\"my-vocab-url\" />" +
                "</embedder>";
        String component = "<component id=\"test\" class=\"" + PREDEFINED_EMBEDDER_CLASS + "\" bundle=\"model-integration\">" +
                "  <config name=\"" + PREDEFINED_EMBEDDER_CONFIG + "\">" +
                "      <tokenizerVocabUrl>my-vocab-url</tokenizerVocabUrl>" +
                "      <tokenizerVocabPath></tokenizerVocabPath>" +
                "      <transformerModelUrl>my-model-url</transformerModelUrl>" +
                "      <transformerModelPath></transformerModelPath>" +
                "  </config>" +
                "</component>";
        assertTransform(embedder, component, false);

        // Path has priority:
        embedder = "<embedder id=\"test\" class=\"" + PREDEFINED_EMBEDDER_CLASS + "\">" +
                "  <model id=\"my_model_id\" url=\"my-model-url\" path=\"files/model.onnx\" />" +
                "  <vocab id=\"my_vocab_id\" url=\"my-vocab-url\" path=\"files/vocab.txt\" />" +
                "</embedder>";
        component = "<component id=\"test\" class=\"" + PREDEFINED_EMBEDDER_CLASS + "\" bundle=\"model-integration\">" +
                "  <config name=\"" + PREDEFINED_EMBEDDER_CONFIG + "\">" +
                "      <tokenizerVocabPath>files/vocab.txt</tokenizerVocabPath>" +
                "      <tokenizerVocabUrl></tokenizerVocabUrl>" +
                "      <transformerModelPath>files/model.onnx</transformerModelPath>" +
                "      <transformerModelUrl></transformerModelUrl>" +
                "  </config>" +
                "</component>";
        assertTransform(embedder, component, false);
    }

    @Test
    public void testPredefinedEmbedConfigCloud() throws IOException, SAXException {
        String embedder = "<embedder id=\"test\" class=\"" + PREDEFINED_EMBEDDER_CLASS + "\" />";
        String component = "<component id=\"test\" class=\"" + PREDEFINED_EMBEDDER_CLASS + "\" bundle=\"model-integration\">" +
                "  <config name=\"" + PREDEFINED_EMBEDDER_CONFIG + "\">" +
                "      <tokenizerVocabUrl>some url</tokenizerVocabUrl>" +
                "      <tokenizerVocabPath></tokenizerVocabPath>" +
                "      <transformerModelUrl>some url</transformerModelUrl>" +
                "      <transformerModelPath></transformerModelPath>" +
                "  </config>" +
                "</component>";
        assertTransform(embedder, component, true);

        embedder = "<embedder id=\"test\" class=\"" + PREDEFINED_EMBEDDER_CLASS + "\">" +
                "  <model id=\"my_model_id\" />" +
                "  <vocab id=\"my_vocab_id\" />" +
                "</embedder>";
        assertTransformThrows(embedder, "Unknown model id: 'my_vocab_id'", true);

        embedder = "<embedder id=\"test\" class=\"" + PREDEFINED_EMBEDDER_CLASS + "\">" +
                "  <model id=\"test-model-id\" />" +
                "  <vocab id=\"test-model-id\" />" +
                "</embedder>";
        component = "<component id=\"test\" class=\"" + PREDEFINED_EMBEDDER_CLASS + "\" bundle=\"model-integration\">" +
                "  <config name=\"" + PREDEFINED_EMBEDDER_CONFIG + "\">" +
                "      <tokenizerVocabUrl>test-model-url</tokenizerVocabUrl>" +
                "      <tokenizerVocabPath></tokenizerVocabPath>" +
                "      <transformerModelUrl>test-model-url</transformerModelUrl>" +
                "      <transformerModelPath></transformerModelPath>" +
                "  </config>" +
                "</component>";
        assertTransform(embedder, component, true);
    }

    @Test
    public void testEmbedConfig() throws Exception  {
        final String emptyPathFileName = "services.xml";

        Path applicationDir = Path.fromString("src/test/cfg/application/embed/");
        VespaModel model = loadModel(applicationDir, false);
        ApplicationContainerCluster containerCluster = model.getContainerClusters().get("container");

        Component<?, ?> testComponent = containerCluster.getComponentsMap().get(new ComponentId("test"));
        ConfigPayloadBuilder testConfig = testComponent.getUserConfigs().get(new ConfigDefinitionKey("dummy", "test"));
        assertEquals(testConfig.getObject("num").getValue(), "12");
        assertEquals(testConfig.getObject("str").getValue(), "some text");

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

    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 component, boolean hosted) throws IOException, SAXException {
        Element emb = createElement(embedder);
        Element cmp = createElement(component);
        Element trans = EmbedderConfig.transform(createEmptyDeployState(hosted), emb);
        assertSpec(cmp, trans);
    }

    private void assertSpec(Element e1, Element e2) {
        assertEquals(e1.getTagName(), e2.getTagName());
        assertAttributes(e1, e2);
        assertAttributes(e2, e1);
        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 msg) throws IOException, SAXException {
        assertTransformThrows(embedder, msg, false);
    }

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

    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();
    }

}