ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Conv2D.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __NNOP_CONV2D_H__
18#define __NNOP_CONV2D_H__
19
20#include "nnop/PadInfo.h"
21#include "nnop/StrideInfo.h"
22
26
29
30namespace nnop
31{
32
33template <typename OutputDType, typename InputDType, typename KernelDType>
36 const nncc::core::ADT::feature::Shape &in_shape,
38 const nncc::core::ADT::kernel::Shape &ker_shape,
39 const nncc::core::ADT::kernel::Reader<KernelDType> &ker_data, const PadInfo &pad_info,
40 const StrideInfo &stride_info)
41{
42 for (uint32_t out_ch = 0; out_ch < out_shape.depth(); ++out_ch)
43 {
44 for (uint32_t out_row = 0; out_row < out_shape.height(); ++out_row)
45 {
46 for (uint32_t out_col = 0; out_col < out_shape.width(); ++out_col)
47 {
48 OutputDType out_value = 0;
49
50 for (uint32_t ker_ch = 0; ker_ch < ker_shape.depth(); ++ker_ch)
51 {
52 for (uint32_t ker_row = 0; ker_row < ker_shape.height(); ++ker_row)
53 {
54 for (uint32_t ker_col = 0; ker_col < ker_shape.width(); ++ker_col)
55 {
56 const int64_t vertical_stride = static_cast<int64_t>(stride_info.vertical());
57 const int64_t horizontal_stride = static_cast<int64_t>(stride_info.horizontal());
58 const int64_t top_padding = static_cast<int64_t>(pad_info.top());
59 const int64_t left_padding = static_cast<int64_t>(pad_info.left());
60
61 const uint32_t in_ch = ker_ch;
62 const int64_t in_row = vertical_stride * out_row - top_padding + ker_row;
63 const int64_t in_col = horizontal_stride * out_col - left_padding + ker_col;
64
65 const bool is_padding = (in_row < 0) || (in_row >= in_shape.height()) ||
66 (in_col < 0) || (in_col >= in_shape.width());
67
68 const auto in_value = (is_padding) ? 0
69 : in_data.at(in_ch, static_cast<uint32_t>(in_row),
70 static_cast<uint32_t>(in_col));
71
72 const auto ker_value = ker_data.at(out_ch, in_ch, ker_row, ker_col);
73
74 out_value += in_value * ker_value;
75 }
76 }
77 }
78
79 out_data.at(out_ch, out_row, out_col) = out_value;
80 }
81 }
82 }
83}
84
85} // namespace nnop
86
87#endif // __NNOP_CONV2D_H__
uint32_t depth(void) const
Definition Shape.h:44
uint32_t width(void) const
Definition Shape.h:46
uint32_t height(void) const
Definition Shape.h:45
uint32_t width(void) const
Definition Shape.h:47
uint32_t height(void) const
Definition Shape.h:46
uint32_t depth(void) const
Definition Shape.h:45
uint32_t left(void) const
Definition PadInfo.h:37
uint32_t top(void) const
Definition PadInfo.h:35
uint32_t vertical(void) const
Definition StrideInfo.h:34
uint32_t horizontal(void) const
Definition StrideInfo.h:35
Definition Conv2D.h:31
void conv(const nncc::core::ADT::feature::Shape &out_shape, nncc::core::ADT::feature::Accessor< OutputDType > &out_data, const nncc::core::ADT::feature::Shape &in_shape, const nncc::core::ADT::feature::Reader< InputDType > &in_data, const nncc::core::ADT::kernel::Shape &ker_shape, const nncc::core::ADT::kernel::Reader< KernelDType > &ker_data, const PadInfo &pad_info, const StrideInfo &stride_info)
Definition Conv2D.h:34
virtual T & at(uint32_t ch, uint32_t row, uint32_t col)=0
virtual T at(uint32_t ch, uint32_t row, uint32_t col) const =0
virtual T at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) const =0