aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionWithTensorFlowTestCase.java
blob: 3ec621618e565c26b98faf1758e7e4bfc787b04e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.processing;

import com.yahoo.searchdefinition.parser.ParseException;
import com.yahoo.tensor.Tensor;
import com.yahoo.yolean.Exceptions;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

/**
 * @author bratseth
 */
public class RankingExpressionWithTensorFlowTestCase {

    // The "../" is to escape the "models/" element prepended to the path
    private final String modelDirectory = "../src/test/integration/tensorflow/mnist_softmax/saved";
    private final String vespaExpression = "join(rename(reduce(join(Placeholder, rename(constant(Variable), (d0, d1), (d1, d3)), f(a,b)(a * b)), sum, d1), d3, d1), rename(constant(Variable_1), d0, d1), f(a,b)(a + b))";

    @Test
    public void testMinimalTensorFlowReference() throws ParseException {
        RankProfileSearchFixture search = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                "    first-phase {\n" +
                "      expression: tensorflow('" + modelDirectory + "')" +
                "    }\n" +
                "  }");
        search.assertFirstPhaseExpression(vespaExpression, "my_profile");

        Tensor variable_1 = search.rankProfile("my_profile").getConstants().get("Variable_1").asTensor();
        assertNotNull("Variable_1 is imported", variable_1);
        assertEquals(10, variable_1.size());

        Tensor variable = search.rankProfile("my_profile").getConstants().get("Variable").asTensor();
        assertNotNull("Variable is imported", variable);
        assertEquals(7840, variable.size());
    }

    @Test
    public void testNestedTensorFlowReference() throws ParseException {
        RankProfileSearchFixture search = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                "    first-phase {\n" +
                "      expression: 5 + sum(tensorflow('" + modelDirectory + "'))" +
                "    }\n" +
                "  }");
        search.assertFirstPhaseExpression("5 + reduce(" + vespaExpression + ", sum)", "my_profile");

        Tensor variable_1 = search.rankProfile("my_profile").getConstants().get("Variable_1").asTensor();
        assertNotNull("Variable_1 is imported", variable_1);
        assertEquals(10, variable_1.size());

        Tensor variable = search.rankProfile("my_profile").getConstants().get("Variable").asTensor();
        assertNotNull("Variable is imported", variable);
        assertEquals(7840, variable.size());
    }

    @Test
    public void testTensorFlowReferenceSpecifyingSignature() throws ParseException {
        RankProfileSearchFixture search = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                "    first-phase {\n" +
                "      expression: tensorflow('" + modelDirectory + "', 'serving_default')" +
                "    }\n" +
                "  }");
        search.assertFirstPhaseExpression(vespaExpression, "my_profile");
    }

    @Test
    public void testTensorFlowReferenceSpecifyingSignatureAndOutput() throws ParseException {
        RankProfileSearchFixture search = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                "    first-phase {\n" +
                "      expression: tensorflow('" + modelDirectory + "', 'serving_default', 'y')" +
                "    }\n" +
                "  }");
        search.assertFirstPhaseExpression(vespaExpression, "my_profile");
    }

    @Test
    public void testTensorFlowReferenceSpecifyingNonExistingSignature() throws ParseException {
        try {
            RankProfileSearchFixture search = new RankProfileSearchFixture(
                    "  rank-profile my_profile {\n" +
                    "    first-phase {\n" +
                    "      expression: tensorflow('" + modelDirectory + "', 'serving_defaultz')" +
                    "    }\n" +
                    "  }");
            search.assertFirstPhaseExpression(vespaExpression, "my_profile");
            fail("Expecting exception");
        }
        catch (IllegalArgumentException expected) {
            assertEquals("Rank profile 'my_profile' is invalid: Could not use tensorflow model from tensorflow('" +
                         modelDirectory + "','serving_defaultz'): Model does not have the specified signature 'serving_defaultz'",
                         Exceptions.toMessageString(expected));
        }
    }

    @Test
    public void testTensorFlowReferenceSpecifyingNonExistingOutput() throws ParseException {
        try {
            RankProfileSearchFixture search = new RankProfileSearchFixture(
                    "  rank-profile my_profile {\n" +
                    "    first-phase {\n" +
                    "      expression: tensorflow('" + modelDirectory + "', 'serving_default', 'x')" +
                    "    }\n" +
                    "  }");
            search.assertFirstPhaseExpression(vespaExpression, "my_profile");
            fail("Expecting exception");
        }
        catch (IllegalArgumentException expected) {
            assertEquals("Rank profile 'my_profile' is invalid: Could not use tensorflow model from tensorflow('" +
                         modelDirectory + "','serving_default','x'): Model does not have the specified output 'x'",
                         Exceptions.toMessageString(expected));
        }
    }

}