\brief Create a serialized engine for a network defintion \return Whether the engine creation succeeds or fails.
| 1178 | //! \return Whether the engine creation succeeds or fails. |
| 1179 | //! |
| 1180 | bool networkToSerializedEngine( |
| 1181 | BuildOptions const& build, SystemOptions const& sys, IBuilder& builder, BuildEnvironment& env, std::ostream& err) |
| 1182 | { |
| 1183 | std::unique_ptr<IBuilderConfig> config{builder.createBuilderConfig()}; |
| 1184 | std::unique_ptr<nvinfer1::IInt8Calibrator> calibrator; |
| 1185 | std::vector<std::vector<int8_t>> sparseWeights; |
| 1186 | SMP_RETVAL_IF_FALSE(config != nullptr, "Config creation failed", false, err); |
| 1187 | SMP_RETVAL_IF_FALSE( |
| 1188 | setupNetworkAndConfig(build, sys, builder, *env.network, *config, calibrator, err, sparseWeights), |
| 1189 | "Network And Config setup failed", false, err); |
| 1190 | |
| 1191 | std::unique_ptr<ITimingCache> timingCache{nullptr}; |
| 1192 | // Try to load cache from file. Create a fresh cache if the file doesn't exist |
| 1193 | if (build.timingCacheMode == TimingCacheMode::kGLOBAL) |
| 1194 | { |
| 1195 | std::vector<char> loadedCache = samplesCommon::loadTimingCacheFile(build.timingCacheFile); |
| 1196 | timingCache.reset(config->createTimingCache(static_cast<const void*>(loadedCache.data()), loadedCache.size())); |
| 1197 | SMP_RETVAL_IF_FALSE(timingCache != nullptr, "TimingCache creation failed", false, err); |
| 1198 | config->setTimingCache(*timingCache, false); |
| 1199 | } |
| 1200 | |
| 1201 | // CUDA stream used for profiling by the builder. |
| 1202 | auto profileStream = samplesCommon::makeCudaStream(); |
| 1203 | SMP_RETVAL_IF_FALSE(profileStream != nullptr, "Cuda stream creation failed", false, err); |
| 1204 | config->setProfileStream(*profileStream); |
| 1205 | |
| 1206 | std::unique_ptr<IHostMemory> serializedEngine{builder.buildSerializedNetwork(*env.network, *config)}; |
| 1207 | SMP_RETVAL_IF_FALSE(serializedEngine != nullptr, "Engine could not be created from network", false, err); |
| 1208 | |
| 1209 | env.engine.setBlob(serializedEngine->data(), serializedEngine->size()); |
| 1210 | |
| 1211 | if (build.safe && build.consistency) |
| 1212 | { |
| 1213 | checkSafeEngine(serializedEngine->data(), serializedEngine->size()); |
| 1214 | } |
| 1215 | |
| 1216 | if (build.timingCacheMode == TimingCacheMode::kGLOBAL) |
| 1217 | { |
| 1218 | auto timingCache = config->getTimingCache(); |
| 1219 | samplesCommon::updateTimingCacheFile(build.timingCacheFile, timingCache); |
| 1220 | } |
| 1221 | |
| 1222 | return true; |
| 1223 | } |
| 1224 | |
| 1225 | //! |
| 1226 | //! \brief Parse a given model, create a network and an engine. |
no test coverage detected