summaryrefslogtreecommitdiffstats
path: root/config-model/src/main
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2022-09-01 15:56:28 +0200
committerGitHub <noreply@github.com>2022-09-01 15:56:28 +0200
commit96d24924eed043c9d8df7694375828745889faa2 (patch)
tree5810bbe78d7beccdbe5751df1c88d6b8680d093e /config-model/src/main
parenta5a7789218d9fb5a569eb8f3feec6747f9ec2ca3 (diff)
parent38bfeeadafabeeef540157e440224b03ba80ed4d (diff)
Merge pull request #23878 from vespa-engine/bratseth/moodel-config
Bratseth/model config
Diffstat (limited to 'config-model/src/main')
-rw-r--r--config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/DomConfigPayloadBuilder.java41
-rw-r--r--config-model/src/main/java/com/yahoo/vespa/model/container/xml/ContainerModelBuilder.java2
-rw-r--r--config-model/src/main/java/com/yahoo/vespa/model/container/xml/ModelConfigTransformer.java73
-rw-r--r--config-model/src/main/java/com/yahoo/vespa/model/container/xml/ModelIdResolver.java51
4 files changed, 74 insertions, 93 deletions
diff --git a/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/DomConfigPayloadBuilder.java b/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/DomConfigPayloadBuilder.java
index 7ff01cbf82e..9390986c0c4 100644
--- a/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/DomConfigPayloadBuilder.java
+++ b/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/DomConfigPayloadBuilder.java
@@ -52,19 +52,19 @@ public class DomConfigPayloadBuilder {
public static ConfigDefinitionKey parseConfigName(Element configE) {
if (!configE.getNodeName().equals("config")) {
- throw new ConfigurationRuntimeException("The root element must be 'config', but was '" + configE.getNodeName() + "'.");
+ throw new ConfigurationRuntimeException("The root element must be 'config', but was '" + configE.getNodeName() + "'");
}
if (!configE.hasAttribute("name")) {
throw new ConfigurationRuntimeException
- ("The 'config' element must have a 'name' attribute that matches the name of the config definition.");
+ ("The 'config' element must have a 'name' attribute that matches the name of the config definition");
}
String elementString = configE.getAttribute("name");
if (!elementString.contains(".")) {
throw new ConfigurationRuntimeException("The config name '" + elementString +
- "' contains illegal characters. Only names with the pattern " +
- namespacePattern.pattern() + "." + namePattern.pattern() + " are legal.");
+ "' contains illegal characters. Only names with the pattern " +
+ namespacePattern.pattern() + "." + namePattern.pattern() + " are legal.");
}
Tuple2<String, String> t = ConfigUtils.getNameAndNamespaceFromString(elementString);
@@ -73,28 +73,26 @@ public class DomConfigPayloadBuilder {
if (!validName(xmlName)) {
throw new ConfigurationRuntimeException("The config name '" + xmlName +
- "' contains illegal characters. Only names with the pattern " + namePattern.toString() + " are legal.");
+ "' contains illegal characters. Only names with the pattern " +
+ namePattern.toString() + " are legal.");
}
if (!validNamespace(xmlNamespace)) {
throw new ConfigurationRuntimeException("The config namespace '" + xmlNamespace +
- "' contains illegal characters. Only namespaces with the pattern " + namespacePattern.toString() + " are legal.");
+ "' contains illegal characters. Only namespaces with the pattern " +
+ namespacePattern.toString() + " are legal.");
}
return new ConfigDefinitionKey(xmlName, xmlNamespace);
}
private static boolean validName(String name) {
if (name == null) return false;
-
- Matcher m = namePattern.matcher(name);
- return m.matches();
+ return namePattern.matcher(name).matches();
}
private static boolean validNamespace(String namespace) {
if (namespace == null) return false;
-
- Matcher m = namespacePattern.matcher(namespace);
- return m.matches();
+ return namespacePattern.matcher(namespace).matches();
}
private String extractName(Element element) {
@@ -118,12 +116,11 @@ public class DomConfigPayloadBuilder {
return buf.toString();
}
- /**
- * Parse leaf value in an xml tree
- */
+ /** Parse leaf value in an xml tree. */
private void parseLeaf(Element element, ConfigPayloadBuilder payloadBuilder, String parentName) {
String name = extractName(element);
String value = XML.getValue(element);
+ var definition = payloadBuilder.getConfigDefinition();
if (value == null) {
throw new ConfigurationRuntimeException("Element '" + name + "' must have either children or a value");
}
@@ -136,8 +133,14 @@ public class DomConfigPayloadBuilder {
} else {
payloadBuilder.getArray(parentName).append(value);
}
- } else {
- // leaf scalar, e.g. <intVal>3</intVal>
+ }
+ else if (definition != null && definition.getModelDefs().containsKey(name)) { // model field special syntax
+ String modelString = XML.attribute("model-id", element).orElse("\"\"");
+ modelString += " " + XML.attribute("url", element).orElse("\"\"");
+ modelString += " " + XML.attribute("path", element).orElse("\"\"");
+ payloadBuilder.setField(name, modelString);
+ }
+ else { // leaf value: <myValueName>value</myValue>
payloadBuilder.setField(name, value);
}
}
@@ -196,8 +199,8 @@ public class DomConfigPayloadBuilder {
parseComplex(currElem, children, payloadBuilder, parentName);
}
} catch (Exception exception) {
- throw new ConfigurationRuntimeException("Error parsing element at " + XML.getNodePath(currElem, " > ") + ": " +
- Exceptions.toMessageString(exception));
+ throw new ConfigurationRuntimeException("Error parsing element at " + XML.getNodePath(currElem, " > ") +
+ ": " + Exceptions.toMessageString(exception));
}
}
diff --git a/config-model/src/main/java/com/yahoo/vespa/model/container/xml/ContainerModelBuilder.java b/config-model/src/main/java/com/yahoo/vespa/model/container/xml/ContainerModelBuilder.java
index 1cf73c1ed03..0bf586a089f 100644
--- a/config-model/src/main/java/com/yahoo/vespa/model/container/xml/ContainerModelBuilder.java
+++ b/config-model/src/main/java/com/yahoo/vespa/model/container/xml/ContainerModelBuilder.java
@@ -957,7 +957,7 @@ public class ContainerModelBuilder extends ConfigModelBuilder<ContainerModel> {
private static void addConfiguredComponents(DeployState deployState, ContainerCluster<? extends Container> cluster,
Element parent, String componentName) {
for (Element component : XML.getChildren(parent, componentName)) {
- component = ModelConfigTransformer.transform(deployState, component);
+ ModelIdResolver.resolveModelIds(component, deployState.isHosted());
cluster.addComponent(new DomComponentBuilder().build(deployState, cluster, component));
}
}
diff --git a/config-model/src/main/java/com/yahoo/vespa/model/container/xml/ModelConfigTransformer.java b/config-model/src/main/java/com/yahoo/vespa/model/container/xml/ModelConfigTransformer.java
deleted file mode 100644
index 0065a582145..00000000000
--- a/config-model/src/main/java/com/yahoo/vespa/model/container/xml/ModelConfigTransformer.java
+++ /dev/null
@@ -1,73 +0,0 @@
-// 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.config.model.deploy.DeployState;
-import com.yahoo.text.XML;
-import org.w3c.dom.Element;
-
-import java.util.Map;
-import java.util.stream.Collectors;
-
-/**
- * Translates model references in component configs.
- *
- * @author lesters
- * @author bratseth
- */
-public class ModelConfigTransformer {
-
- private static final Map<String, String> providedModels =
- Map.of("minilm-l6-v2", "https://data.vespa.oath.cloud/onnx_models/sentence_all_MiniLM_L6_v2.onnx",
- "bert-base-uncased", "https://data.vespa.oath.cloud/onnx_models/bert-base-uncased-vocab.txt");
-
- // Until we have optional path parameters, use services.xml as it is guaranteed to exist
- private final static String dummyPath = "services.xml";
-
- /**
- * Transforms the &lt;embedder ...&gt; element to component configuration.
- *
- * @param deployState the deploy state - as config generation can depend on context
- * @param component the XML element containing the &lt;embedder ...&gt;
- * @return a new XML element containting the &lt;component ...&gt; configuration
- */
- public static Element transform(DeployState deployState, Element component) {
- for (Element config : XML.getChildren(component, "config")) {
- for (Element value : XML.getChildren(config))
- transformModelValue(value, config, deployState.isHosted());
- }
- return component;
- }
-
- /** Expans a model config value into regular config values. */
- private static void transformModelValue(Element value, Element config, boolean hosted) {
- if (value.hasAttribute("path")) {
- addChild(value.getTagName() + "Url", "", config);
- addChild(value.getTagName() + "Path", value.getAttribute("path"), config);
- config.removeChild(value);
- }
- else if (value.hasAttribute("id") && hosted) {
- addChild(value.getTagName() + "Url", modelIdToUrl(value.getAttribute("id")), config);
- addChild(value.getTagName() + "Path", dummyPath, config);
- config.removeChild(value);
- }
- else if (value.hasAttribute("url")) {
- addChild(value.getTagName() + "Url", value.getAttribute("url"), config);
- addChild(value.getTagName() + "Path", dummyPath, config);
- config.removeChild(value);
- }
- }
-
- private static void addChild(String name, String value, Element parent) {
- Element element = parent.getOwnerDocument().createElement(name);
- element.setTextContent(value);
- parent.appendChild(element);
- }
-
- private static String modelIdToUrl(String id) {
- if ( ! providedModels.containsKey(id))
- throw new IllegalArgumentException("Unknown embedder model '" + id + "'. Available models are [" +
- providedModels.keySet().stream().sorted().collect(Collectors.joining(", ")) + "]");
- return providedModels.get(id);
- }
-
-}
diff --git a/config-model/src/main/java/com/yahoo/vespa/model/container/xml/ModelIdResolver.java b/config-model/src/main/java/com/yahoo/vespa/model/container/xml/ModelIdResolver.java
new file mode 100644
index 00000000000..463832ea263
--- /dev/null
+++ b/config-model/src/main/java/com/yahoo/vespa/model/container/xml/ModelIdResolver.java
@@ -0,0 +1,51 @@
+// 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.config.model.deploy.DeployState;
+import com.yahoo.text.XML;
+import org.w3c.dom.Element;
+
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Replaces model id references in configs by their url.
+ *
+ * @author lesters
+ * @author bratseth
+ */
+public class ModelIdResolver {
+
+ private static final Map<String, String> providedModels =
+ Map.of("minilm-l6-v2", "https://data.vespa.oath.cloud/onnx_models/sentence_all_MiniLM_L6_v2.onnx",
+ "bert-base-uncased", "https://data.vespa.oath.cloud/onnx_models/bert-base-uncased-vocab.txt");
+
+ /**
+ * Finds any config values of type 'model' below the given config element and
+ * supplies the url attribute of them if a model id is specified and hosted is true
+ * (regardless of whether an url is already specified).
+ *
+ * @param component the XML element of any component
+ */
+ public static void resolveModelIds(Element component, boolean hosted) {
+ if ( ! hosted) return;
+ for (Element config : XML.getChildren(component, "config")) {
+ for (Element value : XML.getChildren(config))
+ transformModelValue(value);
+ }
+ }
+
+ /** Expands a model config value into regular config values. */
+ private static void transformModelValue(Element value) {
+ if ( ! value.hasAttribute("model-id")) return;
+ value.setAttribute("url", modelIdToUrl(value.getTagName(), value.getAttribute("model-id")));
+ }
+
+ private static String modelIdToUrl(String valueName, String modelId) {
+ if ( ! providedModels.containsKey(modelId))
+ throw new IllegalArgumentException("Unknown model id '" + modelId + "' on '" + valueName + "'. Available models are [" +
+ providedModels.keySet().stream().sorted().collect(Collectors.joining(", ")) + "]");
+ return providedModels.get(modelId);
+ }
+
+}