• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include <boost/test/unit_test.hpp>
6 #include "armnnCaffeParser/ICaffeParser.hpp"
7 #include "ParserPrototxtFixture.hpp"
8 #include <sstream>
9 #include <initializer_list>
10 
11 namespace
12 {
13 
14 template <typename T>
TaggedSequence(const std::string & tag,const std::initializer_list<T> & data)15 std::string TaggedSequence(const std::string & tag, const std::initializer_list<T> & data)
16 {
17     bool first = true;
18     std::stringstream ss;
19     for (auto && d : data)
20     {
21         if (!first)
22         {
23             ss << " , ";
24         }
25         else
26         {
27             first = false;
28         }
29         ss << " " << tag << " : " << d << " ";
30     }
31     return ss.str();
32 }
33 
34 template <typename T>
TaggedSequence(const std::string & tag,T data,unsigned int n)35 std::string TaggedSequence(const std::string & tag, T data, unsigned int n)
36 {
37     std::stringstream ss;
38     for (unsigned int i=0; i<n; ++i)
39     {
40         if (i>0)
41         {
42             ss << " , ";
43         }
44         ss << " " << tag << " : " << data << " ";
45     }
46     return ss.str();
47 }
48 
49 } // namespace <anonymous>
50 
51 BOOST_AUTO_TEST_SUITE(CaffeParser)
52 
53 struct ConvolutionFixture : public armnnUtils::ParserPrototxtFixture<armnnCaffeParser::ICaffeParser>
54 {
ConvolutionFixtureConvolutionFixture55     ConvolutionFixture(const std::initializer_list<unsigned int> & inputDims,
56                        const std::initializer_list<float> & filterData,
57                        unsigned int kernelSize,
58                        unsigned int numOutput=1,
59                        unsigned int group=1)
60     {
61         m_Prototext = R"(
62             name: "ConvolutionTest"
63             layer {
64                 name: "input1"
65                 type: "Input"
66                 top: "input1"
67                 input_param { shape: { )" + TaggedSequence("dim", inputDims) + R"( } }
68             }
69             layer {
70                 name: "conv1"
71                 type: "Convolution"
72                 bottom: "input1"
73                 top: "conv1"
74                 blobs: { )" + TaggedSequence("data", filterData) + R"( }
75                 blobs: { )" + TaggedSequence("data", 0, numOutput) + R"( }
76                 convolution_param {
77                     num_output: )" + std::to_string(numOutput) + R"(
78                     pad: 0
79                     kernel_size: )" +  std::to_string(kernelSize) + R"(
80                     stride: 1
81                     group: )" +  std::to_string(group) + R"(
82                 }
83             }
84         )";
85         SetupSingleInputSingleOutput("input1", "conv1");
86     }
87 };
88 
89 struct SimpleConvolutionFixture : public ConvolutionFixture
90 {
SimpleConvolutionFixtureSimpleConvolutionFixture91     SimpleConvolutionFixture()
92     : ConvolutionFixture( {1, 1, 2, 2}, {1.0f, 1.0f, 1.0f, 1.0f}, 2)
93     {
94     }
95 };
96 
BOOST_FIXTURE_TEST_CASE(SimpleConvolution,SimpleConvolutionFixture)97 BOOST_FIXTURE_TEST_CASE(SimpleConvolution, SimpleConvolutionFixture)
98 {
99     RunTest<4>({ 1, 3, 5, 7 }, { 16 });
100 }
101 
102 struct GroupConvolutionFixture : public ConvolutionFixture
103 {
GroupConvolutionFixtureGroupConvolutionFixture104     GroupConvolutionFixture()
105     : ConvolutionFixture(
106         {1, 2, 2, 2},
107         {
108             1.0f, 1.0f, 1.0f, 1.0f, // filter for channel #0
109             2.0f, 2.0f, 2.0f, 2.0f  // filter for channel #1
110         },
111         2, // kernel size is 2x2
112         2, // number of output channels is 2
113         2) // number of groups (separate filters)
114     {
115     }
116 };
117 
BOOST_FIXTURE_TEST_CASE(GroupConvolution,GroupConvolutionFixture)118 BOOST_FIXTURE_TEST_CASE(GroupConvolution, GroupConvolutionFixture)
119 {
120     RunTest<4>(
121         {
122             1, 2, 3, 4, // input channel #0
123             5, 6, 7, 8, // input channel #1
124         },
125         {
126             10, // convolution result for channel #0 applying filter #0
127             52  // same for channel #1 and filter #1
128         }
129     );
130 }
131 
132 
133 BOOST_AUTO_TEST_SUITE_END()