tensorflow/core/kernels/quantization_utils_test.cc
// If eigen_device is NULL, then the reference implementation is tested.
If eigen_device is NULL, then the reference implementation is tested.
If eigen_device is NULL then the reference implementation is tested .
void TestRequantizeManyInNewRange32To8Bit(
    Eigen::ThreadPoolDevice* eigen_device) {
  if (true) {
    const size_t values_count = 6;
    const float values[values_count] = {0.0f,  0.45f,  1.0f,
                                        -1.0f, 127.0f, 255.0f};
    const size_t ranges_count = 6;
    const float ranges[ranges_count][4] = {
        {0.0f, 255.0f, 0.0f, 255.0f},    
        {0.0f, 1.0f, 0.0f, 1.0f},        
        {-1.0f, 1.0f, -1.0f, 1.0f},      
        {-1.0f, 1.0f, -255.0f, 255.0f},  
        {3.0f, 3.0f, 0.0f, 255.0f},       input min == max
        {0.0f, 255.0f, 5.0f, 5.0f},       output min == max
    };
    for (int i = 0; i < ranges_count; ++i) {
      const auto& r = ranges[i];
      std::vector<qint32> values_quantized;
      for (int value_index = 0; value_index < values_count; ++value_index) {
        const float v = values[value_index];
        values_quantized.push_back(FloatToQuantized<qint32>(v, r[0], r[1]));
      }
      TestRequantizeMany(eigen_device, r[0], r[1], r[2], r[3],
                         values_quantized);
    }
     Test with many different values in the input quantized range.
    qint32 low = Eigen::NumTraits<qint32>::lowest();
    qint32 high = Eigen::NumTraits<qint32>::highest();
    std::vector<qint32> vals{low, high};
    int num_steps = 14419;
    qint32 step = static_cast<int32>((1LL << 32) / num_steps);
    qint32 v = low + static_cast<qint32>(1);
    for (int i = 0; i < num_steps; ++i) {
      vals.push_back(v);
      v += step;
    }
    TestRequantizeMany(eigen_device, -1.0f, 1.0f, -1.0f, 1.0f, vals);
    TestRequantizeMany(eigen_device, -255.0f, 255.0f, -255.0f, 255.0f, vals);
    TestRequantizeMany(eigen_device, -1.0f, 1.0f, -12345678.0f, 12345678.0f,
                       vals);
    TestRequantizeMany(eigen_device, -1.0f, 12345678.0f, -12345678.0f,
                       12345678.0f, vals);
  }
   Test when the input range is large and output range is small.
   Use all quantized values where the float is in the output range.
  const float out_min = -29.1234;
  const float out_max = 23.1234;
  const float in_min = -1e6;
  const float in_max = 1e6;
  qint32 low = FloatToQuantized<qint32>(out_min, in_min, in_max);
  qint32 high = FloatToQuantized<qint32>(out_max, in_min, in_max);
  std::vector<qint32> vals;
  vals.clear();
  for (int32_t i = low; i <= high; ++i) vals.push_back(i);
  TestRequantizeMany(eigen_device, in_min, in_max, out_min, out_max, vals);
}
void TestRequantizeManyInNewRange32To8Bit ( Eigen :: ThreadPoolDevice * eigen_device ) { if ( true ) { const size_t values_count = 6 ; const float values [ values_count ] = { 0.0f , 0.45f , 1.0f , -1.0f , 127.0f , 255.0f } ; const size_t ranges_count = 6 ; const float ranges [ ranges_count ] [ 4 ] = { { 0.0f , 255.0f , 0.0f , 255.0f } , { 0.0f , 1.0f , 0.0f , 1.0f } , { -1.0f , 1.0f , -1.0f , 1.0f } , { -1.0f , 1.0f , -255.0f , 255.0f } , { 3.0f , 3.0f , 0.0f , 255.0f } , { 0.0f , 255.0f , 5.0f , 5.0f } , } ; for ( int i = 0 ; i < ranges_count ; ++ i ) { const auto & r = ranges [ i ] ; std :: vector < qint32 > values_quantized ; for ( int value_index = 0 ; value_index < values_count ; ++ value_index ) { const float v = values [ value_index ] ; values_quantized . push_back ( FloatToQuantized < qint32 > ( v , r [ 0 ] , r [ 1 ] ) ) ; } TestRequantizeMany ( eigen_device , r [ 0 ] , r [ 1 ] , r [ 2 ] , r [ 3 ] , values_quantized ) ; } qint32 low = Eigen :: NumTraits < qint32 > :: lowest ( ) ; qint32 high = Eigen :: NumTraits < qint32 > :: highest ( ) ; std :: vector < qint32 > vals { low , high } ; int num_steps = 14419 ; qint32 step = static_cast < int32 > ( ( 1LL << 32 ) / num_steps ) ; qint32 v = low + static_cast < qint32 > ( 1 ) ; for ( int i = 0 ; i < num_steps ; ++ i ) { vals . push_back ( v ) ; v += step ; } TestRequantizeMany ( eigen_device , -1.0f , 1.0f , -1.0f , 1.0f , vals ) ; TestRequantizeMany ( eigen_device , -255.0f , 255.0f , -255.0f , 255.0f , vals ) ; TestRequantizeMany ( eigen_device , -1.0f , 1.0f , -12345678.0f , 12345678.0f , vals ) ; TestRequantizeMany ( eigen_device , -1.0f , 12345678.0f , -12345678.0f , 12345678.0f , vals ) ; } const float out_min = -29.1234 ; const float out_max = 23.1234 ; const float in_min = -1e6 ; const float in_max = 1e6 ; qint32 low = FloatToQuantized < qint32 > ( out_min , in_min , in_max ) ; qint32 high = FloatToQuantized < qint32 > ( out_max , in_min , in_max ) ; std :: vector < qint32 > vals ; vals . clear ( ) ; for ( int32_t i = low ; i <= high ; ++ i ) vals . push_back ( i ) ; TestRequantizeMany ( eigen_device , in_min , in_max , out_min , out_max , vals ) ; }
If eigen_device is NULL, then the reference implementation is tested.
If eigen_device is NULL then the reference implementation is tested .
// These are the float values we're going to test the conversions on. // These are the input and output ranges we'll test. // // // // // input min == max // output min == max // Test with many different values in the input quantized range. // Test when the input range is large and output range is small. // Use all quantized values where the float is in the output range.