diff options
Diffstat (limited to 'config-model')
22 files changed, 428 insertions, 103 deletions
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java b/config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java index 41cb40da4d6..01d4042573c 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java @@ -266,7 +266,7 @@ public class MapEvaluationTypeContext extends FunctionReferenceContext implement String modelConfigName = OnnxModelTransformer.getModelConfigName(reference); String modelOutput = OnnxModelTransformer.getModelOutput(reference, null); - reference = new Reference("onnxModel", new Arguments(new ReferenceNode(modelConfigName)), modelOutput); + reference = new Reference("onnxModel", new Arguments(new ReferenceNode(modelConfigName)), modelOutput); if ( ! featureTypes.containsKey(reference)) { throw new IllegalArgumentException("Missing onnx-model config for '" + configOrFileName + "'"); } diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/OnnxModel.java b/config-model/src/main/java/com/yahoo/searchdefinition/OnnxModel.java index d20eecb82c5..3c42987512b 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/OnnxModel.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/OnnxModel.java @@ -60,6 +60,12 @@ public class OnnxModel extends DistributableResource { public void setModelInfo(OnnxModelInfo modelInfo) { Objects.requireNonNull(modelInfo, "Onnx model info cannot be null"); + for (String onnxName : modelInfo.getInputs()) { + addInputNameMapping(onnxName, OnnxModelInfo.asValidIdentifier(onnxName), false); + } + for (String onnxName : modelInfo.getOutputs()) { + addOutputNameMapping(onnxName, OnnxModelInfo.asValidIdentifier(onnxName), false); + } this.modelInfo = modelInfo; } diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java b/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java index 8b467288326..b0a7d2aaca2 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java @@ -122,6 +122,9 @@ public class RankProfile implements Cloneable { private List<ImmutableSDField> allFieldsList; + /** Global onnx models not tied to a search definition */ + private OnnxModels onnxModels = new OnnxModels(); + /** * Creates a new rank profile for a particular search definition * @@ -143,11 +146,12 @@ public class RankProfile implements Cloneable { * @param name the name of the new profile * @param model the model owning this profile */ - public RankProfile(String name, VespaModel model, RankProfileRegistry rankProfileRegistry) { + public RankProfile(String name, VespaModel model, RankProfileRegistry rankProfileRegistry, OnnxModels onnxModels) { this.name = Objects.requireNonNull(name, "name cannot be null"); this.search = null; this.model = Objects.requireNonNull(model, "model cannot be null"); this.rankProfileRegistry = rankProfileRegistry; + this.onnxModels = onnxModels; } public String getName() { return name; } @@ -170,7 +174,7 @@ public class RankProfile implements Cloneable { } public Map<String, OnnxModel> onnxModels() { - return search != null ? search.onnxModels().asMap() : Collections.emptyMap(); + return search != null ? search.onnxModels().asMap() : onnxModels.asMap(); } private Stream<ImmutableSDField> allFields() { diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/derived/RankProfileList.java b/config-model/src/main/java/com/yahoo/searchdefinition/derived/RankProfileList.java index c0a4117de3e..d414b9ed79f 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/derived/RankProfileList.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/derived/RankProfileList.java @@ -80,6 +80,9 @@ public class RankProfileList extends Derived implements RankProfilesConfig.Produ for (RankProfile rank : rankProfileRegistry.rankProfilesOf(search)) { if (search != null && "default".equals(rank.getName())) continue; + if (search == null) { + this.onnxModels.add(rank.onnxModels()); + } RawRankProfile rawRank = new RawRankProfile(rank, queryProfiles, importedModels, attributeFields, deployProperties); rankProfiles.put(rawRank.getName(), rawRank); diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/processing/OnnxModelTypeResolver.java b/config-model/src/main/java/com/yahoo/searchdefinition/processing/OnnxModelTypeResolver.java index 5c37b345edf..4d8fba8c603 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/processing/OnnxModelTypeResolver.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/processing/OnnxModelTypeResolver.java @@ -28,23 +28,8 @@ public class OnnxModelTypeResolver extends Processor { @Override public void process(boolean validate, boolean documentsOnly) { if (documentsOnly) return; - for (OnnxModel onnxModel : search.onnxModels().asMap().values()) { OnnxModelInfo onnxModelInfo = OnnxModelInfo.load(onnxModel.getFileName(), search.applicationPackage()); - - // Temporary, to disregard type information when model info is not available - if (onnxModelInfo == null) { - continue; - } - - // Add any missing input and output fields that were not specified in the onnx-model configuration - for (String onnxName : onnxModelInfo.getInputs()) { - onnxModel.addInputNameMapping(onnxName, OnnxModelInfo.asValidIdentifier(onnxName), false); - } - for (String onnxName : onnxModelInfo.getOutputs()) { - onnxModel.addOutputNameMapping(onnxName, OnnxModelInfo.asValidIdentifier(onnxName), false); - } - onnxModel.setModelInfo(onnxModelInfo); } } diff --git a/config-model/src/main/java/com/yahoo/vespa/model/VespaModel.java b/config-model/src/main/java/com/yahoo/vespa/model/VespaModel.java index 85ff134b385..ab00e9d295f 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/VespaModel.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/VespaModel.java @@ -30,6 +30,9 @@ import com.yahoo.config.model.producer.UserConfigRepo; import com.yahoo.config.provision.AllocatedHosts; import com.yahoo.config.provision.ClusterSpec; import com.yahoo.container.QrConfig; +import com.yahoo.path.Path; +import com.yahoo.searchdefinition.OnnxModel; +import com.yahoo.searchdefinition.OnnxModels; import com.yahoo.searchdefinition.RankExpressionFiles; import com.yahoo.searchdefinition.RankProfile; import com.yahoo.searchdefinition.RankProfileRegistry; @@ -58,6 +61,7 @@ import com.yahoo.vespa.model.filedistribution.FileDistributor; import com.yahoo.vespa.model.generic.service.ServiceCluster; import com.yahoo.vespa.model.ml.ConvertedModel; import com.yahoo.vespa.model.ml.ModelName; +import com.yahoo.vespa.model.ml.OnnxModelInfo; import com.yahoo.vespa.model.routing.Routing; import com.yahoo.vespa.model.search.AbstractSearchCluster; import com.yahoo.vespa.model.utils.internal.ReflectionUtil; @@ -294,7 +298,8 @@ public final class VespaModel extends AbstractConfigProducerRoot implements Seri QueryProfiles queryProfiles) { if ( ! importedModels.all().isEmpty()) { // models/ directory is available for (ImportedMlModel model : importedModels.all()) { - RankProfile profile = new RankProfile(model.name(), this, rankProfileRegistry); + OnnxModels onnxModels = onnxModelInfoFromSource(model); + RankProfile profile = new RankProfile(model.name(), this, rankProfileRegistry, onnxModels); rankProfileRegistry.add(profile); ConvertedModel convertedModel = ConvertedModel.fromSource(new ModelName(model.name()), model.name(), profile, queryProfiles.getRegistry(), model); @@ -306,7 +311,8 @@ public final class VespaModel extends AbstractConfigProducerRoot implements Seri for (ApplicationFile generatedModelDir : generatedModelsDir.listFiles()) { String modelName = generatedModelDir.getPath().last(); if (modelName.contains(".")) continue; // Name space: Not a global profile - RankProfile profile = new RankProfile(modelName, this, rankProfileRegistry); + OnnxModels onnxModels = onnxModelInfoFromStore(modelName); + RankProfile profile = new RankProfile(modelName, this, rankProfileRegistry, onnxModels); rankProfileRegistry.add(profile); ConvertedModel convertedModel = ConvertedModel.fromStore(new ModelName(modelName), modelName, profile); convertedModel.expressions().values().forEach(f -> profile.addFunction(f, false)); @@ -315,6 +321,42 @@ public final class VespaModel extends AbstractConfigProducerRoot implements Seri new Processing().processRankProfiles(deployLogger, rankProfileRegistry, queryProfiles, true, false); } + private OnnxModels onnxModelInfoFromSource(ImportedMlModel model) { + OnnxModels onnxModels = new OnnxModels(); + if (model.modelType().equals(ImportedMlModel.ModelType.ONNX)) { + String path = model.source(); + String applicationPath = this.applicationPackage.getFileReference(Path.fromString("")).toString(); + if (path.startsWith(applicationPath)) { + path = path.substring(applicationPath.length() + 1); + } + loadOnnxModelInfo(onnxModels, model.name(), path); + } + return onnxModels; + } + + private OnnxModels onnxModelInfoFromStore(String modelName) { + OnnxModels onnxModels = new OnnxModels(); + String path = ApplicationPackage.MODELS_DIR.append(modelName + ".onnx").toString(); + loadOnnxModelInfo(onnxModels, modelName, path); + return onnxModels; + } + + private void loadOnnxModelInfo(OnnxModels onnxModels, String name, String path) { + boolean modelExists = OnnxModelInfo.modelExists(path, this.applicationPackage); + if ( ! modelExists) { + path = ApplicationPackage.MODELS_DIR.append(path).toString(); + modelExists = OnnxModelInfo.modelExists(path, this.applicationPackage); + } + if (modelExists) { + OnnxModelInfo onnxModelInfo = OnnxModelInfo.load(path, this.applicationPackage); + if (onnxModelInfo.getModelPath() != null) { + OnnxModel onnxModel = new OnnxModel(name, onnxModelInfo.getModelPath()); + onnxModel.setModelInfo(onnxModelInfo); + onnxModels.add(onnxModel); + } + } + } + /** Returns the global rank profiles as a rank profile list */ public RankProfileList rankProfileList() { return rankProfileList; } diff --git a/config-model/src/main/java/com/yahoo/vespa/model/ml/ConvertedModel.java b/config-model/src/main/java/com/yahoo/vespa/model/ml/ConvertedModel.java index 9086ca9f40e..62f911c9f1a 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/ml/ConvertedModel.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/ml/ConvertedModel.java @@ -79,8 +79,11 @@ public class ConvertedModel { * * @param modelPath the path to the model * @param pathIsFile true if that path (this kind of model) is stored in a file, false if it is in a directory + * @param context the transform context */ - public static ConvertedModel fromSourceOrStore(Path modelPath, boolean pathIsFile, RankProfileTransformContext context) { + public static ConvertedModel fromSourceOrStore(Path modelPath, + boolean pathIsFile, + RankProfileTransformContext context) { ImportedMlModel sourceModel = // TODO: Convert to name here, make sure its done just one way context.importedModels().get(sourceModelFile(context.rankProfile().applicationPackage(), modelPath)); ModelName modelName = new ModelName(context.rankProfile().getName(), modelPath, pathIsFile); @@ -90,6 +93,9 @@ public class ConvertedModel { context.importedModels().all().stream().map(ImportedMlModel::source).collect(Collectors.joining(", "))); if (sourceModel != null) { + if ( ! sourceModel.isNative()) { + sourceModel = sourceModel.asNative(); + } return fromSource(modelName, modelPath.toString(), context.rankProfile(), @@ -592,7 +598,7 @@ public class ConvertedModel { // Write content explicitly as a file on the file system as this is distributed using file distribution // - but only if this is a global model to avoid writing the same constants for each rank profile // where they are used - if (modelFiles.modelName.isGlobal()) { + if (modelFiles.modelName.isGlobal() || ! application.getFileReference(constantPath).exists()) { createIfNeeded(constantsPath); IOUtils.writeFile(application.getFileReference(constantPath), TypedBinaryFormat.encode(constant)); } diff --git a/config-model/src/main/java/com/yahoo/vespa/model/ml/ModelName.java b/config-model/src/main/java/com/yahoo/vespa/model/ml/ModelName.java index 7e33faadfc0..b6a9c855aeb 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/ml/ModelName.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/ml/ModelName.java @@ -4,7 +4,7 @@ package com.yahoo.vespa.model.ml; import com.yahoo.path.Path; /** - * Models used in a rank profile has the rank profile name as name space while gGlobal model names have no namespace + * Models used in a rank profile has the rank profile name as name space while global model names have no namespace * * @author bratseth */ diff --git a/config-model/src/main/java/com/yahoo/vespa/model/ml/OnnxModelInfo.java b/config-model/src/main/java/com/yahoo/vespa/model/ml/OnnxModelInfo.java index 0a838e5d915..58381fe5c3c 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/ml/OnnxModelInfo.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/ml/OnnxModelInfo.java @@ -36,17 +36,23 @@ import java.util.stream.Collectors; */ public class OnnxModelInfo { + private final String modelPath; private final String defaultOutput; private final Map<String, OnnxTypeInfo> inputs; private final Map<String, OnnxTypeInfo> outputs; private final Map<String, TensorType> vespaTypes = new HashMap<>(); - private OnnxModelInfo(Map<String, OnnxTypeInfo> inputs, Map<String, OnnxTypeInfo> outputs, String defaultOutput) { + private OnnxModelInfo(String path, Map<String, OnnxTypeInfo> inputs, Map<String, OnnxTypeInfo> outputs, String defaultOutput) { + this.modelPath = path; this.inputs = Collections.unmodifiableMap(inputs); this.outputs = Collections.unmodifiableMap(outputs); this.defaultOutput = defaultOutput; } + public String getModelPath() { + return modelPath; + } + public Set<String> getInputs() { return inputs.keySet(); } @@ -125,12 +131,7 @@ public class OnnxModelInfo { if (app.getFile(generatedModelInfoPath(pathInApplicationPackage)).exists()) { return loadFromGeneratedInfo(pathInApplicationPackage, app); } - - // Temporary: - return null; - - // This is the correct behaviour after we've gotten applications through. - // throw new IllegalArgumentException("Unable to find ONNX model file or generated ONNX info file"); + throw new IllegalArgumentException("Unable to find ONNX model file or generated ONNX info file"); } static public boolean modelExists(String path, ApplicationPackage app) { @@ -147,7 +148,7 @@ public class OnnxModelInfo { static private OnnxModelInfo loadFromFile(Path path, ApplicationPackage app) { try (InputStream inputStream = app.getFile(path).createInputStream()) { Onnx.ModelProto model = Onnx.ModelProto.parseFrom(inputStream); - String json = onnxModelToJson(model); + String json = onnxModelToJson(model, path); storeGeneratedInfo(json, path, app); return jsonToModelInfo(json); } catch (IOException e) { @@ -178,11 +179,12 @@ public class OnnxModelInfo { return ApplicationPackage.MODELS_GENERATED_REPLICATED_DIR.append(fileName); } - static private String onnxModelToJson(Onnx.ModelProto model) throws IOException { + static private String onnxModelToJson(Onnx.ModelProto model, Path path) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator g = new JsonFactory().createGenerator(out, JsonEncoding.UTF8); g.writeStartObject(); + g.writeStringField("path", path.toString()); g.writeArrayFieldStart("inputs"); for (Onnx.ValueInfoProto valueInfo : model.getGraph().getInputList()) { onnxTypeToJson(g, valueInfo); @@ -207,6 +209,10 @@ public class OnnxModelInfo { Map<String, OnnxTypeInfo> outputs = new HashMap<>(); String defaultOutput = ""; + String path = null; + if (root.has("path")) { + path = root.get("path").textValue(); + } for (JsonNode input : root.get("inputs")) { inputs.put(input.get("name").textValue(), jsonToTypeInfo(input)); } @@ -216,7 +222,7 @@ public class OnnxModelInfo { if (root.get("outputs").has(0)) { defaultOutput = root.get("outputs").get(0).get("name").textValue(); } - return new OnnxModelInfo(inputs, outputs, defaultOutput); + return new OnnxModelInfo(path, inputs, outputs, defaultOutput); } static private void onnxTypeToJson(JsonGenerator g, Onnx.ValueInfoProto valueInfo) throws IOException { diff --git a/config-model/src/test/cfg/application/ml_serving/models/add_mul.onnx b/config-model/src/test/cfg/application/ml_serving/models/add_mul.onnx new file mode 100644 index 00000000000..ab054d112e9 --- /dev/null +++ b/config-model/src/test/cfg/application/ml_serving/models/add_mul.onnx @@ -0,0 +1,24 @@ + +add_mul.py:£ + +input1 +input2output1"Mul + +input1 +input2output2"Addadd_mulZ +input1 + + +Z +input2 + + +b +output1 + + +b +output2 + + +B
\ No newline at end of file diff --git a/config-model/src/test/cfg/application/ml_serving/models/mnist_softmax.onnx b/config-model/src/test/cfg/application/ml_serving/models/mnist_softmax.onnx Binary files differdeleted file mode 100644 index a86019bf53a..00000000000 --- a/config-model/src/test/cfg/application/ml_serving/models/mnist_softmax.onnx +++ /dev/null diff --git a/config-model/src/test/cfg/application/ml_serving/models/sqrt.onnx b/config-model/src/test/cfg/application/ml_serving/models/sqrt.onnx new file mode 100644 index 00000000000..04a6420002c --- /dev/null +++ b/config-model/src/test/cfg/application/ml_serving/models/sqrt.onnx @@ -0,0 +1,11 @@ +sqrt.py:V + +input
out/layer/1:1"SqrtsqrtZ +input + + +b +
out/layer/1:1 + + +B
\ No newline at end of file diff --git a/config-model/src/test/cfg/application/ml_serving/models/sqrt.py b/config-model/src/test/cfg/application/ml_serving/models/sqrt.py new file mode 100644 index 00000000000..b7b99b3850c --- /dev/null +++ b/config-model/src/test/cfg/application/ml_serving/models/sqrt.py @@ -0,0 +1,23 @@ +# Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +import onnx +from onnx import helper, TensorProto + +INPUT = helper.make_tensor_value_info('input', TensorProto.FLOAT, [1]) +OUTPUT = helper.make_tensor_value_info('out/layer/1:1', TensorProto.FLOAT, [1]) + +nodes = [ + helper.make_node( + 'Sqrt', + ['input'], + ['out/layer/1:1'], + ), +] +graph_def = helper.make_graph( + nodes, + 'sqrt', + [INPUT], + [OUTPUT], +) +model_def = helper.make_model(graph_def, producer_name='sqrt.py', opset_imports=[onnx.OperatorSetIdProto(version=12)]) +onnx.save(model_def, 'sqrt.onnx') diff --git a/config-model/src/test/cfg/application/onnx/files/add.onnx b/config-model/src/test/cfg/application/onnx/files/add.onnx new file mode 100644 index 00000000000..28318dbba4d --- /dev/null +++ b/config-model/src/test/cfg/application/onnx/files/add.onnx @@ -0,0 +1,16 @@ +add.py:f + +input1 +input2output"AddaddZ +input1 + + +Z +input2 + + +b +output + + +B
\ No newline at end of file diff --git a/config-model/src/test/cfg/application/onnx/files/add.py b/config-model/src/test/cfg/application/onnx/files/add.py new file mode 100755 index 00000000000..63b7dc87796 --- /dev/null +++ b/config-model/src/test/cfg/application/onnx/files/add.py @@ -0,0 +1,26 @@ +# Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +import onnx +from onnx import helper, TensorProto + +INPUT_1 = helper.make_tensor_value_info('input1', TensorProto.FLOAT, [1]) +INPUT_2 = helper.make_tensor_value_info('input2', TensorProto.FLOAT, [1]) +OUTPUT = helper.make_tensor_value_info('output', TensorProto.FLOAT, [1]) + +nodes = [ + helper.make_node( + 'Add', + ['input1', 'input2'], + ['output'], + ), +] +graph_def = helper.make_graph( + nodes, + 'add', + [ + INPUT_1, + INPUT_2 + ], + [OUTPUT], +) +model_def = helper.make_model(graph_def, producer_name='add.py', opset_imports=[onnx.OperatorSetIdProto(version=12)]) +onnx.save(model_def, 'add.onnx') diff --git a/config-model/src/test/cfg/application/onnx/models/mul.onnx b/config-model/src/test/cfg/application/onnx/models/mul.onnx new file mode 100644 index 00000000000..087e2c3427f --- /dev/null +++ b/config-model/src/test/cfg/application/onnx/models/mul.onnx @@ -0,0 +1,16 @@ +mul.py:f + +input1 +input2output"MulmulZ +input1 + + +Z +input2 + + +b +output + + +B
\ No newline at end of file diff --git a/config-model/src/test/cfg/application/onnx/models/mul.py b/config-model/src/test/cfg/application/onnx/models/mul.py new file mode 100755 index 00000000000..db01561c355 --- /dev/null +++ b/config-model/src/test/cfg/application/onnx/models/mul.py @@ -0,0 +1,26 @@ +# Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +import onnx +from onnx import helper, TensorProto + +INPUT_1 = helper.make_tensor_value_info('input1', TensorProto.FLOAT, [1]) +INPUT_2 = helper.make_tensor_value_info('input2', TensorProto.FLOAT, [1]) +OUTPUT = helper.make_tensor_value_info('output', TensorProto.FLOAT, [1]) + +nodes = [ + helper.make_node( + 'Mul', + ['input1', 'input2'], + ['output'], + ), +] +graph_def = helper.make_graph( + nodes, + 'mul', + [ + INPUT_1, + INPUT_2 + ], + [OUTPUT], +) +model_def = helper.make_model(graph_def, producer_name='mul.py', opset_imports=[onnx.OperatorSetIdProto(version=12)]) +onnx.save(model_def, 'mul.onnx') diff --git a/config-model/src/test/cfg/application/onnx/searchdefinitions/test.sd b/config-model/src/test/cfg/application/onnx/searchdefinitions/test.sd new file mode 100644 index 00000000000..d49782ddf39 --- /dev/null +++ b/config-model/src/test/cfg/application/onnx/searchdefinitions/test.sd @@ -0,0 +1,27 @@ +# Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +search test { + + document test { + field document_value type tensor<float>(d0[1]) { + indexing: attribute + } + } + + onnx-model my_add { + file: files/add.onnx + input input1: attribute(document_value) + input input2: my_input_func + output output: out + } + + rank-profile test { + function my_function() { + expression: tensor<float>(d0[1])(1) + } + first-phase { + expression: onnx(my_add).out{d0:1} + } + } + +} diff --git a/config-model/src/test/cfg/application/onnx/services.xml b/config-model/src/test/cfg/application/onnx/services.xml new file mode 100644 index 00000000000..8731558c6f7 --- /dev/null +++ b/config-model/src/test/cfg/application/onnx/services.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8" ?> +<!-- Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. --> +<services version="1.0"> + + <container version="1.0"> + <model-evaluation/> + <nodes> + <node hostalias="node1" /> + </nodes> + </container> + + <content id="test" version="1.0"> + <redundancy>1</redundancy> + <documents> + <document mode="index" type="test"/> + </documents> + <nodes> + <node distribution-key="0" hostalias="node1" /> + </nodes> + </content> + +</services> diff --git a/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionWithOnnxTestCase.java b/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionWithOnnxTestCase.java index 40bf970a313..a64b36b327d 100644 --- a/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionWithOnnxTestCase.java +++ b/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionWithOnnxTestCase.java @@ -44,30 +44,6 @@ public class RankingExpressionWithOnnxTestCase { } @Test - public void testGlobalOnnxModel() throws IOException { - ImportedModelTester tester = new ImportedModelTester(name, applicationDir); - VespaModel model = tester.createVespaModel(); - tester.assertLargeConstant(name + "_layer_Variable_1", model, Optional.of(10L)); - tester.assertLargeConstant(name + "_layer_Variable", model, Optional.of(7840L)); - - // At this point the expression is stored - copy application to another location which do not have a models dir - Path storedAppDir = applicationDir.append("copy"); - try { - storedAppDir.toFile().mkdirs(); - IOUtils.copy(applicationDir.append("services.xml").toString(), storedAppDir.append("services.xml").toString()); - IOUtils.copyDirectory(applicationDir.append(ApplicationPackage.MODELS_GENERATED_DIR).toFile(), - storedAppDir.append(ApplicationPackage.MODELS_GENERATED_DIR).toFile()); - ImportedModelTester storedTester = new ImportedModelTester(name, storedAppDir); - VespaModel storedModel = storedTester.createVespaModel(); - tester.assertLargeConstant(name + "_layer_Variable_1", storedModel, Optional.of(10L)); - tester.assertLargeConstant(name + "_layer_Variable", storedModel, Optional.of(7840L)); - } - finally { - IOUtils.recursiveDeleteDir(storedAppDir.toFile()); - } - } - - @Test public void testOnnxReferenceWithConstantFeature() { RankProfileSearchFixture search = fixtureWith("constant(mytensor)", "onnx_vespa('mnist_softmax.onnx')", diff --git a/config-model/src/test/java/com/yahoo/vespa/model/ml/ModelEvaluationTest.java b/config-model/src/test/java/com/yahoo/vespa/model/ml/ModelEvaluationTest.java index 9f18cfb4bd9..bdae01d5e09 100644 --- a/config-model/src/test/java/com/yahoo/vespa/model/ml/ModelEvaluationTest.java +++ b/config-model/src/test/java/com/yahoo/vespa/model/ml/ModelEvaluationTest.java @@ -3,16 +3,13 @@ package com.yahoo.vespa.model.ml; import ai.vespa.models.evaluation.Model; import ai.vespa.models.evaluation.ModelsEvaluator; -import ai.vespa.models.evaluation.RankProfilesConfigImporter; import ai.vespa.models.handler.ModelsEvaluationHandler; import com.yahoo.component.ComponentId; -import com.yahoo.config.FileReference; import com.yahoo.config.application.api.ApplicationPackage; import com.yahoo.filedistribution.fileacquirer.FileAcquirer; import com.yahoo.filedistribution.fileacquirer.MockFileAcquirer; import com.yahoo.io.IOUtils; import com.yahoo.path.Path; -import com.yahoo.tensor.Tensor; import com.yahoo.tensor.TensorType; import com.yahoo.vespa.config.search.RankProfilesConfig; import com.yahoo.vespa.config.search.core.OnnxModelsConfig; @@ -22,7 +19,10 @@ import com.yahoo.vespa.model.VespaModel; import com.yahoo.vespa.model.container.ApplicationContainerCluster; import org.junit.Test; +import java.io.File; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -65,7 +65,7 @@ public class ModelEvaluationTest { Path storedAppDir = appDir.append("copy"); try { ImportedModelTester tester = new ImportedModelTester("ml_serving", appDir); - assertHasMlModels(tester.createVespaModel()); + assertHasMlModels(tester.createVespaModel(), appDir); // At this point the expression is stored - copy application to another location which do not have a models dir storedAppDir.toFile().mkdirs(); @@ -73,7 +73,7 @@ public class ModelEvaluationTest { IOUtils.copyDirectory(appDir.append(ApplicationPackage.MODELS_GENERATED_DIR).toFile(), storedAppDir.append(ApplicationPackage.MODELS_GENERATED_DIR).toFile()); ImportedModelTester storedTester = new ImportedModelTester("ml_serving", storedAppDir); - assertHasMlModels(storedTester.createVespaModel()); + assertHasMlModels(storedTester.createVespaModel(), appDir); } finally { IOUtils.recursiveDeleteDir(appDir.append(ApplicationPackage.MODELS_GENERATED_DIR).toFile()); @@ -81,7 +81,7 @@ public class ModelEvaluationTest { } } - private void assertHasMlModels(VespaModel model) { + private void assertHasMlModels(VespaModel model, Path appDir) { ApplicationContainerCluster cluster = model.getContainerClusters().get("container"); assertNotNull(cluster.getComponentsMap().get(new ComponentId(ModelsEvaluator.class.getName()))); @@ -105,12 +105,13 @@ public class ModelEvaluationTest { cluster.getConfig(ob); OnnxModelsConfig onnxModelsConfig = new OnnxModelsConfig(ob); - assertEquals(4, config.rankprofile().size()); + assertEquals(5, config.rankprofile().size()); Set<String> modelNames = config.rankprofile().stream().map(v -> v.name()).collect(Collectors.toSet()); assertTrue(modelNames.contains("xgboost_2_2")); assertTrue(modelNames.contains("lightgbm_regression")); - assertTrue(modelNames.contains("mnist_softmax")); + assertTrue(modelNames.contains("add_mul")); assertTrue(modelNames.contains("small_constants_and_functions")); + assertTrue(modelNames.contains("sqrt")); // Compare profile content in a denser format than config: StringBuilder sb = new StringBuilder(); @@ -118,10 +119,14 @@ public class ModelEvaluationTest { sb.append(p.name()).append(": ").append(p.value()).append("\n"); assertEquals(profile, sb.toString()); - ModelsEvaluator evaluator = new ModelsEvaluator(new ToleratingMissingConstantFilesRankProfilesConfigImporter(MockFileAcquirer.returnFile(null)) - .importFrom(config, constantsConfig, expressionsConfig, onnxModelsConfig)); + Map<String, File> fileMap = new HashMap<>(); + for (OnnxModelsConfig.Model onnxModel : onnxModelsConfig.model()) { + fileMap.put(onnxModel.fileref().value(), appDir.append(onnxModel.fileref().value()).toFile()); + } + FileAcquirer fileAcquirer = MockFileAcquirer.returnFiles(fileMap); + ModelsEvaluator evaluator = new ModelsEvaluator(config, constantsConfig, onnxModelsConfig, fileAcquirer); - assertEquals(4, evaluator.models().size()); + assertEquals(5, evaluator.models().size()); Model xgboost = evaluator.models().get("xgboost_2_2"); assertNotNull(xgboost); @@ -133,31 +138,37 @@ public class ModelEvaluationTest { assertNotNull(lightgbm.evaluatorOf()); assertNotNull(lightgbm.evaluatorOf("lightgbm_regression")); - Model onnx_mnist_softmax = evaluator.models().get("mnist_softmax"); - assertNotNull(onnx_mnist_softmax); - assertEquals(1, onnx_mnist_softmax.functions().size()); - assertNotNull(onnx_mnist_softmax.evaluatorOf()); - assertNotNull(onnx_mnist_softmax.evaluatorOf("default")); - assertNotNull(onnx_mnist_softmax.evaluatorOf("default", "add")); - assertNotNull(onnx_mnist_softmax.evaluatorOf("default.add")); - assertNotNull(onnx_mnist_softmax.evaluatorOf("add")); - assertNotNull(onnx_mnist_softmax.evaluatorOf("serving_default")); - assertNotNull(onnx_mnist_softmax.evaluatorOf("serving_default", "add")); - assertNotNull(onnx_mnist_softmax.evaluatorOf("serving_default.add")); - assertNotNull(evaluator.evaluatorOf("mnist_softmax", "default.add")); - assertNotNull(evaluator.evaluatorOf("mnist_softmax", "default", "add")); - assertNotNull(evaluator.evaluatorOf("mnist_softmax", "add")); - assertNotNull(evaluator.evaluatorOf("mnist_softmax", "serving_default.add")); - assertNotNull(evaluator.evaluatorOf("mnist_softmax", "serving_default", "add")); - assertEquals(TensorType.fromSpec("tensor<float>(d0[],d1[784])"), onnx_mnist_softmax.functions().get(0).argumentTypes().get("Placeholder")); + Model add_mul = evaluator.models().get("add_mul"); + assertNotNull(add_mul); + assertEquals(2, add_mul.functions().size()); + assertNotNull(add_mul.evaluatorOf("output1")); + assertNotNull(add_mul.evaluatorOf("output2")); + assertNotNull(add_mul.evaluatorOf("default.output1")); + assertNotNull(add_mul.evaluatorOf("default.output2")); + assertNotNull(add_mul.evaluatorOf("default", "output1")); + assertNotNull(add_mul.evaluatorOf("default", "output2")); + assertNotNull(evaluator.evaluatorOf("add_mul", "output1")); + assertNotNull(evaluator.evaluatorOf("add_mul", "output2")); + assertNotNull(evaluator.evaluatorOf("add_mul", "default.output1")); + assertNotNull(evaluator.evaluatorOf("add_mul", "default.output2")); + assertNotNull(evaluator.evaluatorOf("add_mul", "default", "output1")); + assertNotNull(evaluator.evaluatorOf("add_mul", "default", "output2")); + assertEquals(TensorType.fromSpec("tensor<float>(d0[1])"), add_mul.functions().get(0).argumentTypes().get("input1")); + assertEquals(TensorType.fromSpec("tensor<float>(d0[1])"), add_mul.functions().get(0).argumentTypes().get("input2")); + + Model sqrt = evaluator.models().get("sqrt"); + assertNotNull(sqrt); + assertEquals(1, sqrt.functions().size()); + assertNotNull(sqrt.evaluatorOf()); + assertNotNull(sqrt.evaluatorOf("out_layer_1_1")); // converted from "out/layer/1:1" + assertNotNull(evaluator.evaluatorOf("sqrt")); + assertNotNull(evaluator.evaluatorOf("sqrt", "out_layer_1_1")); + assertEquals(TensorType.fromSpec("tensor<float>(d0[1])"), sqrt.functions().get(0).argumentTypes().get("input")); } private final String profile = - "rankingExpression(imported_ml_function_small_constants_and_functions_exp_output).rankingScript: map(input, f(a)(exp(a)))\n" + - "rankingExpression(imported_ml_function_small_constants_and_functions_exp_output).type: tensor<float>(d0[3])\n" + - "rankingExpression(default.output).rankingScript: join(rankingExpression(imported_ml_function_small_constants_and_functions_exp_output), reduce(join(join(reduce(rankingExpression(imported_ml_function_small_constants_and_functions_exp_output), sum, d0), tensor<float>(d0[1])(1.0), f(a,b)(a * b)), 9.999999974752427E-7, f(a,b)(a + b)), sum, d0), f(a,b)(a / b))\n" + - "rankingExpression(default.output).input.type: tensor<float>(d0[3])\n" + - "rankingExpression(default.output).type: tensor<float>(d0[3])\n"; + "rankingExpression(output).rankingScript: onnxModel(small_constants_and_functions).output\n" + + "rankingExpression(output).type: tensor<float>(d0[3])\n"; private RankProfilesConfig.Rankprofile.Fef findProfile(String name, RankProfilesConfig config) { for (RankProfilesConfig.Rankprofile profile : config.rankprofile()) { @@ -167,17 +178,4 @@ public class ModelEvaluationTest { throw new IllegalArgumentException("No profile named " + name); } - // We don't have function file distribution so just return empty tensor constants - private static class ToleratingMissingConstantFilesRankProfilesConfigImporter extends RankProfilesConfigImporter { - - public ToleratingMissingConstantFilesRankProfilesConfigImporter(FileAcquirer fileAcquirer) { - super(fileAcquirer); - } - - protected Tensor readTensorFromFile(String name, TensorType type, FileReference fileReference) { - return Tensor.from(type, "{}"); - } - - } - } diff --git a/config-model/src/test/java/com/yahoo/vespa/model/ml/StatelessOnnxEvaluationTest.java b/config-model/src/test/java/com/yahoo/vespa/model/ml/StatelessOnnxEvaluationTest.java new file mode 100644 index 00000000000..5dea4a04229 --- /dev/null +++ b/config-model/src/test/java/com/yahoo/vespa/model/ml/StatelessOnnxEvaluationTest.java @@ -0,0 +1,108 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.model.ml; + +import ai.vespa.models.evaluation.FunctionEvaluator; +import ai.vespa.models.evaluation.Model; +import ai.vespa.models.evaluation.ModelsEvaluator; +import ai.vespa.models.evaluation.RankProfilesConfigImporter; +import ai.vespa.models.handler.ModelsEvaluationHandler; +import com.yahoo.component.ComponentId; +import com.yahoo.config.FileReference; +import com.yahoo.config.application.api.ApplicationPackage; +import com.yahoo.filedistribution.fileacquirer.FileAcquirer; +import com.yahoo.filedistribution.fileacquirer.MockFileAcquirer; +import com.yahoo.io.IOUtils; +import com.yahoo.path.Path; +import com.yahoo.tensor.Tensor; +import com.yahoo.tensor.TensorType; +import com.yahoo.vespa.config.search.RankProfilesConfig; +import com.yahoo.vespa.config.search.core.OnnxModelsConfig; +import com.yahoo.vespa.config.search.core.RankingConstantsConfig; +import com.yahoo.vespa.model.VespaModel; +import com.yahoo.vespa.model.container.ApplicationContainerCluster; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * Tests stateless model evaluation (turned on by the "model-evaluation" tag in "container") + * for ONNX models. + * + * @author lesters + */ +public class StatelessOnnxEvaluationTest { + + @Test + public void testStatelessOnnxModelEvaluation() throws IOException { + Path appDir = Path.fromString("src/test/cfg/application/onnx"); + Path storedAppDir = appDir.append("copy"); + try { + ImportedModelTester tester = new ImportedModelTester("onnx_rt", appDir); + assertModelEvaluation(tester.createVespaModel(), appDir); + + // At this point the expression is stored - copy application to another location which does not have a models dir + storedAppDir.toFile().mkdirs(); + IOUtils.copy(appDir.append("services.xml").toString(), storedAppDir.append("services.xml").toString()); + IOUtils.copyDirectory(appDir.append(ApplicationPackage.MODELS_GENERATED_DIR).toFile(), + storedAppDir.append(ApplicationPackage.MODELS_GENERATED_DIR).toFile()); + IOUtils.copyDirectory(appDir.append(ApplicationPackage.SEARCH_DEFINITIONS_DIR).toFile(), + storedAppDir.append(ApplicationPackage.SEARCH_DEFINITIONS_DIR).toFile()); + ImportedModelTester storedTester = new ImportedModelTester("onnx_rt", storedAppDir); + assertModelEvaluation(storedTester.createVespaModel(), appDir); + + } finally { + IOUtils.recursiveDeleteDir(appDir.append(ApplicationPackage.MODELS_GENERATED_DIR).toFile()); + IOUtils.recursiveDeleteDir(storedAppDir.toFile()); + } + } + + private void assertModelEvaluation(VespaModel model, Path appDir) { + ApplicationContainerCluster cluster = model.getContainerClusters().get("container"); + assertNotNull(cluster.getComponentsMap().get(new ComponentId(ModelsEvaluator.class.getName()))); + + RankProfilesConfig.Builder b = new RankProfilesConfig.Builder(); + cluster.getConfig(b); + RankProfilesConfig config = new RankProfilesConfig(b); + + RankingConstantsConfig.Builder cb = new RankingConstantsConfig.Builder(); + cluster.getConfig(cb); + RankingConstantsConfig constantsConfig = new RankingConstantsConfig(cb); + + OnnxModelsConfig.Builder ob = new OnnxModelsConfig.Builder(); + cluster.getConfig(ob); + OnnxModelsConfig onnxModelsConfig = new OnnxModelsConfig(ob); + + assertEquals(1, config.rankprofile().size()); + Set<String> modelNames = config.rankprofile().stream().map(v -> v.name()).collect(Collectors.toSet()); + assertTrue(modelNames.contains("mul")); + + // This is actually how ModelsEvaluator is injected + Map<String, File> fileMap = new HashMap<>(); + for (OnnxModelsConfig.Model onnxModel : onnxModelsConfig.model()) { + fileMap.put(onnxModel.fileref().value(), appDir.append(onnxModel.fileref().value()).toFile()); + } + FileAcquirer fileAcquirer = MockFileAcquirer.returnFiles(fileMap); + ModelsEvaluator modelsEvaluator = new ModelsEvaluator(config, constantsConfig, onnxModelsConfig, fileAcquirer); + assertEquals(1, modelsEvaluator.models().size()); + + Model mul = modelsEvaluator.models().get("mul"); + FunctionEvaluator evaluator = mul.evaluatorOf(); // or "default.output" - or actually use name of model output + + Tensor input1 = Tensor.from("tensor<float>(d0[1]):[2]"); + Tensor input2 = Tensor.from("tensor<float>(d0[1]):[3]"); + Tensor output = evaluator.bind("input1", input1).bind("input2", input2).evaluate(); + assertEquals(6.0, output.sum().asDouble(), 1e-9); + + } + +} |