ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnc::Serializer Class Reference

Serializer of network parameters for soft backend. More...

#include <SBSerializer.h>

Collaboration diagram for nnc::Serializer:

Public Member Functions

void visit (mir::ops::AbsOp &op) override
 
void visit (mir::ops::AddOp &op) override
 
void visit (mir::ops::AvgPool2DOp &op) override
 
void visit (mir::ops::BroadcastOp &op) override
 
void visit (mir::ops::CappedReluOp &op) override
 
void visit (mir::ops::ConcatOp &op) override
 
void visit (mir::ops::ConstantOp &op) override
 
void visit (mir::ops::Conv2DOp &op) override
 
void visit (mir::ops::DeConv2DOp &op) override
 
void visit (mir::ops::DepthwiseConv2DOp &op) override
 
void visit (mir::ops::DivOp &op) override
 
void visit (mir::ops::EluOp &op) override
 
void visit (mir::ops::FullyConnectedOp &op) override
 
void visit (mir::ops::GatherOp &op) override
 
void visit (mir::ops::InputOp &op) override
 
void visit (mir::ops::LeakyReluOp &op) override
 
void visit (mir::ops::MaxOp &op) override
 
void visit (mir::ops::MaxPool2DOp &op) override
 
void visit (mir::ops::MulOp &op) override
 
void visit (mir::ops::OutputOp &op) override
 
void visit (mir::ops::PadOp &op) override
 
void visit (mir::ops::ReduceMeanOp &op) override
 
void visit (mir::ops::ReluOp &op) override
 
void visit (mir::ops::ReshapeOp &op) override
 
void visit (mir::ops::ResizeOp &op) override
 
void visit (mir::ops::SigmoidOp &op) override
 
void visit (mir::ops::SliceOp &op) override
 
void visit (mir::ops::SoftmaxOp &op) override
 
void visit (mir::ops::SqrtOp &op) override
 
void visit (mir::ops::SqueezeOp &op) override
 
void visit (mir::ops::SubOp &op) override
 
void visit (mir::ops::TanhOp &op) override
 
void visit (mir::ops::TransposeOp &op) override
 
void serialize (std::vector< std::unique_ptr< sir::Action > > &inference_sequence)
 
const std::vector< char > & getBuffer () const
 
uint32_t getFormatVersion () const
 
uint32_t getModelHash () const
 
- Public Member Functions inherited from mir::IVisitor
virtual ~IVisitor ()=default
 

Protected Member Functions

void visit_fallback (mir::Operation &op) override
 

Detailed Description

Serializer of network parameters for soft backend.

Serializer class responsible for serialization of given computational graph parameters and binding of inference operations to this data. It owns buffer that contains serialized data. To serialize data serialize method should be called with sequence from ModelAnalyzer object To gather this vector use getBuffer method. Objects of this class are one-off and not designed to serialize more than one IR

Definition at line 41 of file SBSerializer.h.

Member Function Documentation

◆ getBuffer()

const std::vector< char > & nnc::Serializer::getBuffer ( ) const
inline

Definition at line 80 of file SBSerializer.h.

80{ return _buffer; }

◆ getFormatVersion()

uint32_t nnc::Serializer::getFormatVersion ( ) const
inline

Definition at line 82 of file SBSerializer.h.

82{ return _formatVersion; }

◆ getModelHash()

uint32_t nnc::Serializer::getModelHash ( ) const
inline

Definition at line 84 of file SBSerializer.h.

84{ return _modelHash; }

◆ serialize()

void nnc::Serializer::serialize ( std::vector< std::unique_ptr< sir::Action > > &  inference_sequence)

Definition at line 355 of file SBSerializer.cpp.

356{
357 for (unique_ptr<sir::Action> &action : inference_sequence)
358 {
360 continue;
361 _curOp = dynamic_cast<sir::CallFunction *>(action.get());
362 _curOp->mirOp->accept(this);
363 }
364}
void accept(IVisitor *v)
Definition Operation.cpp:56
mir::Operation * mirOp

References mir::Operation::accept(), nnc::sir::Action::callFunction, and nnc::sir::CallFunction::mirOp.

Referenced by nnc::CPPCodeGenerator::run().

◆ visit() [1/33]

void nnc::Serializer::visit ( mir::ops::AbsOp op)
override

Definition at line 371 of file SBSerializer.cpp.

372{
373 _curOp->paramStartOffset = _buffer.size();
374 // no parameters to dump
375}

References nnc::sir::CallFunction::paramStartOffset.

◆ visit() [2/33]

void nnc::Serializer::visit ( mir::ops::AddOp op)
override

Definition at line 377 of file SBSerializer.cpp.

378{
379 _curOp->paramStartOffset = _buffer.size();
380 // Op type is known at codegen Time
381 serializeShape(op.getOutputShape(0));
382}
const Shape & getOutputShape(std::size_t index) const
Definition Operation.h:163

References mir::Operation::getOutputShape(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [3/33]

void nnc::Serializer::visit ( mir::ops::AvgPool2DOp op)
override

Definition at line 170 of file SBSerializer.cpp.

171{
172 _curOp->paramStartOffset = _buffer.size();
173 // serialize window shape
174 serializeShape(Shape(op.getWindowSize()));
175 // serialize strindes
176 serializeStrides(op.getStrides());
177 // serialize pads
178 int32_t number_of_pads = 2; // windowShape.rank();
179 serializePads(op, number_of_pads);
180 serializeT<int32_t>(op.getIncludePad());
181 // serialize output shape
182 serializeShape(op.getOutputShape(0));
183}
const std::vector< std::int32_t > & getWindowSize() const
Definition AvgPool2DOp.h:45
bool getIncludePad() const
Definition AvgPool2DOp.h:53
const std::vector< std::int32_t > & getStrides() const
Definition AvgPool2DOp.h:47
Definition Shape.h:28

References mir::ops::AvgPool2DOp::getIncludePad(), mir::Operation::getOutputShape(), mir::ops::AvgPool2DOp::getStrides(), mir::ops::AvgPool2DOp::getWindowSize(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [4/33]

void nnc::Serializer::visit ( mir::ops::BroadcastOp op)
override

Definition at line 205 of file SBSerializer.cpp.

206{
207 _curOp->paramStartOffset = _buffer.size();
208 serializeShape(op.getOutputShape(0));
209}

References mir::Operation::getOutputShape(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [5/33]

void nnc::Serializer::visit ( mir::ops::CappedReluOp op)
override

Definition at line 211 of file SBSerializer.cpp.

212{
213 _curOp->paramStartOffset = _buffer.size();
214 serializeT<float>(op.getCap());
215}

References mir::ops::CappedReluOp::getCap(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [6/33]

void nnc::Serializer::visit ( mir::ops::ConcatOp op)
override

Definition at line 129 of file SBSerializer.cpp.

130{
131 _curOp->paramStartOffset = _buffer.size();
132 // axis number should fit into one byte
133 assert(op.getAxis() <= MAX_DIMS);
134 serializeT<int32_t>(op.getAxis());
135 serializeShape(op.getOutputShape(0));
136}
int32_t getAxis() const
Definition ConcatOp.h:44

References mir::ops::ConcatOp::getAxis(), mir::Operation::getOutputShape(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [7/33]

void nnc::Serializer::visit ( mir::ops::ConstantOp op)
override

Definition at line 222 of file SBSerializer.cpp.

223{
224 _curOp->paramStartOffset = _buffer.size();
225 serializeTensor(op.getValue());
226}
const TensorVariant & getValue() const
Definition ConstantOp.h:36

References mir::ops::ConstantOp::getValue(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [8/33]

void nnc::Serializer::visit ( mir::ops::Conv2DOp op)
override

Definition at line 138 of file SBSerializer.cpp.

139{
140 _curOp->paramStartOffset = _buffer.size();
141 // serialize strides
142 serializeStrides(op.getStrides());
143 // serialize pads
144 int32_t padsRank = 2; // op.getInputShape(0).rank();
145 serializePads(op, padsRank);
146 // serialize output shape
147 serializeShape(op.getOutputShape(0));
148}
const std::vector< std::int32_t > & getStrides() const
Definition Conv2DOp.h:52

References mir::Operation::getOutputShape(), mir::ops::Conv2DOp::getStrides(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [9/33]

void nnc::Serializer::visit ( mir::ops::DeConv2DOp op)
override

Definition at line 260 of file SBSerializer.cpp.

261{
262 _curOp->paramStartOffset = _buffer.size();
263 // serialize strides
264 serializeStrides(op.getStrides());
265 // serialize pads
266 int32_t number_of_pads = 2; // op.getInputShape(0).rank();
267 serializePads(op, number_of_pads);
268 // serialize output shape
269 serializeShape(op.getOutputShape(0));
270}
const std::vector< std::int32_t > & getStrides() const
Definition Deconv2DOp.h:58

References mir::Operation::getOutputShape(), mir::ops::DeConv2DOp::getStrides(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [10/33]

void nnc::Serializer::visit ( mir::ops::DepthwiseConv2DOp op)
override

Definition at line 150 of file SBSerializer.cpp.

151{
152 _curOp->paramStartOffset = _buffer.size();
153 // serialize strides
154 serializeStrides(op.getStrides());
155 // serialize pads
156 int32_t padsRank = 2; // kernel.getShape().rank();
157 serializePads(op, padsRank);
158 // serialize output shape
159 serializeShape(op.getOutputShape(0));
160}
const std::vector< std::int32_t > & getStrides() const

References mir::Operation::getOutputShape(), mir::ops::DepthwiseConv2DOp::getStrides(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [11/33]

void nnc::Serializer::visit ( mir::ops::DivOp op)
override

Definition at line 384 of file SBSerializer.cpp.

385{
386 _curOp->paramStartOffset = _buffer.size();
387 // Op type is known at codegen Time
388 serializeShape(op.getOutputShape(0));
389}

References mir::Operation::getOutputShape(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [12/33]

void nnc::Serializer::visit ( mir::ops::EluOp op)
override

Definition at line 254 of file SBSerializer.cpp.

255{
256 _curOp->paramStartOffset = _buffer.size();
257 serializeT<float>(op.getAlpha());
258}
float getAlpha() const
Definition EluOp.h:40

References mir::ops::EluOp::getAlpha(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [13/33]

void nnc::Serializer::visit ( mir::ops::FullyConnectedOp op)
override

Definition at line 199 of file SBSerializer.cpp.

200{
201 _curOp->paramStartOffset = _buffer.size();
202 serializeShape(op.getOutputShape(0));
203}

References mir::Operation::getOutputShape(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [14/33]

void nnc::Serializer::visit ( mir::ops::GatherOp op)
override

Definition at line 337 of file SBSerializer.cpp.

338{
339 _curOp->paramStartOffset = _buffer.size();
340 // serialize parameters
341 serializeT<int32_t>(op.getAxis());
342 // serialize output shape
343 serializeShape(op.getOutputShape(0));
344}
int32_t getAxis() const
Definition GatherOp.h:46

References mir::ops::GatherOp::getAxis(), mir::Operation::getOutputShape(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [15/33]

void nnc::Serializer::visit ( mir::ops::InputOp op)
override

Definition at line 217 of file SBSerializer.cpp.

218{
219 // no parameters to dump
220}

◆ visit() [16/33]

void nnc::Serializer::visit ( mir::ops::LeakyReluOp op)
override

Definition at line 348 of file SBSerializer.cpp.

349{
350 _curOp->paramStartOffset = _buffer.size();
351 serializeT<float>(op.getAlpha());
352 serializeShape(op.getOutputShape(0));
353}
float getAlpha() const
Definition LeakyReluOp.h:41

References mir::ops::LeakyReluOp::getAlpha(), mir::Operation::getOutputShape(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [17/33]

void nnc::Serializer::visit ( mir::ops::MaxOp op)
override

Definition at line 391 of file SBSerializer.cpp.

392{
393 _curOp->paramStartOffset = _buffer.size();
394 // Op type is known at codegen Time
395 serializeShape(op.getOutputShape(0));
396}

References mir::Operation::getOutputShape(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [18/33]

void nnc::Serializer::visit ( mir::ops::MaxPool2DOp op)
override

Definition at line 185 of file SBSerializer.cpp.

186{
187 _curOp->paramStartOffset = _buffer.size();
188 // serialize window shape
189 serializeShape(Shape(op.getWindowSize()));
190 // serialize strindes
191 serializeStrides(op.getStrides());
192 // serialize pads
193 int32_t number_of_pads = 2; // windowShape.rank();
194 serializePads(op, number_of_pads);
195 // serialize output shape
196 serializeShape(op.getOutputShape(0));
197}
const std::vector< std::int32_t > & getWindowSize() const
Definition MaxPool2DOp.h:45
const std::vector< std::int32_t > & getStrides() const
Definition MaxPool2DOp.h:47

References mir::Operation::getOutputShape(), mir::ops::MaxPool2DOp::getStrides(), mir::ops::MaxPool2DOp::getWindowSize(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [19/33]

void nnc::Serializer::visit ( mir::ops::MulOp op)
override

Definition at line 398 of file SBSerializer.cpp.

399{
400 _curOp->paramStartOffset = _buffer.size();
401 // Op type is known at codegen Time
402 serializeShape(op.getOutputShape(0));
403}

References mir::Operation::getOutputShape(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [20/33]

void nnc::Serializer::visit ( mir::ops::OutputOp op)
override

Definition at line 366 of file SBSerializer.cpp.

367{
368 // no parameters to dump
369}

◆ visit() [21/33]

void nnc::Serializer::visit ( mir::ops::PadOp op)
override

Definition at line 278 of file SBSerializer.cpp.

279{
280 _curOp->paramStartOffset = _buffer.size();
281
282 // serialize paddings
283 const int num_dims = op.getInputShape(0).rank();
284
285 // serialize output shape
286 serializeShape(op.getOutputShape(0));
287
288 // serialize num dimensions
289 serializeT<int32_t>(num_dims);
290
291 const auto &padding_before = op.getPaddingBefore();
292 const auto &padding_after = op.getPaddingAfter();
293 for (int i = 0; i < num_dims; i++)
294 {
295 serializeT<int32_t>(padding_before[num_dims - 1 - i]);
296 serializeT<int32_t>(padding_after[num_dims - 1 - i]);
297 }
298
299 // FIXME Make use of padding value.
300 assert(op.getPaddingValue() == 0.0f);
301}
const Shape & getInputShape(std::size_t index) const
Definition Operation.h:161
int32_t rank() const
Definition Shape.h:43
float getPaddingValue() const
Definition PadOp.h:47
const std::vector< std::int32_t > & getPaddingBefore() const
Definition PadOp.h:43
const std::vector< std::int32_t > & getPaddingAfter() const
Definition PadOp.h:45

References mir::Operation::getInputShape(), mir::Operation::getOutputShape(), mir::ops::PadOp::getPaddingAfter(), mir::ops::PadOp::getPaddingBefore(), mir::ops::PadOp::getPaddingValue(), nnc::sir::CallFunction::paramStartOffset, and mir::Shape::rank().

◆ visit() [22/33]

void nnc::Serializer::visit ( mir::ops::ReduceMeanOp op)
override

Definition at line 316 of file SBSerializer.cpp.

317{
318 _curOp->paramStartOffset = _buffer.size();
319 serializeShape(Shape(op.getReductionDims())); // reuse shape serialization
320 serializeT<int32_t>(op.getKeepDims());
321 serializeShape(op.getOutputShape(0));
322}
const std::vector< int > & getReductionDims() const
Definition ReduceOp.h:38
bool getKeepDims() const
Definition ReduceOp.h:40

References mir::ops::ReduceOp::getKeepDims(), mir::Operation::getOutputShape(), mir::ops::ReduceOp::getReductionDims(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [23/33]

void nnc::Serializer::visit ( mir::ops::ReluOp op)
override

Definition at line 228 of file SBSerializer.cpp.

229{
230 _curOp->paramStartOffset = _buffer.size();
231 // no parameters to dump
232}

References nnc::sir::CallFunction::paramStartOffset.

◆ visit() [24/33]

void nnc::Serializer::visit ( mir::ops::ReshapeOp op)
override

Definition at line 234 of file SBSerializer.cpp.

235{
236 _curOp->paramStartOffset = _buffer.size();
237 serializeShape(op.getOutputShape(0));
238}

References mir::Operation::getOutputShape(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [25/33]

void nnc::Serializer::visit ( mir::ops::ResizeOp op)
override

Definition at line 309 of file SBSerializer.cpp.

310{
311 _curOp->paramStartOffset = _buffer.size();
312 // Result shape is the same as Output shape
313 serializeShape(op.getOutputShape(0));
314}

References mir::Operation::getOutputShape(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [26/33]

void nnc::Serializer::visit ( mir::ops::SigmoidOp op)
override

Definition at line 346 of file SBSerializer.cpp.

346{ _curOp->paramStartOffset = _buffer.size(); }

References nnc::sir::CallFunction::paramStartOffset.

◆ visit() [27/33]

void nnc::Serializer::visit ( mir::ops::SliceOp op)
override

Definition at line 240 of file SBSerializer.cpp.

241{
242 _curOp->paramStartOffset = _buffer.size();
243 serializeShape(op.getStarts());
244 serializeShape(op.getSizes());
245 serializeShape(op.getOutputShape(0));
246}
const Shape & getStarts()
Definition SliceOp.h:41
const Shape & getSizes()
Definition SliceOp.h:43

References mir::Operation::getOutputShape(), mir::ops::SliceOp::getSizes(), mir::ops::SliceOp::getStarts(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [28/33]

void nnc::Serializer::visit ( mir::ops::SoftmaxOp op)
override

Definition at line 162 of file SBSerializer.cpp.

163{
164 _curOp->paramStartOffset = _buffer.size();
165 // axis number should fit into one byte
166 assert(op.getAxis() <= MAX_DIMS);
167 serializeT<int32_t>(op.getAxis());
168}
int32_t getAxis() const
Definition SoftmaxOp.h:43

References mir::ops::SoftmaxOp::getAxis(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [29/33]

void nnc::Serializer::visit ( mir::ops::SqrtOp op)
override

Definition at line 303 of file SBSerializer.cpp.

304{
305 _curOp->paramStartOffset = _buffer.size();
306 // no parameters to dump
307}

References nnc::sir::CallFunction::paramStartOffset.

◆ visit() [30/33]

void nnc::Serializer::visit ( mir::ops::SqueezeOp op)
override

Definition at line 272 of file SBSerializer.cpp.

273{
274 _curOp->paramStartOffset = _buffer.size();
275 serializeShape(op.getOutputShape(0));
276}

References mir::Operation::getOutputShape(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [31/33]

void nnc::Serializer::visit ( mir::ops::SubOp op)
override

Definition at line 405 of file SBSerializer.cpp.

406{
407 _curOp->paramStartOffset = _buffer.size();
408 // Op type is known at codegen Time
409 serializeShape(op.getOutputShape(0));
410}

References mir::Operation::getOutputShape(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit() [32/33]

void nnc::Serializer::visit ( mir::ops::TanhOp op)
override

Definition at line 248 of file SBSerializer.cpp.

249{
250 _curOp->paramStartOffset = _buffer.size();
251 // no parameters to dump
252}

References nnc::sir::CallFunction::paramStartOffset.

◆ visit() [33/33]

void nnc::Serializer::visit ( mir::ops::TransposeOp op)
override

Definition at line 324 of file SBSerializer.cpp.

325{
326 _curOp->paramStartOffset = _buffer.size();
327 // serializer parameters
328 auto &axis_order = op.getAxisOrder();
329 serializeT(static_cast<int32_t>(axis_order.size()));
330 for (auto &axis : axis_order)
331 serializeT(static_cast<int32_t>(axis));
332
333 // serialize output shape
334 serializeShape(op.getOutputShape(0));
335}
const std::vector< std::size_t > & getAxisOrder() const
Definition TransposeOp.h:38

References mir::ops::TransposeOp::getAxisOrder(), mir::Operation::getOutputShape(), and nnc::sir::CallFunction::paramStartOffset.

◆ visit_fallback()

void nnc::Serializer::visit_fallback ( mir::Operation op)
overrideprotectedvirtual

Reimplemented from mir::Visitor.

Definition at line 412 of file SBSerializer.cpp.

412{ throw std::runtime_error("NYI operation"); }

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