[cpp] elena cpu

Viewer

copydownloadembedprintName: elena cpu
  1.  
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #define EQUAL(a,b) (strcmp((a),(b))==0)
  7. #define ABORT(Msg)                                                   \
  8.   {                                                                        \
  9.     std::cerr << ": \033[1;91m"                                            \
  10.               << "[Fatal]"                                                 \
  11.               << "\033[m " << __FILE__ << ": " << __FUNCTION__ << ": Line" \
  12.               << __LINE__ << ": " << Msg << std::endl;                     \
  13.     std::abort();                                                               \
  14.   }
  15.  
  16. #include "elena_int.h"
  17.  
  18.  
  19. #include "elena_registry.h"#define INCREASE(x, l) ((x + 1) >= (l) ? (x) : ((x) + 1))
  20.  
  21.  void bilinear_resize_preprocess(uint64_t src_h, uint64_t src_w, uint64_t dst_h, uint64_t dst_w,
  22.                        int16_t* __restrict__ cubfh, int16_t* __restrict__ cubfw,
  23.                        int32_t* __restrict__ inth, int32_t* __restrict__ intw) {
  24.   float scale_h = double(src_h) / dst_h;
  25.   float scale_w = double(src_w) / dst_w;
  26.  
  27.   for (int j = 0; j < dst_h; ++j) {
  28.     float fh = (float)((+ 0.5) * scale_h - 0.5f);
  29.     int sh = floor(fh);
  30.     fh -= sh;
  31.     if (sh < 0) {
  32.       fh = 0;
  33.       sh = 0;
  34.     }
  35.     if (sh >= src_h) {
  36.       fh = 0;
  37.       sh = src_h - 1;
  38.     }
  39.  
  40.     int int_h1 = INCREASE(sh, src_h);
  41.  
  42.     fh = fh * 2048;
  43.     cubfh[j] = rint(2048 - fh);
  44.     cubfh[dst_h + j] = rint(fh);
  45.  
  46.     inth[j] = sh;
  47.     inth[dst_h + j] = int_h1;
  48.   }
  49.  
  50.   for (int i = 0; i < dst_w; ++i) {
  51.     float fw = (float)((+ 0.5) * scale_w - 0.5f);
  52.     int sw = floor(fw);
  53.     fw -= sw;
  54.  
  55.     if (sw < 0) {
  56.       fw = 0;
  57.       sw = 0;
  58.     }
  59.     if (sw >= src_w) {
  60.       fw = 0;
  61.       sw = src_w - 1;
  62.     }
  63.     int int_w1 = INCREASE(sw, src_w);
  64.     fw = fw * 2048;
  65.     cubfw[i] = rint(2048 - rint(fw));
  66.     cubfw[dst_w + i] = rint(fw);
  67.  
  68.     intw[i] = sw;
  69.     intw[dst_w + i] = int_w1;
  70.   }
  71. }
  72.  
  73.  
  74.  
  75.  void FuseKernel(uint64_t resize_h, uint64_t resize_w, uint64_t crop_h, uint64_t crop_w, int32_t crop_top, int32_t crop_left, float norm_mean_0, float norm_mean_1, float norm_mean_2, float norm_std_0, float norm_std_1, float norm_std_2, uint64_t pad_h, uint64_t pad_w, int32_t pad_top, int32_t pad_left, int32_t pad_bottom, int32_t pad_right, float pad_value, uint8_t* __restrict__ src_raw_data, float* __restrict__ dst_raw_data, uint64_t src_h, uint64_t src_w, const char *format, const char *interpolation = "nearest"){
  76.     if (resize_h && resize_w && EQUAL(interpolation, "nearest")) {
  77.         if(EQUAL(format, "BGR")){
  78.           BGR_Nearest_Kernel(resize_h, resize_w, crop_h, crop_w, crop_top, crop_left, norm_mean_0, norm_mean_1, norm_mean_2, norm_std_0, norm_std_1, norm_std_2, pad_h, pad_w, pad_top, pad_left, pad_bottom, pad_right, pad_value, src_raw_data, dst_raw_data, src_h, src_w);
  79.         } else if(EQUAL(format, "RGB")){
  80.           RGB_Nearest_Kernel(resize_h, resize_w, crop_h, crop_w, crop_top, crop_left, norm_mean_0, norm_mean_1, norm_mean_2, norm_std_0, norm_std_1, norm_std_2, pad_h, pad_w, pad_top, pad_left, pad_bottom, pad_right, pad_value, src_raw_data, dst_raw_data, src_h, src_w);
  81.         } else if(EQUAL(format, "GRAY")){
  82.           GRAY_Nearest_Kernel(resize_h, resize_w, crop_h, crop_w, crop_top, crop_left, norm_mean_0, norm_mean_1, norm_mean_2, norm_std_0, norm_std_1, norm_std_2, pad_h, pad_w, pad_top, pad_left, pad_bottom, pad_right, pad_value, src_raw_data, dst_raw_data, src_h, src_w);
  83.         } else if(EQUAL(format, "BGRA")){
  84.           BGRA_Nearest_Kernel(resize_h, resize_w, crop_h, crop_w, crop_top, crop_left, norm_mean_0, norm_mean_1, norm_mean_2, norm_std_0, norm_std_1, norm_std_2, pad_h, pad_w, pad_top, pad_left, pad_bottom, pad_right, pad_value, src_raw_data, dst_raw_data, src_h, src_w);
  85.         } else if(EQUAL(format, "NV12")){
  86.           NV12_Nearest_Kernel(resize_h, resize_w, crop_h, crop_w, crop_top, crop_left, norm_mean_0, norm_mean_1, norm_mean_2, norm_std_0, norm_std_1, norm_std_2, pad_h, pad_w, pad_top, pad_left, pad_bottom, pad_right, pad_value, src_raw_data, dst_raw_data, src_h, src_w);
  87.         } else if(EQUAL(format, "NV21")){
  88.           NV21_Nearest_Kernel(resize_h, resize_w, crop_h, crop_w, crop_top, crop_left, norm_mean_0, norm_mean_1, norm_mean_2, norm_std_0, norm_std_1, norm_std_2, pad_h, pad_w, pad_top, pad_left, pad_bottom, pad_right, pad_value, src_raw_data, dst_raw_data, src_h, src_w);
  89.         } else {
  90.            ABORT("This format is not supported");
  91.         }
  92.     } 
  93.     else if(resize_h && resize_w && EQUAL(interpolation, "bilinear")){
  94.         short* cubfh;
  95.         short* cubfw;
  96.         int* inth;
  97.         int* intw;
  98.  
  99.         cubfh = new short[resize_h*2];
  100.         cubfw = new short[resize_w*2];
  101.         inth = new int[resize_h*2];
  102.         intw = new int[resize_w*2];
  103.  
  104.         bilinear_resize_preprocess(src_h, src_w, resize_h, resize_w, cubfh, cubfw, inth, intw);
  105.  
  106.         if(EQUAL(format, "BGR")){
  107.           BGR_Bilinear_Kernel(resize_h, resize_w, crop_h, crop_w, crop_top, crop_left, norm_mean_0, norm_mean_1, norm_mean_2, norm_std_0, norm_std_1, norm_std_2, pad_h, pad_w, pad_top, pad_left, pad_bottom, pad_right, pad_value, cubfh, cubfw, inth, intw, src_raw_data, dst_raw_data, src_h, src_w);
  108.         } else if(EQUAL(format, "RGB")){
  109.           RGB_Bilinear_Kernel(resize_h, resize_w, crop_h, crop_w, crop_top, crop_left, norm_mean_0, norm_mean_1, norm_mean_2, norm_std_0, norm_std_1, norm_std_2, pad_h, pad_w, pad_top, pad_left, pad_bottom, pad_right, pad_value, cubfh, cubfw, inth, intw, src_raw_data, dst_raw_data, src_h, src_w);
  110.         } else if(EQUAL(format, "GRAY")){
  111.           GRAY_Bilinear_Kernel(resize_h, resize_w, crop_h, crop_w, crop_top, crop_left, norm_mean_0, norm_mean_1, norm_mean_2, norm_std_0, norm_std_1, norm_std_2, pad_h, pad_w, pad_top, pad_left, pad_bottom, pad_right, pad_value, cubfh, cubfw, inth, intw, src_raw_data, dst_raw_data, src_h, src_w);
  112.         } else if(EQUAL(format, "BGRA")){
  113.           BGRA_Bilinear_Kernel(resize_h, resize_w, crop_h, crop_w, crop_top, crop_left, norm_mean_0, norm_mean_1, norm_mean_2, norm_std_0, norm_std_1, norm_std_2, pad_h, pad_w, pad_top, pad_left, pad_bottom, pad_right, pad_value, cubfh, cubfw, inth, intw, src_raw_data, dst_raw_data, src_h, src_w);
  114.         } else if(EQUAL(format, "NV12")){
  115.           NV12_Bilinear_Kernel(resize_h, resize_w, crop_h, crop_w, crop_top, crop_left, norm_mean_0, norm_mean_1, norm_mean_2, norm_std_0, norm_std_1, norm_std_2, pad_h, pad_w, pad_top, pad_left, pad_bottom, pad_right, pad_value, cubfh, cubfw, inth, intw, src_raw_data, dst_raw_data, src_h, src_w);
  116.         } else if(EQUAL(format, "NV21")){
  117.           NV21_Bilinear_Kernel(resize_h, resize_w, crop_h, crop_w, crop_top, crop_left, norm_mean_0, norm_mean_1, norm_mean_2, norm_std_0, norm_std_1, norm_std_2, pad_h, pad_w, pad_top, pad_left, pad_bottom, pad_right, pad_value, cubfh, cubfw, inth, intw, src_raw_data, dst_raw_data, src_h, src_w);
  118.         } else {
  119.            ABORT("This format is not supported");
  120.         }
  121.  
  122.         delete[] cubfh;
  123.         delete[] cubfw;
  124.         delete[] inth;
  125.         delete[] intw;
  126.     }
  127.     else {
  128.       ABORT("This interpolation is not supported");
  129.     }
  130. }
  131.  
  132.  
  133. using std::string;
  134.  
  135. void FuseFunc(void* stream, uint8_t* data_in, int src_h, int src_w, const char* format,
  136.               int resize_h, int resize_w, const char* interpolation, int crop_top, int crop_left,
  137.               int crop_h, int crop_w, float mean0, float mean1, float mean2, float std0, float std1,
  138.               float std2, int pad_top, int pad_left, int pad_bottom, int pad_right, int pad_h,
  139.               int pad_w, float pad_value, float* data_out, int data_out_num) {
  140.   const char* interpolation_ = "nearest";
  141.   if (strcmp(interpolation, "bilinear") == 0) {
  142.       interpolation_ = "bilinear";
  143.   }
  144.   FuseKernel(resize_h, resize_w, crop_h, crop_w, crop_top, crop_left, mean0, mean1, mean2, std0, std1, std2,
  145.               pad_h, pad_w, pad_top, pad_left, pad_bottom, pad_right, pad_value, data_in, data_out,
  146.               src_h, src_w, format, interpolation_);
  147. }
  148.  
  149. REGISTER_FUSE_KERNEL(fa993066c1ec32a14d8d06ecfb8d5c54e7a04ec31ba833e1abf1f16c05e027c7_cpu, "fa993066c1ec32a14d8d06ecfb8d5c54e7a04ec31ba833e1abf1f16c05e027c7_cpu",
  150.                      FuseFunc);
  151. }

Editor

You can edit this paste and save as new:


File Description
  • elena cpu
  • Paste Code
  • 12 Jul-2022
  • 50.6 Kb
You can Share it: