Apollo 11.0
自动驾驶开放平台
entropy_calibrator.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <NvInferVersion.h>
20
21#ifdef NV_TENSORRT_MAJOR
22#if NV_TENSORRT_MAJOR == 8
24#endif
25#endif
26
27#include <algorithm>
28#include <fstream>
29
31
32namespace nvinfer1 {
34 const apollo::perception::inference::BatchStream &stream, int first_batch,
35 bool read_cache, std::string network)
36 : stream_(stream), read_cache_(read_cache), network_(network) {
37 DimsNCHW dims = stream_.getDims();
38 input_count_ = stream_.getBatchSize() * dims.c() * dims.h() * dims.w();
39 cudaMalloc(&device_input_, input_count_ * sizeof(float));
40 stream_.reset(first_batch);
41}
42
44 if (device_input_) {
45 (cudaFree(device_input_));
46 }
47}
48
49bool Int8EntropyCalibrator::getBatch(void *bindings[], const char *names[],
50 int nbBindings) noexcept {
51 if (!stream_.next()) {
52 return false;
53 }
54
55 (cudaMemcpy(device_input_, stream_.getBatch(), input_count_ * sizeof(float),
56 cudaMemcpyHostToDevice));
57 bindings[0] = device_input_;
58 return true;
59}
60
62 size_t &length) noexcept {
63 calibration_cache_.clear();
64 std::ifstream input(
65 apollo::perception::inference::locateFile(network_, "CalibrationTable"),
66 std::ios::binary);
67 input >> std::noskipws;
68 if (read_cache_ && input.good()) {
69 std::copy(std::istream_iterator<char>(input), std::istream_iterator<char>(),
70 std::back_inserter(calibration_cache_));
71 }
72
73 length = calibration_cache_.size();
74 return length ? &calibration_cache_[0] : nullptr;
75}
76
78 size_t length) noexcept {
79 std::ofstream output(
80 apollo::perception::inference::locateFile(network_, "CalibrationTable"),
81 std::ios::binary);
82 output.write(reinterpret_cast<const char *>(cache), length);
83}
84
85} // namespace nvinfer1
int32_t & h()
Definition rt_legacy.h:46
int32_t & c()
Definition rt_legacy.h:38
int32_t & w()
Definition rt_legacy.h:54
Int8EntropyCalibrator(const apollo::perception::inference::BatchStream &stream, int first_batch, bool read_cache, std::string network)
void writeCalibrationCache(const void *cache, size_t length) noexcept override
const void * readCalibrationCache(size_t &length) noexcept override
bool getBatch(void *bindings[], const char *names[], int nbBindings) noexcept override
std::string locateFile(const std::string &network, const std::string &input)
Definition mi_utils.cc:63