aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/tensor/functions/MatmulTestCase.java
blob: 46a29ddc13cb409a3f770b5704a902d34dbe9868 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.tensor.functions;

import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;

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

    @Test
    public void testMatmul2d() {
        // d0 is the 'outermost' dimension, etc.
        Tensor.Builder ab = Tensor.Builder.of(TensorType.fromSpec("tensor(d0[2],d1[3])"));
        ab.cell( 1,0, 0);
        ab.cell( 2,0, 1);
        ab.cell( 3,0, 2);
        ab.cell( 4,1, 0);
        ab.cell( 5,1, 1);
        ab.cell( 6,1, 2);
        Tensor a = ab.build();

        Tensor.Builder bb = Tensor.Builder.of(TensorType.fromSpec("tensor(d0[3],d1[2])"));
        bb.cell( 7,0, 0);
        bb.cell( 8,0, 1);
        bb.cell( 9,1, 0);
        bb.cell(10,1, 1);
        bb.cell(11,2, 0);
        bb.cell(12,2, 1);
        Tensor b = bb.build();

        Tensor.Builder rb = Tensor.Builder.of(TensorType.fromSpec("tensor(d0[2],d1[2])"));
        rb.cell( 58,0, 0);
        rb.cell( 64,0, 1);
        rb.cell(139,1, 0);
        rb.cell(154,1, 1);
        Tensor r = rb.build();

        Tensor result = a.matmul(b.rename(List.of("d0","d1"), List.of("d1","d2")), "d1")
                         .rename("d2","d1");
        assertEquals(r, result);
    }

    @Test
    public void testMatmul3d() {
        // Convention: a is the 'outermost' dimension, etc.
        Tensor.Builder ab = Tensor.Builder.of(TensorType.fromSpec("tensor(d0[2],d1[2],d2[3])"));
        ab.cell( 1,0, 0, 0);
        ab.cell( 2,0, 0, 1);
        ab.cell( 3,0, 0, 2);
        ab.cell( 4,0, 1, 0);
        ab.cell( 5,0, 1, 1);
        ab.cell( 6,0, 1, 2);
        ab.cell( 7,1, 0, 0);
        ab.cell( 8,1, 0, 1);
        ab.cell( 9,1, 0, 2);
        ab.cell(10,1, 1, 0);
        ab.cell(11,1, 1, 1);
        ab.cell(12,1, 1, 2);
        Tensor a = ab.build();

        Tensor.Builder bb = Tensor.Builder.of(TensorType.fromSpec("tensor(d0[2],d1[3],d2[2])"));
        bb.cell(13,0, 0, 0);
        bb.cell(14,0, 0, 1);
        bb.cell(15,0, 1, 0);
        bb.cell(16,0, 1, 1);
        bb.cell(17,0, 2, 0);
        bb.cell(18,0, 2, 1);
        bb.cell(19,1, 0, 0);
        bb.cell(20,1, 0, 1);
        bb.cell(21,1, 1, 0);
        bb.cell(22,1, 1, 1);
        bb.cell(23,1, 2, 0);
        bb.cell(24,1, 2, 1);
        Tensor b = bb.build();

        Tensor.Builder rb = Tensor.Builder.of(TensorType.fromSpec("tensor(d0[2],d1[2],d2[2])"));
        rb.cell( 94,0, 0, 0);
        rb.cell(100,0, 0, 1);
        rb.cell(229,0, 1, 0);
        rb.cell(244,0, 1, 1);
        rb.cell(508,1, 0, 0);
        rb.cell(532,1, 0, 1);
        rb.cell(697,1, 1, 0);
        rb.cell(730,1, 1, 1);
        Tensor r = rb.build();

        Tensor result = a.matmul(b.rename(List.of("d1","d2"), List.of("d2","d3")), "d2")
                         .rename("d3","d2");
        assertEquals(r, result);
    }

}