1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_UTILS_SPARSITY_FORMAT_CONVERTER_H_ 16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_UTILS_SPARSITY_FORMAT_CONVERTER_H_ 17 18 #include <vector> 19 20 #include "Eigen/Core" 21 #include "tensorflow/lite/c/common.h" 22 23 namespace tflite { 24 namespace internal { 25 namespace sparsity { 26 27 // A converter that keeps an internal representation of sparse tensor parameters 28 // and converts tensors between dense and sparse formats. 29 template <typename T> 30 class FormatConverter { 31 public: 32 /* 33 * Creates a dense to sparse converter. 34 * @param shape Shape of the dense tensor. 35 * @param traversal_order In what order to traverse all dimensions, 36 * including block dimensions. 37 * @param format Whether each dimension in the dense tensor is 38 * dense or sparse (not in the traversal order). 39 * @param block_size Size of each block dimension. 40 * @param block_map Map from block dimension to original tensor 41 * dimension. 42 */ 43 FormatConverter(const std::vector<int>& shape, 44 const std::vector<int>& traversal_order, 45 const std::vector<TfLiteDimensionType>& format, 46 const std::vector<int>& block_size = {}, 47 const std::vector<int>& block_map = {}); 48 49 /* 50 * Creates a sparse to dense converter. 51 * @param shape Shape of the target dense tensor. 52 * @param traversal_order In what order to traverse all dimensions, 53 * including block dimensions. 54 * @param format Whether each dimension in the dense tensor is 55 * dense or sparse (not in the traversal order). 56 * @param dense_size Size of each dense dimension in the sparse tensor. 57 * Should be 0 for sparse dimensions. 58 * @param segments Segments of each dimension in the sparse tensor. 59 * Should be empty for dense dimensions. 60 * @param indices Indices in the dense tensor for each dimension. 61 * Should be empty for dense dimensions. 62 * @param block_map Map from block dimension to original tensor 63 * dimension. 64 */ 65 FormatConverter(const std::vector<int>& shape, 66 const std::vector<int>& traversal_order, 67 const std::vector<TfLiteDimensionType>& format, 68 const std::vector<int>& dense_size, 69 const std::vector<std::vector<int>>& segments, 70 const std::vector<std::vector<int>>& indices, 71 const std::vector<int>& block_map = {}); 72 73 /* Creates a sparse to dense converter. 74 * @param shape Shape of the target dense tensor. 75 * @param sparsity Sparsity parameter of the sparse TfLiteTensor. 76 */ 77 FormatConverter(const std::vector<int>& shape, 78 const TfLiteSparsity& sparsity); 79 GetData()80 const std::vector<T>& GetData() { return data_; } GetDimMetadata()81 const std::vector<std::vector<int>>& GetDimMetadata() { 82 return dim_metadata_; 83 } 84 85 // Method for dense to sparse conversion. Need to call GetData() method to get 86 // the compressed data. 87 TfLiteStatus DenseToSparse(const T* src_data); 88 89 // Method for sparse to dense conversion. Need to call GetData() method to get 90 // the decompressed data. 91 TfLiteStatus SparseToDense(const T* src_data); 92 // Method for sparse to dense conversion with caller provided buffer. No need 93 // to call GetData() with this method. 94 TfLiteStatus SparseToDense(const T* src_data, const size_t dest_size, 95 T* dest_data, TfLiteContext* context = nullptr); 96 97 private: 98 // Helper function for initializing this converter for sparse to dense 99 // conversion. 100 void InitSparseToDenseConverter(std::vector<int> shape, 101 std::vector<int> traversal_order, 102 std::vector<TfLiteDimensionType> format, 103 std::vector<int> dense_size, 104 std::vector<std::vector<int>> segments, 105 std::vector<std::vector<int>> indices, 106 std::vector<int> block_map); 107 108 // A recursive function to fetch data from the compressed src_data buffer and 109 // populate the dense buffer. 110 void Populate(const T* src_data, std::vector<int> indices, int level, 111 int prev_idx, int* src_data_ptr, T* dest_data); 112 113 // Check if val is equal to zero. 114 bool IsZero(const T val); 115 116 // Shape of the conceptual dense tensor. 117 std::vector<int> dense_shape_; 118 // Shape of the dense tensor with inner blocks reduced. For example, a (4, 4) 119 // tensor with (2, 2) block has blocked_shape (2, 2). 120 std::vector<int> blocked_shape_; 121 // Total number of elements in the dense tensor. 122 size_t dense_size_; 123 // Has n(original dimension)+k(block_dimension) elements. 124 std::vector<int> traversal_order_; 125 // Format of each dimension in the traversal order. 126 std::vector<TfLiteDimensionType> format_; 127 // Size of each block dimension, in the same order as block map. 128 std::vector<int> block_size_; 129 // Map from block dimension to the original tensor dimension. 130 std::vector<int> block_map_; 131 // Metadata of each dimension in the traversal order. 132 // Each dimension needs two vectors. For dense dimensions, the first vector 133 // stores the size of that dimension, and the second vector is empty. For 134 // sparse dimensions, the first vector stores the segments and the second one 135 // stores the indices. 136 std::vector<std::vector<int>> dim_metadata_; 137 // Actual buffer holding data after conversion. Could be sparse buffer or 138 // dense buffer. 139 std::vector<T> data_; 140 }; 141 142 extern template class FormatConverter<int32_t>; 143 extern template class FormatConverter<int8_t>; 144 extern template class FormatConverter<float>; 145 extern template class FormatConverter<Eigen::half>; 146 } // namespace sparsity 147 } // namespace internal 148 } // namespace tflite 149 150 #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_UTILS_SPARSITY_FORMAT_CONVERTER_H_ 151