ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
onert::loader::BaseLoader< LoaderDomain > Class Template Referenceabstract

#include <BaseLoader.h>

Collaboration diagram for onert::loader::BaseLoader< LoaderDomain >:

Public Member Functions

 BaseLoader (std::unique_ptr< ir::Model > &model)
 Construct a new Loader object.
 
void loadFromFile (const std::string &file_path)
 Load a model from file.
 
void loadFromBuffer (uint8_t *buffer, size_t size)
 Load a model from a buffer.
 

Protected Types

using Verifier = typename LoaderDomain::Verifier
 
using ActivationFunctionType = typename LoaderDomain::ActivationFunctionType
 
using Buffer = typename LoaderDomain::Buffer
 
using BuiltinOperator = typename LoaderDomain::BuiltinOperator
 
using CustomOptionsFormat = typename LoaderDomain::CustomOptionsFormat
 
using Metadata = typename LoaderDomain::Metadata
 
using Model = typename LoaderDomain::Model
 
using Operator = typename LoaderDomain::Operator
 
using Padding = typename LoaderDomain::Padding
 
using Pool2DOptions = typename LoaderDomain::Pool2DOptions
 
using SubGraph = typename LoaderDomain::SubGraph
 
using Tensor = typename LoaderDomain::Tensor
 
using TensorType = typename LoaderDomain::TensorType
 
using DimensionType = typename LoaderDomain::DimensionType
 
using SparseIndexVector = typename LoaderDomain::SparseIndexVector
 

Protected Member Functions

bool isOptionalInputTensor (std::int32_t idx)
 
virtual bool allowOptionalInputTensor (BuiltinOperator)=0
 
 ~BaseLoader ()=default
 
void loadModel ()
 
ir::Activation convertActivation (ActivationFunctionType type)
 
virtual ir::DataType tensorTypeToDataType (const TensorType type)
 
ir::OperandIndex tensorIdxToOperandIdx (int32_t tensorIdx)
 
flexbuffers::Map getCustomOpAttrMap (const Operator *op)
 
ir::OperandIndex loadOperand (const Tensor *tensor, ir::Graph &subg)
 
void loadQuantization (const Tensor *tensor, ir::TypeInfo &typeInfo)
 
void loadSparsity (const Tensor *tensor, ir::TypeInfo &typeInfo)
 
void loadOperationIO (const Operator *op, ir::OperandIndexSequence &inputs, ir::OperandIndexSequence &outputs)
 
void loadOperation (const Operator *op, ir::Graph &subg)
 
template<typename Param , typename OptionsType >
void loadStridesAndPaddings (Param &param, const OptionsType *options)
 
template<typename Param >
void loadPool2DOptions (Param &param, const Pool2DOptions *options)
 
BuiltinOperator getBuiltinOperator (const Operator *op)
 

Protected Attributes

uint8_t * _base
 
int32_t _pagesize
 
int _fd
 
std::unique_ptr< ir::Model > & _model
 
const Model_domain_model
 
std::vector< ir::OperandIndex_tensor_to_operand
 
std::unordered_map< ir::OperandIndex, std::string > _tensor_names
 
std::unique_ptr< Verifier_verifier
 
bool _use_mmaped_data = false
 
std::unordered_map< uint32_t, std::shared_ptr< ir::Data > > _buf_to_data
 

Detailed Description

template<typename LoaderDomain>
class onert::loader::BaseLoader< LoaderDomain >

Definition at line 40 of file BaseLoader.h.

Member Typedef Documentation

◆ ActivationFunctionType

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::ActivationFunctionType = typename LoaderDomain::ActivationFunctionType
protected

Definition at line 44 of file BaseLoader.h.

◆ Buffer

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::Buffer = typename LoaderDomain::Buffer
protected

Definition at line 45 of file BaseLoader.h.

◆ BuiltinOperator

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::BuiltinOperator = typename LoaderDomain::BuiltinOperator
protected

Definition at line 46 of file BaseLoader.h.

◆ CustomOptionsFormat

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::CustomOptionsFormat = typename LoaderDomain::CustomOptionsFormat
protected

Definition at line 47 of file BaseLoader.h.

◆ DimensionType

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::DimensionType = typename LoaderDomain::DimensionType
protected

Definition at line 56 of file BaseLoader.h.

◆ Metadata

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::Metadata = typename LoaderDomain::Metadata
protected

Definition at line 48 of file BaseLoader.h.

◆ Model

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::Model = typename LoaderDomain::Model
protected

Definition at line 49 of file BaseLoader.h.

◆ Operator

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::Operator = typename LoaderDomain::Operator
protected

Definition at line 50 of file BaseLoader.h.

◆ Padding

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::Padding = typename LoaderDomain::Padding
protected

Definition at line 51 of file BaseLoader.h.

◆ Pool2DOptions

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::Pool2DOptions = typename LoaderDomain::Pool2DOptions
protected

Definition at line 52 of file BaseLoader.h.

◆ SparseIndexVector

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::SparseIndexVector = typename LoaderDomain::SparseIndexVector
protected

Definition at line 57 of file BaseLoader.h.

◆ SubGraph

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::SubGraph = typename LoaderDomain::SubGraph
protected

Definition at line 53 of file BaseLoader.h.

◆ Tensor

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::Tensor = typename LoaderDomain::Tensor
protected

Definition at line 54 of file BaseLoader.h.

◆ TensorType

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::TensorType = typename LoaderDomain::TensorType
protected

Definition at line 55 of file BaseLoader.h.

◆ Verifier

template<typename LoaderDomain >
using onert::loader::BaseLoader< LoaderDomain >::Verifier = typename LoaderDomain::Verifier
protected

Definition at line 43 of file BaseLoader.h.

Constructor & Destructor Documentation

◆ BaseLoader()

template<typename LoaderDomain >
onert::loader::BaseLoader< LoaderDomain >::BaseLoader ( std::unique_ptr< ir::Model > &  model)
inlineexplicit

Construct a new Loader object.

Parameters
modelreference to model

Definition at line 69 of file BaseLoader.h.

70 : _base{nullptr}, _pagesize(getpagesize()), _fd(-1), _model(model), _domain_model{nullptr}
71 {
72 _use_mmaped_data = util::getConfigBool(util::config::USE_MMAPED_DATA);
73 }
std::unique_ptr< ir::Model > & _model
Definition BaseLoader.h:192
const Model * _domain_model
Definition BaseLoader.h:193
bool getConfigBool(const std::string &key)

References onert::loader::BaseLoader< LoaderDomain >::_use_mmaped_data, and onert::util::getConfigBool().

◆ ~BaseLoader()

template<typename LoaderDomain >
onert::loader::BaseLoader< LoaderDomain >::~BaseLoader ( )
protecteddefault

Member Function Documentation

◆ allowOptionalInputTensor()

template<typename LoaderDomain >
virtual bool onert::loader::BaseLoader< LoaderDomain >::allowOptionalInputTensor ( BuiltinOperator  )
protectedpure virtual

◆ convertActivation()

template<typename LoaderDomain >
ir::Activation onert::loader::BaseLoader< LoaderDomain >::convertActivation ( ActivationFunctionType  type)
protected

◆ getBuiltinOperator()

template<typename LoaderDomain >
BuiltinOperator onert::loader::BaseLoader< LoaderDomain >::getBuiltinOperator ( const Operator op)
inlineprotected

Definition at line 113 of file BaseLoader.h.

114 {
115 auto const builtin_opcode = _domain_model->operator_codes()->Get(op->opcode_index());
116 auto builtin_op = builtin_opcode->builtin_code();
117 if (builtin_op < BuiltinOperator::BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES)
118 builtin_op = static_cast<BuiltinOperator>(builtin_opcode->deprecated_builtin_code());
119
120 return builtin_op;
121 }
typename LoaderDomain::BuiltinOperator BuiltinOperator
Definition BaseLoader.h:46

References onert::loader::BaseLoader< LoaderDomain >::_domain_model.

◆ getCustomOpAttrMap()

template<typename LoaderDomain >
flexbuffers::Map onert::loader::BaseLoader< LoaderDomain >::getCustomOpAttrMap ( const Operator op)
protected

◆ isOptionalInputTensor()

template<typename LoaderDomain >
bool onert::loader::BaseLoader< LoaderDomain >::isOptionalInputTensor ( std::int32_t  idx)
inlineprotected

Definition at line 60 of file BaseLoader.h.

60{ return idx == -1; }

◆ loadFromBuffer()

template<typename LoaderDomain >
void onert::loader::BaseLoader< LoaderDomain >::loadFromBuffer ( uint8_t *  buffer,
size_t  size 
)

Load a model from a buffer.

Parameters
bufferbuffer pointer
sizebuffer size

◆ loadFromFile()

template<typename LoaderDomain >
void onert::loader::BaseLoader< LoaderDomain >::loadFromFile ( const std::string &  file_path)

Load a model from file.

Parameters
file_path

◆ loadModel()

template<typename LoaderDomain >
void onert::loader::BaseLoader< LoaderDomain >::loadModel ( )
protected

Definition at line 1749 of file BaseLoader.h.

1750{
1751 LoaderDomain::VerifyModelBuffer(*_verifier.get());
1752 _domain_model = LoaderDomain::GetModel(_base);
1753
1754 auto model = std::make_unique<ir::Model>();
1755 // Version unused
1756 // const auto version = _model->version();
1757 // Description unused
1758
1759 // Load Metadata
1760 auto const metadata_list = _domain_model->metadata();
1761 if (metadata_list != nullptr)
1762 {
1763 for (uint32_t i = 0; i < metadata_list->size(); ++i)
1764 {
1765 const auto metadata = metadata_list->Get(i);
1766 if (metadata->name() == nullptr)
1767 continue; // metadata should have name
1768
1769 std::unique_ptr<const ir::Data> data = loadMetadata(metadata->buffer());
1770 model->add_metadata(metadata->name()->str(), std::move(data));
1771 }
1772 }
1773
1774 // const auto *description = _model->description();
1775 // Load subgraphs and map operations on subgraph
1776 const auto subgraphs = _domain_model->subgraphs();
1777 if (subgraphs->size() - 1 > ir::SubgraphIndex::max())
1778 throw std::runtime_error{"The number of subgraphs cannot exceed " +
1779 std::to_string(ir::SubgraphIndex::max() + 1)};
1780 for (uint16_t subgraph_index = 0; subgraph_index < subgraphs->size(); ++subgraph_index)
1781 {
1782 auto subg = loadSubgraph((*_domain_model->subgraphs())[subgraph_index]);
1783 // NOTE: Used () instead of {}, which does not check narrowing.
1784 // It is okay since overflow is checked the above if-statement.
1785 model->push(ir::SubgraphIndex(subgraph_index), std::move(subg));
1786 }
1787 _model = std::move(model);
1788}
std::unique_ptr< Verifier > _verifier
Definition BaseLoader.h:198
static uint16_t max()
Return max index value.
Definition Index.h:146
::onert::util::Index< uint16_t, SubgraphIndexTag > SubgraphIndex
Definition Index.h:39

References onert::util::Index< uint16_t, SubgraphIndexTag >::max().

◆ loadOperand()

template<typename LoaderDomain >
ir::OperandIndex onert::loader::BaseLoader< LoaderDomain >::loadOperand ( const Tensor tensor,
ir::Graph subg 
)
protected

Definition at line 361 of file BaseLoader.h.

362{
363 ir::Shape shape;
364 // Shape
365 const auto *tensor_shape =
366 tensor->shape_signature() ? tensor->shape_signature() : tensor->shape();
367 if (tensor_shape != nullptr)
368 {
369 for (const auto &dim : *tensor_shape)
370 {
371 shape.append(dim);
372 }
373 }
374
375 // Note for tensor->shape_signature()
376 // We don't handle shape signature
377 // How we handle:
378 // If shape_signature[k] == -1, we will use tensor->shape()[k] == 1
379 // If app wants to change the input shape, call nnfw_apply_input_tensorinfo() can
380 // be used.
381
382 // TypeInfo
383 ir::TypeInfo type_info(tensorTypeToDataType(tensor->type()));
384 loadQuantization(tensor, type_info);
385 loadSparsity(tensor, type_info);
386
387 // Create operand
388 const auto operand_index = subg.addOperand(shape, type_info);
389
390 // Constant tensors are indicated by non-empty data.
391 const auto *data = _domain_model->buffers()->Get(tensor->buffer())->data();
392 if (data != nullptr)
393 {
394 using std::ptrdiff_t;
395 std::shared_ptr<ir::Data> data_obj;
396
397 if (_fd == -1) // Model is from memory
398 {
399 data_obj = std::make_shared<ir::ExternalData>(data->data(), data->size());
400 }
401 else // Model is loaded(mmap'd) from a file
402 {
403 size_t data_size = data->size();
404 ptrdiff_t unaligned_offset_start = data->data() - _base;
405 ptrdiff_t offset_end = unaligned_offset_start + data_size;
406
407 // Calculated aligned offset from base address of mapped region
408 // munmap accepts memory address which is a multiple of the pagesize
409 ptrdiff_t aligned_offset_start = (unaligned_offset_start / _pagesize) * _pagesize;
410 size_t mmap_size = offset_end - aligned_offset_start;
411
412 uint32_t buf_idx = tensor->buffer();
413 auto buffer_found = _buf_to_data.find(buf_idx);
414
415 if (buffer_found != _buf_to_data.end())
416 {
417 // Another tensor points this buffer and its matching Data(either CachedData or MMapedData)
418 // was already created. Let's reuse the Data
419 data_obj = buffer_found->second;
420 }
421 else if (_use_mmaped_data)
422 {
423 data_obj = std::make_shared<ir::MMapedData>(_fd, aligned_offset_start, mmap_size,
424 unaligned_offset_start, data_size);
425 _buf_to_data[buf_idx] = data_obj;
426 }
427 else
428 {
429 size_t offset = unaligned_offset_start - aligned_offset_start;
430 uint8_t *mmap_base = static_cast<uint8_t *>(
431 mmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE, _fd, aligned_offset_start));
432
433 data_obj = std::make_shared<ir::CachedData>(mmap_base + offset, data_size);
434 _buf_to_data[buf_idx] = data_obj;
435
436 munmap(mmap_base, mmap_size);
437 }
438 }
439 subg.setOperandValue(operand_index, std::move(data_obj));
440 }
441
442 _tensor_names.emplace(operand_index, tensor->name()->str());
443
444 // Variable
445 if (tensor->is_variable())
446 {
447 if (data != nullptr)
448 throw std::runtime_error("Variable tensor with buffer is not supported!");
449
450 subg.operands().at(operand_index).info().setAsVariable();
451 }
452
453 return operand_index;
454}
std::unordered_map< uint32_t, std::shared_ptr< ir::Data > > _buf_to_data
Definition BaseLoader.h:203
virtual ir::DataType tensorTypeToDataType(const TensorType type)
void loadQuantization(const Tensor *tensor, ir::TypeInfo &typeInfo)
Definition BaseLoader.h:457
void loadSparsity(const Tensor *tensor, ir::TypeInfo &typeInfo)
Definition BaseLoader.h:496
std::unordered_map< ir::OperandIndex, std::string > _tensor_names
Definition BaseLoader.h:196
__global uchar * offset(const Image *img, int x, int y)
Definition helpers.h:540

References onert::ir::Graph::addOperand(), onert::util::ObjectManager< Index, Object >::at(), offset(), onert::ir::Graph::operands(), and onert::ir::Graph::setOperandValue().

◆ loadOperation()

template<typename LoaderDomain >
void onert::loader::BaseLoader< LoaderDomain >::loadOperation ( const Operator op,
ir::Graph subg 
)
protected

Definition at line 1458 of file BaseLoader.h.

1459{
1460 auto const builtin_op = getBuiltinOperator(op);
1461
1462 switch (builtin_op)
1463 {
1464 case BuiltinOperator::BuiltinOperator_ADD_N:
1465 loadOperationTo<ir::operation::AddN>(op, subg);
1466 return;
1467 case BuiltinOperator::BuiltinOperator_CONV_2D:
1468 loadConv2D(op, subg);
1469 return;
1470 case BuiltinOperator::BuiltinOperator_AVERAGE_POOL_2D:
1471 loadPool2D(op, subg, ir::operation::Pool2D::PoolType::AVG);
1472 return;
1473 case BuiltinOperator::BuiltinOperator_DEPTHWISE_CONV_2D:
1474 loadDepthwiseConv2D(op, subg);
1475 return;
1476 case BuiltinOperator::BuiltinOperator_TRANSPOSE_CONV:
1477 loadTransposeConv(op, subg);
1478 return;
1479 case BuiltinOperator::BuiltinOperator_RESHAPE:
1480 loadReshape(op, subg);
1481 return;
1482 case BuiltinOperator::BuiltinOperator_SOFTMAX:
1483 loadSoftmax(op, subg);
1484 return;
1485 case BuiltinOperator::BuiltinOperator_MAX_POOL_2D:
1486 loadPool2D(op, subg, ir::operation::Pool2D::PoolType::MAX);
1487 return;
1488 case BuiltinOperator::BuiltinOperator_CONCATENATION:
1489 loadConcatenation(op, subg);
1490 return;
1491 case BuiltinOperator::BuiltinOperator_FLOOR:
1492 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::FLOOR);
1493 return;
1494 case BuiltinOperator::BuiltinOperator_FULLY_CONNECTED:
1495 loadFC(op, subg);
1496 return;
1497 case BuiltinOperator::BuiltinOperator_ADD:
1498 loadBinaryArithmetic(op, subg, ir::operation::BinaryArithmetic::ArithmeticType::ADD);
1499 return;
1500 case BuiltinOperator::BuiltinOperator_SUB:
1501 loadBinaryArithmetic(op, subg, ir::operation::BinaryArithmetic::ArithmeticType::SUB);
1502 return;
1503 case BuiltinOperator::BuiltinOperator_MUL:
1504 loadBinaryArithmetic(op, subg, ir::operation::BinaryArithmetic::ArithmeticType::MUL);
1505 return;
1506 case BuiltinOperator::BuiltinOperator_DIV:
1507 loadBinaryArithmetic(op, subg, ir::operation::BinaryArithmetic::ArithmeticType::DIV);
1508 return;
1509 case BuiltinOperator::BuiltinOperator_PACK:
1510 loadPack(op, subg);
1511 return;
1512 case BuiltinOperator::BuiltinOperator_ELU:
1513 loadElementwiseActivation(op, subg, ir::operation::ElementwiseActivation::Type::ELU);
1514 return;
1515 case BuiltinOperator::BuiltinOperator_RELU:
1516 loadElementwiseActivation(op, subg, ir::operation::ElementwiseActivation::Type::RELU,
1518 return;
1519 case BuiltinOperator::BuiltinOperator_RELU_N1_TO_1:
1520 loadElementwiseActivation(op, subg, ir::operation::ElementwiseActivation::Type::RELU, 1.f,
1521 -1.f);
1522 return;
1523 case BuiltinOperator::BuiltinOperator_RELU6:
1524 loadElementwiseActivation(op, subg, ir::operation::ElementwiseActivation::Type::RELU, 6.f,
1525 0.f);
1526 return;
1527 case BuiltinOperator::BuiltinOperator_RESIZE_BILINEAR:
1528 loadResizeBilinear(op, subg);
1529 return;
1530 case BuiltinOperator::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR:
1531 loadResizeNearestNeighbor(op, subg);
1532 return;
1533 case BuiltinOperator::BuiltinOperator_RSQRT:
1534 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::RSQRT);
1535 return;
1536 case BuiltinOperator::BuiltinOperator_SELECT:
1537 case BuiltinOperator::BuiltinOperator_SELECT_V2:
1538 loadOperationTo<ir::operation::Select>(op, subg);
1539 return;
1540 case BuiltinOperator::BuiltinOperator_SQRT:
1541 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::SQRT);
1542 return;
1543 case BuiltinOperator::BuiltinOperator_SQUARE:
1544 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::SQUARE);
1545 return;
1546 case BuiltinOperator::BuiltinOperator_SQUARED_DIFFERENCE:
1547 loadOperationTo<ir::operation::SquaredDifference>(op, subg);
1548 return;
1549 case BuiltinOperator::BuiltinOperator_TANH:
1550 loadElementwiseActivation(op, subg, ir::operation::ElementwiseActivation::Type::TANH, 1.f,
1551 1.f);
1552 return;
1553 case BuiltinOperator::BuiltinOperator_TRANSPOSE:
1554 loadOperationTo<ir::operation::Transpose>(op, subg);
1555 return;
1556 case BuiltinOperator::BuiltinOperator_MEAN:
1557 loadReduce(op, subg, ir::operation::Reduce::ReduceType::MEAN);
1558 return;
1559 case BuiltinOperator::BuiltinOperator_REDUCE_ANY:
1560 loadReduce(op, subg, ir::operation::Reduce::ReduceType::ANY);
1561 return;
1562 case BuiltinOperator::BuiltinOperator_REDUCE_MAX:
1563 loadReduce(op, subg, ir::operation::Reduce::ReduceType::MAX);
1564 return;
1565 case BuiltinOperator::BuiltinOperator_REVERSE_V2:
1566 loadOperationTo<ir::operation::Reverse>(op, subg);
1567 return;
1568 case BuiltinOperator::BuiltinOperator_PAD:
1569 case BuiltinOperator::BuiltinOperator_PADV2:
1570 loadOperationTo<ir::operation::Pad>(op, subg);
1571 return;
1572 case BuiltinOperator::BuiltinOperator_LOGISTIC:
1573 loadElementwiseActivation(op, subg, ir::operation::ElementwiseActivation::Type::LOGISTIC);
1574 return;
1575 case BuiltinOperator::BuiltinOperator_EXP:
1576 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::EXP);
1577 return;
1578 case BuiltinOperator::BuiltinOperator_EXPAND_DIMS:
1579 loadOperationTo<ir::operation::ExpandDims>(op, subg);
1580 return;
1581 case BuiltinOperator::BuiltinOperator_GATHER:
1582 loadGather(op, subg);
1583 return;
1584 case BuiltinOperator::BuiltinOperator_SPACE_TO_BATCH_ND:
1585 loadOperationTo<ir::operation::SpaceToBatchND>(op, subg);
1586 return;
1587 case BuiltinOperator::BuiltinOperator_BATCH_TO_SPACE_ND:
1588 loadOperationTo<ir::operation::BatchToSpaceND>(op, subg);
1589 return;
1590 case BuiltinOperator::BuiltinOperator_SUM:
1591 loadReduce(op, subg, ir::operation::Reduce::ReduceType::SUM);
1592 return;
1593 case BuiltinOperator::BuiltinOperator_CUSTOM:
1594 loadCustom(op, subg);
1595 return;
1596 case BuiltinOperator::BuiltinOperator_SQUEEZE:
1597 loadSqueeze(op, subg);
1598 return;
1599 case BuiltinOperator::BuiltinOperator_PRELU:
1600 loadOperationTo<ir::operation::PReLU>(op, subg);
1601 return;
1602 case BuiltinOperator::BuiltinOperator_SPLIT:
1603 loadSplit(op, subg);
1604 return;
1605 case BuiltinOperator::BuiltinOperator_SPLIT_V:
1606 loadSplitV(op, subg);
1607 return;
1608 case BuiltinOperator::BuiltinOperator_SLICE:
1609 loadOperationTo<ir::operation::Slice>(op, subg);
1610 return;
1611 case BuiltinOperator::BuiltinOperator_STRIDED_SLICE:
1612 loadStridedSlice(op, subg);
1613 return;
1614 case BuiltinOperator::BuiltinOperator_UNPACK:
1615 loadUnpack(op, subg);
1616 return;
1617 case BuiltinOperator::BuiltinOperator_FLOOR_DIV:
1618 loadElementwiseBinary(op, subg,
1620 return;
1621 case BuiltinOperator::BuiltinOperator_FLOOR_MOD:
1622 loadElementwiseBinary(op, subg,
1624 return;
1625 case BuiltinOperator::BuiltinOperator_MINIMUM:
1626 loadElementwiseBinary(op, subg, ir::operation::ElementwiseBinary::ElementwiseBinaryType::MIN);
1627 return;
1628 case BuiltinOperator::BuiltinOperator_MAXIMUM:
1629 loadElementwiseBinary(op, subg, ir::operation::ElementwiseBinary::ElementwiseBinaryType::MAX);
1630 return;
1631 case BuiltinOperator::BuiltinOperator_CAST:
1632 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::CAST);
1633 return;
1634 case BuiltinOperator::BuiltinOperator_EQUAL:
1635 case BuiltinOperator::BuiltinOperator_NOT_EQUAL:
1636 case BuiltinOperator::BuiltinOperator_GREATER_EQUAL:
1637 case BuiltinOperator::BuiltinOperator_GREATER:
1638 case BuiltinOperator::BuiltinOperator_LESS_EQUAL:
1639 case BuiltinOperator::BuiltinOperator_LESS:
1640 loadComparison(op, subg);
1641 return;
1642 case BuiltinOperator::BuiltinOperator_ONE_HOT:
1643 loadOneHot(op, subg);
1644 return;
1645 case BuiltinOperator::BuiltinOperator_ABS:
1646 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::ABS);
1647 return;
1648 case BuiltinOperator::BuiltinOperator_COS:
1649 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::COS);
1650 return;
1651 case BuiltinOperator::BuiltinOperator_SIN:
1652 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::SIN);
1653 return;
1654 case BuiltinOperator::BuiltinOperator_SHAPE:
1655 loadOperationTo<ir::operation::Shape>(op, subg);
1656 return;
1657 case BuiltinOperator::BuiltinOperator_REDUCE_PROD:
1658 loadReduce(op, subg, ir::operation::Reduce::ReduceType::PROD);
1659 return;
1660 case BuiltinOperator::BuiltinOperator_IF:
1661 loadIf(op, subg);
1662 return;
1663 case BuiltinOperator::BuiltinOperator_WHILE:
1664 loadWhile(op, subg);
1665 return;
1666 case BuiltinOperator::BuiltinOperator_NEG:
1667 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::NEG);
1668 return;
1669 case BuiltinOperator::BuiltinOperator_ARG_MAX:
1670 loadArgMinMax(op, subg, true);
1671 return;
1672 case BuiltinOperator::BuiltinOperator_ARG_MIN:
1673 loadArgMinMax(op, subg, false);
1674 return;
1675 case BuiltinOperator::BuiltinOperator_LOG:
1676 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::LOG);
1677 return;
1678 case BuiltinOperator::BuiltinOperator_ROUND:
1679 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::ROUND);
1680 return;
1681 case BuiltinOperator::BuiltinOperator_POW:
1682 loadOperationTo<ir::operation::Pow>(op, subg);
1683 return;
1684 case BuiltinOperator::BuiltinOperator_LOGICAL_NOT:
1685 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::LOGICAL_NOT);
1686 return;
1687 case BuiltinOperator::BuiltinOperator_LOGICAL_AND:
1688 loadElementwiseBinary(op, subg,
1690 return;
1691 case BuiltinOperator::BuiltinOperator_LOGICAL_OR:
1692 loadElementwiseBinary(op, subg,
1694 return;
1695 case BuiltinOperator::BuiltinOperator_FILL:
1696 loadOperationTo<ir::operation::Fill>(op, subg);
1697 return;
1698 case BuiltinOperator::BuiltinOperator_ZEROS_LIKE:
1699 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::ZEROS_LIKE);
1700 return;
1701 case BuiltinOperator::BuiltinOperator_TILE:
1702 loadOperationTo<ir::operation::Tile>(op, subg);
1703 return;
1704 case BuiltinOperator::BuiltinOperator_RANGE:
1705 loadOperationTo<ir::operation::Range>(op, subg);
1706 return;
1707 case BuiltinOperator::BuiltinOperator_BATCH_MATMUL:
1708 loadBatchMatMul(op, subg);
1709 return;
1710 case BuiltinOperator::BuiltinOperator_LOG_SOFTMAX:
1711 loadLogSoftmax(op, subg);
1712 return;
1713 case BuiltinOperator::BuiltinOperator_QUANTIZE:
1714 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::QUANTIZE);
1715 return;
1716 case BuiltinOperator::BuiltinOperator_DEQUANTIZE:
1717 loadElementwiseUnary(op, subg, ir::operation::ElementwiseUnary::Type::DEQUANTIZE);
1718 return;
1719 case BuiltinOperator::BuiltinOperator_SPACE_TO_DEPTH:
1720 loadSpaceToDepth(op, subg);
1721 return;
1722 case BuiltinOperator::BuiltinOperator_L2_NORMALIZATION:
1723 loadOperationTo<ir::operation::L2Normalization>(op, subg);
1724 break;
1725 case BuiltinOperator::BuiltinOperator_LEAKY_RELU:
1726 loadLeakyRelu(op, subg);
1727 return;
1728 case BuiltinOperator::BuiltinOperator_RANK:
1729 loadOperationTo<ir::operation::Rank>(op, subg);
1730 return;
1731 case BuiltinOperator::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM:
1732 loadUnidirectionalSequenceLSTM(op, subg);
1733 return;
1734 case BuiltinOperator::BuiltinOperator_DEPTH_TO_SPACE:
1735 loadDepthToSpace(op, subg);
1736 return;
1737 case BuiltinOperator::BuiltinOperator_EMBEDDING_LOOKUP:
1738 loadOperationTo<ir::operation::EmbeddingLookup>(op, subg);
1739 return;
1740 case BuiltinOperator::BuiltinOperator_HASHTABLE_LOOKUP:
1741 loadOperationTo<ir::operation::HashtableLookup>(op, subg);
1742 return;
1743 default:
1744 throw std::runtime_error(
1745 std::string("Unsupported operation: ").append(EnumNameBuiltinOperator(builtin_op)));
1746 }
1747}
BuiltinOperator getBuiltinOperator(const Operator *op)
Definition BaseLoader.h:113

References onert::ir::operation::ElementwiseUnary::ABS, onert::ir::operation::BinaryArithmetic::ADD, onert::ir::operation::Reduce::ANY, onert::ir::operation::Pool2D::AVG, onert::ir::operation::ElementwiseUnary::CAST, onert::ir::operation::ElementwiseUnary::COS, onert::ir::operation::ElementwiseUnary::DEQUANTIZE, onert::ir::operation::BinaryArithmetic::DIV, onert::ir::operation::ElementwiseActivation::ELU, onert::ir::operation::ElementwiseUnary::EXP, onert::ir::operation::ElementwiseUnary::FLOOR, onert::ir::operation::ElementwiseBinary::FLOOR_DIV, onert::ir::operation::ElementwiseBinary::FLOOR_MOD, onert::ir::operation::ElementwiseActivation::infinity, onert::ir::operation::ElementwiseUnary::LOG, onert::ir::operation::ElementwiseBinary::LOGICAL_AND, onert::ir::operation::ElementwiseUnary::LOGICAL_NOT, onert::ir::operation::ElementwiseBinary::LOGICAL_OR, onert::ir::operation::ElementwiseActivation::LOGISTIC, onert::ir::operation::ElementwiseBinary::MAX, onert::ir::operation::Pool2D::MAX, onert::ir::operation::Reduce::MAX, onert::ir::operation::Reduce::MEAN, onert::ir::operation::ElementwiseBinary::MIN, onert::ir::operation::BinaryArithmetic::MUL, onert::ir::operation::ElementwiseUnary::NEG, onert::ir::operation::Reduce::PROD, onert::ir::operation::ElementwiseUnary::QUANTIZE, onert::ir::operation::ElementwiseActivation::RELU, onert::ir::operation::ElementwiseUnary::ROUND, onert::ir::operation::ElementwiseUnary::RSQRT, onert::ir::operation::ElementwiseUnary::SIN, onert::ir::operation::ElementwiseUnary::SQRT, onert::ir::operation::ElementwiseUnary::SQUARE, onert::ir::operation::BinaryArithmetic::SUB, onert::ir::operation::Reduce::SUM, onert::ir::operation::ElementwiseActivation::TANH, and onert::ir::operation::ElementwiseUnary::ZEROS_LIKE.

◆ loadOperationIO()

template<typename LoaderDomain >
void onert::loader::BaseLoader< LoaderDomain >::loadOperationIO ( const Operator op,
ir::OperandIndexSequence inputs,
ir::OperandIndexSequence outputs 
)
protected

Definition at line 592 of file BaseLoader.h.

594{
595 for (const std::int32_t idx : *op->inputs())
596 {
597 // Optional tensors are not supported yet except for FULLY_CONNECTED and BCQ_FULLY_CONNECTED
598 auto check_optional_input = [&]() {
599 auto builtin_code = getBuiltinOperator(op);
600 if (isOptionalInputTensor(idx) && !allowOptionalInputTensor(builtin_code))
601 throw std::runtime_error(
602 std::string("loader doesn't support optional input tensor yet for ")
603 .append(EnumNameBuiltinOperator(builtin_code)));
604 };
605 check_optional_input();
606 inputs.append(tensorIdxToOperandIdx(idx));
607 }
608
609 for (const std::int32_t idx : *op->outputs())
610 {
611 outputs.append(tensorIdxToOperandIdx(idx));
612 }
613}
bool isOptionalInputTensor(std::int32_t idx)
Definition BaseLoader.h:60
virtual bool allowOptionalInputTensor(BuiltinOperator)=0
ir::OperandIndex tensorIdxToOperandIdx(int32_t tensorIdx)

References onert::ir::OperandIndexSequence::append().

◆ loadPool2DOptions()

template<typename LoaderDomain >
template<typename Param >
void onert::loader::BaseLoader< LoaderDomain >::loadPool2DOptions ( Param &  param,
const Pool2DOptions options 
)
protected

Definition at line 639 of file BaseLoader.h.

640{
641 // Strides and Paddings
642 if (options->stride_h() <= 0 || options->stride_w() <= 0)
643 throw std::runtime_error{"Invalid stride vertical or horizontal - both must be bigger than 0"};
644 loadStridesAndPaddings(param, options);
645 // Filter width and height
646 // Strides
647 if (options->filter_width() <= 0 || options->filter_height() <= 0)
648 throw std::runtime_error{"Invalid filter width or height - both must be bigger than 0"};
649 param.kw = options->filter_width();
650 param.kh = options->filter_height();
651 // Activation
652 param.activation = convertActivation(options->fused_activation_function());
653}
ir::Activation convertActivation(ActivationFunctionType type)
void loadStridesAndPaddings(Param &param, const OptionsType *options)
Definition BaseLoader.h:617

◆ loadQuantization()

template<typename LoaderDomain >
void onert::loader::BaseLoader< LoaderDomain >::loadQuantization ( const Tensor tensor,
ir::TypeInfo typeInfo 
)
protected

Definition at line 457 of file BaseLoader.h.

458{
459 auto q_params = tensor->quantization();
460 if (q_params == nullptr || q_params->scale() == nullptr || q_params->scale()->size() == 0)
461 {
462 typeInfo.quantization(0., 0);
463 return;
464 }
465 if (q_params->zero_point() == nullptr)
466 {
467 throw std::runtime_error("Quantization params: scale is not null, but zero_point is null.");
468 }
469 const size_t num_scales = q_params->scale()->size();
470 if (num_scales != q_params->zero_point()->size())
471 {
472 throw std::runtime_error("Quantization params: scale size != zero_point size");
473 }
474 std::vector<float> scales;
475 std::vector<int32_t> zero_points;
476 scales.resize(num_scales);
477 zero_points.resize(num_scales);
478 for (size_t i = 0; i < num_scales; ++i)
479 {
480 scales[i] = q_params->scale()->Get(i);
481 // zero_point is defined as long (i64) in schema while TypeInfo's zero_point is int32_t.
482 // int64_t is used instead of long because long is 4 byte in most 32bit architecture.
483 int64_t zero_point = q_params->zero_point()->Get(i);
484 if (zero_point < std::numeric_limits<int32_t>::min() ||
485 zero_point > std::numeric_limits<int32_t>::max())
486 throw std::runtime_error("Zero_point is out of int32 range.");
487 zero_points[i] = static_cast<int32_t>(zero_point);
488 }
489 auto details = q_params->details_as_CustomQuantization();
490 if (details != nullptr)
491 throw std::runtime_error("Custom Quantization is not supported");
492 typeInfo.quantization(std::move(scales), std::move(zero_points));
493}

References onert::ir::TypeInfo::quantization().

◆ loadSparsity()

template<typename LoaderDomain >
void onert::loader::BaseLoader< LoaderDomain >::loadSparsity ( const Tensor tensor,
ir::TypeInfo typeInfo 
)
protected

Definition at line 496 of file BaseLoader.h.

497{
498 auto src_sparsity = tensor->sparsity();
499 if (src_sparsity != nullptr)
500 {
501 std::vector<uint16_t> w1_segments;
502 std::vector<uint16_t> w1_indices;
503 // check traversal_order
504 if (src_sparsity->traversal_order())
505 {
506 const int traversal_order_size = src_sparsity->traversal_order()->size();
507 for (int i = 0; i < traversal_order_size; ++i)
508 {
509 if (i != src_sparsity->traversal_order()->Get(i))
510 throw std::runtime_error("traversal_order [0, 1, ..., n-1] is only supported.");
511 }
512 }
513 // check block_map
514 int block_rank = 0;
515 if (src_sparsity->block_map())
516 {
517 block_rank = src_sparsity->block_map()->size();
518 for (int i = 0; i < block_rank; ++i)
519 {
520 if (i != src_sparsity->block_map()->Get(i))
521 throw std::runtime_error("block_map [0, 1, ..., n-1] is only supported.");
522 }
523 }
524 // load metadata
525 const auto dim_metadata_size = src_sparsity->dim_metadata()->size();
526 const auto dense_rank = tensor->shape() ? tensor->shape()->size() : 0;
527 if (dense_rank + block_rank != dim_metadata_size)
528 throw std::runtime_error("sparsity dim_metadata length is wrong.");
529 bool random_sparsity = dim_metadata_size == 2 && block_rank == 0;
530 bool block2D_sparsity = dim_metadata_size == 4 && block_rank == 2;
531 if (dim_metadata_size != !random_sparsity && !block2D_sparsity)
532 throw std::runtime_error(
533 "sparsity is supported only for 2D tensor with random or 16x1 block sparsity.");
534
535 const auto *src_metadata = src_sparsity->dim_metadata()->Get(0);
536 if (src_metadata->format() != DimensionType::DimensionType_DENSE)
537 throw std::runtime_error("sparse tensor dim[0] is not DENSE");
538 src_metadata = src_sparsity->dim_metadata()->Get(1);
539 if (src_metadata->format() != DimensionType::DimensionType_SPARSE_CSR)
540 throw std::runtime_error("sparse tensor dim[0] is not SPARSE_CSR");
541 auto ParseSparseIndexVector = [src_metadata, &w1_segments, &w1_indices]() {
542 if (src_metadata->array_segments() == nullptr || src_metadata->array_indices() == nullptr)
543 return false;
544 bool status = true;
545 /* `onert` inernally uses uint16 type regardless of the value of
546 the array_segments_type and array_indices_type */
547 switch (src_metadata->array_segments_type())
548 {
549 case SparseIndexVector::SparseIndexVector_Int32Vector:
550 throw std::runtime_error("sparse tensor with int32 segment type is not supported");
551 case SparseIndexVector::SparseIndexVector_Uint16Vector:
552 status = Copy(src_metadata->array_segments_as_Uint16Vector(), w1_segments);
553 break;
554 case SparseIndexVector::SparseIndexVector_Uint8Vector:
555 status = Copy(src_metadata->array_segments_as_Uint8Vector(), w1_segments);
556 break;
557 default:
558 return false;
559 }
560 if (status != true)
561 return false;
562 switch (src_metadata->array_indices_type())
563 {
564 case SparseIndexVector::SparseIndexVector_Int32Vector:
565 throw std::runtime_error("sparse tensor with int32 indices type is not supported");
566 case SparseIndexVector::SparseIndexVector_Uint16Vector:
567 return Copy(src_metadata->array_indices_as_Uint16Vector(), w1_indices);
568 case SparseIndexVector::SparseIndexVector_Uint8Vector:
569 return Copy(src_metadata->array_indices_as_Uint8Vector(), w1_indices);
570 default:
571 break;
572 }
573 return false;
574 };
575 if (ParseSparseIndexVector() == false)
576 throw std::runtime_error("Error during parsing sparsity index information");
577 // Get block size
578 std::vector<int32_t> block_size;
579 for (int i = 0; i < block_rank; ++i)
580 {
581 auto block_metadata = src_sparsity->dim_metadata()->Get(dense_rank + i);
582 if (block_metadata->format() != DimensionType::DimensionType_DENSE)
583 throw std::runtime_error("block dimension must be DENSE.");
584 block_size.push_back(block_metadata->dense_size());
585 }
586 typeInfo.sparsity(std::make_shared<ir::Sparsity>(std::move(w1_segments), std::move(w1_indices),
587 std::move(block_size)));
588 }
589}
bool Copy(const T *data_ptr, std::vector< uint16_t > &arr)
Definition BaseLoader.h:344

References onert::loader::Copy(), and onert::ir::TypeInfo::sparsity().

◆ loadStridesAndPaddings()

template<typename LoaderDomain >
template<typename Param , typename OptionsType >
void onert::loader::BaseLoader< LoaderDomain >::loadStridesAndPaddings ( Param &  param,
const OptionsType *  options 
)
protected

Definition at line 617 of file BaseLoader.h.

618{
619 // Strides
620 param.stride.vertical = options->stride_h();
621 param.stride.horizontal = options->stride_w();
622 // Paddings
623 switch (options->padding())
624 {
625 case Padding::Padding_SAME:
626 param.padding.type = ir::PaddingType::SAME;
627 break;
628 case Padding::Padding_VALID:
629 param.padding.type = ir::PaddingType::VALID;
630 break;
631 default:
632 throw std::runtime_error{"Invalid padding type"};
633 }
634 // param paddings indexes unused
635}

References onert::ir::SAME, and onert::ir::VALID.

◆ tensorIdxToOperandIdx()

template<typename LoaderDomain >
ir::OperandIndex onert::loader::BaseLoader< LoaderDomain >::tensorIdxToOperandIdx ( int32_t  tensorIdx)
protected

◆ tensorTypeToDataType()

template<typename LoaderDomain >
virtual ir::DataType onert::loader::BaseLoader< LoaderDomain >::tensorTypeToDataType ( const TensorType  type)
protectedvirtual

Field Documentation

◆ _base

template<typename LoaderDomain >
uint8_t* onert::loader::BaseLoader< LoaderDomain >::_base
protected

Definition at line 186 of file BaseLoader.h.

◆ _buf_to_data

template<typename LoaderDomain >
std::unordered_map<uint32_t , std::shared_ptr<ir::Data> > onert::loader::BaseLoader< LoaderDomain >::_buf_to_data
protected

Definition at line 203 of file BaseLoader.h.

◆ _domain_model

template<typename LoaderDomain >
const Model* onert::loader::BaseLoader< LoaderDomain >::_domain_model
protected

◆ _fd

template<typename LoaderDomain >
int onert::loader::BaseLoader< LoaderDomain >::_fd
protected

Definition at line 190 of file BaseLoader.h.

◆ _model

◆ _pagesize

template<typename LoaderDomain >
int32_t onert::loader::BaseLoader< LoaderDomain >::_pagesize
protected

Definition at line 188 of file BaseLoader.h.

◆ _tensor_names

template<typename LoaderDomain >
std::unordered_map<ir::OperandIndex, std::string> onert::loader::BaseLoader< LoaderDomain >::_tensor_names
protected

Definition at line 196 of file BaseLoader.h.

◆ _tensor_to_operand

template<typename LoaderDomain >
std::vector<ir::OperandIndex> onert::loader::BaseLoader< LoaderDomain >::_tensor_to_operand
protected

Definition at line 195 of file BaseLoader.h.

◆ _use_mmaped_data

template<typename LoaderDomain >
bool onert::loader::BaseLoader< LoaderDomain >::_use_mmaped_data = false
protected

Definition at line 200 of file BaseLoader.h.

Referenced by onert::loader::BaseLoader< LoaderDomain >::BaseLoader().

◆ _verifier

template<typename LoaderDomain >
std::unique_ptr<Verifier> onert::loader::BaseLoader< LoaderDomain >::_verifier
protected

Definition at line 198 of file BaseLoader.h.


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