ONE - On-device Neural Engine
Loading...
Searching...
No Matches
PadLayer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 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#include "PadLayer.h"
18
19#include "../KernelGenerator.h"
20#include "../Validator.h"
21
22#include <cker/operation/Pad.h>
23
24namespace onert::backend::cpu
25{
26
27void Validator::visit(const ir::operation::Pad &) { _supported = true; }
28
29void KernelGenerator::visit(const ir::operation::Pad &node)
30{
31 const auto input_index{node.getInputs().at(ir::operation::Pad::Input::INPUT)};
32 const auto pad_index{node.getInputs().at(ir::operation::Pad::Input::PAD)};
33 const auto output_index{node.getOutputs().at(0)};
34
35 auto input = _tensor_reg->getPortableTensor(input_index);
36 auto pad = _tensor_reg->getPortableTensor(pad_index);
37 auto output = _tensor_reg->getPortableTensor(output_index);
38
39 auto fn = std::make_unique<ops::PadLayer>();
40
41 IPortableTensor *value = nullptr;
42 if (node.getInputs().size() == 3) // isPadV2
43 {
44 const auto value_index{node.getInputs().at(ir::operation::Pad::Input::VALUE)};
45 value = _tensor_reg->getPortableTensor(value_index);
46 }
47
48 fn->configure(input, pad, value, output);
49 _return_fn = std::move(fn);
50}
51
52} // namespace onert::backend::cpu
53
55{
56
58 : _input(nullptr), _pad(nullptr), _value(nullptr), _output(nullptr), _constantValueData()
59{
60 // DO NOTHING
61}
62
63template <typename T> void PadLayer::padImpl(const T *constant_value_data)
64{
65 assert(_pad->data_type() == onert::ir::DataType::INT32);
66 assert(_pad->buffer());
67 const auto pad_data = reinterpret_cast<const int32_t *>(_pad->buffer());
68 auto pad_rank = _pad->getShape().dim(0);
69 nnfw::cker::Pad<T>(pad_data, pad_rank, getShape(_input), getBuffer<T>(_input), getShape(_output),
70 getBuffer<T>(_output), constant_value_data);
71}
72
74 const IPortableTensor *value, IPortableTensor *output)
75{
76 _input = input;
77 _pad = pad;
78 _value = value;
79 _output = output;
80}
81
83{
84 if (_value != nullptr) // isPadV2
85 {
86 assert(_value->buffer());
87 _constantValueData.v = reinterpret_cast<const void *>(_value->buffer());
88 }
89
90 switch (_input->data_type())
91 {
92 case OperandType::FLOAT32:
93 padImpl<float>(_constantValueData.f);
94 break;
95 case OperandType::QUANT_UINT8_ASYMM:
96 if (_constantValueData.u8 == nullptr)
97 {
98 uint8_t pad_value = static_cast<uint8_t>(_output->data_zero_point());
99 padImpl<uint8_t>(&pad_value);
100 }
101 else
102 {
103 padImpl<uint8_t>(_constantValueData.u8);
104 }
105 break;
106 case OperandType::QUANT_INT8_ASYMM:
107 if (_constantValueData.i8 == nullptr)
108 {
109 int8_t pad_value = static_cast<int8_t>(_output->data_zero_point());
110 padImpl<int8_t>(&pad_value);
111 }
112 else
113 {
114 padImpl<int8_t>(_constantValueData.i8);
115 }
116 break;
117 default:
118 throw std::runtime_error{"Pad: unsupported data type"};
119 }
120}
121
122} // namespace onert::backend::cpu::ops
A tensor class that is portable for other backends.
int32_t data_zero_point() const override final
ir::DataType data_type() const override final
ir::Shape getShape() const override final
Get ir::Shape of tensor.
virtual uint8_t * buffer() const =0
std::unique_ptr< exec::IFunction > _return_fn
const IPortableTensor * _input
Definition PadLayer.h:44
const IPortableTensor * _value
Definition PadLayer.h:46
void padImpl(const T *constant_value_data)
Definition PadLayer.cc:63
const IPortableTensor * _pad
Definition PadLayer.h:45
void configure(const IPortableTensor *input, const IPortableTensor *pad, const IPortableTensor *value, IPortableTensor *output)
Definition PadLayer.cc:73
nnfw::cker::Shape getShape(const IPortableTensor *tensor)