ONE - On-device Neural Engine
Loading...
Searching...
No Matches
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 42 of file BaseLoader.h.

Member Typedef Documentation

◆ ActivationFunctionType

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

Definition at line 46 of file BaseLoader.h.

◆ Buffer

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

Definition at line 47 of file BaseLoader.h.

◆ BuiltinOperator

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

Definition at line 48 of file BaseLoader.h.

◆ CustomOptionsFormat

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

Definition at line 49 of file BaseLoader.h.

◆ DimensionType

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

Definition at line 58 of file BaseLoader.h.

◆ Metadata

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

Definition at line 50 of file BaseLoader.h.

◆ Model

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

Definition at line 51 of file BaseLoader.h.

◆ Operator

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

Definition at line 52 of file BaseLoader.h.

◆ Padding

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

Definition at line 53 of file BaseLoader.h.

◆ Pool2DOptions

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

Definition at line 54 of file BaseLoader.h.

◆ SparseIndexVector

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

Definition at line 59 of file BaseLoader.h.

◆ SubGraph

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

Definition at line 55 of file BaseLoader.h.

◆ Tensor

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

Definition at line 56 of file BaseLoader.h.

◆ TensorType

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

Definition at line 57 of file BaseLoader.h.

◆ Verifier

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

Definition at line 45 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 71 of file BaseLoader.h.

72 : _base{nullptr}, _pagesize(getpagesize()), _fd(-1), _model(model), _domain_model{nullptr}
73 {
74 _use_mmaped_data = util::getConfigBool(util::config::USE_MMAPED_DATA);
75 }
std::unique_ptr< ir::Model > & _model
Definition BaseLoader.h:194
const Model * _domain_model
Definition BaseLoader.h:195
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 115 of file BaseLoader.h.

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

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 62 of file BaseLoader.h.

62{ 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 1750 of file BaseLoader.h.

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

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 363 of file BaseLoader.h.

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

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

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 593 of file BaseLoader.h.

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

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

◆ loadQuantization()

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

Definition at line 458 of file BaseLoader.h.

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

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 497 of file BaseLoader.h.

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

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 618 of file BaseLoader.h.

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

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 188 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 205 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 192 of file BaseLoader.h.

◆ _model

◆ _pagesize

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

Definition at line 190 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 198 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 197 of file BaseLoader.h.

◆ _use_mmaped_data

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

Definition at line 202 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 200 of file BaseLoader.h.


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