aboutsummaryrefslogtreecommitdiff
path: root/src/backends/backendsCommon/test/SliceEndToEndTestImpl.hpp
blob: 43bc66875317e8d34f0ee590a3432ac996d76131 (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
129
130
131
132
//
// Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//
#pragma once

#include <armnn/INetwork.hpp>

#include <CommonTestUtils.hpp>
#include <ResolveType.hpp>

#include <doctest/doctest.h>

namespace
{

template<typename armnn::DataType DataType>
armnn::INetworkPtr CreateSliceNetwork(const armnn::TensorShape& inputShape,
                                      const armnn::TensorShape& outputShape,
                                      const armnn::SliceDescriptor& descriptor,
                                      const float qScale = 1.0f,
                                      const int32_t qOffset = 0)
{
    using namespace armnn;

    INetworkPtr network(INetwork::Create());

    TensorInfo inputTensorInfo(inputShape, DataType, qScale, qOffset, true);
    TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset);


    IConnectableLayer* slice  = network->AddSliceLayer(descriptor, "slice");
    IConnectableLayer* input  = network->AddInputLayer(0, "input");
    IConnectableLayer* output = network->AddOutputLayer(0, "output");

    Connect(input, slice, inputTensorInfo, 0, 0);
    Connect(slice, output, outputTensorInfo, 0, 0);

    return network;
}

template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
void SliceEndToEnd(const std::vector<armnn::BackendId>& backends)
{
    using namespace armnn;

    const TensorShape& inputShape  = { 3, 2, 3 };
    const TensorShape& outputShape = { 2, 1, 3 };

    SliceDescriptor descriptor;
    descriptor.m_Begin = { 1, 0, 0 };
    descriptor.m_Size  = { 2, 1, 3 };

    INetworkPtr network = CreateSliceNetwork<ArmnnType>(inputShape, outputShape, descriptor);

    CHECK(network);

    std::vector<T> inputData{ 1, 1, 1, 2, 2, 2,
                              3, 3, 3, 4, 4, 4,
                              5, 5, 5, 6, 6, 6 };
    std::vector<T> expectedOutput{ 3, 3, 3,
                                   5, 5, 5 };

    std::map<int, std::vector<T>> inputTensorData = { { 0, inputData } };
    std::map<int, std::vector<T>> expectedOutputData = { { 0, expectedOutput } };

    EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends);
}

template<armnn::DataType ArmnnType>
void SliceEndToEndFloat16(const std::vector<armnn::BackendId>& backends)
{
    using namespace armnn;
    using namespace half_float::literal;
    using Half = half_float::half;

    const TensorShape& inputShape = { 3, 2, 3 };
    const TensorShape& outputShape = { 2, 1, 3 };

    SliceDescriptor descriptor;
    descriptor.m_Begin = { 1, 0, 0 };
    descriptor.m_Size  = { 2, 1, 3 };

    INetworkPtr network = CreateSliceNetwork<ArmnnType>(inputShape, outputShape, descriptor);
    CHECK(network);

    std::vector<Half> inputData{ 1._h, 1._h, 1._h, 2._h, 2._h, 2._h,
                                 3._h, 3._h, 3._h, 4._h, 4._h, 4._h,
                                 5._h, 5._h, 5._h, 6._h, 6._h, 6._h };
    std::vector<Half> expectedOutput{ 3._h, 3._h, 3._h,
                                      5._h, 5._h, 5._h };

    std::map<int, std::vector<Half>> inputTensorData = { { 0, inputData } };
    std::map<int, std::vector<Half>> expectedOutputData = { { 0, expectedOutput } };

    EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends);
}

template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
void SliceEndToEnd4Dim(const std::vector<armnn::BackendId>& backends)
{
    using namespace armnn;

    const TensorShape& inputShape  = { 2, 3, 2, 3 };
    const TensorShape& outputShape = { 1, 3, 2, 1 };

    SliceDescriptor descriptor;
    descriptor.m_Begin = { 1, 0, 0, 0 };
    descriptor.m_Size  = { 1, 3, 2, 1 };

    INetworkPtr network = CreateSliceNetwork<ArmnnType>(inputShape, outputShape, descriptor);

    CHECK(network);

    std::vector<T> inputData{ 1, 1, 1, 2, 2, 2,
                              3, 3, 3, 4, 4, 4,
                              5, 5, 5, 6, 6, 6,
                              1, 1, 1, 2, 2, 2,
                              3, 3, 3, 4, 4, 4,
                              5, 5, 5, 6, 6, 6 };

    std::vector<T> expectedOutput{ 1, 2,
                                   3, 4,
                                   5, 6 };

    std::map<int, std::vector<T>> inputTensorData = { { 0, inputData } };
    std::map<int, std::vector<T>> expectedOutputData = { { 0, expectedOutput } };

    EndToEndLayerTestImpl<ArmnnType, ArmnnType>(std::move(network), inputTensorData, expectedOutputData, backends);
}

} // anonymous namespace