blob: ecc582b181648169e848c025f9ff771f4eb3d0ec [file] [log] [blame]
Sanghoon Leef47bfb92018-01-23 15:16:47 +00001/*
2 * Copyright (c) 2017-2018 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "LocallyConnected.h"
25
26#include "tests/validation/Helpers.h"
27#include "tests/validation/reference/Convolution3d.h"
28#include "tests/validation/reference/Utils.h"
29
30#include "tests/framework/Asserts.h"
31
32namespace arm_compute
33{
34namespace test
35{
36namespace validation
37{
38namespace reference
39{
40template <typename T, typename TB>
41SimpleTensor<T> locally_connected(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, const TensorShape &output_shape, const PadStrideInfo &info)
42{
43 // Create reference
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010044 SimpleTensor<T> dst{ output_shape, src.data_type(), 1, src.quantization_info() };
Sanghoon Leef47bfb92018-01-23 15:16:47 +000045
46 // Compute reference
47 const int width_in = src.shape().x();
48 const int height_in = src.shape().y();
49 const int depth_in = src.shape().z();
50
51 const int width_out = dst.shape().x();
52 const int height_out = dst.shape().y();
53 const int depth_out = dst.shape().z();
54
55 const int width_weights = weights.shape().x();
56 const int height_weights = weights.shape().y();
57 const int depth_weights = weights.shape().z();
58
59 const int pad_left = info.pad_left();
60 const int pad_top = info.pad_top();
61 const int stride_xi = info.stride().first;
62 const int stride_yi = info.stride().second;
63
64 auto output_wh = scaled_dimensions(width_in, height_in, width_weights, height_weights, info);
65
66 const int start_xi = width_weights / 2 - pad_left;
67 const int start_yi = height_weights / 2 - pad_top;
68 const int end_xi = output_wh.first * stride_xi;
69 const int end_yi = output_wh.second * stride_yi;
70 const int num_batches = src.shape().total_size() / (width_in * height_in * depth_in);
71
72 for(int r = 0; r < num_batches; ++r)
73 {
74 int count = 0;
75 for(int yi = start_yi; yi < start_yi + end_yi; yi += stride_yi)
76 {
77 for(int xi = start_xi; xi < start_xi + end_xi; xi += stride_xi)
78 {
79 for(int ofm = 0; ofm < depth_out; ++ofm)
80 {
81 // Compute input and output offsets
82 const int offset_in = r * width_in * height_in * depth_in;
83 const int xo = (xi - start_xi) / stride_xi;
84 const int yo = (yi - start_yi) / stride_yi;
85 const int offset_out = xo + yo * width_out + ofm * width_out * height_out + r * width_out * height_out * depth_out;
86
87 ARM_COMPUTE_ASSERT(xo < width_out);
88 ARM_COMPUTE_ASSERT(yo < height_out);
89
90 // Compute 3D convolution
91 convolution_3d::detail::convolution3d(src, weights, bias, dst,
92 offset_in, count * width_weights * height_weights * depth_weights, count, offset_out,
93 xi, yi,
94 width_in, height_in, depth_in,
95 width_weights, height_weights);
96 count++;
97 }
98 }
99 }
100 }
101
102 return dst;
103}
104
105// Locally Connected only supports F32
106template SimpleTensor<float> locally_connected(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &output_shape,
107 const PadStrideInfo &info);
108} // namespace reference
109} // namespace validation
110} // namespace test
111} // namespace arm_compute