ONE - On-device Neural Engine
Loading...
Searching...
No Matches
mir Namespace Reference

Namespaces

namespace  ops
 

Data Structures

class  AffineQuantization
 
struct  AvgPool2DOpAttributes
 
struct  Conv2DOpAttributes
 
struct  Deconv2DOpAttributes
 
struct  DotEdge
 
class  DotGraph
 
struct  DotNode
 
class  DotNodeBuilder
 
class  ExternalRegion
 
class  Graph
 
class  GraphPatternMatcher
 
class  Index
 
class  IVisitor
 Interface for visitors Use in MIR component if you want to enforce to implement visits for all operations. More...
 
struct  MaxPool2DOpAttributes
 
class  Operation
 
struct  PadOpAttributes
 
struct  Region
 
class  Shape
 
class  ShapeIter
 
class  ShapeRange
 
class  Tensor
 
class  TensorType
 
class  TensorVariant
 
class  Visitor
 Base visitor with empty fallback function. More...
 

Enumerations

enum class  DataFormat { NCHW , NHWC }
 
enum class  DataType {
  UNKNOWN , FLOAT32 , FLOAT64 , INT32 ,
  INT64 , UINT8
}
 

Functions

constexpr std::size_t wrap_index (std::int32_t index, std::size_t limit) noexcept
 
int getDataBatchDimIndex (DataFormat data_format)
 
int getDataChannelDimIndex (DataFormat data_format)
 
int getDataSpatialDimIndex (DataFormat data_format, int dim)
 
std::string toString (DataFormat data_format)
 
std::size_t getDataTypeSize (DataType type)
 
std::vector< Operation * > getSortedNodes (Graph *graph)
 Returns nodes of the graph sorted topologically.
 
std::ostream & operator<< (std::ostream &s, const Index &idx)
 
void dumpGraph (const Graph *graph, std::ostream &stream)
 
const std::string & getTypeName (Operation::Type type)
 
Shape broadcastShapes (const Shape &lhs_shape, const Shape &rhs_shape)
 
std::string toString (const Shape &shape)
 
template<int32_t... Ints>
Shape transposeShape (const Shape &shape)
 
template<unsigned int... Ints>
TensorVariant transposeTensor (const TensorVariant &tensor)
 
std::ostream & operator<< (std::ostream &stream, const DotGraph &graph)
 

Variables

constexpr std::size_t MAX_DIMENSION_COUNT = 8
 maximum number of dimensions what an Index, Shape or Tensor can have
 

Enumeration Type Documentation

◆ DataFormat

enum class mir::DataFormat
strong
Enumerator
NCHW 
NHWC 

Definition at line 26 of file DataFormat.h.

27{
28 NCHW,
29 NHWC
30};

◆ DataType

enum class mir::DataType
strong
Enumerator
UNKNOWN 
FLOAT32 
FLOAT64 
INT32 
INT64 
UINT8 

Definition at line 26 of file DataType.h.

27{
28 UNKNOWN,
29 FLOAT32,
30 FLOAT64,
31 INT32,
32 INT64,
33 UINT8
34};

Function Documentation

◆ broadcastShapes()

Shape mir::broadcastShapes ( const Shape lhs_shape,
const Shape rhs_shape 
)

Definition at line 43 of file Shape.cpp.

44{
45 const int num_dims = std::max(lhs_shape.rank(), rhs_shape.rank());
46 Shape result_shape(num_dims);
47
48 for (int i = 0; i < num_dims; ++i)
49 {
50 const std::int32_t lhs_dim =
51 (i >= num_dims - lhs_shape.rank()) ? lhs_shape.dim(i - (num_dims - lhs_shape.rank())) : 1;
52 const std::int32_t rhs_dim =
53 (i >= num_dims - rhs_shape.rank()) ? rhs_shape.dim(i - (num_dims - rhs_shape.rank())) : 1;
54 if (lhs_dim == 1)
55 {
56 result_shape.dim(i) = rhs_dim;
57 }
58 else
59 {
60 assert(rhs_dim == 1 || rhs_dim == lhs_dim);
61 result_shape.dim(i) = lhs_dim;
62 }
63 }
64
65 return result_shape;
66}
int32_t & dim(int32_t axis) noexcept
Definition Shape.h:47
int32_t rank() const
Definition Shape.h:43

References mir::Shape::dim(), and mir::Shape::rank().

Referenced by TEST().

◆ dumpGraph()

void mir::dumpGraph ( const Graph graph,
std::ostream &  stream 
)

Definition at line 25 of file IrDotDumper.cpp.

26{
27 DotGraph dot_graph;
28
29 for (const auto *node : graph->getNodes())
30 {
31 dot_graph.addNode(DotNodeBuilder(*node).getDotNode());
32 for (const Operation::Output *input : node->getInputs())
33 {
34 dot_graph.addEdge({input->getNode()->getId(), node->getId()});
35 }
36 }
37
38 stream << dot_graph;
39}
void addNode(DotNode node)
Definition DotGraph.cpp:22
void addEdge(DotEdge edge)
Definition DotGraph.cpp:24
DotNode getDotNode() const

References mir::DotGraph::addEdge(), mir::DotGraph::addNode(), and mir::DotNodeBuilder::getDotNode().

Referenced by main(), and nnc::DumperPass::run().

◆ getDataBatchDimIndex()

int mir::getDataBatchDimIndex ( DataFormat  data_format)
inline

Definition at line 32 of file DataFormat.h.

33{
34 switch (data_format)
35 {
36 case DataFormat::NCHW:
37 case DataFormat::NHWC:
38 return 0;
39 default:
40 assert(false);
41 return -1; // Dummy value to silence compiler warning.
42 }
43}

References NCHW, and NHWC.

◆ getDataChannelDimIndex()

int mir::getDataChannelDimIndex ( DataFormat  data_format)
inline

Definition at line 45 of file DataFormat.h.

46{
47 switch (data_format)
48 {
49 case DataFormat::NCHW:
50 return 1;
51 case DataFormat::NHWC:
52 return 3;
53 default:
54 assert(false);
55 return -1; // Dummy value to silene compiler warning.
56 }
57}

References NCHW, and NHWC.

◆ getDataSpatialDimIndex()

int mir::getDataSpatialDimIndex ( DataFormat  data_format,
int  dim 
)
inline

Definition at line 59 of file DataFormat.h.

60{
61 assert(dim >= 0 && dim <= 1);
62 switch (data_format)
63 {
64 case DataFormat::NCHW:
65 return 2 + dim;
66 case DataFormat::NHWC:
67 return 1 + dim;
68 default:
69 assert(false);
70 return -1; // Dummy value to silence compiler warning.
71 }
72}

References NCHW, and NHWC.

◆ getDataTypeSize()

std::size_t mir::getDataTypeSize ( DataType  type)
inline

Definition at line 36 of file DataType.h.

37{
38 switch (type)
39 {
40 case DataType::FLOAT32:
41 return sizeof(float);
42 case DataType::FLOAT64:
43 return sizeof(double);
44 case DataType::INT32:
45 return sizeof(int32_t);
46 case DataType::INT64:
47 return sizeof(int64_t);
48 case DataType::UINT8:
49 return sizeof(uint8_t);
50 default:
51 assert(false);
52 return 0;
53 }
54}

Referenced by mir::TensorVariant::TensorVariant().

◆ getSortedNodes()

std::vector< Operation * > mir::getSortedNodes ( Graph graph)

Returns nodes of the graph sorted topologically.

Note
Sorting order priority 1) Graph input node (input index order) 2) Constant node (unordered - cannot predict order) 3) Ready node (unordered - cannot predict order)

Definition at line 42 of file Graph.cpp.

43{
44 std::deque<Operation *> ready_nodes;
45 std::unordered_map<Operation *, std::size_t> num_visited_input_edges;
46
47 // Use input vector first to maintain correct input order
48 for (Operation *op : graph->getInputs())
49 {
50 ready_nodes.push_back(op);
51 }
52
53 for (Operation *op : graph->getNodes())
54 {
55 // Skip already pushed input node
56 if ((op->getNumInputs() == 0) && (op->getType() != Operation::Type::input))
57 {
58 ready_nodes.push_back(op);
59 }
60 }
61
62 std::vector<Operation *> sorted_nodes;
63 while (!ready_nodes.empty())
64 {
65 Operation *src_node = ready_nodes.front();
66 ready_nodes.pop_front();
67 sorted_nodes.push_back(src_node);
68 for (Operation::Output &output : src_node->getOutputs())
69 {
70 for (const auto use : output.getUses())
71 {
72 Operation *dst_node = use.getNode();
73 if (++num_visited_input_edges[dst_node] == dst_node->getNumInputs())
74 {
75 ready_nodes.push_back(dst_node);
76 }
77 }
78 }
79 }
80
81 return sorted_nodes;
82}

References mir::Operation::getNumInputs(), and mir::Operation::getOutputs().

Referenced by mir::Graph::accept(), and nnc::DeadCodeElimination::run().

◆ getTypeName()

const std::string & mir::getTypeName ( Operation::Type  type)
Returns
the opcode of operation in string format, like "Add", "Conv2d", etc.

Definition at line 71 of file Operation.cpp.

72{
73 switch (type)
74 {
75#define HANDLE_OP(OpType, OpClass) \
76 case Operation::Type::OpType: \
77 { \
78 static const std::string name(#OpType); \
79 return name; \
80 }
81#include "mir/Operations.inc"
82#undef HANDLE_OP
83 }
84 throw std::runtime_error("unexpected opcode");
85}

Referenced by mir::DotNodeBuilder::DotNodeBuilder().

◆ operator<<() [1/2]

std::ostream & mir::operator<< ( std::ostream &  s,
const Index idx 
)

Definition at line 36 of file Index.cpp.

37{
38 s << "[ ";
39 for (int32_t i = 0; i < idx.rank(); ++i)
40 {
41 if (i != 0)
42 s << ", ";
43 s << idx.at(i);
44 }
45 s << "]";
46
47 return s;
48}
int32_t rank() const
Definition Index.h:43
int32_t & at(int32_t axis)
return position on given axis
Definition Index.h:64

References mir::Index::at(), and mir::Index::rank().

◆ operator<<() [2/2]

std::ostream & mir::operator<< ( std::ostream &  stream,
const DotGraph graph 
)

Definition at line 26 of file DotGraph.cpp.

27{
28 stream << "digraph D {" << std::endl;
29 for (const auto &node : graph._nodes)
30 {
31 stream << node.id << " [shape=record label=\"" << node.label << "\"];" << std::endl;
32 }
33 for (const auto &edge : graph._edges)
34 {
35 stream << edge.src_id << " -> " << edge.dst_id << ";" << std::endl;
36 }
37 stream << "}" << std::endl;
38 return stream;
39}

◆ toString() [1/2]

std::string mir::toString ( const Shape shape)

Definition at line 68 of file Shape.cpp.

69{
70 std::stringstream ss;
71
72 ss << "[";
73 for (int32_t axis = 0; axis < shape.rank(); ++axis)
74 {
75 if (axis != 0)
76 ss << ", ";
77 if (shape.dim(axis) == Shape::autoDim)
78 ss << "AUTO";
79 else
80 ss << shape.dim(axis);
81 }
82 ss << "]";
83
84 return ss.str();
85}

References mir::Shape::dim(), and mir::Shape::rank().

◆ toString() [2/2]

std::string mir::toString ( DataFormat  data_format)
inline

Definition at line 74 of file DataFormat.h.

75{
76 switch (data_format)
77 {
78 case DataFormat::NCHW:
79 return "NCHW";
80 case DataFormat::NHWC:
81 return "NHWC";
82 default:
83 assert(false);
84 return ""; // Dummy value to silence compiler warning.
85 }
86}

References NCHW, and NHWC.

Referenced by mir::DotNodeBuilder::DotNodeBuilder(), mir::DotNodeBuilder::visit(), mir::DotNodeBuilder::visit(), mir::DotNodeBuilder::visit(), mir::DotNodeBuilder::visit(), mir::DotNodeBuilder::visit(), mir::DotNodeBuilder::visit(), mir::DotNodeBuilder::visit(), mir::DotNodeBuilder::visit(), mir::DotNodeBuilder::visit(), and mir::DotNodeBuilder::visit().

◆ transposeShape()

template<int32_t... Ints>
Shape mir::transposeShape ( const Shape shape)

Definition at line 29 of file TensorUtil.h.

30{
31 assert(sizeof...(Ints) == shape.rank());
32 return {shape.dim(Ints)...};
33}

References mir::Shape::dim(), and mir::Shape::rank().

◆ transposeTensor()

template<unsigned int... Ints>
TensorVariant mir::transposeTensor ( const TensorVariant tensor)

Definition at line 35 of file TensorUtil.h.

36{
37 const auto &shape = tensor.getShape();
38 Shape transposed_shape{shape.dim(Ints)...};
39
40 auto elem_type = tensor.getElementType();
41 auto elem_size = tensor.getElementSize();
42 TensorType transposed_type(elem_type, transposed_shape);
43 if (tensor.getType().isQuantized())
44 transposed_type.setQuantization(tensor.getType().getQuantization());
45
46 TensorVariant transposed_tensor(transposed_type);
47
48 for (const auto &index : ShapeRange(shape))
49 {
50 Index transposed_index{index.at(Ints)...};
51 std::memcpy(transposed_tensor.at(transposed_index), tensor.at(index), elem_size);
52 }
53
54 return transposed_tensor;
55}

References mir::TensorVariant::at(), mir::Shape::dim(), and mir::TensorType::setQuantization().

◆ wrap_index()

constexpr std::size_t mir::wrap_index ( std::int32_t  index,
std::size_t  limit 
)
inlineconstexprnoexcept

Definition at line 30 of file Common.h.

31{
32 return static_cast<std::size_t>(index >= 0 ? index : limit + index);
33}

Referenced by mir::Index::at(), mir::Index::at(), mir::Shape::dim(), and mir::Shape::dim().

Variable Documentation

◆ MAX_DIMENSION_COUNT

constexpr std::size_t mir::MAX_DIMENSION_COUNT = 8
constexpr

maximum number of dimensions what an Index, Shape or Tensor can have

Definition at line 28 of file Common.h.