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

Function DedupeConstantArrays

tensorflow/lite/toco/tooling_util.cc:1296–1352  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1294}
1295
1296void DedupeConstantArrays(Model* model, size_t min_size) {
1297 // Walk all 0..N and compare with the remaining n+1..N.
1298 // This lets us avoid N^2 comparisons and erase duplicate arrays while
1299 // iterating.
1300 const auto& array_map = model->GetArrayMap();
1301 for (auto lhs_array_it = array_map.begin(); lhs_array_it != array_map.end();
1302 ++lhs_array_it) {
1303 const auto& lhs_array_name = lhs_array_it->first;
1304 const auto& lhs_array = *lhs_array_it->second;
1305 if (!IsConstantParameterArray(*model, lhs_array_name)) {
1306 // Not a constant array; skip.
1307 continue;
1308 }
1309 ArrayDataType final_data_type =
1310 lhs_array.final_data_type != ArrayDataType::kNone
1311 ? lhs_array.final_data_type
1312 : lhs_array.data_type;
1313 // Ignore small arrays, don't check string arrays because it is not possible
1314 // to estimate its size.
1315 if (final_data_type != ArrayDataType::kString) {
1316 size_t array_byte_size =
1317 lhs_array.buffer->Length() * ElementSize(final_data_type);
1318 if (array_byte_size < min_size) {
1319 // Too small; skip.
1320 continue;
1321 }
1322 }
1323
1324 auto next_lhs_array_it = lhs_array_it;
1325 ++next_lhs_array_it;
1326 for (auto rhs_array_it = next_lhs_array_it;
1327 rhs_array_it != array_map.end();) {
1328 const auto& rhs_array_name = rhs_array_it->first;
1329 const auto& rhs_array = *rhs_array_it->second;
1330 ++rhs_array_it;
1331 if (!IsConstantParameterArray(*model, rhs_array_name)) {
1332 // Not a constant array; skip.
1333 continue;
1334 }
1335 if (!IsDiscardableArray(*model, rhs_array_name)) {
1336 // Can't remove the array as it's not discardable (such as an IO edge).
1337 continue;
1338 }
1339 if (!CompareConstantArrays(lhs_array, rhs_array)) {
1340 // Arrays aren't equal; skip.
1341 continue;
1342 }
1343
1344 // Arrays can be deduped!
1345 VLOG(1) << "Deduplicating arrays; using " << lhs_array_name
1346 << " in place of " << rhs_array_name;
1347 ReplaceArrayUsage(model, rhs_array_name, lhs_array_name);
1348 // Note: rhs_array_it above is already incremented so this is safe.
1349 model->EraseArray(rhs_array_name);
1350 }
1351 }
1352}
1353

Callers 1

TransformWithStatusFunction · 0.85

Calls 9

IsConstantParameterArrayFunction · 0.85
ElementSizeFunction · 0.85
IsDiscardableArrayFunction · 0.85
CompareConstantArraysFunction · 0.85
ReplaceArrayUsageFunction · 0.85
EraseArrayMethod · 0.80
beginMethod · 0.45
endMethod · 0.45
LengthMethod · 0.45

Tested by

no test coverage detected