aboutsummaryrefslogtreecommitdiff
path: root/src/armnnTfLiteParser/test/GetBuffer.cpp
blob: 9dfc9fff13fbf975b1dd7f6b4e68ca0ef3a73d60 (plain)
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
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "ParserFlatbuffersFixture.hpp"
#include "../TfLiteParser.hpp"
#include <sstream>

using armnnTfLiteParser::TfLiteParserImpl;

TEST_SUITE("TensorflowLiteParser_GetBuffer")
{
struct GetBufferFixture : public ParserFlatbuffersFixture
{
    explicit GetBufferFixture()
    {
        m_JsonString = R"(
            {
                "version": 3,
                "operator_codes": [ { "builtin_code": "CONV_2D" } ],
                "subgraphs": [ {
                    "tensors": [
                        {
                            "shape": [ 1, 3, 3, 1 ],
                            "type": "UINT8",
                            "buffer": 0,
                            "name": "inputTensor",
                            "quantization": {
                                "min": [ 0.0 ],
                                "max": [ 255.0 ],
                                "scale": [ 1.0 ],
                                "zero_point": [ 0 ],
                            }
                        },
                        {
                            "shape": [ 1, 1, 1, 1 ],
                            "type": "UINT8",
                            "buffer": 1,
                            "name": "outputTensor",
                            "quantization": {
                                "min": [ 0.0 ],
                                "max": [ 511.0 ],
                                "scale": [ 2.0 ],
                                "zero_point": [ 0 ],
                            }
                        },
                        {
                            "shape": [ 1, 3, 3, 1 ],
                            "type": "UINT8",
                            "buffer": 2,
                            "name": "filterTensor",
                            "quantization": {
                                "min": [ 0.0 ],
                                "max": [ 255.0 ],
                                "scale": [ 1.0 ],
                                "zero_point": [ 0 ],
                            }
                        }
                    ],
                    "inputs": [ 0 ],
                    "outputs": [ 1 ],
                    "operators": [
                        {
                            "opcode_index": 0,
                            "inputs": [ 0, 2 ],
                            "outputs": [ 1 ],
                            "builtin_options_type": "Conv2DOptions",
                            "builtin_options": {
                                "padding": "VALID",
                                "stride_w": 1,
                                "stride_h": 1,
                                "fused_activation_function": "NONE"
                            },
                            "custom_options_format": "FLEXBUFFERS"
                        }
                    ],
                } ],
                "buffers" : [
                    { },
                    { },
                    { "data": [ 2,1,0,  6,2,1, 4,1,2 ], },
                    { },
                ]
            }
        )";
        ReadStringToBinary();
    }

    void CheckBufferContents(const TfLiteParserImpl::ModelPtr& model,
                             std::vector<int32_t> bufferValues, size_t bufferIndex)
    {
        for(long unsigned int i=0; i<bufferValues.size(); i++)
        {
            CHECK_EQ(TfLiteParserImpl::GetBuffer(model, bufferIndex)->data[i], bufferValues[i]);
        }
    }
};

TEST_CASE_FIXTURE(GetBufferFixture, "GetBufferCheckContents")
{
    //Check contents of buffer are correct
    TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
                                                                             m_GraphBinary.size());
    std::vector<int32_t> bufferValues = {2,1,0,6,2,1,4,1,2};
    CheckBufferContents(model, bufferValues, 2);
}

TEST_CASE_FIXTURE(GetBufferFixture, "GetBufferCheckEmpty")
{
    //Check if test fixture buffers are empty or not
    TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
                                                                             m_GraphBinary.size());
    CHECK(TfLiteParserImpl::GetBuffer(model, 0)->data.empty());
    CHECK(TfLiteParserImpl::GetBuffer(model, 1)->data.empty());
    CHECK(!TfLiteParserImpl::GetBuffer(model, 2)->data.empty());
    CHECK(TfLiteParserImpl::GetBuffer(model, 3)->data.empty());
}

TEST_CASE_FIXTURE(GetBufferFixture, "GetBufferCheckParseException")
{
    //Check if armnn::ParseException thrown when invalid buffer index used
    TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
                                                                             m_GraphBinary.size());
    CHECK_THROWS_AS(TfLiteParserImpl::GetBuffer(model, 4), armnn::Exception);
}

}