ONE - On-device Neural Engine
Loading...
Searching...
No Matches
ConvBackend Class Reference

#include <ConvBackend.h>

Collaboration diagram for ConvBackend:

Public Member Functions

 ConvBackend (const nnsuite::conv::Model &model)
 
::tflite::Interpreter & interpreter (void) override
 
- Public Member Functions inherited from nnkit::support::tflite::AbstractBackend
virtual ~AbstractBackend ()=default
 
void prepare (const std::function< void(TensorContext &)> &f) override
 
void run (void) override
 
void teardown (const std::function< void(TensorContext &)> &f) override
 
- Public Member Functions inherited from nnkit::Backend
virtual ~Backend ()=default
 
virtual void prepare (const std::function< void(TensorContext &)> &f)=0
 
virtual void teardown (const std::function< void(TensorContext &)> &f)=0
 

Static Public Member Functions

static std::unique_ptr< nnkit::Backendcreate (const nnsuite::conv::Model &)
 

Detailed Description

Definition at line 25 of file ConvBackend.h.

Constructor & Destructor Documentation

◆ ConvBackend()

ConvBackend::ConvBackend ( const nnsuite::conv::Model model)
explicit

Definition at line 76 of file ConvBackend.cpp.

77 : _ifm_name{model.ifm_name()}, _ofm_name{model.ofm_name()}
78{
81
84
85 // Set kernel data
86 const auto &ker_shape = model.ker_shape();
87
88 _kernel.resize(num_elements(ker_shape));
89
90 auto kernel_overlay = make_overlay<float, NHWCLayout>(ker_shape, _kernel.data());
91
92 for (uint32_t n = 0; n < ker_shape.count(); ++n)
93 {
94 for (uint32_t ch = 0; ch < ker_shape.depth(); ++ch)
95 {
96 for (uint32_t row = 0; row < ker_shape.height(); ++row)
97 {
98 for (uint32_t col = 0; col < ker_shape.width(); ++col)
99 {
100 kernel_overlay.at(n, ch, row, col) = model.ker_data().at(n, ch, row, col);
101 }
102 }
103 }
104 }
105
106 // Set bias data
107 _bias.resize(ker_shape.count(), 0.0f);
108
109 // Initialize interpreter
110 auto quantization = make_default_quantization();
111
112 // Create Tensors
113 // 0 -> OFM
114 // 1 -> IFM
115 // 2 -> Kernel
116 // 3 -> Bias
117 _interp.AddTensors(4);
118
119 _interp.SetTensorParametersReadWrite(0, kTfLiteFloat32 /* type */, _ofm_name.c_str(),
120 as_dims(model.ofm_shape()), quantization);
121
122 _interp.SetTensorParametersReadWrite(1, kTfLiteFloat32 /* type */, _ifm_name.c_str(),
123 as_dims(model.ifm_shape()), quantization);
124
125 _interp.SetTensorParametersReadOnly(
126 2, kTfLiteFloat32 /* type */, "kernel" /* name */, as_dims(model.ker_shape()), quantization,
127 reinterpret_cast<const char *>(_kernel.data()), _kernel.size() * sizeof(float));
128
129 _interp.SetTensorParametersReadOnly(
130 3, kTfLiteFloat32 /* type */, "bias" /* name */, {static_cast<int>(_bias.size())}, quantization,
131 reinterpret_cast<const char *>(_bias.data()), _bias.size() * sizeof(float));
132
133 auto param = typed_malloc<TfLiteConvParams>();
134
135 param->padding = kTfLitePaddingValid;
136 param->stride_width = 1;
137 param->stride_height = 1;
138 param->activation = kTfLiteActNone;
139
140 _interp.AddNodeWithParameters({1, 2, 3}, {0}, nullptr, 0, reinterpret_cast<void *>(param),
141 BuiltinOpResolver().FindOp(BuiltinOperator_CONV_2D));
142
143 _interp.SetInputs({1});
144 _interp.SetOutputs({0});
145}
Dims< uint32_t > as_dims(const nncc::core::ADT::tensor::Shape &)
Definition dims.cpp:24
uint32_t num_elements(const Shape &shape)
The number of elements of a feature map of a given shape.
Definition Shape.h:59
uint32_t num_elements(const Shape &shape)
Return the number of elements in a kernel of a given shape.
Definition Shape.h:61
Overlay< T, T * > make_overlay(const Shape &shape, T *base)
Definition Overlay.h:71

References nncc::core::ADT::kernel::make_overlay(), and nncc::core::ADT::kernel::num_elements().

Member Function Documentation

◆ create()

std::unique_ptr< nnkit::Backend > ConvBackend::create ( const nnsuite::conv::Model model)
static

Definition at line 30 of file ConvBackend.cpp.

31{
32 ::caffe::NetParameter param;
33
34 param.set_name("conv");
35
36 // Create 'Input' layer
37 {
38 auto input = param.add_layer();
39 input->set_name("input");
40 input->set_type("Input");
41 input->add_top(model.ifm_name());
42
43 auto input_param = new ::caffe::InputParameter{};
44 auto input_shape = input_param->add_shape();
45 input_shape->add_dim(1);
46 input_shape->add_dim(model.ifm_shape().depth());
47 input_shape->add_dim(model.ifm_shape().height());
48 input_shape->add_dim(model.ifm_shape().width());
49 input->set_allocated_input_param(input_param);
50 }
51
52 // Create 'Convolution' layer
53 {
54 auto conv = param.add_layer();
55 conv->set_name("conv");
56 conv->set_type("Convolution");
57 conv->add_bottom(model.ifm_name());
58 conv->add_top(model.ofm_name());
59
60 const auto &ker_shape = model.ker_shape();
61
62 auto ker_blob_shape = new ::caffe::BlobShape{};
63
64 ker_blob_shape->add_dim(ker_shape.count());
65 ker_blob_shape->add_dim(ker_shape.depth());
66 ker_blob_shape->add_dim(ker_shape.height());
67 ker_blob_shape->add_dim(ker_shape.width());
68
69 auto ker_blob = conv->add_blobs();
70
71 for (uint32_t n = 0; n < ker_shape.count(); ++n)
72 {
73 for (uint32_t ch = 0; ch < ker_shape.depth(); ++ch)
74 {
75 for (uint32_t row = 0; row < ker_shape.height(); ++row)
76 {
77 for (uint32_t col = 0; col < ker_shape.width(); ++col)
78 {
79 ker_blob->add_data(model.ker_data().at(n, ch, row, col));
80 }
81 }
82 }
83 }
84
85 ker_blob->set_allocated_shape(ker_blob_shape);
86
87 auto conv_param = new ::caffe::ConvolutionParameter{};
88 conv_param->set_num_output(model.ker_shape().count());
89 conv_param->set_bias_term(false);
90 conv_param->add_kernel_size(model.ker_shape().height());
91 conv_param->add_kernel_size(model.ker_shape().width());
92 conv->set_allocated_convolution_param(conv_param);
93 }
94
95 auto net = make_unique<::caffe::Net<float>>(param);
96 return make_unique<nnkit::support::caffe::Backend<float>>(std::move(net));
97}
void conv(const nncc::core::ADT::feature::Shape &out_shape, nncc::core::ADT::feature::Accessor< OutputDType > &out_data, const nncc::core::ADT::feature::Shape &in_shape, const nncc::core::ADT::feature::Reader< InputDType > &in_data, const nncc::core::ADT::kernel::Shape &ker_shape, const nncc::core::ADT::kernel::Reader< KernelDType > &ker_data, const PadInfo &pad_info, const StrideInfo &stride_info)
Definition Conv2D.h:34

Referenced by make_backend().

◆ interpreter()

::tflite::Interpreter & ConvBackend::interpreter ( void  )
inlineoverridevirtual

Implements nnkit::support::tflite::AbstractBackend.

Definition at line 31 of file ConvBackend.h.

31{ return _interp; }

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