diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-11-10 09:31:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-10 09:31:39 +0100 |
commit | bc091d2ebecd7add8703fc1d062c3b24e5dadf4d (patch) | |
tree | 207c9dc61d3207e4630e73dc51ac5bdff1ac6d40 | |
parent | ba3ac72d3146d9e64a4bf32c993fd4969b272c7d (diff) | |
parent | eebbd7db35d858a392160dbe5067b2f52b1640a8 (diff) |
Merge pull request #24815 from vespa-engine/balder/model-importing-code-in-config-model-5
Balder/model importing code in confiig-model [run-systemtest]
9 files changed, 56 insertions, 28 deletions
diff --git a/config-model-fat/pom.xml b/config-model-fat/pom.xml index 44880d052b6..7be7f8eee94 100644 --- a/config-model-fat/pom.xml +++ b/config-model-fat/pom.xml @@ -37,12 +37,6 @@ </exclusion> </exclusions> </dependency> - <dependency> - <groupId>com.yahoo.vespa</groupId> - <artifactId>model-integration</artifactId> - <version>${project.version}</version> - <scope>provided</scope> - </dependency> </dependencies> <build> diff --git a/config-model/src/main/java/com/yahoo/vespa/model/VespaModelFactory.java b/config-model/src/main/java/com/yahoo/vespa/model/VespaModelFactory.java index e676667ae89..1ba8b470891 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/VespaModelFactory.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/VespaModelFactory.java @@ -2,6 +2,11 @@ package com.yahoo.vespa.model; import ai.vespa.rankingexpression.importer.configmodelview.MlModelImporter; +import ai.vespa.rankingexpression.importer.lightgbm.LightGBMImporter; +import ai.vespa.rankingexpression.importer.onnx.OnnxImporter; +import ai.vespa.rankingexpression.importer.tensorflow.TensorFlowImporter; +import ai.vespa.rankingexpression.importer.vespa.VespaImporter; +import ai.vespa.rankingexpression.importer.xgboost.XGBoostImporter; import com.yahoo.component.annotation.Inject; import com.yahoo.component.Version; import com.yahoo.component.provider.ComponentRegistry; @@ -56,7 +61,6 @@ public class VespaModelFactory implements ModelFactory { /** Creates a factory for Vespa models for this version of the source */ @Inject public VespaModelFactory(ComponentRegistry<ConfigModelPlugin> pluginRegistry, - ComponentRegistry<MlModelImporter> modelImporters, ComponentRegistry<Validator> additionalValidators, Zone zone) { this.version = new Version(VespaVersion.major, VespaVersion.minor, VespaVersion.micro); @@ -67,7 +71,12 @@ public class VespaModelFactory implements ModelFactory { } } this.configModelRegistry = new MapConfigModelRegistry(modelBuilders); - this.modelImporters = modelImporters.allComponents(); + this.modelImporters = List.of( + new VespaImporter(), + new OnnxImporter(), + new TensorFlowImporter(), + new XGBoostImporter(), + new LightGBMImporter()); this.zone = zone; this.additionalValidators = List.copyOf(additionalValidators.allComponents()); 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 9ba87fd24bf..96a6b39dc1a 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 @@ -199,8 +199,8 @@ public class ConvertedModel { ModelStore store) { // Add constants Set<String> constantsReplacedByFunctions = new HashSet<>(); - model.smallConstants().forEach((k, v) -> transformSmallConstant(store, profile, k, v)); - model.largeConstants().forEach((k, v) -> transformLargeConstant(store, profile, queryProfiles, + model.smallConstantTensors().forEach((k, v) -> transformSmallConstant(store, profile, k, v)); + model.largeConstantTensors().forEach((k, v) -> transformLargeConstant(store, profile, queryProfiles, constantsReplacedByFunctions, k, v)); // Add functions @@ -294,8 +294,7 @@ public class ConvertedModel { } private static void transformSmallConstant(ModelStore store, RankProfile profile, String constantName, - String constantValueString) { - Tensor constantValue = Tensor.from(constantValueString); + Tensor constantValue) { store.writeSmallConstant(constantName, constantValue); Reference name = FeatureNames.asConstantFeature(constantName); profile.add(new RankProfile.Constant(name, constantValue)); @@ -306,8 +305,7 @@ public class ConvertedModel { QueryProfileRegistry queryProfiles, Set<String> constantsReplacedByFunctions, String constantName, - String constantValueString) { - Tensor constantValue = Tensor.from(constantValueString); + Tensor constantValue) { RankProfile.RankingExpressionFunction rankingExpressionFunctionOverridingConstant = profile.getFunctions().get(constantName); if (rankingExpressionFunctionOverridingConstant != null) { TensorType functionType = rankingExpressionFunctionOverridingConstant.function().getBody().type(profile.typeContext(queryProfiles)); diff --git a/fat-model-dependencies/pom.xml b/fat-model-dependencies/pom.xml index f3312bd5d89..9801098d5b6 100644 --- a/fat-model-dependencies/pom.xml +++ b/fat-model-dependencies/pom.xml @@ -71,6 +71,11 @@ </dependency> <dependency> <groupId>com.yahoo.vespa</groupId> + <artifactId>model-integration</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>com.yahoo.vespa</groupId> <artifactId>metrics</artifactId> <version>${project.version}</version> </dependency> diff --git a/model-integration/src/main/java/ai/vespa/rankingexpression/importer/ImportedModel.java b/model-integration/src/main/java/ai/vespa/rankingexpression/importer/ImportedModel.java index 4e7710aa449..35c409a637c 100644 --- a/model-integration/src/main/java/ai/vespa/rankingexpression/importer/ImportedModel.java +++ b/model-integration/src/main/java/ai/vespa/rankingexpression/importer/ImportedModel.java @@ -81,10 +81,19 @@ public class ImportedModel implements ImportedMlModel { } /** + * Returns an immutable map of the small constants of this. + * These should have sizes up to a few kb at most, and correspond to constant values given in the source model. + */ + @Override + public Map<String, Tensor> smallConstantTensors() { return Map.copyOf(smallConstants); } + /** * Returns an immutable map of the small constants of this, represented as strings on the standard tensor form. * These should have sizes up to a few kb at most, and correspond to constant values given in the source model. + * @deprecated Use smallConstantTensors instead */ @Override + @SuppressWarnings("removal") + @Deprecated(forRemoval = true) public Map<String, String> smallConstants() { return asStrings(smallConstants); } boolean hasSmallConstant(String name) { return smallConstants.containsKey(name); } @@ -92,9 +101,17 @@ public class ImportedModel implements ImportedMlModel { /** * Returns an immutable map of the large constants of this. * These can have sizes in gigabytes and must be distributed to nodes separately from configuration. - * For TensorFlow this corresponds to Variable files stored separately. */ @Override + public Map<String, Tensor> largeConstantTensors() { return Map.copyOf(largeConstants); } + /** + * Returns an immutable map of the large constants of this, represented as strings on the standard tensor form. + * These can have sizes in gigabytes and must be distributed to nodes separately from configuration. + * @deprecated Use largeConstantTensors instead + */ + @Override + @SuppressWarnings("removal") + @Deprecated(forRemoval = true) public Map<String, String> largeConstants() { return asStrings(largeConstants); } boolean hasLargeConstant(String name) { return largeConstants.containsKey(name); } diff --git a/model-integration/src/main/java/ai/vespa/rankingexpression/importer/configmodelview/ImportedMlModel.java b/model-integration/src/main/java/ai/vespa/rankingexpression/importer/configmodelview/ImportedMlModel.java index a2626818f87..8c8fc5c4b11 100644 --- a/model-integration/src/main/java/ai/vespa/rankingexpression/importer/configmodelview/ImportedMlModel.java +++ b/model-integration/src/main/java/ai/vespa/rankingexpression/importer/configmodelview/ImportedMlModel.java @@ -1,6 +1,8 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package ai.vespa.rankingexpression.importer.configmodelview; +import com.yahoo.tensor.Tensor; + import java.util.List; import java.util.Map; import java.util.Optional; @@ -21,8 +23,12 @@ public interface ImportedMlModel { ModelType modelType(); Optional<String> inputTypeSpec(String input); + @Deprecated(forRemoval = true) Map<String, String> smallConstants(); + @Deprecated(forRemoval = true) Map<String, String> largeConstants(); + Map<String, Tensor> smallConstantTensors(); + Map<String, Tensor> largeConstantTensors(); Map<String, String> functions(); List<ImportedMlFunction> outputExpressions(); diff --git a/model-integration/src/test/java/ai/vespa/rankingexpression/importer/onnx/OnnxMnistSoftmaxImportTestCase.java b/model-integration/src/test/java/ai/vespa/rankingexpression/importer/onnx/OnnxMnistSoftmaxImportTestCase.java index b6b63912c52..7fb167ee6f1 100644 --- a/model-integration/src/test/java/ai/vespa/rankingexpression/importer/onnx/OnnxMnistSoftmaxImportTestCase.java +++ b/model-integration/src/test/java/ai/vespa/rankingexpression/importer/onnx/OnnxMnistSoftmaxImportTestCase.java @@ -21,15 +21,15 @@ public class OnnxMnistSoftmaxImportTestCase { ImportedModel model = new OnnxImporter().importModel("test", "src/test/models/onnx/mnist_softmax/mnist_softmax.onnx").asNative(); // Check constants - assertEquals(2, model.largeConstants().size()); + assertEquals(2, model.largeConstantTensors().size()); - Tensor constant0 = Tensor.from(model.largeConstants().get("test_Variable")); + Tensor constant0 = model.largeConstantTensors().get("test_Variable"); assertNotNull(constant0); assertEquals(new TensorType.Builder(TensorType.Value.FLOAT).indexed("d2", 784).indexed("d1", 10).build(), constant0.type()); assertEquals(7840, constant0.size()); - Tensor constant1 = Tensor.from(model.largeConstants().get("test_Variable_1")); + Tensor constant1 = model.largeConstantTensors().get("test_Variable_1"); assertNotNull(constant1); assertEquals(new TensorType.Builder(TensorType.Value.FLOAT).indexed("d1", 10).build(), constant1.type()); assertEquals(10, constant1.size()); diff --git a/model-integration/src/test/java/ai/vespa/rankingexpression/importer/onnx/TestableModel.java b/model-integration/src/test/java/ai/vespa/rankingexpression/importer/onnx/TestableModel.java index 5d13697df06..f78150d9875 100644 --- a/model-integration/src/test/java/ai/vespa/rankingexpression/importer/onnx/TestableModel.java +++ b/model-integration/src/test/java/ai/vespa/rankingexpression/importer/onnx/TestableModel.java @@ -63,8 +63,8 @@ public class TestableModel { static Context contextFrom(ImportedModel result) { TestableModelContext context = new TestableModelContext(); - result.largeConstants().forEach((name, tensor) -> context.put("constant(" + name + ")", new TensorValue(Tensor.from(tensor)))); - result.smallConstants().forEach((name, tensor) -> context.put("constant(" + name + ")", new TensorValue(Tensor.from(tensor)))); + result.largeConstantTensors().forEach((name, tensor) -> context.put("constant(" + name + ")", new TensorValue(tensor))); + result.smallConstantTensors().forEach((name, tensor) -> context.put("constant(" + name + ")", new TensorValue(tensor))); return context; } diff --git a/model-integration/src/test/java/ai/vespa/rankingexpression/importer/vespa/VespaImportTestCase.java b/model-integration/src/test/java/ai/vespa/rankingexpression/importer/vespa/VespaImportTestCase.java index 25c51a75b0b..d9c7e67c946 100644 --- a/model-integration/src/test/java/ai/vespa/rankingexpression/importer/vespa/VespaImportTestCase.java +++ b/model-integration/src/test/java/ai/vespa/rankingexpression/importer/vespa/VespaImportTestCase.java @@ -4,7 +4,6 @@ package ai.vespa.rankingexpression.importer.vespa; import ai.vespa.rankingexpression.importer.ImportedModel; import ai.vespa.rankingexpression.importer.configmodelview.ImportedMlFunction; import com.yahoo.searchlib.rankingexpression.RankingExpression; -import com.yahoo.searchlib.rankingexpression.evaluation.Context; import com.yahoo.searchlib.rankingexpression.evaluation.MapContext; import com.yahoo.searchlib.rankingexpression.evaluation.TensorValue; import com.yahoo.searchlib.rankingexpression.parser.ParseException; @@ -39,12 +38,12 @@ public class VespaImportTestCase { assertEquals("tensor(name{},x[3])", model.inputs().get("input1").toString()); assertEquals("tensor(x[3])", model.inputs().get("input2").toString()); - assertEquals(2, model.smallConstants().size()); - assertEquals("tensor(x[3]):[0.5, 1.5, 2.5]", model.smallConstants().get("constant1")); - assertEquals("tensor():{3.0}", model.smallConstants().get("constant2")); + assertEquals(2, model.smallConstantTensors().size()); + assertEquals("tensor(x[3]):[0.5, 1.5, 2.5]", model.smallConstantTensors().get("constant1").toString()); + assertEquals("tensor():{3.0}", model.smallConstantTensors().get("constant2").toString()); - assertEquals(1, model.largeConstants().size()); - assertEquals("tensor(x[3]):[0.5, 1.5, 2.5]", model.largeConstants().get("constant1asLarge")); + assertEquals(1, model.largeConstantTensors().size()); + assertEquals("tensor(x[3]):[0.5, 1.5, 2.5]", model.largeConstantTensors().get("constant1asLarge").toString()); assertEquals(2, model.expressions().size()); assertEquals("reduce(reduce(input1 * input2, sum, name) * constant1, max, x) * constant2", @@ -72,8 +71,8 @@ public class VespaImportTestCase { assertTrue(model.expressions().isEmpty()); assertTrue(model.functions().isEmpty()); assertTrue(model.inputs().isEmpty()); - assertTrue(model.largeConstants().isEmpty()); - assertTrue(model.smallConstants().isEmpty()); + assertTrue(model.largeConstantTensors().isEmpty()); + assertTrue(model.smallConstantTensors().isEmpty()); } @Test |