aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/tensor/serialization/SerializationTestCase.java
blob: e3896e8a2d07d0312d0fc00c28a966fe940ccba8 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.tensor.serialization;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yahoo.io.GrowableByteBuffer;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

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

public class SerializationTestCase {

    private static String testPath = "eval/src/apps/make_tensor_binary_format_test_spec/test_spec.json";
    private static List<String> tests = new ArrayList<>();

    @Before
    public void loadTests() throws IOException {
        File testSpec = new File(testPath);
        if (!testSpec.exists()) {
            testSpec = new File("../" + testPath);
        }
        try(BufferedReader br = new BufferedReader(new FileReader(testSpec))) {
            String test = br.readLine();
            while (test != null) {
                tests.add(test);
                test = br.readLine();
            }
        }
    }

    @Test
    public void testSerialization() throws IOException {
        for (String test : tests) {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode node = mapper.readTree(test);
            if (node.has("tensor") && node.has("binary")) {
                System.out.println("Running test: " + test);

                Tensor tensor = buildTensor(node.get("tensor"));
                String spec = getSpec(node.get("tensor"));
                byte[] encodedTensor = TypedBinaryFormat.encode(tensor);
                boolean serializedToABinaryRepresentation = false;

                JsonNode binaryNode = node.get("binary");
                for (int i = 0; i < binaryNode.size(); ++i) {
                    byte[] bin = getBytes(binaryNode.get(i).asText());
                    Tensor decodedTensor = TypedBinaryFormat.decode(Optional.empty(), GrowableByteBuffer.wrap(bin));

                    if (spec.equalsIgnoreCase("double")) {
                        assertEquals(tensor.asDouble(), decodedTensor.asDouble(), 1e-6);
                    } else {
                        assertEquals(tensor, decodedTensor);
                    }

                    if (Arrays.equals(encodedTensor, bin)) {
                        serializedToABinaryRepresentation = true;
                    }
                }
                assertTrue("Tensor serialized to one of the given representations",
                           serializedToABinaryRepresentation);
            }
        }
    }

    private Tensor buildTensor(JsonNode tensor) {
        TensorType type = tensorType(tensor);
        Tensor.Builder builder = Tensor.Builder.of(type);
        tensorCells(tensor, builder);
        return builder.build();
    }

    private TensorType tensorType(JsonNode tensor) {
        String spec = getSpec(tensor);
        if (spec.equalsIgnoreCase("double")) {
            spec = "tensor()";
        }
        return TensorType.fromSpec(spec);
    }

    private String getSpec(JsonNode tensor) {
        return tensor.get("type").asText();
    }

    private void tensorCells(JsonNode tensor, Tensor.Builder builder) {
        JsonNode cells = tensor.get("cells");
        for (JsonNode cell : cells) {
            tensorCell(cell, builder.cell());
        }
    }

    private void tensorCell(JsonNode cell, Tensor.Builder.CellBuilder cellBuilder) {
        tensorCellAddress(cellBuilder, cell.get("address"));
        tensorCellValue(cellBuilder, cell.get("value"));
    }

    private void tensorCellValue(Tensor.Builder.CellBuilder cellBuilder, JsonNode value) {
        cellBuilder.value(value.doubleValue());
    }

    private void tensorCellAddress(Tensor.Builder.CellBuilder cellBuilder, JsonNode address) {
        Iterator<String> dimension = address.fieldNames();
        while (dimension.hasNext()) {
            String name = dimension.next();
            JsonNode label = address.get(name);
            cellBuilder.label(name, label.asText());
        }
    }

    private byte[] getBytes(String binaryRepresentation) {
        return parseHexValue(binaryRepresentation.substring(2));
    }

    private byte[] parseHexValue(String s) {
        final int len = s.length();
        byte[] bytes = new byte[len/2];
        for (int i = 0; i < len; i += 2) {
            int c1 = hexValue(s.charAt(i)) << 4;
            int c2 = hexValue(s.charAt(i + 1));
            bytes[i/2] = (byte)(c1 + c2);
        }
        return bytes;
    }

    private int hexValue(Character c) {
        if (c >= 'a' && c <= 'f') {
            return c - 'a' + 10;
        } else if (c >= 'A' && c <= 'F') {
            return c - 'A' + 10;
        } else if (c >= '0' && c <= '9') {
            return c - '0';
        }
        throw new IllegalArgumentException("Hex contains illegal characters");
    }

}