| 1509 | } |
| 1510 | |
| 1511 | bool timeRefit(INetworkDefinition const& network, nvinfer1::ICudaEngine& engine, bool multiThreading) |
| 1512 | { |
| 1513 | using time_point = std::chrono::time_point<std::chrono::steady_clock>; |
| 1514 | using durationMs = std::chrono::duration<float, std::milli>; |
| 1515 | |
| 1516 | auto const nbLayers = network.getNbLayers(); |
| 1517 | std::unique_ptr<IRefitter> refitter{createRefitter(engine)}; |
| 1518 | // Set max threads that can be used by refitter. |
| 1519 | if (multiThreading && !refitter->setMaxThreads(10)) |
| 1520 | { |
| 1521 | sample::gLogError << "Failed to set max threads to refitter." << std::endl; |
| 1522 | return false; |
| 1523 | } |
| 1524 | auto const& layerWeightsRolePair = getLayerWeightsRolePair(*refitter); |
| 1525 | // We use std::string instead of char const* since we can have copies of layer names. |
| 1526 | std::set<std::pair<std::string, WeightsRole>> layerRoleSet; |
| 1527 | |
| 1528 | auto const& layerNames = layerWeightsRolePair.first; |
| 1529 | auto const& weightsRoles = layerWeightsRolePair.second; |
| 1530 | |
| 1531 | std::transform(layerNames.begin(), layerNames.end(), weightsRoles.begin(), |
| 1532 | std::inserter(layerRoleSet, layerRoleSet.begin()), |
| 1533 | [](std::string const& layerName, WeightsRole const role) { return std::make_pair(layerName, role); }); |
| 1534 | |
| 1535 | auto const isRefittable = [&layerRoleSet](char const* layerName, WeightsRole const role) { |
| 1536 | return layerRoleSet.find(std::make_pair(layerName, role)) != layerRoleSet.end(); |
| 1537 | }; |
| 1538 | |
| 1539 | auto const setWeights = [&] { |
| 1540 | for (int32_t i = 0; i < nbLayers; i++) |
| 1541 | { |
| 1542 | auto const layer = network.getLayer(i); |
| 1543 | auto const roleWeightsVec = getAllRefitWeightsForLayer(*layer); |
| 1544 | for (auto const& roleWeights : roleWeightsVec) |
| 1545 | { |
| 1546 | if (isRefittable(layer->getName(), roleWeights.first)) |
| 1547 | { |
| 1548 | bool const success = refitter->setWeights(layer->getName(), roleWeights.first, roleWeights.second); |
| 1549 | if (!success) |
| 1550 | { |
| 1551 | return false; |
| 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | return true; |
| 1557 | }; |
| 1558 | |
| 1559 | auto const reportMissingWeights = [&] { |
| 1560 | auto const& missingPair = getMissingLayerWeightsRolePair(*refitter); |
| 1561 | auto const& layerNames = missingPair.first; |
| 1562 | auto const& weightsRoles = missingPair.second; |
| 1563 | for (size_t i = 0; i < layerNames.size(); ++i) |
| 1564 | { |
| 1565 | sample::gLogError << "Missing (" << layerNames[i] << ", " << weightsRoles[i] << ") for refitting." |
| 1566 | << std::endl; |
| 1567 | } |
| 1568 | return layerNames.empty(); |
no test coverage detected