Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * Copyright (C) 2021 Jijoong Moon <jijoong.moon@samsung.com>
4 : *
5 : * @file conv1d_layer.h
6 : * @date 13 Oct 2021
7 : * @see https://github.com/nnstreamer/nntrainer
8 : * @author Jijoong Moon <jijoong.moon@samsung.com>
9 : * @author Parichay Kapoor <pk.kapoor@samsung.com>
10 : * @bug No known bugs except for NYI items
11 : * @brief This is Convolution 1D Layer Class for Neural Network
12 : *
13 : */
14 : #include <algorithm>
15 : #include <cstring>
16 : #include <limits>
17 : #include <string>
18 :
19 : #include <conv1d_layer.h>
20 : #include <conv2d_layer.h>
21 : #include <layer_context.h>
22 : #include <nntrainer_error.h>
23 : #include <nntrainer_log.h>
24 : #include <node_exporter.h>
25 : #include <util_func.h>
26 :
27 : namespace nntrainer {
28 :
29 : static constexpr size_t SINGLE_INOUT_IDX = 0;
30 :
31 40 : Conv1DLayer::Conv1DLayer() :
32 : LayerImpl(),
33 120 : conv_props(props::FilterSize(), props::KernelSize(), props::Stride(),
34 120 : props::Padding1D(), props::Dilation()) {
35 : wt_idx.fill(std::numeric_limits<unsigned>::max());
36 40 : conv2d_layer = std::make_unique<Conv2DLayer>();
37 40 : }
38 :
39 80 : Conv1DLayer::~Conv1DLayer() {}
40 :
41 26 : void Conv1DLayer::finalize(InitLayerContext &context) {
42 26 : NNTR_THROW_IF(context.getNumInputs() != 1, std::invalid_argument)
43 : << "Convolution layer takes only one input";
44 :
45 26 : NNTR_THROW_IF(context.getInputDimensions()[SINGLE_INOUT_IDX].height() != 1,
46 : std::invalid_argument)
47 : << "Conv1D layer requires input with height 1";
48 :
49 : const TensorDim &in_dim = context.getInputDimensions()[0];
50 : const unsigned int kernel_size =
51 26 : std::get<props::KernelSize>(conv_props).get();
52 26 : const unsigned int stride = std::get<props::Stride>(conv_props).get();
53 26 : const unsigned int dilation = std::get<props::Dilation>(conv_props).get();
54 :
55 : const std::array<unsigned int, 2> padding =
56 : std::get<props::Padding1D>(conv_props)
57 26 : .compute(in_dim, kernel_size, stride, dilation);
58 : const std::string padding_str =
59 78 : "0,0," + std::to_string(padding[0]) + "," + std::to_string(padding[1]);
60 :
61 : /** set the given properties as key value pair */
62 130 : auto setPropertyKV = [this](const std::string &key,
63 : const std::string &value) {
64 130 : auto const &prop = key + "=" + value;
65 390 : conv2d_layer->setProperty({prop});
66 130 : };
67 :
68 52 : setPropertyKV(props::FilterSize::key,
69 26 : std::to_string(std::get<props::FilterSize>(conv_props).get()));
70 78 : setPropertyKV(props::KernelSize::key, "1," + std::to_string(kernel_size));
71 78 : setPropertyKV(props::Stride::key, "1," + std::to_string(stride));
72 26 : setPropertyKV(props::Padding2D::key, padding_str);
73 104 : setPropertyKV(props::Dilation::key, "1," + std::to_string(dilation));
74 :
75 26 : conv2d_layer->finalize(context);
76 26 : }
77 :
78 110 : void Conv1DLayer::forwarding(RunLayerContext &context, bool training) {
79 110 : conv2d_layer->forwarding(context, training);
80 110 : }
81 :
82 22 : void Conv1DLayer::calcDerivative(RunLayerContext &context) {
83 22 : conv2d_layer->calcDerivative(context);
84 22 : }
85 :
86 22 : void Conv1DLayer::calcGradient(RunLayerContext &context) {
87 22 : conv2d_layer->calcGradient(context);
88 22 : }
89 :
90 0 : void Conv1DLayer::exportTo(Exporter &exporter,
91 : const ml::train::ExportMethods &method) const {
92 0 : LayerImpl::exportTo(exporter, method);
93 0 : exporter.saveResult(conv_props, method, this);
94 0 : }
95 :
96 55 : void Conv1DLayer::setProperty(const std::vector<std::string> &values) {
97 55 : auto remain_props = loadProperties(values, conv_props);
98 54 : LayerImpl::setProperty(remain_props);
99 54 : }
100 :
101 : } // namespace nntrainer
|