@@ -449,66 +449,123 @@ void test_quantize_per_tensor_and_dequantize(
449449 const at::IntArrayRef input_shape,
450450 const double input_scale,
451451 const int input_zero_point,
452- const float tolerance = 0 ) {
453- at::Tensor input = at::rand (input_shape, at::device (at:: kCPU ). dtype (at:: kFloat ) );
452+ const c10::ScalarType dtype = c10::ScalarType::QUInt8 ) {
453+ at::Tensor input = produce_random_tensor (input_shape);
454454
455455 // quantize tensors
456456 at::Tensor out_q_cpu = at::quantize_per_tensor (
457- input, input_scale, input_zero_point, c10::ScalarType::QUInt8 );
457+ input, input_scale, input_zero_point, dtype );
458458 at::Tensor out_q_vk = at::quantize_per_tensor (
459- input.vulkan (), input_scale, input_zero_point, c10::ScalarType::QUInt8 );
459+ input.vulkan (), input_scale, input_zero_point, dtype );
460460
461461 // dequantize tensors
462462 const auto out_cpu_deq = at::dequantize (out_q_cpu);
463463 const auto out_vk_deq = at::dequantize (out_q_vk);
464+ const auto out_vk_deq_cpu = out_vk_deq.cpu ();
464465
465466 // check dequantized tensor are equal
466- const auto check = almostEqual (out_cpu_deq, out_vk_deq.cpu (), tolerance);
467+ const float tolerance = input_scale;
468+ // tolerated error = scale, to allow for precision differences after dividing
469+ // by random scale, which could result on a difference of 1 unit in the
470+ // quantized result.
471+ const auto check = almostEqual (out_cpu_deq, out_vk_deq_cpu, tolerance);
467472
468473 if (!check) {
474+ const auto error = at::abs (out_vk_deq_cpu - out_cpu_deq).max ().item <float >();
469475 std::cout
470476 << " Quantize and Dequantize failed with input shape: " << input_shape
471477 << " scale: " << input_scale << " and zero point: " << input_zero_point
472478 << std::endl;
479+ std::cout << " Error: " << error << std::endl;
473480 }
474481 ASSERT_TRUE (check);
475482}
476483
477- void test_quantize_per_tensor_and_dequantize_random () {
478- const double scale = 0.0001 + (double )rand () / (double )RAND_MAX;
479- const int zero_point = int ((double )rand () / (double )RAND_MAX * 255 );
480- const int n = 1 + int ((double )rand () / (double )RAND_MAX * 30 );
481- const int c = 1 + int ((double )rand () / (double )RAND_MAX * 30 );
482- const int h = 1 + int ((double )rand () / (double )RAND_MAX * 100 );
483- const int w = 1 + int ((double )rand () / (double )RAND_MAX * 100 );
484- // tolerated error = scale, to allow for precision differences after dividing
485- // by random scale, which could result on a difference of 1 unit in the
486- // quantized result.
487- test_quantize_per_tensor_and_dequantize ({n, c, h, w}, scale, zero_point, scale);
484+ void test_quantize_per_tensor_and_dequantize_random (
485+ const c10::ScalarType dtype) {
486+ const double scale = produce_random_scale ();
487+ const int64_t zero_point = produce_random_zero_point (dtype);
488+ const at::IntArrayRef tensor_shape =
489+ {rand_pos_int (30 ), rand_pos_int (30 ), rand_pos_int (100 ), rand_pos_int (100 )};
490+ test_quantize_per_tensor_and_dequantize (
491+ tensor_shape, scale, zero_point, dtype);
492+ }
493+
494+ TEST_F (VulkanAPITest, quantize_per_tensor_and_dequantize_quint8) {
495+ const c10::ScalarType dtype = c10::ScalarType::QUInt8;
496+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 1 , 1 }, 0.13 , 21 , dtype);
497+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 1 , 4 }, 0.3 , 87 , dtype);
498+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 4 , 1 }, 0.2 , 120 , dtype);
499+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 7 , 7 }, 0.3 , 87 , dtype);
500+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 8 , 8 }, 0.1 , 10 , dtype);
501+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 8 , 8 }, 0.04 , 97 , dtype);
502+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 11 , 17 }, 0.07 , 15 , dtype);
503+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 12 , 17 }, 0.1 , 10 , dtype);
504+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 12 , 17 }, 0.1 , 10 , dtype);
505+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 17 , 12 }, 0.1 , 10 , dtype);
506+ test_quantize_per_tensor_and_dequantize ({2 , 4 , 17 , 12 }, 0.1 , 10 , dtype);
507+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 10 , 14 }, 0.001 , 101 , dtype);
508+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 10 , 14 }, 0.009 , 43 , dtype);
509+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 10 , 15 }, 0.1 , 19 , dtype);
510+ test_quantize_per_tensor_and_dequantize ({4 , 4 , 9 , 17 }, 0.1 , 19 , dtype);
511+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 25 , 29 }, 0.1 , 19 , dtype);
512+ test_quantize_per_tensor_and_dequantize ({4 , 4 , 25 , 29 }, 0.1 , 19 , dtype);
513+ test_quantize_per_tensor_and_dequantize ({11 , 17 , 25 , 29 }, 0.027 , 89 , dtype);
514+
515+ for (int i = 0 ; i < 20 ; i += 1 ) {
516+ test_quantize_per_tensor_and_dequantize_random (dtype);
517+ }
518+ }
519+
520+ TEST_F (VulkanAPITest, quantize_per_tensor_and_dequantize_qint8) {
521+ const c10::ScalarType dtype = c10::ScalarType::QInt8;
522+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 1 , 1 }, 0.13 , -21 , dtype);
523+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 1 , 4 }, 0.3 , 87 , dtype);
524+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 4 , 1 }, 0.2 , -120 , dtype);
525+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 7 , 7 }, 0.3 , 87 , dtype);
526+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 8 , 8 }, 0.1 , -10 , dtype);
527+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 8 , 8 }, 0.04 , 97 , dtype);
528+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 11 , 17 }, 0.07 , -15 , dtype);
529+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 12 , 17 }, 0.1 , 10 , dtype);
530+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 12 , 17 }, 0.1 , -10 , dtype);
531+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 17 , 12 }, 0.1 , 10 , dtype);
532+ test_quantize_per_tensor_and_dequantize ({2 , 4 , 17 , 12 }, 0.1 , -10 , dtype);
533+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 10 , 14 }, 0.001 , 101 , dtype);
534+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 10 , 14 }, 0.009 , -43 , dtype);
535+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 10 , 15 }, 0.1 , 19 , dtype);
536+ test_quantize_per_tensor_and_dequantize ({4 , 4 , 9 , 17 }, 0.1 , -19 , dtype);
537+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 25 , 29 }, 0.1 , 19 , dtype);
538+ test_quantize_per_tensor_and_dequantize ({4 , 4 , 25 , 29 }, 0.1 , -19 , dtype);
539+ test_quantize_per_tensor_and_dequantize ({11 , 17 , 25 , 29 }, 0.027 , 89 , dtype);
540+
541+ for (int i = 0 ; i < 20 ; i += 1 ) {
542+ test_quantize_per_tensor_and_dequantize_random (dtype);
543+ }
488544}
489545
490- TEST_F (VulkanAPITest, quantize_per_tensor_and_dequantize) {
491- test_quantize_per_tensor_and_dequantize ({1 , 1 , 1 , 1 }, 0.13 , 21 );
492- test_quantize_per_tensor_and_dequantize ({1 , 1 , 1 , 4 }, 0.3 , 87 );
493- test_quantize_per_tensor_and_dequantize ({1 , 1 , 4 , 1 }, 0.2 , 120 );
494- test_quantize_per_tensor_and_dequantize ({1 , 1 , 7 , 7 }, 0.3 , 87 );
495- test_quantize_per_tensor_and_dequantize ({1 , 1 , 8 , 8 }, 0.1 , 10 );
496- test_quantize_per_tensor_and_dequantize ({3 , 5 , 8 , 8 }, 0.04 , 97 );
497- test_quantize_per_tensor_and_dequantize ({1 , 1 , 11 , 17 }, 0.07 , 15 );
498- test_quantize_per_tensor_and_dequantize ({1 , 1 , 12 , 17 }, 0.1 , 10 );
499- test_quantize_per_tensor_and_dequantize ({3 , 5 , 12 , 17 }, 0.1 , 10 );
500- test_quantize_per_tensor_and_dequantize ({1 , 1 , 17 , 12 }, 0.1 , 10 );
501- test_quantize_per_tensor_and_dequantize ({2 , 4 , 17 , 12 }, 0.1 , 10 );
502- test_quantize_per_tensor_and_dequantize ({1 , 1 , 10 , 14 }, 0.0001 , 101 );
503- test_quantize_per_tensor_and_dequantize ({3 , 5 , 10 , 14 }, 0.009 , 43 );
504- test_quantize_per_tensor_and_dequantize ({3 , 5 , 10 , 15 }, 0.1 , 19 );
505- test_quantize_per_tensor_and_dequantize ({4 , 4 , 9 , 17 }, 0.1 , 19 );
506- test_quantize_per_tensor_and_dequantize ({3 , 5 , 25 , 29 }, 0.1 , 19 );
507- test_quantize_per_tensor_and_dequantize ({4 , 4 , 25 , 29 }, 0.1 , 19 );
508- test_quantize_per_tensor_and_dequantize ({11 , 17 , 25 , 29 }, 0.027 , 89 );
546+ TEST_F (VulkanAPITest, quantize_per_tensor_and_dequantize_qint32) {
547+ const c10::ScalarType dtype = c10::ScalarType::QInt32;
548+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 1 , 1 }, 0.13 , -21123 , dtype);
549+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 1 , 4 }, 0.339 , 8734 , dtype);
550+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 4 , 1 }, 0.228 , -12023 , dtype);
551+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 7 , 7 }, 0.338 , 8723 , dtype);
552+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 8 , 8 }, 0.193 , -1023 , dtype);
553+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 8 , 8 }, 0.0449 , 972 , dtype);
554+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 11 , 17 }, 0.073 , -15 , dtype);
555+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 12 , 17 }, 0.1572 , 102 , dtype);
556+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 12 , 17 }, 0.147 , -156 , dtype);
557+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 17 , 12 }, 0.129 , 10448 , dtype);
558+ test_quantize_per_tensor_and_dequantize ({2 , 4 , 17 , 12 }, 0.137 , -10 , dtype);
559+ test_quantize_per_tensor_and_dequantize ({1 , 1 , 10 , 14 }, 0.001 , 101 , dtype);
560+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 10 , 14 }, 0.009 , -43267 , dtype);
561+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 10 , 15 }, 0.1243 , 19 , dtype);
562+ test_quantize_per_tensor_and_dequantize ({4 , 4 , 9 , 17 }, 0.1889 , -19784 , dtype);
563+ test_quantize_per_tensor_and_dequantize ({3 , 5 , 25 , 29 }, 0.1345 , 196 , dtype);
564+ test_quantize_per_tensor_and_dequantize ({4 , 4 , 25 , 29 }, 0.129 , -19489 , dtype);
565+ test_quantize_per_tensor_and_dequantize ({11 , 17 , 25 , 29 }, 0.027 , 89 , dtype);
509566
510567 for (int i = 0 ; i < 20 ; i += 1 ) {
511- test_quantize_per_tensor_and_dequantize_random ();
568+ test_quantize_per_tensor_and_dequantize_random (dtype );
512569 }
513570}
514571
0 commit comments