| 394 | }; |
| 395 | |
| 396 | RndInt8Calibrator::RndInt8Calibrator(int32_t batches, std::vector<int64_t>& elemCount, std::string const& cacheFile, |
| 397 | INetworkDefinition const& network, std::ostream& err) |
| 398 | : mBatches(batches) |
| 399 | , mCurrentBatch(0) |
| 400 | , mCacheFile(cacheFile) |
| 401 | , mErr(err) |
| 402 | { |
| 403 | std::ifstream tryCache(cacheFile, std::ios::binary); |
| 404 | if (tryCache.good()) |
| 405 | { |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | std::default_random_engine generator; |
| 410 | std::uniform_real_distribution<float> distribution(-1.0F, 1.0F); |
| 411 | auto gen = [&generator, &distribution]() { return distribution(generator); }; |
| 412 | |
| 413 | for (int32_t i = 0; i < network.getNbInputs(); i++) |
| 414 | { |
| 415 | auto* input = network.getInput(i); |
| 416 | std::vector<float> rnd_data(elemCount[i]); |
| 417 | std::generate_n(rnd_data.begin(), elemCount[i], gen); |
| 418 | |
| 419 | void* data; |
| 420 | cudaCheck(cudaMalloc(&data, elemCount[i] * sizeof(float)), mErr); |
| 421 | cudaCheck(cudaMemcpy(data, rnd_data.data(), elemCount[i] * sizeof(float), cudaMemcpyHostToDevice), mErr); |
| 422 | |
| 423 | mInputDeviceBuffers.insert(std::make_pair(input->getName(), data)); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | bool RndInt8Calibrator::getBatch(void* bindings[], char const* names[], int32_t nbBindings) noexcept |
| 428 | { |