aboutsummaryrefslogtreecommitdiffstats
path: root/model-integration/src/test/java/ai/vespa/rankingexpression/importer/onnx/OnnxMnistSoftmaxImportTestCase.java
blob: 7fb167ee6f1cd56e832b9871ebe16682c565a4c3 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.rankingexpression.importer.onnx;

import ai.vespa.rankingexpression.importer.configmodelview.ImportedMlFunction;
import ai.vespa.rankingexpression.importer.ImportedModel;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import org.junit.Test;

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

/**
 * @author lesters
 */
public class OnnxMnistSoftmaxImportTestCase {

    @Test
    public void testMnistSoftmaxImport() {
        ImportedModel model = new OnnxImporter().importModel("test", "src/test/models/onnx/mnist_softmax/mnist_softmax.onnx").asNative();

        // Check constants
        assertEquals(2, model.largeConstantTensors().size());

        Tensor constant0 = model.largeConstantTensors().get("test_Variable");
        assertNotNull(constant0);
        assertEquals(new TensorType.Builder(TensorType.Value.FLOAT).indexed("d2", 784).indexed("d1", 10).build(),
                     constant0.type());
        assertEquals(7840, constant0.size());

        Tensor constant1 = model.largeConstantTensors().get("test_Variable_1");
        assertNotNull(constant1);
        assertEquals(new TensorType.Builder(TensorType.Value.FLOAT).indexed("d1", 10).build(), constant1.type());
        assertEquals(10, constant1.size());

        // Check inputs
        assertEquals(1, model.inputs().size());
        assertTrue(model.inputs().containsKey("Placeholder"));
        assertEquals(TensorType.fromSpec("tensor<float>(d0[],d1[784])"), model.inputs().get("Placeholder"));

        // Check signature
        ImportedMlFunction output = model.defaultSignature().outputFunction("add", "add");
        assertNotNull(output);
        assertEquals("join(reduce(join(rename(Placeholder, (d0, d1), (d0, d2)), constant(test_Variable), f(a,b)(a * b)), sum, d2), constant(test_Variable_1), f(a,b)(a + b))",
                     output.expression());
        assertEquals(TensorType.fromSpec("tensor<float>(d0[],d1[784])"),
                     model.inputs().get(model.defaultSignature().inputs().get("Placeholder")));
        assertEquals("{Placeholder=tensor<float>(d0[],d1[784])}", output.argumentTypes().toString());
    }

}