[服务器推理加速工作-6.17]

1.解决头文件互相引用问题:

使用#parama once, 和#ifndef... #define ... #endif功能一样。详细区别https://blog.csdn.net/WInScar/article/details/7016146

2. ReQuantizeOutput初始化失败,原因是column_offsets_类型为std::shared_ptr> column_offsets_,并没有初始化,因此调用empty()失败,初始化方法为

column_offsets_ = std::make_shared>();
column_offsets_->resize(H_W);
  ReQuantizeOutput outputProcObj(
      doNothingObj,
      requantization_multipliers_.data(), //  C_multiplier
      0, //  C_zero_point
      // column_offsets_ empty means column_offsets_ are folded into bias
      0, //  Aq_zero_point
      filter_zero_points_.data(), //  Bq_zero_point
      packA.getRowOffsetBuffer(), //  row_offsets
      column_offsets_->empty() ? nullptr: column_offsets_->data(), //  col_offsets
      b_quantized_data_, //  bias
      M, //  num_output_
      1); //  group

3.vector指针的初始化方式

std::vector* top_data_ = new std::vector[sizes];    //   vector指针初始化
float * data_fp = new float[sizes]                                  //    普通float指针初始化
for (int i = 0; i < 10; i++){
		
	top_data_->push_back(i*2.3f);
	std::cout << top_data_->data()[i] << std::endl;
}

float* top_data_ptr = top_data_->data();
top_data_ptr[0] = 12.f;                                    //    如果没有赋值语句会访问出错

4.FBGEMM计算偏移量,在src/Refimplementations.cc中定义了row_offsets和column_offsets的计算方式(用于gtest)

/*
    每行所有的值相加
*/
void row_offsets_u8acc32_ref(    //    输入数据为uint8, row_offsets为int32
    int M,
    int K,
    int ld,
    const uint8_t* Aint8,
    int32_t* row_offsets) {
  // row offset
  for (int i = 0; i < M; ++i) {
    int32_t sum = 0;
    for (int k = 0; k < K; ++k) {
      sum += static_cast(Aint8[i * ld + k]);
    }
    row_offsets[i] = sum;
  }
}

/*
    每列所有的值相加
*/
void col_offsets_with_zero_pt_s8acc32_ref(    //    输入数据为sint8, col_offsets为int32,量化时指定0映射位置zero_point
    int K,
    int N,
    int ld,
    const int8_t* Bint8,
    const int32_t* B_zero_point,
    int32_t* col_offsets,
    int ncols_per_quant_group) {
  for (int j = 0; j < N; ++j) {
    int32_t sum = 0;
    for (int k = 0; k < K; ++k) {
      sum += Bint8[k * ld + j];
    }
    col_offsets[j] = sum - B_zero_point[j / ncols_per_quant_group] * K;
  }
}

5. 正确显示uint8_t的值

C++中uint8_t的实现是typedef unsigned char      uint8_t;因此实际的执行结果是打印对应的ASCII码,需要用一元运算符+或者把uint8_t转换成unsigned类型(https://ce39906.github.io/2018/06/29/C-cout%E6%89%93%E5%8D%B0uint8-t%E7%9A%84%E6%AD%A3%E7%A1%AE%E6%96%B9%E5%BC%8F/)

float* bottom_data = new float[10];
std::uint8_t* bottom_data2 = reinterpret_cast(bottom_data);    // 型强制转换
for(int i = 0; i < 10; i++){
    std::cout << unsigned(bottom_data2[i]);    //+bottom_data2[i]
}

 

你可能感兴趣的:(工作笔记)