diff options
author | Arnstein Ressem <aressem@gmail.com> | 2022-05-18 13:45:07 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-18 13:45:07 +0200 |
commit | c080a3c9b1ebd39b947aeddd1e5a0bf5e46d474c (patch) | |
tree | aad434533f9514747e80f339d87b984088e2f1f7 | |
parent | a4dbfc43c7df534ee5b032204ef19a7b038d7e3e (diff) |
Revert "Bratseth/model syntax"
13 files changed, 99 insertions, 487 deletions
diff --git a/config-model/src/main/javacc/SchemaParser.jj b/config-model/src/main/javacc/SchemaParser.jj index 86a623071b5..421894e51cd 100644 --- a/config-model/src/main/javacc/SchemaParser.jj +++ b/config-model/src/main/javacc/SchemaParser.jj @@ -1667,15 +1667,15 @@ void indexBody(ParsedIndex index) : double threshold; } { - ( <PREFIX> { index.setPrefix(true); } - | <ALIAS> <COLON> str = identifierWithDash() { index.addAlias(str); } - | <STEMMING> <COLON> str = identifierWithDash() { index.setStemming(Stemming.get(str)); } - | <ARITY> <COLON> arity = integer() { index.setArity(arity); } - | <LOWERBOUND> <COLON> num = longValue() { index.setLowerBound(num); } - | <UPPERBOUND> <COLON> num = longValue() { index.setUpperBound(num); } - | <DENSEPOSTINGLISTTHRESHOLD> <COLON> threshold = floatValue() { index.setDensePostingListThreshold(threshold); } - | <ENABLE_BM25> { index.setEnableBm25(true); } - | hnswIndex(index) { } + ( <PREFIX> { index.setPrefix(true); } + | <ALIAS> <COLON> str = identifierWithDash() { index.addAlias(str); } + | <STEMMING> <COLON> str = identifierWithDash() { index.setStemming(Stemming.get(str)); } + | <ARITY> <COLON> arity = integer() { index.setArity(arity); } + | <LOWERBOUND> <COLON> num = consumeLong() { index.setLowerBound(num); } + | <UPPERBOUND> <COLON> num = consumeLong() { index.setUpperBound(num); } + | <DENSEPOSTINGLISTTHRESHOLD> <COLON> threshold = consumeFloat() { index.setDensePostingListThreshold(threshold); } + | <ENABLE_BM25> { index.setEnableBm25(true); } + | hnswIndex(index) { } ) } @@ -1793,7 +1793,6 @@ String fileItem() : { (<FILE> <COLON> ( <FILE_PATH> | <STRING> | <IDENTIFIER>) { path = com.yahoo.path.Path.fromString(token.image).getRelative(); } { } (<NL>)*) { return path; } } - String uriItem() : { String path; @@ -1906,7 +1905,7 @@ String mutate_expr() : Number constant = null; } { - (("+=" | "-=" | "=") { op = token.image; } constant = number()) + (("+=" | "-=" | "=") { op = token.image; } constant = consumeNumber()) { return constant != null ? (op + constant) : op; } } @@ -1978,9 +1977,9 @@ void matchPhaseItem(MatchPhaseSettings settings) : | <ORDER> <COLON> ( <ASCENDING> { settings.setAscending(true); } | <DESCENDING> { settings.setAscending(false); } ) | <MAXHITS> <COLON> num = integer() { settings.setMaxHits(num); } - | <MAXFILTERCOVERAGE> <COLON> coverage = floatValue() { settings.setMaxFilterCoverage(coverage); } - | <EVALUATION_POINT> <COLON> multiplier = floatValue() { settings.setEvaluationPoint(multiplier); } - | <PRE_POST_FILTER_TIPPING_POINT> <COLON> multiplier = floatValue() { settings.setPrePostFilterTippingPoint(multiplier); } + | <MAXFILTERCOVERAGE> <COLON> coverage = consumeFloat() { settings.setMaxFilterCoverage(coverage); } + | <EVALUATION_POINT> <COLON> multiplier = consumeFloat() { settings.setEvaluationPoint(multiplier); } + | <PRE_POST_FILTER_TIPPING_POINT> <COLON> multiplier = consumeFloat() { settings.setPrePostFilterTippingPoint(multiplier); } ) } @@ -2009,7 +2008,7 @@ void diversityItem(DiversitySettings settings) : { ( <ATTRIBUTE> <COLON> str = identifier() { settings.setAttribute(str); } | <MIN_GROUPS> <COLON> num = integer() { settings.setMinGroups(num); } - | <CUTOFF_FACTOR> <COLON> multiplier = floatValue() { settings.setCutoffFactor(multiplier); } + | <CUTOFF_FACTOR> <COLON> multiplier = consumeFloat() { settings.setCutoffFactor(multiplier); } | <CUTOFF_STRATEGY> <COLON> ( <STRICT> { settings.setCutoffStrategy(Diversity.CutoffStrategy.strict); } | <LOOSE> { settings.setCutoffStrategy(Diversity.CutoffStrategy.loose); } @@ -2039,9 +2038,9 @@ void firstPhaseItem(ParsedRankProfile profile) : double dropLimit; } { - ( expression = expression() { profile.setFirstPhaseRanking(expression); } - | (<KEEPRANKCOUNT> <COLON> keepRankCount = integer()) { profile.setKeepRankCount(keepRankCount); } - | (<RANKSCOREDROPLIMIT> <COLON> dropLimit = floatValue()) { profile.setRankScoreDropLimit(dropLimit); } + ( expression = expression() { profile.setFirstPhaseRanking(expression); } + | (<KEEPRANKCOUNT> <COLON> keepRankCount = integer()) { profile.setKeepRankCount(keepRankCount); } + | (<RANKSCOREDROPLIMIT> <COLON> dropLimit = consumeFloat()) { profile.setRankScoreDropLimit(dropLimit); } ) } @@ -2255,7 +2254,7 @@ void termwiseLimit(ParsedRankProfile profile) : double num; } { - (<TERMWISELIMIT> <COLON> num = floatValue()) { profile.setTermwiseLimit(num); } + (<TERMWISELIMIT> <COLON> num = consumeFloat()) { profile.setTermwiseLimit(num); } } /** @@ -2268,7 +2267,7 @@ void postFilterThreshold(ParsedRankProfile profile) : double threshold; } { - (<POSTFILTERTHRESHOLD> <COLON> threshold = floatValue()) { profile.setPostFilterThreshold(threshold); } + (<POSTFILTERTHRESHOLD> <COLON> threshold = consumeFloat()) { profile.setPostFilterThreshold(threshold); } } /** @@ -2281,7 +2280,7 @@ void approximateThreshold(ParsedRankProfile profile) : double threshold; } { - (<APPROXIMATETHRESHOLD> <COLON> threshold = floatValue()) { profile.setApproximateThreshold(threshold); } + (<APPROXIMATETHRESHOLD> <COLON> threshold = consumeFloat()) { profile.setApproximateThreshold(threshold); } } /** @@ -2382,7 +2381,7 @@ void rankDegradationBinSize() : double freq; } { - <RPBINSIZE> <COLON> freq = floatValue() + <RPBINSIZE> <COLON> freq = consumeFloat() { deployLogger.logApplicationPackage(Level.WARNING, "Specifying 'doc-frequency' in 'rank-degradation' is deprecated and has no effect."); } } @@ -2407,7 +2406,7 @@ void rankDegradationPosbinSize() : double avgOcc; } { - <RPPOSBINSIZE> <COLON> avgOcc = floatValue() + <RPPOSBINSIZE> <COLON> avgOcc = consumeFloat() { deployLogger.logApplicationPackage(Level.WARNING, "Specifying 'occurrences-per-doc' in 'rank-degradation' is deprecated and has no effect."); } } @@ -2430,7 +2429,7 @@ void rankDegradation() : double freq; } { - ( <RANKDEGRADATIONFREQ> <COLON> freq = floatValue() + ( <RANKDEGRADATIONFREQ> <COLON> freq = consumeFloat() { deployLogger.logApplicationPackage(Level.WARNING, "Specifying 'rank-degradation-frequency' in 'rank-profile' is deprecated and has no effect."); } | <RANKDEGRADATION> lbrace() ( rankDegradationItem() (<NL>)*)+ <RBRACE> ) @@ -2527,7 +2526,7 @@ Tensor tensorValue(TensorType type) : Number doubleValue = null; } { - ( mappedTensorValue(builder) | indexedTensorValues(builder) | doubleValue = number() ) + ( mappedTensorValue(builder) | indexedTensorValues(builder) | doubleValue = consumeNumber() ) { if (doubleValue != null) { if (type.rank() > 0) @@ -2541,10 +2540,7 @@ Tensor tensorValue(TensorType type) : /** A mapped or mixed tensor value. */ void mappedTensorValue(Tensor.Builder builder) : {} { - "{" - ( mappedTensorBlock(builder) )* - ( <COMMA> (<NL>)* mappedTensorBlock(builder) )* - "}" + "{" ( mappedTensorBlock(builder) )* ( <COMMA> (<NL>)* mappedTensorBlock(builder) )* "}" } @@ -2553,11 +2549,11 @@ void mappedTensorBlock(Tensor.Builder builder) : TensorAddress mappedAddress; } { - mappedAddress = tensorAddress(builder.type()) <COLON> (<NL>)* + mappedAddress = tensorAddress(builder.type().mappedSubtype()) <COLON> (<NL>)* ( mappedTensorCellValue(mappedAddress, builder) | indexedTensorBlockValues(mappedAddress, builder) ) } -void indexedTensorBlockValues(TensorAddress mappedAddress, Tensor.Builder builder) : +void indexedTensorBlockValues(TensorAddress sparseAddress, Tensor.Builder builder) : { List<Double> values = new ArrayList<Double>(); } @@ -2569,7 +2565,7 @@ void indexedTensorBlockValues(TensorAddress mappedAddress, Tensor.Builder builde for (int i = 0; i < values.size(); i++ ) { arrayValues[i] = values.get(i); } - boundBuilder.block(mappedAddress, arrayValues); + boundBuilder.block(sparseAddress, arrayValues); } } @@ -2602,7 +2598,7 @@ void indexedTensorValue(List<Double> values) : Number value; } { - value = number() + value = consumeNumber() { values.add(value.doubleValue()); } } @@ -2617,7 +2613,7 @@ void mappedTensorCellValue(TensorAddress address, Tensor.Builder builder) : TensorAddress tensorAddress(TensorType type) : { - TensorAddress.Builder builder = new TensorAddress.PartialBuilder(type); + TensorAddress.Builder builder = new TensorAddress.Builder(type); String label; } { @@ -2653,7 +2649,7 @@ double tensorCellValue() : Number value; } { - value = number() + value = consumeNumber() { return value.doubleValue(); } } @@ -2926,7 +2922,7 @@ int integer() : { } } /** Consumes a long or integer token and returns its numeric value. */ -long longValue() : { } +long consumeLong() : { } { ( <INTEGER> { return Long.parseLong(token.image); } | <LONG> { return Long.parseLong(token.image.substring(0, token.image.length()-1)); } @@ -2934,17 +2930,17 @@ long longValue() : { } } /** Consumes a floating-point token and returns its numeric value. */ -double floatValue() : { } +double consumeFloat() : { } { <DOUBLE> { return Double.valueOf(token.image); } } -Number number() : +Number consumeNumber() : { Number num; } { - ( num = floatValue() | num = longValue() ) { return num; } + (num = consumeFloat() | num = consumeLong()) { return num; } } /** Consumes an opening brace with leading and trailing newline tokens. */ diff --git a/config-model/src/test/cfg/application/stateless_eval/example.model b/config-model/src/test/cfg/application/stateless_eval/example.model index af1c85be4f0..1d2db15c3ba 100644 --- a/config-model/src/test/cfg/application/stateless_eval/example.model +++ b/config-model/src/test/cfg/application/stateless_eval/example.model @@ -7,6 +7,7 @@ model example { constants { constant1: tensor(x[3]):{{x:0}:0.5, {x:1}:1.5, {x:2}:2.5} constant2: 3.0 + #constant1asLarge tensor(x[3]): file:constant1asLarge.json } constant constant1asLarge { diff --git a/config-model/src/test/java/com/yahoo/searchdefinition/parser/SchemaParserTestCase.java b/config-model/src/test/java/com/yahoo/searchdefinition/parser/SchemaParserTestCase.java index 2284acc705c..17d94639d87 100644 --- a/config-model/src/test/java/com/yahoo/searchdefinition/parser/SchemaParserTestCase.java +++ b/config-model/src/test/java/com/yahoo/searchdefinition/parser/SchemaParserTestCase.java @@ -93,7 +93,6 @@ public class SchemaParserTestCase { assertNotEquals("", schema.name()); } - // TODO: Many (all)? of the files below are parsed from other tests and can be removed from here @Test public void parse_various_old_sdfiles() throws Exception { checkFileParses("src/test/cfg/search/data/travel/schemas/TTData.sd"); @@ -181,6 +180,7 @@ public class SchemaParserTestCase { checkFileParses("src/test/derived/namecollision/collision.sd"); checkFileParses("src/test/derived/namecollision/collisionstruct.sd"); checkFileParses("src/test/derived/nearestneighbor/test.sd"); + checkFileParses("src/test/derived/neuralnet/neuralnet.sd"); checkFileParses("src/test/derived/newrank/newrank.sd"); checkFileParses("src/test/derived/nuwa/newsindex.sd"); checkFileParses("src/test/derived/orderilscripts/orderilscripts.sd"); diff --git a/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionWithTensorTestCase.java b/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionWithTensorTestCase.java index 5bf2115c3f7..114486a5ddc 100644 --- a/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionWithTensorTestCase.java +++ b/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionWithTensorTestCase.java @@ -13,7 +13,7 @@ import static org.junit.Assert.fail; public class RankingExpressionWithTensorTestCase { @Test - public void requireThatSingleLineConstantMappedTensorCanBeParsed() throws ParseException { + public void requireThatSingleLineConstantTensorAndTypeCanBeParsed() throws ParseException { RankProfileSearchFixture f = new RankProfileSearchFixture( " rank-profile my_profile {\n" + " first-phase {\n" + @@ -30,40 +30,6 @@ public class RankingExpressionWithTensorTestCase { } @Test - public void requireThatSingleLineConstantIndexedTensorCanBeParsed() throws ParseException { - RankProfileSearchFixture f = new RankProfileSearchFixture( - " rank-profile my_profile {\n" + - " first-phase {\n" + - " expression: sum(my_tensor)\n" + - " }\n" + - " constants {\n" + - " my_tensor tensor(x[3]):{ {x:0}:1, {x:1}:2, {x:2}:3 }\n" + - " }\n" + - " }"); - f.compileRankProfile("my_profile"); - f.assertFirstPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile"); - f.assertRankProperty("tensor(x[3]):[1.0, 2.0, 3.0]", "constant(my_tensor).value", "my_profile"); - f.assertRankProperty("tensor(x[3])", "constant(my_tensor).type", "my_profile"); - } - - @Test - public void requireThatSingleLineConstantIndexedTensorShortFormCanBeParsed() throws ParseException { - RankProfileSearchFixture f = new RankProfileSearchFixture( - " rank-profile my_profile {\n" + - " first-phase {\n" + - " expression: sum(my_tensor)\n" + - " }\n" + - " constants {\n" + - " my_tensor tensor(x[3]):[1, 2, 3]\n" + - " }\n" + - " }"); - f.compileRankProfile("my_profile"); - f.assertFirstPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile"); - f.assertRankProperty("tensor(x[3]):[1.0, 2.0, 3.0]", "constant(my_tensor).value", "my_profile"); - f.assertRankProperty("tensor(x[3])", "constant(my_tensor).type", "my_profile"); - } - - @Test public void requireConstantTensorCanBeReferredViaConstantFeature() throws ParseException { RankProfileSearchFixture f = new RankProfileSearchFixture( " rank-profile my_profile {\n" + diff --git a/config-model/src/test/java/com/yahoo/vespa/model/container/ml/ModelsEvaluatorTest.java b/config-model/src/test/java/com/yahoo/vespa/model/container/ml/ModelsEvaluatorTest.java index 9183ed316e2..3deeef7f2a2 100644 --- a/config-model/src/test/java/com/yahoo/vespa/model/container/ml/ModelsEvaluatorTest.java +++ b/config-model/src/test/java/com/yahoo/vespa/model/container/ml/ModelsEvaluatorTest.java @@ -21,7 +21,7 @@ public class ModelsEvaluatorTest { public void testModelsEvaluator() { // Assumption fails but test passes on Intel macs // Assumption fails and test fails on ARM64 - //assumeTrue(OnnxEvaluator.isRuntimeAvailable()); + assumeTrue(OnnxEvaluator.isRuntimeAvailable()); ModelsEvaluator modelsEvaluator = ModelsEvaluatorTester.create("src/test/cfg/application/stateless_eval"); assertEquals(3, modelsEvaluator.models().size()); diff --git a/config-model/src/test/java/com/yahoo/vespa/model/search/test/SchemaInfoTestCase.java b/config-model/src/test/java/com/yahoo/vespa/model/search/test/SchemaInfoTestCase.java index b8a14bc763b..34afca703e9 100644 --- a/config-model/src/test/java/com/yahoo/vespa/model/search/test/SchemaInfoTestCase.java +++ b/config-model/src/test/java/com/yahoo/vespa/model/search/test/SchemaInfoTestCase.java @@ -25,8 +25,7 @@ public class SchemaInfoTestCase { " query(myDouble1) double: 0.5" + " query(myDouble2) tensor()" + " query(myMap) tensor(key{}): { label1:1.0,\n \"label2\": 2.0, 'label3': 3.0 }" + - " query(myVector1) tensor(x[3]):\n\n[1 ,2.0,3]" + - " query(myVector2) tensor(x[3]):{{x:0}:1,{x: 1}: 2 , { x:2}:3.0 }" + + " query(myVector) tensor(x[3]):\n\n[1 ,2.0,3]" + " query(myMatrix) tensor(x[2],y[3]):[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]" + " query(myMixed1) tensor(key{},x[2]): { key1:[-1.0, 1.1], key2: [1,2]}" + " query(myMixed2) tensor(k1{},k2{},x[2]): { {k1:l1,k2:l1}:[-1.0, 1.1], {k1:l1,k2:l2}: [1,2]}" + @@ -59,7 +58,7 @@ public class SchemaInfoTestCase { tester.assertRankProfile(schema, 5, "rankfeatures", false, true); var schemaInfoProfile = tester.assertRankProfile(schema, 6, "inputs", false, false); - assertEquals(10, schemaInfoProfile.input().size()); + assertEquals(9, schemaInfoProfile.input().size()); var rankProfilesProfile = rankProfilesConfig.rankprofile().get(6); assertEquals("inputs", rankProfilesProfile.name()); assertInput("query(foo)", "tensor<float>(x[10])", null, 0, schemaInfoProfile, rankProfilesProfile); @@ -67,11 +66,10 @@ public class SchemaInfoTestCase { assertInput("query(myDouble1)", "tensor()", "0.5", 2, schemaInfoProfile, rankProfilesProfile); assertInput("query(myDouble2)", "tensor()", null, 3, schemaInfoProfile, rankProfilesProfile); assertInput("query(myMap)", "tensor(key{})", "tensor(key{}):{{key:label1}:1.0, {key:label2}:2.0, {key:label3}:3.0}", 4, schemaInfoProfile, rankProfilesProfile); - assertInput("query(myVector1)", "tensor(x[3])", "tensor(x[3]):{{x:0}:1.0, {x:1}:2.0, {x:2}:3.0}", 5, schemaInfoProfile, rankProfilesProfile); - assertInput("query(myVector2)", "tensor(x[3])", "tensor(x[3]):{{x:0}:1.0, {x:1}:2.0, {x:2}:3.0}", 6, schemaInfoProfile, rankProfilesProfile); - assertInput("query(myMatrix)", "tensor(x[2],y[3])", "tensor(x[2],y[3]):{{x:0,y:0}:1.0, {x:0,y:1}:2.0, {x:0,y:2}:3.0, {x:1,y:0}:4.0, {x:1,y:1}:5.0, {x:1,y:2}:6.0}", 7, schemaInfoProfile, rankProfilesProfile); - assertInput("query(myMixed1)", "tensor(key{},x[2])", "tensor(key{},x[2]):{{key:key1,x:0}:-1.0, {key:key1,x:1}:1.1, {key:key2,x:0}:1.0, {key:key2,x:1}:2.0}", 8, schemaInfoProfile, rankProfilesProfile); - assertInput("query(myMixed2)", "tensor(k1{},k2{},x[2])", "tensor(k1{},k2{},x[2]):{{k1:l1,k2:l1,x:0}:-1.0, {k1:l1,k2:l1,x:1}:1.1, {k1:l1,k2:l2,x:0}:1.0, {k1:l1,k2:l2,x:1}:2.0}", 9, schemaInfoProfile, rankProfilesProfile); + assertInput("query(myVector)", "tensor(x[3])", "tensor(x[3]):{{x:0}:1.0, {x:1}:2.0, {x:2}:3.0}", 5, schemaInfoProfile, rankProfilesProfile); + assertInput("query(myMatrix)", "tensor(x[2],y[3])", "tensor(x[2],y[3]):{{x:0,y:0}:1.0, {x:0,y:1}:2.0, {x:0,y:2}:3.0, {x:1,y:0}:4.0, {x:1,y:1}:5.0, {x:1,y:2}:6.0}", 6, schemaInfoProfile, rankProfilesProfile); + assertInput("query(myMixed1)", "tensor(key{},x[2])", "tensor(key{},x[2]):{{key:key1,x:0}:-1.0, {key:key1,x:1}:1.1, {key:key2,x:0}:1.0, {key:key2,x:1}:2.0}", 7, schemaInfoProfile, rankProfilesProfile); + assertInput("query(myMixed2)", "tensor(k1{},k2{},x[2])", "tensor(k1{},k2{},x[2]):{{k1:l1,k2:l1,x:0}:-1.0, {k1:l1,k2:l1,x:1}:1.1, {k1:l1,k2:l2,x:0}:1.0, {k1:l1,k2:l2,x:1}:2.0}", 8, schemaInfoProfile, rankProfilesProfile); assertEquals(2, schema.summaryclass().size()); assertEquals("default", schema.summaryclass(0).name()); diff --git a/container-core/src/main/java/com/yahoo/container/handler/Coverage.java b/container-core/src/main/java/com/yahoo/container/handler/Coverage.java index 6b510aadd3f..95494190734 100644 --- a/container-core/src/main/java/com/yahoo/container/handler/Coverage.java +++ b/container-core/src/main/java/com/yahoo/container/handler/Coverage.java @@ -63,8 +63,9 @@ public class Coverage { } public void merge(Coverage other) { - if (other == null) return; - + if (other == null) { + return; + } docs += other.getDocs(); nodes += other.getNodes(); nodesTried += other.nodesTried; @@ -76,17 +77,17 @@ public class Coverage { // explicitly incomplete beats doc count beats explicitly full switch (other.fullReason) { - case EXPLICITLY_FULL: - // do nothing - break; - case EXPLICITLY_INCOMPLETE: - fullReason = FullCoverageDefinition.EXPLICITLY_INCOMPLETE; - break; - case DOCUMENT_COUNT: - if (fullReason == FullCoverageDefinition.EXPLICITLY_FULL) { - fullReason = FullCoverageDefinition.DOCUMENT_COUNT; - } - break; + case EXPLICITLY_FULL: + // do nothing + break; + case EXPLICITLY_INCOMPLETE: + fullReason = FullCoverageDefinition.EXPLICITLY_INCOMPLETE; + break; + case DOCUMENT_COUNT: + if (fullReason == FullCoverageDefinition.EXPLICITLY_FULL) { + fullReason = FullCoverageDefinition.DOCUMENT_COUNT; + } + break; } } diff --git a/model-integration/src/main/javacc/ModelParser.jj b/model-integration/src/main/javacc/ModelParser.jj index c850d223612..6f6f3508beb 100644 --- a/model-integration/src/main/javacc/ModelParser.jj +++ b/model-integration/src/main/javacc/ModelParser.jj @@ -30,11 +30,8 @@ import java.util.List; import java.util.ArrayList; import ai.vespa.rankingexpression.importer.ImportedModel; import com.yahoo.io.IOUtils; -import com.yahoo.tensor.Tensor; -import com.yahoo.tensor.IndexedTensor; -import com.yahoo.tensor.MixedTensor; -import com.yahoo.tensor.TensorAddress; import com.yahoo.tensor.TensorType; +import com.yahoo.tensor.Tensor; import com.yahoo.tensor.serialization.JsonFormat; import com.yahoo.searchlib.rankingexpression.RankingExpression; @@ -83,7 +80,8 @@ TOKEN : { < NL: "\n" > | < FUNCTION: "function" > -| < TENSOR_TYPE: "tensor" ("<" (~["<",">"])+ ">")? "(" (~["(",")"])* ")" > +| < TENSOR_TYPE: "tensor(" (~["(",")"])+ ")" > +| < TENSORVALUE: (" ")* ":" (" ")* ("{"<BRACE_SL_LEVEL_1>) ("\n")? > | < TENSOR_VALUE_SL: "value" (" ")* ":" (" ")* ("{"<BRACE_SL_LEVEL_1>) ("\n")? > | < TENSOR_VALUE_ML: "value" (<SEARCHLIB_SKIP>)? "{" (["\n"," "])* ("{"<BRACE_ML_LEVEL_1>) (["\n"," "])* "}" ("\n")? > | < LBRACE: "{" > @@ -91,8 +89,6 @@ TOKEN : | < COLON: ":" > | < DOT: "." > | < COMMA: "," > -| < DOUBLE_KEYWORD: "double" > -| < INPUTS: "inputs" > | < MODEL: "model" > | < TYPE: "type" > | < EXPRESSION_SL: "expression" (" ")* ":" (("{"<BRACE_SL_LEVEL_1>)|<BRACE_SL_CONTENT>)* ("\n")? > @@ -111,12 +107,8 @@ TOKEN : | < FILE: "file" > | < URI: "uri" > | < IDENTIFIER: ["a"-"z","A"-"Z", "_"] (["a"-"z","A"-"Z","0"-"9","_"])* > -| < DOUBLEQUOTEDSTRING: "\"" ( ~["\""] )* "\"" > -| < SINGLEQUOTEDSTRING: "'" ( ~["'"] )* "'" > | < CONTEXT: ["a"-"z","A"-"Z"] (["a"-"z", "A"-"Z", "0"-"9"])* > | < DOUBLE: ("-")? (["0"-"9"])+ "." (["0"-"9"])+ > -| < INTEGER: ("-")? (["0"-"9"])+ > -| < LONG: ("-")? (["0"-"9"])+"L" > | < STRING: (["a"-"z","A"-"Z","_","0"-"9","."])+ > | < FILE_PATH: ["a"-"z","A"-"Z", "_"] (["a"-"z","A"-"Z","0"-"9","_","-", "/", "."])+ > | < HTTP: ["h","H"] ["t","T"] ["t","T"] ["p","P"] (["s","S"])? > @@ -157,23 +149,10 @@ void model() : } void modelContent() : -{} { - ( <NL> | - constants() | - largeConstant() | - function() | - inputs() | - input() - )* } - -void inputs() : -{} { - <INPUTS> (<NL>)* <LBRACE> (<NL>)* - ( input() (<NL>)* )* - <RBRACE> + ( <NL> | input() | constants() | largeConstant() | function() )* } /** Declared input variables (aka features). All non-scalar inputs must be declared. */ @@ -212,244 +191,36 @@ void constants() : } { <CONSTANTS> <LBRACE> (<NL>)* - ( constant() (<NL>)* )* + ( name = identifier() <COLON> ( constantDouble(name) | constantTensor(name) ) (<NL>)* )* <RBRACE> } -String constantTensorErrorMessage(String constantTensorName) : {} -{ - { return "For constant tensor '" + constantTensorName + "' in '" + model + "'"; } -} - -void constant() : -{ - String name = null; - TensorType type = TensorType.empty; - Tensor value = null; - String valuePath = null; -} -{ - ( - name = identifier() (<COLON>)? - ( - LOOKAHEAD(4) ( ( type = valueType(name) )? (<COLON>)? (<NL>)* ( value = tensorValue(type) | valuePath = fileItem()) - { - if (value != null) { - model.smallConstant(name, value); - } - else { - try { - value = JsonFormat.decode(type, IOUtils.readFileBytes(model.relativeFile(valuePath, "constant '" + name + "'"))); - model.largeConstant(name, value); - } - catch (Exception e) { - throw new IllegalArgumentException("Could not read constant '" + name + "'", e); - } - } - } - ) - | // Deprecated forms (TODO: Add warning on Vespa 8): - ( constantValue(name) | constantTensor(name) ) - ) - ) -} - -// Deprecated form -void constantValue(String name) : +void constantDouble(String name) : { Token value; } { - <COLON> ( value = <DOUBLE> | value = <INTEGER> | value = <IDENTIFIER> ) - { model.smallConstant(name, Tensor.from(value.image)); } + value = <DOUBLE> { model.smallConstant(name, Tensor.from(Double.parseDouble(value.image))); } } -// Deprecated form void constantTensor(String name) : { - String tensorString = ""; - TensorType type = null; -} -{ - <LBRACE> (<NL>)* - (( tensorString = tensorValuePrefixedByValue() | - type = tensorTypeWithPrefix(constantTensorErrorMessage(name)) ) (<NL>)* )* <RBRACE> - { model.smallConstant(name, type != null ? Tensor.from(type, tensorString) : Tensor.from(tensorString)); } -} - -TensorType valueType(String name) : -{ TensorType type; - -} -{ - ( - ( type = tensorType("Type of " + name) ) - | - ( <DOUBLE_KEYWORD> { type = TensorType.empty; } ) - ) - { return type; } -} - -TensorType tensorType(String errorMessage) : -{ - String tensorTypeString; -} -{ - <TENSOR_TYPE> { tensorTypeString = token.image; } - { - TensorType tensorType; - try { - tensorType = TensorType.fromSpec(tensorTypeString); - } catch (IllegalArgumentException e) { - throw new IllegalArgumentException(errorMessage + ": Illegal tensor type spec: " + e.getMessage()); - } - return tensorType; - } -} - -/** - * Parses a tensor written in a tensor literal form, - * https://docs.vespa.ai/en/reference/tensor.html#tensor-literal-form - */ -Tensor tensorValue(TensorType type) : -{ - Tensor.Builder builder = Tensor.Builder.of(type); - Number doubleValue = null; -} -{ - ( mappedTensorValue(builder) | indexedTensorValues(builder) | doubleValue = number() ) - { - if (doubleValue != null) { - if (type.rank() > 0) - throw new IllegalArgumentException("A tensor of type " + type + " cannot be a number"); - builder.cell(doubleValue.doubleValue()); - } - return builder.build(); - } -} - -/** A mapped or mixed tensor value. */ -void mappedTensorValue(Tensor.Builder builder) : {} -{ - "{" - ( mappedTensorBlock(builder) )* - ( <COMMA> (<NL>)* mappedTensorBlock(builder) )* - "}" -} - - -void mappedTensorBlock(Tensor.Builder builder) : -{ - TensorAddress mappedAddress; -} -{ - mappedAddress = tensorAddress(builder.type()) <COLON> (<NL>)* - ( mappedTensorCellValue(mappedAddress, builder) | indexedTensorBlockValues(mappedAddress, builder) ) -} - -void indexedTensorBlockValues(TensorAddress mappedAddress, Tensor.Builder builder) : -{ - List<Double> values = new ArrayList<Double>(); -} -{ - arrayTensorValues(values) - { - MixedTensor.BoundBuilder boundBuilder = (MixedTensor.BoundBuilder)builder; - double[] arrayValues = new double[values.size()]; - for (int i = 0; i < values.size(); i++ ) { - arrayValues[i] = values.get(i); - } - boundBuilder.block(mappedAddress, arrayValues); - } -} - -void indexedTensorValues(Tensor.Builder builder) : -{ - List<Double> values = new ArrayList<Double>(); -} -{ - arrayTensorValues(values) - { - IndexedTensor.BoundBuilder boundBuilder = (IndexedTensor.BoundBuilder)builder; - double[] arrayValues = new double[values.size()]; - for (int i = 0; i < values.size(); i++ ) { - arrayValues[i] = values.get(i); - } - boundBuilder.fill(arrayValues); - } -} - -/** Tensor array values. Using sub-bracketing for multiple dimensions is optional and therefore ignored here. */ -void arrayTensorValues(List<Double> values) : {} -{ - "[" ( ( indexedTensorValue(values) | arrayTensorValues(values)) )* - ( <COMMA> (<NL>)* ( indexedTensorValue(values) | arrayTensorValues(values)) )* - "]" -} - -void indexedTensorValue(List<Double> values) : -{ - Number value; -} -{ - value = number() - { values.add(value.doubleValue()); } -} - -void mappedTensorCellValue(TensorAddress address, Tensor.Builder builder) : -{ - double value; -} -{ - value = tensorCellValue() - { builder.cell(address, value); } -} - -TensorAddress tensorAddress(TensorType type) : -{ - TensorAddress.Builder builder = new TensorAddress.PartialBuilder(type); - String label; -} -{ - ( - label = tensorAddressLabel() { builder.add(label); } - | - ( "{" ( tensorAddressElement(builder) )* ( <COMMA> tensorAddressElement(builder) )* "}" ) - ) - { return builder.build(); } -} - -void tensorAddressElement(TensorAddress.Builder builder) : -{ - String dimension; - String label; -} -{ - dimension = identifier() <COLON> (<NL>)* label = tensorAddressLabel() - { builder.add(dimension, label); } -} - -String tensorAddressLabel() : -{ - String label; + Token value; } { - ( label = identifier() | label = quotedString() ) - { return label; } + type = tensorType("constant '" + name + "'") value = <TENSORVALUE> + { + model.smallConstant(name, Tensor.from(type, value.image.substring(1))); + } } -double tensorCellValue() : -{ - Number value; -} +String constantTensorErrorMessage(String model, String constantTensorName) : {} { - value = number() - { return value.doubleValue(); } + { return "For constant tensor '" + constantTensorName + "' in model '" + model + "'"; } } -/** Undocumented syntax for supplying a tensor constant value by a string prefixed by "value" */ -String tensorValuePrefixedByValue() : +String tensorValue() : { String tensor; } @@ -462,7 +233,7 @@ String tensorValuePrefixedByValue() : } } -TensorType tensorTypeWithPrefix(String errorMessage) : +TensorType tensorType(String errorMessage) : { String tensorTypeString; } @@ -479,7 +250,7 @@ TensorType tensorTypeWithPrefix(String errorMessage) : } } -/** Consumes a large constant. */ // TODO: Remove on Vespa 9 +/** Consumes a large constant. */ void largeConstant() : { String name; @@ -540,65 +311,18 @@ String expression() : String identifier() : { } { ( - <CONSTANT> | - <CONSTANTS> | - <DOUBLE_KEYWORD> | - <FILE> | - <IDENTIFIER> | - <INPUTS> | - <INTEGER> | - <MODEL> | - <TYPE> | - <URI> + <IDENTIFIER> + | <DOUBLE> + | <FILE> + | <URI> + | <MODEL> + | <TYPE> ) { return token.image; } } -Number number() : -{ - Number num; -} -{ - (num = floatValue() | num = longValue() ) { return num; } -} - -/** Consumes a long or integer token and returns its numeric value. */ -long longValue() : { } -{ - ( <INTEGER> { return Long.parseLong(token.image); } | - <LONG> { return Long.parseLong(token.image.substring(0, token.image.length()-1)); } - ) -} - -/** Consumes a floating-point token and returns its numeric value. */ -double floatValue() : { } -{ - <DOUBLE> { return Double.valueOf(token.image); } -} - /** Consumes an opening brace with leading and trailing newline tokens. */ void lbrace() : { } { (<NL>)* <LBRACE> (<NL>)* } - -String fileItem() : -{ - String path; -} -{ - (<FILE> <COLON> ( <FILE_PATH> | <STRING> | <IDENTIFIER>) { path = com.yahoo.path.Path.fromString(token.image).getRelative(); } { } (<NL>)*) { return path; } -} - -/** - * Consumes a quoted string token and returns the token image minus the quotes. This does not perform - * unescaping of the content, it simply removes the first and last character of the image. However, the token itself can - * contain anything but a double quote. - * - * @return the unquoted token image - */ -String quotedString() : { } -{ - ( <DOUBLEQUOTEDSTRING> | <SINGLEQUOTEDSTRING> ) - { return token.image.substring(1, token.image.length() - 1); } -} 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..fc92883a90f 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 @@ -25,16 +25,7 @@ public class VespaImportTestCase { @Test public void testExample() { ImportedModel model = importModel("example"); - assertModel(model); - } - - @Test - public void testLegacySyntax() { - ImportedModel model = importModel("legacy_syntax"); - assertModel(model); - } - private void assertModel(ImportedModel model) { assertEquals(2, model.inputs().size()); assertEquals("tensor(name{},x[3])", model.inputs().get("input1").toString()); assertEquals("tensor(x[3])", model.inputs().get("input2").toString()); diff --git a/model-integration/src/test/models/vespa/example.model b/model-integration/src/test/models/vespa/example.model index 25d27033cfd..269ed83b695 100644 --- a/model-integration/src/test/models/vespa/example.model +++ b/model-integration/src/test/models/vespa/example.model @@ -1,15 +1,17 @@ model example { # All inputs that are not scalar (aka 0-dimensional tensor) must be declared - inputs { - input1: tensor(name{}, x[3]) - input2: tensor(x[3]) - } + input1: tensor(name{}, x[3]) + input2: tensor(x[3]) constants { constant1: tensor(x[3]):{{x:0}:0.5, {x:1}:1.5, {x:2}:2.5} constant2: 3.0 - constant1asLarge tensor(x[3]): file:constant1asLarge.json + } + + constant constant1asLarge { + type: tensor(x[3]) + file: constant1asLarge.json } function foo1() { diff --git a/model-integration/src/test/models/vespa/legacy_syntax.model b/model-integration/src/test/models/vespa/legacy_syntax.model deleted file mode 100644 index 2a5031a5ff9..00000000000 --- a/model-integration/src/test/models/vespa/legacy_syntax.model +++ /dev/null @@ -1,26 +0,0 @@ -model legacy_syntax { - - # Syntax not supported in rank profiles which probably should be removed on Vespa 9 - input1: tensor(name{}, x[3]) - input2: tensor(x[3]) - - constants { - constant1: tensor(x[3]):{{x:0}:0.5, {x:1}:1.5, {x:2}:2.5} - constant2: 3.0 - } - - # Syntax to be removed on Vespa 9 - constant constant1asLarge { - type: tensor(x[3]) - file: constant1asLarge.json - } - - function foo1() { - expression: file:test.expression - } - - function foo2() { - expression: reduce(sum(input1 * input2, name) * constant(constant1asLarge), max, x) * constant2 - } - -}
\ No newline at end of file diff --git a/vespajlib/abi-spec.json b/vespajlib/abi-spec.json index 654042372cf..f7be61946ba 100644 --- a/vespajlib/abi-spec.json +++ b/vespajlib/abi-spec.json @@ -1273,18 +1273,6 @@ ], "fields": [] }, - "com.yahoo.tensor.TensorAddress$PartialBuilder": { - "superClass": "com.yahoo.tensor.TensorAddress$Builder", - "interfaces": [], - "attributes": [ - "public" - ], - "methods": [ - "public void <init>(com.yahoo.tensor.TensorType)", - "public com.yahoo.tensor.TensorAddress$Builder copy()" - ], - "fields": [] - }, "com.yahoo.tensor.TensorAddress": { "superClass": "java.lang.Object", "interfaces": [ diff --git a/vespajlib/src/main/java/com/yahoo/tensor/TensorAddress.java b/vespajlib/src/main/java/com/yahoo/tensor/TensorAddress.java index 92bdfb2b3a4..d9ab67d6c5f 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/TensorAddress.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/TensorAddress.java @@ -61,10 +61,8 @@ public abstract class TensorAddress implements Comparable<TensorAddress> { @Override public int hashCode() { int result = 1; - for (int i = 0; i < size(); i++) { - if (label(i) != null) - result = 31 * result + label(i).hashCode(); - } + for (int i = 0; i < size(); i++) + result = 31 * result + label(i).hashCode(); return result; } @@ -75,7 +73,7 @@ public abstract class TensorAddress implements Comparable<TensorAddress> { TensorAddress other = (TensorAddress)o; if (other.size() != this.size()) return false; for (int i = 0; i < this.size(); i++) - if ( ! Objects.equals(this.label(i), other.label(i))) + if ( ! this.label(i).equals(other.label(i))) return false; return true; } @@ -173,8 +171,8 @@ public abstract class TensorAddress implements Comparable<TensorAddress> { /** Builder of a tensor address */ public static class Builder { - final TensorType type; - final String[] labels; + private final TensorType type; + private final String[] labels; public Builder(TensorType type) { this(type, new String[type.dimensions().size()]); @@ -186,16 +184,14 @@ public abstract class TensorAddress implements Comparable<TensorAddress> { } /** - * Adds the label to the only mapped dimension of this. + * Adds the label to the only dimension of this. * * @throws IllegalArgumentException if this does not have exactly one dimension */ public Builder add(String label) { - var mappedSubtype = type.mappedSubtype(); - if (mappedSubtype.rank() != 1) - throw new IllegalArgumentException("Cannot add a label without explicit dimension to a tensor of type " + - type + ": Must have exactly one sparse dimension"); - add(mappedSubtype.dimensions().get(0).name(), label); + if (type.rank() != 1) + throw new IllegalArgumentException("Cannot add a label without explicit dimension to a tensor of type " + type); + add(type.dimensions().get(0).name(), label); return this; } @@ -222,39 +218,14 @@ public abstract class TensorAddress implements Comparable<TensorAddress> { /** Returns the type of the tensor this address is being built for. */ public TensorType type() { return type; } - void validate() { + public TensorAddress build() { for (int i = 0; i < labels.length; i++) if (labels[i] == null) throw new IllegalArgumentException("Missing a label for dimension " + type.dimensions().get(i).name() + " for " + type); - } - - public TensorAddress build() { - validate(); return TensorAddress.of(labels); } } - /** Builder of an address to a subset of the dimensions of a tensor type */ - public static class PartialBuilder extends Builder { - - public PartialBuilder(TensorType type) { - super(type); - } - - private PartialBuilder(TensorType type, String[] labels) { - super(type, labels); - } - - /** Creates a copy of this which can be modified separately */ - public Builder copy() { - return new PartialBuilder(type, Arrays.copyOf(labels, labels.length)); - } - - @Override - void validate() { } - - } - } |