Line data Source code
1 : /**
2 : * Copyright (C) 2020 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 : * http://www.apache.org/licenses/LICENSE-2.0
8 : * Unless required by applicable law or agreed to in writing, software
9 : * distributed under the License is distributed on an "AS IS" BASIS,
10 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 : * See the License for the specific language governing permissions and
12 : * limitations under the License.
13 : *
14 : *
15 : * @file input_layer.cpp
16 : * @date 14 May 2020
17 : * @brief This is Input Layer Class for Neural Network
18 : * @see https://github.com/nnstreamer/nntrainer
19 : * @author Jijoong Moon <jijoong.moon@samsung.com>
20 : * @bug No known bugs except for NYI items
21 : *
22 : */
23 :
24 : #include <input_layer.h>
25 : #include <layer_context.h>
26 : #include <nntrainer_error.h>
27 : #include <nntrainer_log.h>
28 : #include <node_exporter.h>
29 : #include <util_func.h>
30 :
31 : namespace nntrainer {
32 :
33 : static constexpr size_t SINGLE_INOUT_IDX = 0;
34 :
35 1432 : InputLayer::InputLayer() :
36 1432 : Layer(), input_props(props::Normalization(), props::Standardization()) {}
37 :
38 6240 : void InputLayer::setProperty(const std::vector<std::string> &values) {
39 6240 : auto remain_props = loadProperties(values, input_props);
40 6239 : NNTR_THROW_IF(!remain_props.empty(), std::invalid_argument)
41 5 : << "[InputLayer] Unknown Layer Properties count " +
42 10 : std::to_string(values.size());
43 6239 : }
44 :
45 7113 : void InputLayer::forwarding(RunLayerContext &context, bool training) {
46 :
47 7113 : Tensor &hidden_ = context.getOutput(SINGLE_INOUT_IDX);
48 : std::unique_ptr<nntrainer::Quantizer> quantizer;
49 7113 : if (!context.getInPlace()) {
50 1609 : Tensor &input_ = context.getInput(SINGLE_INOUT_IDX);
51 : ///@note: The following code simply copies incoming values (fp32) to the
52 : // input tensor. Supported types include QINT4, QINT8, UINT8, UINT16,
53 : // UINT32, FP16, and FP32.
54 1609 : hidden_.copyData(input_);
55 : }
56 :
57 7113 : if (std::get<props::Normalization>(input_props))
58 167 : hidden_.normalization_i();
59 7113 : if (std::get<props::Standardization>(input_props))
60 0 : hidden_.standardization_i();
61 7113 : }
62 :
63 0 : void InputLayer::calcDerivative(RunLayerContext &context) {
64 : throw exception::not_supported(
65 0 : "calcDerivative for input layer is not supported");
66 : }
67 :
68 590 : void InputLayer::exportTo(Exporter &exporter,
69 : const ml::train::ExportMethods &method) const {
70 590 : exporter.saveResult(input_props, method, this);
71 590 : }
72 :
73 934 : void InputLayer::finalize(InitLayerContext &context) {
74 :
75 934 : std::vector<TensorDim> output_dims = context.getInputDimensions();
76 1868 : for (auto &d : output_dims) {
77 : d.setDataType(context.getActivationDataType());
78 : }
79 :
80 934 : context.setOutputDimensions(output_dims);
81 934 : is_inplace = true;
82 934 : if (context.getActivationDataType() != ml::train::TensorDim::DataType::FP32)
83 0 : is_inplace = false;
84 934 : }
85 :
86 0 : void InputLayer::updateTensorsByInputDimensions(
87 : nntrainer::RunLayerContext &context,
88 : std::vector<nntrainer::TensorDim> input_dimensions) {
89 0 : context.updateInput(SINGLE_INOUT_IDX, input_dimensions[0]);
90 0 : context.updateOutput(SINGLE_INOUT_IDX, input_dimensions[0]);
91 0 : }
92 :
93 : } /* namespace nntrainer */
|