| 566 | } |
| 567 | |
| 568 | void setLayerOutputTypes(INetworkDefinition& network, LayerOutputTypes const& layerOutputTypes) |
| 569 | { |
| 570 | bool const hasGlobalOutputType{layerOutputTypes.find("*") != layerOutputTypes.end()}; |
| 571 | auto const globalOutputType = hasGlobalOutputType ? layerOutputTypes.at("*").at(0) : nvinfer1::DataType::kFLOAT; |
| 572 | bool hasLayerOutputTypeSkipped{false}; |
| 573 | for (int32_t layerIdx = 0; layerIdx < network.getNbLayers(); ++layerIdx) |
| 574 | { |
| 575 | auto* layer = network.getLayer(layerIdx); |
| 576 | auto const layerName = layer->getName(); |
| 577 | auto const nbOutputs = layer->getNbOutputs(); |
| 578 | if (layerOutputTypes.find(layer->getName()) != layerOutputTypes.end()) |
| 579 | { |
| 580 | auto const& outputTypes = layerOutputTypes.at(layer->getName()); |
| 581 | bool const isBroadcast = (outputTypes.size() == 1); |
| 582 | if (!isBroadcast && static_cast<int32_t>(outputTypes.size()) != nbOutputs) |
| 583 | { |
| 584 | sample::gLogError << "Layer " << layerName << " has " << nbOutputs << " outputs but " |
| 585 | << outputTypes.size() << " output types are given in --layerOutputTypes flag." |
| 586 | << std::endl; |
| 587 | throw std::invalid_argument("Invalid --layerOutputTypes flag."); |
| 588 | } |
| 589 | for (int32_t outputIdx = 0; outputIdx < nbOutputs; ++outputIdx) |
| 590 | { |
| 591 | layer->setOutputType(outputIdx, outputTypes.at(isBroadcast ? 0 : outputIdx)); |
| 592 | } |
| 593 | } |
| 594 | else if (hasGlobalOutputType) |
| 595 | { |
| 596 | // We should not set the layer output types if its default precision is INT32 or Bool. |
| 597 | if (layer->getPrecision() == nvinfer1::DataType::kINT32 |
| 598 | || layer->getPrecision() == nvinfer1::DataType::kBOOL) |
| 599 | { |
| 600 | hasLayerOutputTypeSkipped = true; |
| 601 | sample::gLogVerbose << "Skipped setting output types for layer " << layerName << " because the " |
| 602 | << " default layer precision is INT32 or Bool." << std::endl; |
| 603 | continue; |
| 604 | } |
| 605 | // We should not set the constant layer output types if its weights are in INT32. |
| 606 | if (layer->getType() == nvinfer1::LayerType::kCONSTANT |
| 607 | && static_cast<IConstantLayer*>(layer)->getWeights().type == nvinfer1::DataType::kINT32) |
| 608 | { |
| 609 | hasLayerOutputTypeSkipped = true; |
| 610 | sample::gLogVerbose << "Skipped setting output types for layer " << layerName << " because this " |
| 611 | << "constant layer has INT32 weights." << std::endl; |
| 612 | continue; |
| 613 | } |
| 614 | for (int32_t outputIdx = 0; outputIdx < nbOutputs; ++outputIdx) |
| 615 | { |
| 616 | // We should not set the output type if the output is a shape tensor. |
| 617 | if (layer->getOutput(0)->isShapeTensor()) |
| 618 | { |
| 619 | hasLayerOutputTypeSkipped = true; |
| 620 | sample::gLogVerbose << "Skipped setting output type for output " << outputIdx << " of layer " |
| 621 | << layerName << " because it is a shape tensor." << std::endl; |
| 622 | continue; |
| 623 | } |
| 624 | layer->setOutputType(outputIdx, globalOutputType); |
| 625 | } |
no test coverage detected