| 192 | } |
| 193 | |
| 194 | void setTensorScalesFromCalibration(nvinfer1::INetworkDefinition& network, std::vector<IOFormat> const& inputFormats, |
| 195 | std::vector<IOFormat> const& outputFormats, std::string const& calibrationFile) |
| 196 | { |
| 197 | auto const tensorScales = readScalesFromCalibrationCache(calibrationFile); |
| 198 | bool const broadcastInputFormats = broadcastIOFormats(inputFormats, network.getNbInputs()); |
| 199 | for (int32_t i = 0, n = network.getNbInputs(); i < n; ++i) |
| 200 | { |
| 201 | int32_t formatIdx = broadcastInputFormats ? 0 : i; |
| 202 | if (!inputFormats.empty() && inputFormats[formatIdx].first == DataType::kINT8) |
| 203 | { |
| 204 | auto* input = network.getInput(i); |
| 205 | auto const calibScale = tensorScales.at(input->getName()); |
| 206 | input->setDynamicRange(-127 * calibScale, 127 * calibScale); |
| 207 | } |
| 208 | } |
| 209 | bool const broadcastOutputFormats = broadcastIOFormats(outputFormats, network.getNbInputs()); |
| 210 | for (int32_t i = 0, n = network.getNbOutputs(); i < n; ++i) |
| 211 | { |
| 212 | int32_t formatIdx = broadcastOutputFormats ? 0 : i; |
| 213 | if (!outputFormats.empty() && outputFormats[formatIdx].first == DataType::kINT8) |
| 214 | { |
| 215 | auto* output = network.getOutput(i); |
| 216 | auto const calibScale = tensorScales.at(output->getName()); |
| 217 | output->setDynamicRange(-127 * calibScale, 127 * calibScale); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | //! |
| 223 | //! \brief Generate a network definition for a given model |
no test coverage detected