\brief Load requested weights from a formatted file into a map. \param file Path to weights file. File has to be the formatted dump from the dumpTFWts.py script. Otherwise, this function will not work as intended. \return A map containing the extracted weights. \note Weight V2 files are in a very simple space delimited format. for each buffer: [name] [type] [shape] <data as
| 362 | //! Note: type is the integer value of the DataType enum in NvInfer.h. |
| 363 | //! |
| 364 | std::map<std::string, nvinfer1::Weights> SampleCharRNNBase::loadWeights(const std::string file) |
| 365 | { |
| 366 | std::map<std::string, nvinfer1::Weights> weightMap; |
| 367 | |
| 368 | std::ifstream input(file, std::ios_base::binary); |
| 369 | ASSERT(input.is_open() && "Unable to load weight file."); |
| 370 | |
| 371 | int32_t count; |
| 372 | input >> count; |
| 373 | ASSERT(count > 0 && "Invalid weight map file."); |
| 374 | |
| 375 | while (count--) |
| 376 | { |
| 377 | if (mParams.weightNames.names.empty()) |
| 378 | { |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | nvinfer1::Weights wt{nvinfer1::DataType::kFLOAT, nullptr, 0}; |
| 383 | |
| 384 | // parse name and DataType |
| 385 | std::string name; |
| 386 | uint32_t type; |
| 387 | input >> name >> std::dec >> type; |
| 388 | wt.type = static_cast<nvinfer1::DataType>(type); |
| 389 | |
| 390 | // extract shape |
| 391 | std::string temp, shape; |
| 392 | std::getline(std::getline(input, temp, '('), shape, ')'); |
| 393 | |
| 394 | // calculate count based on shape |
| 395 | wt.count = 1; |
| 396 | std::istringstream shapeStream(shape); |
| 397 | while (std::getline(shapeStream, temp, ',')) |
| 398 | wt.count *= std::stoul(temp); |
| 399 | size_t numOfBytes = samplesCommon::getElementSize(wt.type) * wt.count; |
| 400 | |
| 401 | // skip reading of weights if name is not in the set of names requested for extraction |
| 402 | if (mParams.weightNames.names.find(name) == mParams.weightNames.names.end()) |
| 403 | { |
| 404 | input.seekg(input.tellg() + static_cast<std::streamoff>(2 + numOfBytes)); |
| 405 | continue; |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | mParams.weightNames.names.erase(name); |
| 410 | } |
| 411 | |
| 412 | // Read weight values |
| 413 | input.seekg(input.tellg() + static_cast<std::streamoff>(1)); // skip space char |
| 414 | // We do not really care about the setup of DataType here. Use char here to avoid additional conversion |
| 415 | auto mem = new samplesCommon::TypedHostMemory<char, nvinfer1::DataType::kINT8>(numOfBytes); |
| 416 | weightsMemory.emplace_back(mem); |
| 417 | auto wtVals = mem->raw(); |
| 418 | input.read(wtVals, numOfBytes); |
| 419 | input.seekg(input.tellg() + static_cast<std::streamoff>(1)); // skip new-line char |
| 420 | wt.values = wtVals; |
| 421 |
nothing calls this directly
no test coverage detected