ONE - On-device Neural Engine
Loading...
Searching...
No Matches
luci_interpreter::kernels::Pad Class Reference

#include <Pad.h>

Collaboration diagram for luci_interpreter::kernels::Pad:

Public Member Functions

 Pad (const Tensor *input, const Tensor *paddings, Tensor *output)
 
const Tensorinput () const
 
const Tensorpaddings () const
 
Tensoroutput () const
 
void configure () override
 
void execute () const override
 
- Public Member Functions inherited from luci_interpreter::Kernel
virtual ~Kernel ()=default
 
const std::vector< const Tensor * > & getInputTensors () const
 
const std::vector< Tensor * > & getOutputTensors () const
 

Additional Inherited Members

- Protected Member Functions inherited from luci_interpreter::Kernel
 Kernel (std::vector< const Tensor * > inputs, std::vector< Tensor * > outputs)
 
- Protected Attributes inherited from luci_interpreter::Kernel
const std::vector< const Tensor * > _inputs
 
const std::vector< Tensor * > _outputs
 

Detailed Description

Definition at line 27 of file Pad.h.

Constructor & Destructor Documentation

◆ Pad()

luci_interpreter::kernels::Pad::Pad ( const Tensor input,
const Tensor paddings,
Tensor output 
)

Definition at line 30 of file Pad.cpp.

32{
33}
Kernel(std::vector< const Tensor * > inputs, std::vector< Tensor * > outputs)
Definition Kernel.h:31
const Tensor * paddings() const
Definition Pad.h:33
const Tensor * input() const
Definition Pad.h:32
Tensor * output() const
Definition Pad.h:34

References input(), and paddings().

Member Function Documentation

◆ configure()

void luci_interpreter::kernels::Pad::configure ( )
overridevirtual

Implements luci_interpreter::Kernel.

Definition at line 35 of file Pad.cpp.

36{
37 const Shape &input_shape = input()->shape();
38 const int num_dims = input_shape.num_dims();
39
40 if (num_dims > 4)
41 throw std::runtime_error("Unsupported number of dimensions.");
42
43 assert(output()->element_type() == input()->element_type());
44 assert(paddings()->element_type() == DataType::S32);
45 // Paddings shape should be [N, 2].
46 assert(paddings()->shape().num_dims() == 2);
47 assert(paddings()->shape().dim(0) == num_dims);
48 assert(paddings()->shape().dim(1) == 2);
49
50 Shape output_shape(num_dims);
51 const auto *paddings_data = getTensorData<int32_t>(paddings());
52 for (int i = 0; i < num_dims; ++i)
53 {
54 const int32_t padding_before = paddings_data[i * 2];
55 const int32_t padding_after = paddings_data[i * 2 + 1];
56 assert(padding_before >= 0 && padding_after >= 0);
57 output_shape.dim(i) = input_shape.dim(i) + padding_before + padding_after;
58 }
59
61}
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
const Shape & shape() const
Definition Tensor.h:107
const luci_interpreter::RuntimeShape output_shape
Definition Shape.h:28

References luci_interpreter::Shape::dim(), input(), luci_interpreter::Shape::num_dims(), output(), output_shape, paddings(), luci_interpreter::Tensor::resize(), and luci_interpreter::Tensor::shape().

◆ execute()

void luci_interpreter::kernels::Pad::execute ( ) const
overridevirtual

Implements luci_interpreter::Kernel.

Definition at line 63 of file Pad.cpp.

64{
65 const int num_dims = input()->shape().num_dims();
66
67 tflite::PadParams params{};
68 params.left_padding_count = num_dims;
69 params.right_padding_count = num_dims;
70
71 const auto *paddings_data = getTensorData<int32_t>(paddings());
72 for (int i = num_dims - 1; i >= 0; --i)
73 {
74 params.left_padding[i] = paddings_data[i * 2];
75 params.right_padding[i] = paddings_data[i * 2 + 1];
76 }
77
78 switch (input()->element_type())
79 {
80 case DataType::FLOAT32:
81 {
82 const float pad_value = 0.0f;
83 tflite::reference_ops::Pad(params, getTensorShape(input()), getTensorData<float>(input()),
84 &pad_value, getTensorShape(output()),
85 getTensorData<float>(output()));
86 break;
87 }
88 case DataType::U8:
89 {
90 assert(output()->zero_point() >= std::numeric_limits<uint8_t>::min());
91 assert(output()->zero_point() <= std::numeric_limits<uint8_t>::max());
92 const auto pad_value = static_cast<uint8_t>(output()->zero_point());
93 tflite::reference_ops::Pad(params, getTensorShape(input()), getTensorData<uint8_t>(input()),
94 &pad_value, getTensorShape(output()),
95 getTensorData<uint8_t>(output()));
96 break;
97 }
98 case DataType::S8:
99 {
100 assert(output()->zero_point() >= std::numeric_limits<int8_t>::min());
101 assert(output()->zero_point() <= std::numeric_limits<int8_t>::max());
102 const auto pad_value = static_cast<int8_t>(output()->zero_point());
103 tflite::reference_ops::Pad(params, getTensorShape(input()), getTensorData<int8_t>(input()),
104 &pad_value, getTensorShape(output()),
105 getTensorData<int8_t>(output()));
106 break;
107 }
108 default:
109 throw std::runtime_error("luci-intp Pad Unsupported type.");
110 }
111}
int num_dims() const
Definition Tensor.h:39
int32_t zero_point() const
Definition Tensor.h:115
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194

References luci_interpreter::kernels::getTensorShape(), input(), luci_interpreter::Shape::num_dims(), output(), paddings(), luci_interpreter::Tensor::shape(), and luci_interpreter::Tensor::zero_point().

◆ input()

const Tensor * luci_interpreter::kernels::Pad::input ( ) const
inline

Definition at line 32 of file Pad.h.

32{ return _inputs[0]; }
const std::vector< const Tensor * > _inputs
Definition Kernel.h:52

References luci_interpreter::Kernel::_inputs.

Referenced by configure(), execute(), and Pad().

◆ output()

Tensor * luci_interpreter::kernels::Pad::output ( ) const
inline

Definition at line 34 of file Pad.h.

34{ return _outputs[0]; }
const std::vector< Tensor * > _outputs
Definition Kernel.h:53

References luci_interpreter::Kernel::_outputs.

Referenced by configure(), and execute().

◆ paddings()

const Tensor * luci_interpreter::kernels::Pad::paddings ( ) const
inline

Definition at line 33 of file Pad.h.

33{ return _inputs[1]; }

References luci_interpreter::Kernel::_inputs.

Referenced by configure(), execute(), and Pad().


The documentation for this class was generated from the following files: