MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / CompressRepeatedField

Function CompressRepeatedField

tensorflow/core/framework/tensor_util.cc:269–310  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

267
268template <typename T>
269bool CompressRepeatedField(float min_compression_ratio,
270 const TensorShape& shape, TensorProto* tensor) {
271 using TypeHelper = internal::TensorProtoHelper<T>;
272 using FieldType = typename internal::TensorProtoHelper<T>::FieldType;
273 const int64 num_tensor_values = shape.num_elements();
274 // Notice that for complex types the tensor is stored as an array of up to
275 // 2 * num_tensor_values real values (real and imaginary parts), possibly
276 // truncated.
277 const int64 num_proto_values = TypeHelper::NumValues(*tensor);
278 if (num_proto_values != num_tensor_values) {
279 // Already compressed or invalid.
280 return false;
281 }
282 const T last_value = TypeHelper::GetValue(num_proto_values - 1, *tensor);
283 int64 last_index = 0;
284 for (int64 i = num_proto_values - 2; i >= 0 && last_index == 0; --i) {
285 const T cur_value = TypeHelper::GetValue(i, *tensor);
286 if (PackedValuesNotEqual(cur_value, last_value)) {
287 last_index = i + 1;
288 }
289 }
290 const int64 num_truncated_proto_values = last_index + 1;
291 const int64 num_bytes_as_field =
292 num_truncated_proto_values * sizeof(FieldType);
293 const int64 num_bytes_as_tensor_content = num_tensor_values * sizeof(T);
294 const int64 num_bytes_before = num_proto_values * sizeof(FieldType);
295 if (std::min(num_bytes_as_field, num_bytes_as_tensor_content) >
296 static_cast<int64>(num_bytes_before / min_compression_ratio)) {
297 return false;
298 }
299 if (num_bytes_as_field <= num_bytes_as_tensor_content) {
300 TypeHelper::Truncate(num_truncated_proto_values, tensor);
301 } else {
302 gtl::InlinedVector<T, 64> tmp(num_tensor_values);
303 TypeHelper::CopyValues(tmp.begin(), *tensor);
304 TypeHelper::Truncate(0, tensor);
305 port::CopyFromArray(tensor->mutable_tensor_content(),
306 reinterpret_cast<const char*>(tmp.data()),
307 num_bytes_as_tensor_content);
308 }
309 return true;
310}
311
312template <typename T>
313bool CompressTensorProtoInPlaceImpl(int64 min_num_elements,

Callers

nothing calls this directly

Calls 7

CopyFromArrayFunction · 0.85
PackedValuesNotEqualFunction · 0.70
GetValueFunction · 0.50
minFunction · 0.50
num_elementsMethod · 0.45
beginMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected