diff options
author | Harald Musum <musum@verizonmedia.com> | 2019-06-12 20:33:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-12 20:33:45 +0200 |
commit | 6ad5df4f4a2ceebcfab47f6ae86d30dcf93efd4e (patch) | |
tree | 19bef88a22a7218c5620cc0f044c094d83379d17 /searchlib | |
parent | 9423f1774e10129522a7f2cc9995bc4778ee9da0 (diff) |
Revert "Require constant() for large constants and fix a type resolving bug (#9769)"
This reverts commit cee1c3a3804d5d3c25407b3c4ac64228e9d194e3.
Diffstat (limited to 'searchlib')
9 files changed, 9 insertions, 131 deletions
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/FunctionNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/FunctionNode.java index 2aedec2109b..c4f3a75f2f8 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/FunctionNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/FunctionNode.java @@ -5,7 +5,6 @@ import com.yahoo.searchlib.rankingexpression.Reference; import com.yahoo.searchlib.rankingexpression.evaluation.Context; import com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue; import com.yahoo.searchlib.rankingexpression.evaluation.Value; -import com.yahoo.searchlib.rankingexpression.transform.TensorMaxMinTransformer; import com.yahoo.tensor.TensorType; import com.yahoo.tensor.evaluation.TypeContext; import com.yahoo.tensor.functions.Join; @@ -68,11 +67,6 @@ public final class FunctionNode extends CompositeNode { @Override public TensorType type(TypeContext<Reference> context) { - // Check if this node should be interpreted as tensor reduce, as this impacts the type - ExpressionNode thisTransformed = TensorMaxMinTransformer.transformFunctionNode(this, context); - if (thisTransformed != this) - return thisTransformed.type(context); - if (arguments.expressions().size() == 0) return TensorType.empty; diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/IfNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/IfNode.java index 92c6d6f8638..28dc623be72 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/IfNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/IfNode.java @@ -85,9 +85,7 @@ public final class IfNode extends CompositeNode { return trueType.dimensionwiseGeneralizationWith(falseType).orElseThrow(() -> new IllegalArgumentException("An if expression must produce compatible types in both " + "alternatives, but the 'true' type is " + trueType + " while the " + - "'false' type is " + falseType + - "\n'true' branch: " + trueExpression + - "\n'false' branch: " + falseExpression) + "'false' type is " + falseType) ); } diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ReferenceNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ReferenceNode.java index e15ce158e83..eb8d2229a6d 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ReferenceNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ReferenceNode.java @@ -95,13 +95,7 @@ public final class ReferenceNode extends CompositeNode { @Override public TensorType type(TypeContext<Reference> context) { - TensorType type = null; - try { - type = context.getType(reference); - } - catch (IllegalArgumentException e) { - throw new IllegalArgumentException(reference + " is invalid", e); - } + TensorType type = context.getType(reference); if (type == null) throw new IllegalArgumentException("Unknown feature '" + toString() + "'"); return type; diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/ExpressionTransformer.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/ExpressionTransformer.java index 31567ba120b..22d314bcb28 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/ExpressionTransformer.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/ExpressionTransformer.java @@ -10,7 +10,7 @@ import java.util.List; /** * Superclass of expression transformers. The scope (lifetime) of a transformer instance is a single compilation - * of all the expressions in one rank profile. + * of alle the expressions in one rank profile. * * @author bratseth */ diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/TensorMaxMinTransformer.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/TensorMaxMinTransformer.java deleted file mode 100644 index 979c5b0f88c..00000000000 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/TensorMaxMinTransformer.java +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.searchlib.rankingexpression.transform; - -import com.yahoo.searchlib.rankingexpression.Reference; -import com.yahoo.searchlib.rankingexpression.rule.CompositeNode; -import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode; -import com.yahoo.searchlib.rankingexpression.rule.FunctionNode; -import com.yahoo.searchlib.rankingexpression.rule.NameNode; -import com.yahoo.searchlib.rankingexpression.rule.ReferenceNode; -import com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode; -import com.yahoo.tensor.TensorType; -import com.yahoo.tensor.evaluation.TypeContext; -import com.yahoo.tensor.functions.Reduce; - -import java.util.Optional; - -/** - * Transforms min(tensor,dim) and max(tensor,dim) to - * reduce(tensor,min/max,dim). This is necessary as the backend does - * not recognize these forms of min and max. - * - * @author lesters - */ -public class TensorMaxMinTransformer<CONTEXT extends TransformContext> extends ExpressionTransformer<CONTEXT> { - - @Override - public ExpressionNode transform(ExpressionNode node, CONTEXT context) { - if (node instanceof CompositeNode) { - node = transformChildren((CompositeNode) node, context); - } - if (node instanceof FunctionNode) { - node = transformFunctionNode((FunctionNode) node, context.types()); - } - return node; - } - - public static ExpressionNode transformFunctionNode(FunctionNode node, TypeContext<Reference> context) { - switch (node.getFunction()) { - case min: - case max: - return transformMaxAndMinFunctionNode(node, context); - } - return node; - } - - /** - * Transforms max and min functions if the first - * argument returns a tensor type and the second argument is a valid - * dimension in the tensor. - */ - private static ExpressionNode transformMaxAndMinFunctionNode(FunctionNode node, TypeContext<Reference> context) { - if (node.children().size() != 2) { - return node; - } - ExpressionNode arg1 = node.children().get(0); - Optional<String> dimension = dimensionName(node.children().get(1)); - if (dimension.isPresent()) { - TensorType type = arg1.type(context); - if (type.dimension(dimension.get()).isPresent()) { - return replaceMaxAndMinFunction(node); - } - } - return node; - } - - private static Optional<String> dimensionName(ExpressionNode node) { - if (node instanceof ReferenceNode) { - Reference reference = ((ReferenceNode)node).reference(); - if (reference.isIdentifier()) - return Optional.of(reference.name()); - else - return Optional.empty(); - } - else if (node instanceof NameNode) { - return Optional.of(((NameNode)node).getValue()); - } - else { - return Optional.empty(); - } - } - - private static ExpressionNode replaceMaxAndMinFunction(FunctionNode node) { - ExpressionNode arg1 = node.children().get(0); - ExpressionNode arg2 = node.children().get(1); - - TensorFunctionNode.TensorFunctionExpressionNode expression = TensorFunctionNode.wrapArgument(arg1); - Reduce.Aggregator aggregator = Reduce.Aggregator.valueOf(node.getFunction().name()); - String dimension = ((ReferenceNode) arg2).getName(); - - return new TensorFunctionNode(new Reduce(expression, aggregator, dimension)); - } - -} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/TransformContext.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/TransformContext.java index 0113a650277..7485ce69f98 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/TransformContext.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/transform/TransformContext.java @@ -1,9 +1,7 @@ // Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.rankingexpression.transform; -import com.yahoo.searchlib.rankingexpression.Reference; import com.yahoo.searchlib.rankingexpression.evaluation.Value; -import com.yahoo.tensor.evaluation.TypeContext; import java.util.Map; @@ -15,19 +13,11 @@ import java.util.Map; public class TransformContext { private final Map<String, Value> constants; - private final TypeContext<Reference> types; - public TransformContext(Map<String, Value> constants, TypeContext<Reference> types) { + public TransformContext(Map<String, Value> constants) { this.constants = constants; - this.types = types; } public Map<String, Value> constants() { return constants; } - /** - * Returns the types known in this context. We may have type information for references - * for which no value is available - */ - public TypeContext<Reference> types() { return types; } - } diff --git a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/TypeResolutionTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/TypeResolutionTestCase.java index 88838b5aed0..a08d510eec4 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/TypeResolutionTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/TypeResolutionTestCase.java @@ -53,9 +53,7 @@ public class TypeResolutionTestCase { } catch (IllegalArgumentException expected) { assertEquals("An if expression must produce compatible types in both alternatives, " + - "but the 'true' type is tensor(x[]) while the 'false' type is tensor(y[])" + - "\n'true' branch: query(x1)" + - "\n'false' branch: query(y1)", + "but the 'true' type is tensor(x[]) while the 'false' type is tensor(y[])", expected.getMessage()); } catch (ParseException e) { diff --git a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/transform/ConstantDereferencerTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/transform/ConstantDereferencerTestCase.java index a41fb02f784..1f28f0b0129 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/transform/ConstantDereferencerTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/transform/ConstantDereferencerTestCase.java @@ -2,7 +2,6 @@ package com.yahoo.searchlib.rankingexpression.transform; import com.yahoo.searchlib.rankingexpression.RankingExpression; -import com.yahoo.searchlib.rankingexpression.evaluation.MapTypeContext; import com.yahoo.searchlib.rankingexpression.evaluation.Value; import com.yahoo.searchlib.rankingexpression.parser.ParseException; import org.junit.Test; @@ -25,7 +24,7 @@ public class ConstantDereferencerTestCase { constants.put("a", Value.parse("1.0")); constants.put("b", Value.parse("2")); constants.put("c", Value.parse("3.5")); - TransformContext context = new TransformContext(constants, new MapTypeContext()); + TransformContext context = new TransformContext(constants); assertEquals("1.0 + 2.0 + 3.5", c.transform(new RankingExpression("a + b + c"), context).toString()); assertEquals("myFunction(1.0,2.0)", c.transform(new RankingExpression("myFunction(a, b)"), context).toString()); diff --git a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/transform/SimplifierTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/transform/SimplifierTestCase.java index f4b1b0ceee2..8fac3395ac0 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/transform/SimplifierTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/transform/SimplifierTestCase.java @@ -1,11 +1,9 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.rankingexpression.transform; -import com.yahoo.log.event.Collection; 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.MapTypeContext; import com.yahoo.searchlib.rankingexpression.parser.ParseException; import com.yahoo.searchlib.rankingexpression.rule.CompositeNode; import org.junit.Test; @@ -22,7 +20,7 @@ public class SimplifierTestCase { @Test public void testSimplify() throws ParseException { Simplifier s = new Simplifier(); - TransformContext c = new TransformContext(Collections.emptyMap(), new MapTypeContext()); + TransformContext c = new TransformContext(Collections.emptyMap()); assertEquals("a + b", s.transform(new RankingExpression("a + b"), c).toString()); assertEquals("6.5", s.transform(new RankingExpression("1.0 + 2.0 + 3.5"), c).toString()); assertEquals("6.5", s.transform(new RankingExpression("1.0 + ( 2.0 + 3.5 )"), c).toString()); @@ -47,7 +45,7 @@ public class SimplifierTestCase { @Test public void testSimplifyComplexExpression() throws ParseException { RankingExpression initial = new RankingExpression("sqrt(if (if (INFERRED * 0.9 < INFERRED, GMP, (1 + 1.1) * INFERRED) < INFERRED * INFERRED - INFERRED, if (GMP < 85.80799542793133 * GMP, INFERRED, if (GMP < GMP, tanh(INFERRED), log(76.89956221113943))), tanh(tanh(INFERRED))) * sqrt(sqrt(GMP + INFERRED)) * GMP ) + 13.5 * (1 - GMP) * pow(GMP * 0.1, 2 + 1.1 * 0)"); - TransformContext c = new TransformContext(Collections.emptyMap(), new MapTypeContext()); + TransformContext c = new TransformContext(Collections.emptyMap()); RankingExpression simplified = new Simplifier().transform(initial, c); Context context = new MapContext(); @@ -72,7 +70,7 @@ public class SimplifierTestCase { @Test public void testParenthesisPreservation() throws ParseException { Simplifier s = new Simplifier(); - TransformContext c = new TransformContext(Collections.emptyMap(), new MapTypeContext()); + TransformContext c = new TransformContext(Collections.emptyMap()); CompositeNode transformed = (CompositeNode)s.transform(new RankingExpression("a + (b + c) / 100000000.0"), c).getRoot(); assertEquals("a + (b + c) / 100000000.0", transformed.toString()); } |