2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2020 The TensorFlow Authors. All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 tflite::Padding as_tflite_padding(const tflchef::Padding &value)
27 return tflite::Padding_SAME;
29 return tflite::Padding_VALID;
34 throw std::runtime_error{"Unknown padding value"};
37 tflite::ActivationFunctionType as_tflite_activation(const tflchef::Activation &value)
42 return tflite::ActivationFunctionType_NONE;
44 return tflite::ActivationFunctionType_RELU;
45 case tflchef::RELU_N1_TO_1:
46 return tflite::ActivationFunctionType_RELU_N1_TO_1;
48 return tflite::ActivationFunctionType_RELU6;
50 return tflite::ActivationFunctionType_TANH;
51 case tflchef::SIGN_BIT:
52 return tflite::ActivationFunctionType_SIGN_BIT;
57 throw std::runtime_error{"Unknown activation"};
60 tflite::TensorType as_tflite_tensortype(const tflchef::TensorType &value)
64 case tflchef::FLOAT32:
65 return tflite::TensorType_FLOAT32;
66 case tflchef::FLOAT16:
67 return tflite::TensorType_FLOAT16;
69 return tflite::TensorType_INT32;
71 return tflite::TensorType_UINT8;
73 return tflite::TensorType_INT64;
75 return tflite::TensorType_STRING;
77 return tflite::TensorType_BOOL;
79 return tflite::TensorType_INT16;
81 return tflite::TensorType_INT8;
86 throw std::runtime_error{"Unknown tensor type"};
89 tflite::MirrorPadMode as_tflite_mirrorpadmode(const tflchef::MirrorPadMode &value)
93 case tflchef::REFLECT:
94 return tflite::MirrorPadMode_REFLECT;
95 case tflchef::SYMMETRIC:
96 return tflite::MirrorPadMode_SYMMETRIC;
101 throw std::runtime_error{"Unknown mirrorpad mode"};
104 tflite::DimensionType as_tflite_dimensiontype(const tflchef::DimensionType &value)
108 case tflchef::DimensionType::DENSE:
109 return tflite::DimensionType_DENSE;
110 case tflchef::DimensionType::SPARSE_CSR:
111 return tflite::DimensionType_SPARSE_CSR;
116 throw std::runtime_error("Unknown dimension type");
119 tflite::SparseIndexVector as_tflite_sparse_idx_vec_type(const tflchef::SparseIndexVecType &value)
123 case tflchef::SparseIndexVecType::SparseIdxVecType_NONE:
124 return tflite::SparseIndexVector_NONE;
125 case tflchef::SparseIndexVecType::INT32VEC:
126 return tflite::SparseIndexVector_Int32Vector;
127 case tflchef::SparseIndexVecType::UINT16VEC:
128 return tflite::SparseIndexVector_Uint16Vector;
129 case tflchef::SparseIndexVecType::UINT8VEC:
130 return tflite::SparseIndexVector_Uint8Vector;
135 throw std::runtime_error("Unknown SparseIndexVector type");
138 flatbuffers::Offset<void>
139 as_tflite_sparse_index_vec(flatbuffers::FlatBufferBuilder &fb,
140 const ::tflchef::TensorSparsity_IndexVec &value)
142 auto sparse_idx_type = value.type();
144 switch (sparse_idx_type)
146 case tflchef::SparseIndexVecType::SparseIdxVecType_NONE:
147 return flatbuffers::Offset<void>();
148 case tflchef::SparseIndexVecType::INT32VEC:
150 auto values_vec_int32 = std::vector<int32_t>{value.dim().begin(), value.dim().end()};
151 auto values_int32 = fb.CreateVector(values_vec_int32);
152 return tflite::CreateInt32Vector(fb, values_int32).Union();
154 case tflchef::SparseIndexVecType::UINT16VEC:
156 auto values_vec_uint16 = std::vector<uint16_t>{value.dim().begin(), value.dim().end()};
157 auto values_uint16 = fb.CreateVector(values_vec_uint16);
158 return tflite::CreateUint16Vector(fb, values_uint16).Union();
160 case tflchef::SparseIndexVecType::UINT8VEC:
162 auto values_vec_uint8 = std::vector<uint8_t>{value.dim().begin(), value.dim().end()};
163 auto values_uint8 = fb.CreateVector(values_vec_uint8);
164 return tflite::CreateUint8Vector(fb, values_uint8).Union();
170 throw std::runtime_error("Unknown SparseIndexVector type");
173 // namespace sparsity code referenced from
174 // https://github.com/tensorflow/tensorflow/blob/3f878cff5b698b82eea85db2b60d65a2e320850e/
175 // tensorflow/lite/kernels/internal/utils/sparsity_format_converter.cc
180 template <typename T>
181 FormatConverter<T>::FormatConverter(const std::vector<int> &shape,
182 const std::vector<int> &traversal_order,
183 const std::vector<TfLiteDimensionType> &format,
184 const std::vector<int> &block_size,
185 const std::vector<int> &block_map)
186 : dense_shape_(shape), traversal_order_(traversal_order), block_size_(block_size),
187 block_map_(block_map)
191 blocked_shape_.resize(shape.size());
192 format_.resize(shape.size() + block_map.size());
193 for (int i = 0; i < shape.size(); i++)
195 format_[i] = format[traversal_order[i]];
196 dense_size_ *= shape[i];
197 if (block_dim < block_map.size() && block_map[block_dim] == i)
199 blocked_shape_[i] = shape[i] / block_size[block_dim];
204 blocked_shape_[i] = shape[i];
208 // Only dense blocks are supported.
209 for (int i = 0; i < block_map.size(); i++)
211 format_[i + shape.size()] = kTfLiteDimDense;
215 template <typename T> bool FormatConverter<T>::DenseToSparse(const T *src_data)
217 int num_original_dims = dense_shape_.size();
218 int num_block_dims = block_map_.size();
219 int num_expanded_dims = num_original_dims + num_block_dims;
220 std::vector<int> expanded_shape(num_expanded_dims);
221 for (int i = 0; i < num_expanded_dims; i++)
223 if (i < num_original_dims)
225 expanded_shape[i] = blocked_shape_[i];
229 expanded_shape[i] = block_size_[i - num_original_dims];
233 std::vector<int> shape_offset(num_original_dims);
234 shape_offset[shape_offset.size() - 1] = 1;
235 for (int i = num_original_dims - 1; i > 0; --i)
237 shape_offset[i - 1] = shape_offset[i] * dense_shape_[i];
240 std::vector<int> expanded_shape_offset(num_expanded_dims);
241 for (int i = 0; i < num_original_dims; ++i)
243 expanded_shape_offset[i] = shape_offset[i];
245 for (int i = 0; i < num_block_dims; ++i)
247 int mapped_dim = block_map_[i];
248 expanded_shape_offset[num_original_dims + i] = shape_offset[mapped_dim];
249 expanded_shape_offset[mapped_dim] *= block_size_[i];
252 std::vector<int> dst_ordered_offset(num_expanded_dims);
253 for (int i = 0; i < num_expanded_dims; ++i)
255 dst_ordered_offset[i] = expanded_shape_offset[traversal_order_[i]];
258 std::vector<bool> dst_dim_has_nonzeroes(num_expanded_dims);
259 std::fill(dst_dim_has_nonzeroes.begin(), dst_dim_has_nonzeroes.end(), false);
260 std::vector<int> inner_compressed_dim(num_expanded_dims);
261 int most_recent_compressed_dim = -1;
262 std::vector<int> num_segments_of_next_compressed_dim(num_expanded_dims);
263 int segment_count = 1;
264 for (int i = num_expanded_dims - 1; i >= 0; --i)
266 inner_compressed_dim[i] = most_recent_compressed_dim;
267 if (format_[i] == kTfLiteDimSparseCSR)
269 most_recent_compressed_dim = i;
270 num_segments_of_next_compressed_dim[i] = segment_count;
275 num_segments_of_next_compressed_dim[i] = -1;
276 segment_count *= expanded_shape[traversal_order_[i]];
280 dim_metadata_.resize(num_expanded_dims * 2);
281 std::vector<int> dst_sparse_dims;
282 dst_sparse_dims.reserve(num_expanded_dims);
283 for (int i = 0; i < num_expanded_dims; ++i)
285 dim_metadata_[i * 2].clear();
286 dim_metadata_[i * 2 + 1].clear();
287 if (format_[i] == kTfLiteDimDense)
289 // If dimension is dense, just store the shape.
290 dim_metadata_[i * 2].push_back(expanded_shape[traversal_order_[i]]);
294 dim_metadata_[i * 2].push_back(0); // Segment array always begins with 0.
295 dst_sparse_dims.push_back(i); // Add dimension to the sparse list.
299 // This algorithm assumes that the block size is small enough for all the
300 // elements to fit in cache, so the strided accesses from different traversal
301 // order and the write-first-erase-later strategy shouldn't be too slow
302 int dst_dim_idx = num_expanded_dims;
303 std::vector<int> coordinate(num_expanded_dims, 0);
304 int dense_tensor_idx = 0;
305 while (dst_dim_idx >= 0)
307 if (dst_dim_idx == num_expanded_dims)
309 // We have a complete coordinate. Add the element to the value array if it
310 // is not zero, or if the last dimension is dense.
311 if (!IsZero(src_data[dense_tensor_idx]))
313 data_.push_back(src_data[dense_tensor_idx]);
314 // Mark all sparse dimensions that their current indices have nonzeroes.
315 for (auto dst_dim : dst_sparse_dims)
317 if (!dst_dim_has_nonzeroes[dst_dim])
319 // Only add the index to the indices array if the current nonzero
320 // is the first nonzero of the block.
321 dim_metadata_[2 * dst_dim + 1].push_back(coordinate[dst_dim]);
322 dst_dim_has_nonzeroes[dst_dim] = true;
326 else if (format_[num_expanded_dims - 1] == kTfLiteDimDense)
328 data_.push_back(src_data[dense_tensor_idx]);
334 int original_dim_idx = traversal_order_[dst_dim_idx];
335 int dim_size = expanded_shape[original_dim_idx];
336 if (dst_dim_has_nonzeroes[dst_dim_idx])
338 // If the previous block has nonzeroes, reset the flag to false since
339 // we have just moved to a new block.
340 dst_dim_has_nonzeroes[dst_dim_idx] = false;
342 else if (format_[dst_dim_idx] == kTfLiteDimSparseCSR)
344 // This block is empty. Delete unnecessary values if compressed.
345 int next_compressed_dim = inner_compressed_dim[dst_dim_idx];
346 int erase_offset = dim_metadata_[2 * dst_dim_idx + 1].size() *
347 num_segments_of_next_compressed_dim[dst_dim_idx];
348 if (next_compressed_dim >= 0)
350 auto &segments = dim_metadata_[2 * inner_compressed_dim[dst_dim_idx]];
351 segments.erase(segments.begin() + 1 + erase_offset, segments.end());
355 data_.erase(data_.begin() + erase_offset, data_.end());
358 if (++coordinate[dst_dim_idx] < dim_size)
360 // The current dst_dim_idx is valid (not out of bound).
361 dense_tensor_idx += dst_ordered_offset[dst_dim_idx];
366 // dst_dim_idx has reached its dim size. Update segment array and go
367 // back to incrementing the previous dimension (dst_dim_idx - 1).
368 if (format_[dst_dim_idx] == kTfLiteDimSparseCSR)
370 dim_metadata_[2 * dst_dim_idx].push_back(dim_metadata_[2 * dst_dim_idx + 1].size());
372 coordinate[dst_dim_idx] = -1;
373 dense_tensor_idx -= dst_ordered_offset[dst_dim_idx] * dim_size;
382 template <typename T> bool FormatConverter<T>::IsZero(const T val)
384 return (val == static_cast<T>(0));
387 template class FormatConverter<float>;
388 template class FormatConverter<uint16_t>; // float16
390 } // namespace sparsity