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 <cker/operation/Pad.h>
20
21namespace onert
22{
23namespace backend
24{
25namespace cpu
26{
27namespace ops
28{
29
31 : _input(nullptr), _pad(nullptr), _value(nullptr), _output(nullptr), _constantValueData()
32{
33 // DO NOTHING
34}
35
36template <typename T> void PadLayer::padImpl(const T *constant_value_data)
37{
38 assert(_pad->data_type() == onert::ir::DataType::INT32);
39 assert(_pad->buffer());
40 const auto pad_data = reinterpret_cast<const int32_t *>(_pad->buffer());
41 auto pad_rank = _pad->getShape().dim(0);
42 nnfw::cker::Pad<T>(pad_data, pad_rank, getShape(_input), getBuffer<T>(_input), getShape(_output),
43 getBuffer<T>(_output), constant_value_data);
44}
45
47 const IPortableTensor *value, IPortableTensor *output)
48{
49 _input = input;
50 _pad = pad;
51 _value = value;
52 _output = output;
53}
54
56{
57 if (_value != nullptr) // isPadV2
58 {
59 assert(_value->buffer());
60 _constantValueData.v = reinterpret_cast<const void *>(_value->buffer());
61 }
62
63 switch (_input->data_type())
64 {
65 case OperandType::FLOAT32:
66 padImpl<float>(_constantValueData.f);
67 break;
68 case OperandType::QUANT_UINT8_ASYMM:
69 if (_constantValueData.u8 == nullptr)
70 {
71 uint8_t pad_value = static_cast<uint8_t>(_output->data_zero_point());
72 padImpl<uint8_t>(&pad_value);
73 }
74 else
75 {
76 padImpl<uint8_t>(_constantValueData.u8);
77 }
78 break;
79 case OperandType::QUANT_INT8_ASYMM:
80 if (_constantValueData.i8 == nullptr)
81 {
82 int8_t pad_value = static_cast<int8_t>(_output->data_zero_point());
83 padImpl<int8_t>(&pad_value);
84 }
85 else
86 {
87 padImpl<int8_t>(_constantValueData.i8);
88 }
89 break;
90 default:
91 throw std::runtime_error{"Pad: unsupported data type"};
92 }
93}
94
95} // namespace ops
96} // namespace cpu
97} // namespace backend
98} // namespace onert
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
const IPortableTensor * _input
Definition PadLayer.h:50
const IPortableTensor * _value
Definition PadLayer.h:52
void padImpl(const T *constant_value_data)
Definition PadLayer.cc:36
const IPortableTensor * _pad
Definition PadLayer.h:51
void configure(const IPortableTensor *input, const IPortableTensor *pad, const IPortableTensor *value, IPortableTensor *output)
Definition PadLayer.cc:46
nnfw::cker::Shape getShape(const IPortableTensor *tensor)