ONE - On-device Neural Engine
Loading...
Searching...
No Matches
luci::SparsifyTensorPass Struct Referencefinal

Pass to sparsify tensor. More...

#include <SparsifyTensorPass.h>

Collaboration diagram for luci::SparsifyTensorPass:

Public Member Functions

 SparsifyTensorPass (const std::string &tensor_name, const std::vector< int32_t > &traversal_order, const std::vector< DimensionType > &format, const std::vector< int32_t > &block_size, const std::vector< int32_t > &block_map)
 
const char * name (void) const final
 
bool run (loco::Graph *g) final
 Run the pass.
 
template<loco::DataType DT>
void sparsify_tensor (luci::CircleConst *cop)
 
- Public Member Functions inherited from logo::Pass
virtual ~Pass ()=default
 

Detailed Description

Pass to sparsify tensor.

Definition at line 32 of file SparsifyTensorPass.h.

Constructor & Destructor Documentation

◆ SparsifyTensorPass()

luci::SparsifyTensorPass::SparsifyTensorPass ( const std::string &  tensor_name,
const std::vector< int32_t > &  traversal_order,
const std::vector< DimensionType > &  format,
const std::vector< int32_t > &  block_size,
const std::vector< int32_t > &  block_map 
)
inline

Definition at line 35 of file SparsifyTensorPass.h.

38 : _tensor_name{tensor_name}, _traversal_order{traversal_order}, _format{format},
39 _block_size{block_size}, _block_map{block_map}
40 {
41 // DO NOTHING
42 }
const char * tensor_name(const circle::Tensor *tensor)

Member Function Documentation

◆ name()

const char * luci::SparsifyTensorPass::name ( void  ) const
inlinefinalvirtual

Reimplemented from logo::Pass.

Definition at line 45 of file SparsifyTensorPass.h.

45{ return "luci::SparsifyTensorPass"; }

◆ run()

bool luci::SparsifyTensorPass::run ( loco::Graph graph)
finalvirtual

Run the pass.

Returns
false if there was nothing changed

Implements logo::Pass.

Definition at line 87 of file SparsifyTensorPass.cpp.

88{
89 bool changed = false;
90 for (auto node : loco::active_nodes(loco::output_nodes(g)))
91 {
92 auto cop = dynamic_cast<luci::CircleConst *>(node);
93 if (not cop)
94 continue;
95
96 if (cop->name() != _tensor_name)
97 continue;
98
99 switch (cop->dtype())
100 {
101 case loco::DataType::S32:
103 break;
104 case loco::DataType::S8:
106 break;
107 case loco::DataType::FLOAT32:
109 break;
110 default:
111 throw std::runtime_error("SparsifyTensorPass: Unsupported dtype.");
112 }
113 changed = true;
114 }
115
116 return changed;
117}
Class to build tensor data.
Definition CircleConst.h:35
std::set< loco::Node * > active_nodes(const std::vector< loco::Node * > &roots)
Enumerate all the nodes required to compute "roots".
std::vector< Node * > output_nodes(Graph *)
Definition Graph.cpp:101
template void SparsifyTensorPass::sparsify_tensor< loco::DataType::S32 >(luci::CircleConst *cop)
template void SparsifyTensorPass::sparsify_tensor< loco::DataType::FLOAT32 >(luci::CircleConst *cop)
template void SparsifyTensorPass::sparsify_tensor< loco::DataType::S8 >(luci::CircleConst *cop)

References loco::active_nodes(), loco::output_nodes(), luci::SparsifyTensorPass::sparsify_tensor< loco::DataType::FLOAT32 >(), luci::SparsifyTensorPass::sparsify_tensor< loco::DataType::S32 >(), and luci::SparsifyTensorPass::sparsify_tensor< loco::DataType::S8 >().

Referenced by package.infer.session::inference(), and luci::CircleOptimizer::sparsify().

◆ sparsify_tensor()

template<loco::DataType DT>
template void luci::SparsifyTensorPass::sparsify_tensor< loco::DataType::FLOAT32 > ( luci::CircleConst cop)

Definition at line 26 of file SparsifyTensorPass.cpp.

27{
28 using PRIMITIVE_DTYPE = typename loco::DataTypeImpl<DT>::Type;
29
30 std::vector<int32_t> dense_tensor_shape(cop->rank());
31 for (uint32_t d = 0; d < cop->rank(); d++)
32 {
33 dense_tensor_shape.at(d) = cop->dim(d).value();
34 }
35
36 Sparsifier<PRIMITIVE_DTYPE> sparsifier(dense_tensor_shape, _traversal_order, _format, _block_size,
37 _block_map);
38 // get dense tensor data
39 uint32_t dense_tensor_data_size = cop->size<DT>();
40 std::vector<PRIMITIVE_DTYPE> dense_tensor_data(dense_tensor_data_size);
41 for (uint32_t i = 0; i < dense_tensor_data_size; i++)
42 {
43 dense_tensor_data.at(i) = cop->at<DT>(i);
44 }
45 // sparsify
46 sparsifier.DenseToSparse(dense_tensor_data.data());
47 // get sparse tensor data
48 std::vector<PRIMITIVE_DTYPE> sparse_tensor_data = sparsifier.GetData();
49 uint32_t sparse_tensor_data_size = sparse_tensor_data.size();
50 cop->size<DT>(sparse_tensor_data_size);
51 for (uint32_t i = 0; i < sparse_tensor_data_size; i++)
52 {
53 cop->at<DT>(i) = sparse_tensor_data.at(i);
54 }
55 // make sparsity parameter
56 auto sparsityparam = std::make_unique<SparsityParam>();
57 sparsityparam->traversal_order = _traversal_order;
58 sparsityparam->block_map = _block_map;
59 // get dimension meta data
60 const auto dim_metadata = sparsifier.GetDimMetadata();
61 for (uint32_t idx = 0; idx < _format.size(); idx++)
62 {
63 if (_format.at(idx) == DimensionType::DENSE)
64 {
65 sparsityparam->dim_metadata.emplace_back(DimensionType::DENSE,
66 dim_metadata.at(idx * 2).at(0));
67 }
68 // TODO Set SparseIndexVectorType according to its data range
69 else if (_format.at(idx) == DimensionType::SPARSE_CSR)
70 {
71 sparsityparam->dim_metadata.emplace_back(
72 DimensionType::SPARSE_CSR, /* dense size */ 0,
73 /* array_segments */
74 SparseIndexVector{SparseIndexVectorType::U16, dim_metadata.at(idx * 2)},
75 /* array_indices */
76 SparseIndexVector{SparseIndexVectorType::U16, dim_metadata.at(idx * 2 + 1)});
77 }
78 }
79 for (uint32_t i = 0; i < _block_size.size(); i++)
80 {
81 assert(_block_size.at(i) == dim_metadata.at((_format.size() + i) * 2).at(0));
82 sparsityparam->dim_metadata.emplace_back(DimensionType::DENSE, _block_size.at(i));
83 }
84 cop->sparsityparam(std::move(sparsityparam));
85}
const loco::DataTypeImpl< DT >::Type & at(uint32_t n) const
uint32_t size(void) const
@ SPARSE_CSR
C++ scalar type corresponding to each DataType.
SparsityParam * sparsityparam(void) const

References luci::CircleConst::at(), luci::DENSE, luci::Sparsifier< T >::DenseToSparse(), luci::Sparsifier< T >::GetData(), luci::Sparsifier< T >::GetDimMetadata(), luci::CircleConst::size(), luci::SPARSE_CSR, luci::CircleNode::sparsityparam(), and luci::U16.


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