| 
 | 1 | +/*  | 
 | 2 | + * Copyright (c) 2019, NVIDIA CORPORATION. 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 | + | 
 | 17 | +#ifndef ENTROPY_CALIBRATOR_HPP  | 
 | 18 | +#define ENTROPY_CALIBRATOR_HPP  | 
 | 19 | + | 
 | 20 | +#include <algorithm>  | 
 | 21 | +#include <numeric>  | 
 | 22 | +#include <iterator>  | 
 | 23 | +#include "NvInfer.h"  | 
 | 24 | + | 
 | 25 | +//! \class EntropyCalibratorImpl  | 
 | 26 | +//!  | 
 | 27 | +//! \brief Implements common functionality for Entropy calibrators.  | 
 | 28 | +//!  | 
 | 29 | +template <typename TBatchStream>  | 
 | 30 | +class EntropyCalibratorImpl  | 
 | 31 | +{  | 
 | 32 | +public:  | 
 | 33 | +    EntropyCalibratorImpl(  | 
 | 34 | +        TBatchStream stream, int firstBatch, std::string cal_table_name, const char* inputBlobName, bool readCache = true)  | 
 | 35 | +        : mStream{stream}  | 
 | 36 | +        , mCalibrationTableName(cal_table_name)  | 
 | 37 | +        , mInputBlobName(inputBlobName)  | 
 | 38 | +        , mReadCache(readCache)  | 
 | 39 | +    {  | 
 | 40 | +        nvinfer1::Dims4 dims = mStream.getDims();  | 
 | 41 | +        mInputCount = std::accumulate(  | 
 | 42 | +                dims.d, dims.d + dims.nbDims, 1, std::multiplies<int64_t>());  | 
 | 43 | +        cout << "dims.nbDims: " << dims.nbDims << endl;  | 
 | 44 | +        for (int i{0}; i < dims.nbDims; ++i) {  | 
 | 45 | +            cout << dims.d[i] << ", ";  | 
 | 46 | +        }  | 
 | 47 | +        cout << endl;  | 
 | 48 | + | 
 | 49 | +        cudaError_t state;  | 
 | 50 | +        state = cudaMalloc(&mDeviceInput, mInputCount * sizeof(float));  | 
 | 51 | +        if (state) {  | 
 | 52 | +            cout << "allocate memory failed\n";   | 
 | 53 | +            std::abort();  | 
 | 54 | +        }  | 
 | 55 | +        cout << "mInputCount: " << mInputCount << endl;  | 
 | 56 | +        mStream.reset(firstBatch);  | 
 | 57 | +    }  | 
 | 58 | + | 
 | 59 | +    virtual ~EntropyCalibratorImpl()  | 
 | 60 | +    {  | 
 | 61 | +        cudaError_t state;  | 
 | 62 | +        state = cudaFree(mDeviceInput);  | 
 | 63 | +        if (state) {  | 
 | 64 | +            cout << "free memory failed\n";   | 
 | 65 | +            std::abort();  | 
 | 66 | +        }  | 
 | 67 | +    }  | 
 | 68 | + | 
 | 69 | +    int getBatchSize() const  | 
 | 70 | +    {  | 
 | 71 | +        return mStream.getBatchSize();  | 
 | 72 | +    }  | 
 | 73 | + | 
 | 74 | +    bool getBatch(void* bindings[], const char* names[], int nbBindings)  | 
 | 75 | +    {  | 
 | 76 | +        if (!mStream.next())  | 
 | 77 | +        {  | 
 | 78 | +            return false;  | 
 | 79 | +        }  | 
 | 80 | +        cudaError_t state;  | 
 | 81 | +        state = cudaMemcpy(mDeviceInput, mStream.getBatch(), mInputCount * sizeof(float), cudaMemcpyHostToDevice);  | 
 | 82 | +        if (state) {  | 
 | 83 | +            cout << "memory copy to device failed\n";   | 
 | 84 | +            std::abort();  | 
 | 85 | +        }  | 
 | 86 | +        assert(!strcmp(names[0], mInputBlobName));  | 
 | 87 | +        bindings[0] = mDeviceInput;  | 
 | 88 | +        return true;  | 
 | 89 | +    }  | 
 | 90 | + | 
 | 91 | +    const void* readCalibrationCache(size_t& length)  | 
 | 92 | +    {  | 
 | 93 | +        mCalibrationCache.clear();  | 
 | 94 | +        std::ifstream input(mCalibrationTableName, std::ios::binary);  | 
 | 95 | +        input >> std::noskipws;  | 
 | 96 | +        if (mReadCache && input.good())  | 
 | 97 | +        {  | 
 | 98 | +            std::copy(std::istream_iterator<char>(input), std::istream_iterator<char>(),  | 
 | 99 | +                std::back_inserter(mCalibrationCache));  | 
 | 100 | +        }  | 
 | 101 | +        length = mCalibrationCache.size();  | 
 | 102 | +        return length ? mCalibrationCache.data() : nullptr;  | 
 | 103 | +    }  | 
 | 104 | + | 
 | 105 | +    void writeCalibrationCache(const void* cache, size_t length)  | 
 | 106 | +    {  | 
 | 107 | +        std::ofstream output(mCalibrationTableName, std::ios::binary);  | 
 | 108 | +        output.write(reinterpret_cast<const char*>(cache), length);  | 
 | 109 | +    }  | 
 | 110 | + | 
 | 111 | +private:  | 
 | 112 | +    TBatchStream mStream;  | 
 | 113 | +    size_t mInputCount;  | 
 | 114 | +    std::string mCalibrationTableName;  | 
 | 115 | +    const char* mInputBlobName;  | 
 | 116 | +    bool mReadCache{true};  | 
 | 117 | +    void* mDeviceInput{nullptr};  | 
 | 118 | +    std::vector<char> mCalibrationCache;  | 
 | 119 | +};  | 
 | 120 | + | 
 | 121 | +//! \class Int8EntropyCalibrator2  | 
 | 122 | +//!  | 
 | 123 | +//! \brief Implements Entropy calibrator 2.  | 
 | 124 | +//!  CalibrationAlgoType is kENTROPY_CALIBRATION_2.  | 
 | 125 | +//!  | 
 | 126 | +template <typename TBatchStream>  | 
 | 127 | +class Int8EntropyCalibrator2 : public nvinfer1::IInt8EntropyCalibrator2  | 
 | 128 | +{  | 
 | 129 | +public:  | 
 | 130 | +    Int8EntropyCalibrator2(  | 
 | 131 | +        TBatchStream stream, int firstBatch, const char* networkName, const char* inputBlobName, bool readCache = true)  | 
 | 132 | +        : mImpl(stream, firstBatch, networkName, inputBlobName, readCache)  | 
 | 133 | +    {  | 
 | 134 | +    }  | 
 | 135 | + | 
 | 136 | +    int getBatchSize() const noexcept override  | 
 | 137 | +    {  | 
 | 138 | +        return mImpl.getBatchSize();  | 
 | 139 | +    }  | 
 | 140 | + | 
 | 141 | +    bool getBatch(void* bindings[], const char* names[], int nbBindings) noexcept override  | 
 | 142 | +    {  | 
 | 143 | +        return mImpl.getBatch(bindings, names, nbBindings);  | 
 | 144 | +    }  | 
 | 145 | + | 
 | 146 | +    const void* readCalibrationCache(size_t& length) noexcept override  | 
 | 147 | +    {  | 
 | 148 | +        return mImpl.readCalibrationCache(length);  | 
 | 149 | +    }  | 
 | 150 | + | 
 | 151 | +    void writeCalibrationCache(const void* cache, size_t length) noexcept override  | 
 | 152 | +    {  | 
 | 153 | +        mImpl.writeCalibrationCache(cache, length);  | 
 | 154 | +    }  | 
 | 155 | + | 
 | 156 | +private:  | 
 | 157 | +    EntropyCalibratorImpl<TBatchStream> mImpl;  | 
 | 158 | +};  | 
 | 159 | + | 
 | 160 | +#endif // ENTROPY_CALIBRATOR_H  | 
0 commit comments