ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Convert.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2020 The TensorFlow Authors. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
22#ifndef __CONVERT_H__
23#define __CONVERT_H__
24
25#include <tflchef.pb.h>
26#include <mio/tflite/schema_generated.h>
27
28tflite::Padding as_tflite_padding(const tflchef::Padding &value);
29tflite::ActivationFunctionType as_tflite_activation(const tflchef::Activation &value);
30tflite::TensorType as_tflite_tensortype(const tflchef::TensorType &value);
31tflite::MirrorPadMode as_tflite_mirrorpadmode(const tflchef::MirrorPadMode &value);
32tflite::DimensionType as_tflite_dimensiontype(const tflchef::DimensionType &value);
33tflite::SparseIndexVector as_tflite_sparse_idx_vec_type(const tflchef::SparseIndexVecType &value);
36 const ::tflchef::TensorSparsity_IndexVec &value);
37
38// codes under namespace sparsity referenced from
39// https://github.com/tensorflow/tensorflow/blob/3f878cff5b698b82eea85db2b60d65a2e320850e/
40// tensorflow/lite/kernels/internal/utils/sparsity_format_converter.h
41// tensorflow/lite/kernels/internal/utils/sparsity_format_converter.cc
42
43namespace sparsity
44{
45
46// Storage format of each dimension in a sparse tensor.
52
53template <typename T> class FormatConverter
54{
55public:
56 FormatConverter(const std::vector<int32_t> &shape, const std::vector<int32_t> &traversal_order,
57 const std::vector<TfLiteDimensionType> &format,
58 const std::vector<int32_t> &block_size = {},
59 const std::vector<int32_t> &block_map = {});
60
61 bool DenseToSparse(const T *src_data);
62
63 const std::vector<T> &GetData() { return data_; }
64 const std::vector<std::vector<int32_t>> &GetDimMetadata() { return dim_metadata_; }
65
66private:
67 bool IsZero(const T val);
68
69private:
70 std::vector<int32_t> dense_shape_;
71 std::vector<int32_t> blocked_shape_;
72 size_t dense_size_;
73 std::vector<int32_t> traversal_order_;
74 std::vector<TfLiteDimensionType> format_;
75 std::vector<int32_t> block_size_;
76 std::vector<int32_t> block_map_;
77 std::vector<std::vector<int32_t>> dim_metadata_;
78 std::vector<T> data_;
79};
80
81extern template class FormatConverter<float>;
82extern template class FormatConverter<uint16_t>; // float16
83
84} // namespace sparsity
85
86#endif // __CONVERT_H__
Helper class to hold data needed in creation of a FlatBuffer. To serialize data, you typically call o...
const std::vector< std::vector< int32_t > > & GetDimMetadata()
Definition Convert.h:64
const std::vector< T > & GetData()
Definition Convert.h:63
bool DenseToSparse(const T *src_data)
Definition Convert.cpp:217
FormatConverter(const std::vector< int32_t > &shape, const std::vector< int32_t > &traversal_order, const std::vector< TfLiteDimensionType > &format, const std::vector< int32_t > &block_size={}, const std::vector< int32_t > &block_map={})
tflite::ActivationFunctionType as_tflite_activation(const tflchef::Activation &value)
Definition Convert.cpp:37
tflite::MirrorPadMode as_tflite_mirrorpadmode(const tflchef::MirrorPadMode &value)
Definition Convert.cpp:91
tflite::DimensionType as_tflite_dimensiontype(const tflchef::DimensionType &value)
Definition Convert.cpp:106
tflite::SparseIndexVector as_tflite_sparse_idx_vec_type(const tflchef::SparseIndexVecType &value)
Definition Convert.cpp:121
flatbuffers::Offset< void > as_tflite_sparse_index_vec(flatbuffers::FlatBufferBuilder &fb, const ::tflchef::TensorSparsity_IndexVec &value)
Definition Convert.cpp:141
tflite::Padding as_tflite_padding(const tflchef::Padding &value)
Definition Convert.cpp:22
tflite::TensorType as_tflite_tensortype(const tflchef::TensorType &value)
Definition Convert.cpp:60