123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- #include "infer.hpp"
- #include "resnet.hpp"
- #include <cfloat>
- namespace resnet
- {
- using namespace std;
- #define GPU_BLOCK_THREADS 512
- #define checkRuntime(call) \
- do { \
- auto ___call__ret_code__ = (call); \
- if (___call__ret_code__ != cudaSuccess) { \
- INFO("CUDA Runtime error💥 %s # %s, code = %s [ %d ]", #call, \
- cudaGetErrorString(___call__ret_code__), cudaGetErrorName(___call__ret_code__), \
- ___call__ret_code__); \
- abort(); \
- } \
- } while (0)
- #define checkKernel(...) \
- do { \
- { (__VA_ARGS__); } \
- checkRuntime(cudaPeekAtLastError()); \
- } while (0)
- enum class NormType : int { None = 0, MeanStd = 1, AlphaBeta = 2 };
- enum class ChannelType : int { None = 0, SwapRB = 1 };
- /* 归一化操作,可以支持均值标准差,alpha beta,和swap RB */
- struct Norm {
- float mean[3];
- float std[3];
- float alpha, beta;
- NormType type = NormType::None;
- ChannelType channel_type = ChannelType::None;
- // out = (x * alpha - mean) / std
- static Norm mean_std(const float mean[3], const float std[3], float alpha = 1 / 255.0f,
- ChannelType channel_type = ChannelType::None);
- // out = x * alpha + beta
- static Norm alpha_beta(float alpha, float beta = 0, ChannelType channel_type = ChannelType::None);
- // None
- static Norm None();
- };
- Norm Norm::mean_std(const float mean[3], const float std[3], float alpha,
- ChannelType channel_type) {
- Norm out;
- out.type = NormType::MeanStd;
- out.alpha = alpha;
- out.channel_type = channel_type;
- memcpy(out.mean, mean, sizeof(out.mean));
- memcpy(out.std, std, sizeof(out.std));
- return out;
- }
- Norm Norm::alpha_beta(float alpha, float beta, ChannelType channel_type) {
- Norm out;
- out.type = NormType::AlphaBeta;
- out.alpha = alpha;
- out.beta = beta;
- out.channel_type = channel_type;
- return out;
- }
- Norm Norm::None() { return Norm(); }
- static dim3 grid_dims(int numJobs) {
- int numBlockThreads = numJobs < GPU_BLOCK_THREADS ? numJobs : GPU_BLOCK_THREADS;
- return dim3(((numJobs + numBlockThreads - 1) / (float)numBlockThreads));
- }
- static dim3 block_dims(int numJobs) {
- return numJobs < GPU_BLOCK_THREADS ? numJobs : GPU_BLOCK_THREADS;
- }
- inline int upbound(int n, int align = 32) { return (n + align - 1) / align * align; }
- static __global__ void warp_affine_bilinear_and_normalize_plane_kernel(
- uint8_t *src, int src_line_size, int src_width, int src_height, float *dst, int dst_width,
- int dst_height, uint8_t const_value_st, float *warp_affine_matrix_2_3, Norm norm) {
- int dx = blockDim.x * blockIdx.x + threadIdx.x;
- int dy = blockDim.y * blockIdx.y + threadIdx.y;
- if (dx >= dst_width || dy >= dst_height) return;
- float m_x1 = warp_affine_matrix_2_3[0];
- float m_y1 = warp_affine_matrix_2_3[1];
- float m_z1 = warp_affine_matrix_2_3[2];
- float m_x2 = warp_affine_matrix_2_3[3];
- float m_y2 = warp_affine_matrix_2_3[4];
- float m_z2 = warp_affine_matrix_2_3[5];
- float src_x = m_x1 * dx + m_y1 * dy + m_z1;
- float src_y = m_x2 * dx + m_y2 * dy + m_z2;
- float c0, c1, c2;
- if (src_x <= -1 || src_x >= src_width || src_y <= -1 || src_y >= src_height) {
- // out of range
- c0 = const_value_st;
- c1 = const_value_st;
- c2 = const_value_st;
- } else {
- int y_low = floorf(src_y);
- int x_low = floorf(src_x);
- int y_high = y_low + 1;
- int x_high = x_low + 1;
- uint8_t const_value[] = {const_value_st, const_value_st, const_value_st};
- float ly = src_y - y_low;
- float lx = src_x - x_low;
- float hy = 1 - ly;
- float hx = 1 - lx;
- float w1 = hy * hx, w2 = hy * lx, w3 = ly * hx, w4 = ly * lx;
- uint8_t *v1 = const_value;
- uint8_t *v2 = const_value;
- uint8_t *v3 = const_value;
- uint8_t *v4 = const_value;
- if (y_low >= 0) {
- if (x_low >= 0) v1 = src + y_low * src_line_size + x_low * 3;
- if (x_high < src_width) v2 = src + y_low * src_line_size + x_high * 3;
- }
- if (y_high < src_height) {
- if (x_low >= 0) v3 = src + y_high * src_line_size + x_low * 3;
- if (x_high < src_width) v4 = src + y_high * src_line_size + x_high * 3;
- }
- // same to opencv
- c0 = floorf(w1 * v1[0] + w2 * v2[0] + w3 * v3[0] + w4 * v4[0] + 0.5f);
- c1 = floorf(w1 * v1[1] + w2 * v2[1] + w3 * v3[1] + w4 * v4[1] + 0.5f);
- c2 = floorf(w1 * v1[2] + w2 * v2[2] + w3 * v3[2] + w4 * v4[2] + 0.5f);
- }
- if (norm.channel_type == ChannelType::SwapRB) {
- float t = c2;
- c2 = c0;
- c0 = t;
- }
- if (norm.type == NormType::MeanStd) {
- c0 = (c0 * norm.alpha - norm.mean[0]) / norm.std[0];
- c1 = (c1 * norm.alpha - norm.mean[1]) / norm.std[1];
- c2 = (c2 * norm.alpha - norm.mean[2]) / norm.std[2];
- } else if (norm.type == NormType::AlphaBeta) {
- c0 = c0 * norm.alpha + norm.beta;
- c1 = c1 * norm.alpha + norm.beta;
- c2 = c2 * norm.alpha + norm.beta;
- }
- int area = dst_width * dst_height;
- float *pdst_c0 = dst + dy * dst_width + dx;
- float *pdst_c1 = pdst_c0 + area;
- float *pdst_c2 = pdst_c1 + area;
- *pdst_c0 = c0;
- *pdst_c1 = c1;
- *pdst_c2 = c2;
- }
- static void warp_affine_bilinear_and_normalize_plane(uint8_t *src, int src_line_size, int src_width,
- int src_height, float *dst, int dst_width,
- int dst_height, float *matrix_2_3,
- uint8_t const_value, const Norm &norm,
- cudaStream_t stream) {
- dim3 grid((dst_width + 31) / 32, (dst_height + 31) / 32);
- dim3 block(32, 32);
- checkKernel(warp_affine_bilinear_and_normalize_plane_kernel<<<grid, block, 0, stream>>>(
- src, src_line_size, src_width, src_height, dst, dst_width, dst_height, const_value,
- matrix_2_3, norm));
- }
- struct AffineMatrix {
- float i2d[6]; // image to dst(network), 2x3 matrix
- float d2i[6]; // dst to image, 2x3 matrix
- void compute(const std::tuple<int, int> &from, const std::tuple<int, int> &to) {
- float scale_x = get<0>(to) / (float)get<0>(from);
- float scale_y = get<1>(to) / (float)get<1>(from);
- float scale = std::min(scale_x, scale_y);
- // letter box
- // i2d[0] = scale;
- // i2d[1] = 0;
- // i2d[2] = -scale * get<0>(from) * 0.5 + get<0>(to) * 0.5 + scale * 0.5 - 0.5;
- // i2d[3] = 0;
- // i2d[4] = scale;
- // i2d[5] = -scale * get<1>(from) * 0.5 + get<1>(to) * 0.5 + scale * 0.5 - 0.5;
- // resize
- i2d[0] = scale;
- i2d[1] = 0;
- i2d[2] = 0;
- i2d[3] = 0;
- i2d[4] = scale;
- i2d[5] = 0;
- double D = i2d[0] * i2d[4] - i2d[1] * i2d[3];
- D = D != 0. ? double(1.) / D : double(0.);
- double A11 = i2d[4] * D, A22 = i2d[0] * D, A12 = -i2d[1] * D, A21 = -i2d[3] * D;
- double b1 = -A11 * i2d[2] - A12 * i2d[5];
- double b2 = -A21 * i2d[2] - A22 * i2d[5];
- d2i[0] = A11;
- d2i[1] = A12;
- d2i[2] = b1;
- d2i[3] = A21;
- d2i[4] = A22;
- d2i[5] = b2;
- }
- };
- static __global__ void softmax(float *predict, int length, int *max_index) {
- extern __shared__ float shared_data[];
- float *shared_max_vals = shared_data;
- int *shared_max_indices = (int*)&shared_max_vals[blockDim.x];
-
- int tid = threadIdx.x;
- // 1. 找到最大值和最大值的下标,存储在共享内存中
- float max_val = -FLT_MAX;
- int max_idx = -1;
- for (int i = tid; i < length; i += blockDim.x) {
- if (predict[i] > max_val) {
- max_val = predict[i];
- max_idx = i;
- }
- }
- shared_max_vals[tid] = max_val;
- shared_max_indices[tid] = max_idx;
- __syncthreads();
- // 在所有线程间找到全局最大值和对应的下标
- if (tid == 0) {
- for (int i = 1; i < blockDim.x; i++) {
- if (shared_max_vals[i] > shared_max_vals[0]) {
- shared_max_vals[0] = shared_max_vals[i];
- shared_max_indices[0] = shared_max_indices[i];
- }
- }
- *max_index = shared_max_indices[0];
- }
- __syncthreads();
- max_val = shared_max_vals[0];
- // 2. 计算指数并求和
- float sum_exp = 0.0f;
- for (int i = tid; i < length; i += blockDim.x) {
- predict[i] = expf(predict[i] - max_val);
- sum_exp += predict[i];
- }
- shared_max_vals[tid] = sum_exp;
- __syncthreads();
- // 汇总所有线程的指数和
- if (tid == 0) {
- for (int i = 1; i < blockDim.x; i++) {
- shared_max_vals[0] += shared_max_vals[i];
- }
- }
- __syncthreads();
- float total_sum = shared_max_vals[0];
- // 3. 每个元素除以总和,得到 softmax 值
- for (int i = tid; i < length; i += blockDim.x) {
- predict[i] /= total_sum;
- }
- }
- static void classfier_softmax(float *predict, int length, int *max_index, cudaStream_t stream) {
- int block_size = 256;
- checkKernel(softmax<<<1, block_size, block_size * sizeof(float), stream>>>(predict, length, max_index));
- }
- class InferImpl : public Infer {
- public:
- shared_ptr<trt::Infer> trt_;
- string engine_file_;
- vector<shared_ptr<trt::Memory<unsigned char>>> preprocess_buffers_;
- trt::Memory<float> input_buffer_, output_array_;
- trt::Memory<int> classes_indices_;
- int network_input_width_, network_input_height_;
- Norm normalize_;
- int num_classes_ = 0;
- bool isdynamic_model_ = false;
- virtual ~InferImpl() = default;
- void adjust_memory(int batch_size) {
- // the inference batch_size
- size_t input_numel = network_input_width_ * network_input_height_ * 3;
- input_buffer_.gpu(batch_size * input_numel);
- output_array_.gpu(batch_size * num_classes_);
- output_array_.cpu(batch_size * num_classes_);
- classes_indices_.gpu(batch_size);
- classes_indices_.cpu(batch_size);
- if ((int)preprocess_buffers_.size() < batch_size) {
- for (int i = preprocess_buffers_.size(); i < batch_size; ++i)
- preprocess_buffers_.push_back(make_shared<trt::Memory<unsigned char>>());
- }
- }
- void preprocess(int ibatch, const Image &image,
- shared_ptr<trt::Memory<unsigned char>> preprocess_buffer,
- void *stream = nullptr) {
- AffineMatrix affine;
- affine.compute(make_tuple(image.width, image.height),
- make_tuple(network_input_width_, network_input_height_));
- size_t input_numel = network_input_width_ * network_input_height_ * 3;
- float *input_device = input_buffer_.gpu() + ibatch * input_numel;
- size_t size_image = image.width * image.height * 3;
- size_t size_matrix = upbound(sizeof(affine.d2i), 32);
- uint8_t *gpu_workspace = preprocess_buffer->gpu(size_matrix + size_image);
- float *affine_matrix_device = (float *)gpu_workspace;
- uint8_t *image_device = gpu_workspace + size_matrix;
- uint8_t *cpu_workspace = preprocess_buffer->cpu(size_matrix + size_image);
- float *affine_matrix_host = (float *)cpu_workspace;
- uint8_t *image_host = cpu_workspace + size_matrix;
- // speed up
- cudaStream_t stream_ = (cudaStream_t)stream;
- memcpy(image_host, image.bgrptr, size_image);
- memcpy(affine_matrix_host, affine.d2i, sizeof(affine.d2i));
- checkRuntime(
- cudaMemcpyAsync(image_device, image_host, size_image, cudaMemcpyHostToDevice, stream_));
- checkRuntime(cudaMemcpyAsync(affine_matrix_device, affine_matrix_host, sizeof(affine.d2i),
- cudaMemcpyHostToDevice, stream_));
- warp_affine_bilinear_and_normalize_plane(image_device, image.width * 3, image.width,
- image.height, input_device, network_input_width_,
- network_input_height_, affine_matrix_device, 114,
- normalize_, stream_);
- }
- bool load(const string &engine_file) {
- trt_ = trt::load(engine_file);
- if (trt_ == nullptr) return false;
- trt_->print();
- auto input_dim = trt_->static_dims(0);
- network_input_width_ = input_dim[3];
- network_input_height_ = input_dim[2];
- isdynamic_model_ = trt_->has_dynamic_dim();
- // normalize_ = Norm::alpha_beta(1 / 255.0f, 0.0f, ChannelType::SwapRB);
- // [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
- float mean[3] = {0.485, 0.456, 0.406};
- float std[3] = {0.229, 0.224, 0.225};
- normalize_ = Norm::mean_std(mean, std, 1/255.0, ChannelType::SwapRB);
- num_classes_ = trt_->static_dims(1)[1];
- return true;
- }
- virtual Attribute forward(const Image &image, void *stream = nullptr) override {
- auto output = forwards({image}, stream);
- if (output.empty()) return {};
- return output[0];
- }
- virtual vector<Attribute> forwards(const vector<Image> &images, void *stream = nullptr) override {
- int num_image = images.size();
- if (num_image == 0) return {};
- auto input_dims = trt_->static_dims(0);
- int infer_batch_size = input_dims[0];
- if (infer_batch_size != num_image) {
- if (isdynamic_model_) {
- infer_batch_size = num_image;
- input_dims[0] = num_image;
- if (!trt_->set_run_dims(0, input_dims)) return {};
- } else {
- if (infer_batch_size < num_image) {
- INFO(
- "When using static shape model, number of images[%d] must be "
- "less than or equal to the maximum batch[%d].",
- num_image, infer_batch_size);
- return {};
- }
- }
- }
- adjust_memory(infer_batch_size);
- cudaStream_t stream_ = (cudaStream_t)stream;
- for (int i = 0; i < num_image; ++i)
- preprocess(i, images[i], preprocess_buffers_[i], stream);
- float *output_array_device = output_array_.gpu();
- vector<void *> bindings{input_buffer_.gpu(), output_array_device};
- if (!trt_->forward(bindings, stream)) {
- INFO("Failed to tensorRT forward.");
- return {};
- }
- for (int ib = 0; ib < num_image; ++ib) {
- float *output_array_device = output_array_.gpu() + ib * num_classes_;
- int *classes_indices_device = classes_indices_.gpu() + ib;
- classfier_softmax(output_array_device, num_classes_, classes_indices_device, stream_);
- }
-
- checkRuntime(cudaMemcpyAsync(output_array_.cpu(), output_array_.gpu(),
- output_array_.gpu_bytes(), cudaMemcpyDeviceToHost, stream_));
- checkRuntime(cudaMemcpyAsync(classes_indices_.cpu(), classes_indices_.gpu(),
- classes_indices_.gpu_bytes(), cudaMemcpyDeviceToHost, stream_));
- checkRuntime(cudaStreamSynchronize(stream_));
- vector<Attribute> arrout;
- arrout.reserve(num_image);
- for (int ib = 0; ib < num_image; ++ib) {
- float *output_array_cpu = output_array_.cpu() + ib * num_classes_;
- int *max_index = classes_indices_.cpu() + ib;
- int index = *max_index;
- float max_score = output_array_cpu[index];
- arrout.emplace_back(max_score, index);
- }
- return arrout;
- }
- };
- Infer *loadraw(const std::string &engine_file) {
- InferImpl *impl = new InferImpl();
- if (!impl->load(engine_file)) {
- delete impl;
- impl = nullptr;
- }
- return impl;
- }
- shared_ptr<Infer> load(const string &engine_file) {
- return std::shared_ptr<InferImpl>(
- (InferImpl *)loadraw(engine_file));
- }
- }
|