aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/schema/processing/VespaMlModelTestCase.java
blob: f4b54a2b10322c2572089f3d8fa1babbd14a463c (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.processing;

import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.io.IOUtils;
import com.yahoo.path.Path;
import com.yahoo.schema.derived.RawRankProfile;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.ml.ImportedModelTester;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * Tests adding Vespa ranking expression based models in the models/ dir
 *
 * @author bratseth
 */
public class VespaMlModelTestCase {

    private final Path applicationDir = Path.fromString("src/test/integration/vespa/");

    private final String expectedRankConfig =
            "constant(constant1).value : tensor(x[3]):[0.5, 1.5, 2.5]\n" +
            "constant(constant1).type : tensor(x[3])\n" +
            "rankingExpression(foo1).rankingScript : reduce(reduce(input1 * input2, sum, name) * constant(constant1), max, x) * 3.0\n" +
            "rankingExpression(foo1).input1.type : tensor(name{},x[3])\n" +
            "rankingExpression(foo1).input2.type : tensor(x[3])\n" +
            "rankingExpression(foo2).rankingScript : reduce(reduce(input1 * input2, sum, name) * constant(constant1asLarge), max, x) * 3.0\n" +
            "rankingExpression(foo2).input1.type : tensor(name{},x[3])\n" +
            "rankingExpression(foo2).input2.type : tensor(x[3])\n";


    /** The model name */
    private final String name = "example";

    @AfterEach
    public void removeGeneratedModelFiles() {
        IOUtils.recursiveDeleteDir(applicationDir.append(ApplicationPackage.MODELS_GENERATED_DIR).toFile());
    }

    @Test
    void testGlobalVespaModel() throws IOException {
        ImportedModelTester tester = new ImportedModelTester(name, applicationDir);
        VespaModel model = tester.createVespaModel();
        tester.assertLargeConstant("constant1asLarge", model, Optional.of(3L));
        assertEquals(expectedRankConfig, rankConfigOf("example", model));

        // 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();
            storedTester.assertLargeConstant("constant1asLarge", model, Optional.of(3L));
            assertEquals(expectedRankConfig, rankConfigOf("example", storedModel));
        }
        finally {
            IOUtils.recursiveDeleteDir(storedAppDir.toFile());
        }
    }

    private String rankConfigOf(String rankProfileName, VespaModel model) {
        StringBuilder b = new StringBuilder();
        RawRankProfile profile = model.rankProfileList().getRankProfiles().get(rankProfileName);
        for (var property : profile.configProperties())
            b.append(property.getFirst()).append(" : ").append(property.getSecond()).append("\n");
        return b.toString();
    }

}