summaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionWithOnnxModelTestCase.java
blob: 7e129410b371183edbb3fcd5f04e532d40eb6f7e (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
// Copyright Verizon Media. 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.expressiontransforms.OnnxModelTransformer;
import com.yahoo.vespa.config.search.RankProfilesConfig;
import com.yahoo.vespa.config.search.core.OnnxModelsConfig;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.search.DocumentDatabase;
import com.yahoo.vespa.model.search.IndexedSearchCluster;
import com.yahoo.vespa.model.test.utils.VespaModelCreatorWithFilePkg;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class RankingExpressionWithOnnxModelTestCase {

    @Test
    public void testOnnxModelFeature() {
        VespaModel model = new VespaModelCreatorWithFilePkg("src/test/integration/onnx-file").create();
        DocumentDatabase db = ((IndexedSearchCluster)model.getSearchClusters().get(0)).getDocumentDbs().get(0);

        String modelName = OnnxModelTransformer.toModelName("files/simple.onnx");

        // Ranking expression should be transformed from
        //     onnxModel("files/simple.onnx", "output")
        // to
        //     onnxModel(files_simple_onnx).output

        assertTransformedFeature(db, modelName);
        assertGeneratedConfig(db, modelName);
    }

    private void assertGeneratedConfig(DocumentDatabase db, String modelName) {
        OnnxModelsConfig.Builder builder = new OnnxModelsConfig.Builder();
        ((OnnxModelsConfig.Producer) db).getConfig(builder);
        OnnxModelsConfig config = new OnnxModelsConfig(builder);
        assertEquals(1, config.model().size());
        assertEquals(modelName, config.model(0).name());
    }

    private void assertTransformedFeature(DocumentDatabase db, String modelName) {
        RankProfilesConfig.Builder builder = new RankProfilesConfig.Builder();
        ((RankProfilesConfig.Producer) db).getConfig(builder);
        RankProfilesConfig config = new RankProfilesConfig(builder);
        assertEquals(3, config.rankprofile().size());
        assertEquals("my_profile", config.rankprofile(2).name());
        assertEquals("vespa.rank.firstphase", config.rankprofile(2).fef().property(0).name());
        assertEquals("onnxModel(" + modelName + ").output", config.rankprofile(2).fef().property(0).value());
    }

}