diff --git a/tests/keras_tests/quantizers_tests/test_activation_inferable_quantizers.py b/tests/keras_tests/quantizers_tests/test_activation_inferable_quantizers.py index 900ba85..d91b7a3 100644 --- a/tests/keras_tests/quantizers_tests/test_activation_inferable_quantizers.py +++ b/tests/keras_tests/quantizers_tests/test_activation_inferable_quantizers.py @@ -43,8 +43,10 @@ def test_symmetric_activation_quantizer(self): self.assertTrue(quantizer_config['threshold'] == thresholds) self.assertTrue(quantizer_config['signed'] == signed) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 50, 3) * 100 - 50, tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 50, 3) * 50 + signs = np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) # Quantize tensor quantized_tensor = quantizer(input_tensor) @@ -87,8 +89,10 @@ def test_unsigned_symmetric_activation_quantizer(self): self.assertTrue(quantizer_config['threshold'] == thresholds) self.assertTrue(quantizer_config['signed'] == signed) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 50, 3) * 100 - 50, tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 50, 3) * 50 + signs = np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) # Quantize tensor quantized_tensor = quantizer(input_tensor) @@ -144,8 +148,10 @@ def test_power_of_two_activation_quantizer(self): self.assertTrue(np.all(quantizer.min_range == -1 * thresholds)) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 50, 3) * 100 - 50, tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 50, 3) * 50 + signs = np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) fake_quantized_tensor = quantizer(input_tensor) self.assertTrue(np.max(fake_quantized_tensor) < thresholds[ @@ -191,8 +197,10 @@ def test_unsigned_power_of_two_activation_quantizer(self): self.assertTrue(np.all(quantizer.min_range == [0])) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 50, 3) * 100 - 50, tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 50, 3) * 50 + signs = np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) fake_quantized_tensor = quantizer(input_tensor) self.assertTrue(np.max(fake_quantized_tensor) < thresholds[ @@ -232,8 +240,10 @@ def test_uniform_activation_quantizer(self): self.assertTrue(quantizer_config['min_range'] == min_range) self.assertTrue(quantizer_config['max_range'] == max_range) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 4, 50) * 100 - 50, tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 4, 50) * 50 + signs = np.where(np.indices((1, 50, 4, 50)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) fake_quantized_tensor = quantizer(input_tensor) # We expect tensor values values to be between min_range to max_range @@ -272,8 +282,10 @@ def test_illegal_range_uniform_activation_quantizer(self): # self.assertTrue(quantizer_config['min_range'] == min_range) # self.assertTrue(quantizer_config['max_range'] == max_range) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 4, 50) * 100 - 50, tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 4, 50) * 50 + signs = np.where(np.indices((1, 50, 4, 50)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) fake_quantized_tensor = quantizer(input_tensor) # We expect each channel values to be between min_range to max_range for each channel diff --git a/tests/keras_tests/quantizers_tests/test_activation_lut_pot_inferable_quantizer.py b/tests/keras_tests/quantizers_tests/test_activation_lut_pot_inferable_quantizer.py index 58c9aa2..50d8973 100644 --- a/tests/keras_tests/quantizers_tests/test_activation_lut_pot_inferable_quantizer.py +++ b/tests/keras_tests/quantizers_tests/test_activation_lut_pot_inferable_quantizer.py @@ -50,8 +50,10 @@ def test_lut_pot_signed_quantizer(self): self.assertTrue(quantizer_config['lut_values_bitwidth'] == lut_values_bitwidth) self.assertTrue(quantizer_config['eps'] == eps) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 50, 3) * 100 - 50, tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 50, 3) * 50 + signs = np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) quantized_tensor = quantizer(input_tensor) # Using a signed quantization, so we expect all values to be between -abs(max(threshold)) @@ -120,8 +122,10 @@ def test_lut_pot_unsigned_quantizer(self): self.assertTrue(quantizer_config['lut_values_bitwidth'] == lut_values_bitwidth) self.assertTrue(quantizer_config['eps'] == eps) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 50, 3) * 100 - 50, tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 50, 3) * 50 + signs = np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) quantized_tensor = quantizer(input_tensor) # Using a unsigned quantization, so we expect all values to be between 0 diff --git a/tests/keras_tests/quantizers_tests/test_illegal_weights_lut_inferable_quantizer.py b/tests/keras_tests/quantizers_tests/test_illegal_weights_lut_inferable_quantizer.py index e1571c6..56436a5 100644 --- a/tests/keras_tests/quantizers_tests/test_illegal_weights_lut_inferable_quantizer.py +++ b/tests/keras_tests/quantizers_tests/test_illegal_weights_lut_inferable_quantizer.py @@ -168,8 +168,10 @@ def weights_inferable_quantizer_test(self, inferable_quantizer, num_bits, thresh perm_vec[channel_axis] = input_rank - 1 perm_vec[input_rank - 1] = channel_axis - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 50, 3) * 100 - 50, dtype=tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 50, 3) * 50 + signs = np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) # change the input only when channel_axis is not the last axis input_tensor = tf.transpose(input_tensor, perm=perm_vec) diff --git a/tests/keras_tests/quantizers_tests/test_weights_inferable_quantizer.py b/tests/keras_tests/quantizers_tests/test_weights_inferable_quantizer.py index 0b56b14..35fe7f2 100644 --- a/tests/keras_tests/quantizers_tests/test_weights_inferable_quantizer.py +++ b/tests/keras_tests/quantizers_tests/test_weights_inferable_quantizer.py @@ -42,8 +42,10 @@ def test_symmetric_weights_quantizer_per_tensor(self): self.assertTrue(quantizer_config['per_channel'] is False) self.assertTrue(quantizer_config['channel_axis'] is None) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 50, 3) * 100 - 50, dtype=tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 50, 3) * 50 + signs = np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) # Quantize tensor quantized_tensor = quantizer(input_tensor) @@ -86,8 +88,10 @@ def test_symmetric_weights_quantizer_per_channel(self): self.assertTrue(quantizer_config['channel_axis'] == 3) thresholds = np.asarray(thresholds) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 50, 3) * 100 - 50, dtype=tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 50, 3) * 50 + signs = np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) # Quantize tensor quantized_tensor = quantizer(input_tensor) @@ -139,8 +143,10 @@ def test_power_of_two_weights_quantizer_per_channel(self): self.assertTrue(np.all(quantizer.min_range == -1 * thresholds)) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 50, 3) * 100 - 50, dtype=tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 50, 3) * 50 + signs = np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) fake_quantized_tensor = quantizer(input_tensor) # We expect each channel values to be between -threshold to threshold since it's a signed quantization @@ -188,8 +194,10 @@ def test_power_of_two_weights_quantizer_per_tensor(self): np.log2(delta) == np.log2(delta).astype(int)) self.assertTrue(is_pot_delta, f'Expected delta to be POT but: {delta}') - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 50, 3) * 100 - 50, dtype=tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 50, 3) * 50 + signs = np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) fake_quantized_tensor = quantizer(input_tensor) self.assertTrue(np.max(fake_quantized_tensor) < thresholds[ @@ -231,8 +239,10 @@ def test_uniform_weights_quantizer_per_channel(self): self.assertTrue(quantizer_config['per_channel'] is True) self.assertTrue(quantizer_config['channel_axis'] == channel_axis) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 4, 50, 50) * 100 - 50, dtype=tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 4, 50, 50) * 50 + signs = np.where(np.indices((1, 4, 50, 50)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) fake_quantized_tensor = quantizer(input_tensor) min_range = np.asarray(min_range) @@ -286,8 +296,10 @@ def test_uniform_weights_quantizer_per_tensor(self): self.assertTrue(quantizer_config['per_channel'] is False) self.assertTrue(quantizer_config['channel_axis'] == channel_axis) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 4, 50) * 100 - 50, dtype=tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 4, 50) * 50 + signs = np.where(np.indices((1, 50, 4, 50)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) fake_quantized_tensor = quantizer(input_tensor) min_range = np.asarray(min_range) @@ -348,8 +360,10 @@ def test_uniform_weights_quantizer_zero_not_in_range(self): # self.assertTrue(quantizer_config['max_range'][i] == max_adj) # self.assertTrue(quantizer_config['min_range'][i] == min_adj) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 4, 50) * 100 - 50, dtype=tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 4, 50) * 50 + signs = np.where(np.indices((1, 50, 4, 50)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) fake_quantized_tensor = quantizer(input_tensor) # We expect each channel values to be between min_range to max_range for each channel diff --git a/tests/keras_tests/quantizers_tests/test_weights_lut_inferable_quantizer.py b/tests/keras_tests/quantizers_tests/test_weights_lut_inferable_quantizer.py index 52f93b7..5d3165f 100644 --- a/tests/keras_tests/quantizers_tests/test_weights_lut_inferable_quantizer.py +++ b/tests/keras_tests/quantizers_tests/test_weights_lut_inferable_quantizer.py @@ -55,8 +55,10 @@ def _weights_lut_quantizer_test(self, inferable_quantizer, num_bits, threshold, perm_vec[channel_axis] = input_rank - 1 perm_vec[input_rank - 1] = channel_axis - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 50, 3) * 100 - 50, dtype=tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 50, 3) * 50 + signs = np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) # change the input only when channel_axis is not the last axis input_tensor = tf.transpose(input_tensor, perm=perm_vec) diff --git a/tests/keras_tests/test_activation_quantizer_holder.py b/tests/keras_tests/test_activation_quantizer_holder.py index eef93d0..d42784e 100644 --- a/tests/keras_tests/test_activation_quantizer_holder.py +++ b/tests/keras_tests/test_activation_quantizer_holder.py @@ -40,8 +40,10 @@ def test_activation_quantization_holder_inference(self): signed=signed) model = keras.Sequential([KerasActivationQuantizationHolder(quantizer)]) - # Initialize a random input to quantize between -50 to 50. - input_tensor = tf.constant(np.random.rand(1, 50, 50, 3) * 100 - 50, tf.float32) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = np.random.rand(1, 50, 50, 3) * 50 + signs = np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8) + input_tensor = tf.constant(input_tensor * signs, dtype=tf.float32) # Quantize tensor quantized_tensor = model(input_tensor) diff --git a/tests/pytorch_tests/quantizers_tests/test_activation_lut_inferable_quantizer.py b/tests/pytorch_tests/quantizers_tests/test_activation_lut_inferable_quantizer.py index ffc787e..1e07009 100644 --- a/tests/pytorch_tests/quantizers_tests/test_activation_lut_inferable_quantizer.py +++ b/tests/pytorch_tests/quantizers_tests/test_activation_lut_inferable_quantizer.py @@ -38,8 +38,10 @@ def test_lut_pot_signed_quantizer(self): lut_values_bitwidth, threshold=thresholds) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 3, 3, 3) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 3, 3, 3) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 3, 3, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs fake_quantized_tensor = quantizer(input_tensor.to(get_working_device())) # Using a signed quantization, so we expect all values to be between -abs(max(threshold)) @@ -98,8 +100,10 @@ def test_lut_pot_unsigned_quantizer(self): lut_values_bitwidth, threshold=thresholds) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 3, 3, 3) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 3, 3, 3) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 3, 3, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs fake_quantized_tensor = quantizer(input_tensor.to(get_working_device())) # Using a unsigned quantization, so we expect all values to be between 0 diff --git a/tests/pytorch_tests/quantizers_tests/test_activations_inferable_quantizer.py b/tests/pytorch_tests/quantizers_tests/test_activations_inferable_quantizer.py index de4b6be..dc7b9f7 100644 --- a/tests/pytorch_tests/quantizers_tests/test_activations_inferable_quantizer.py +++ b/tests/pytorch_tests/quantizers_tests/test_activations_inferable_quantizer.py @@ -35,8 +35,10 @@ def test_symmetric_activation_quantizer(self): threshold=thresholds, signed=True) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 50, 50, 3) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 50, 50, 3) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs quantized_tensor = quantizer(input_tensor.to(get_working_device())) # The maximal threshold is 4 using a signed quantization, so we expect all values to be in this range @@ -68,8 +70,10 @@ def test_unsigned_symmetric_activation_quantizer(self): threshold=thresholds, signed=False) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 50, 50, 3) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 50, 50, 3) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs quantized_tensor = quantizer(input_tensor.to(get_working_device())) # The maximal threshold is 4 using a signed quantization, so we expect all values to be in this range @@ -116,8 +120,10 @@ def test_power_of_two_activation_quantizer(self): signed=True, threshold=thresholds) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 3, 3, 3) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 3, 3, 3) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 3, 3, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs fake_quantized_tensor = quantizer(input_tensor.to(get_working_device())).dequantize() assert torch.max( @@ -146,8 +152,10 @@ def test_unsigned_power_of_two_activation_quantizer(self): signed=False, threshold=thresholds) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 3, 3, 3) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 3, 3, 3) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 3, 3, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs fake_quantized_tensor = quantizer(input_tensor.to(get_working_device())).dequantize() assert torch.max( @@ -173,8 +181,10 @@ def test_uniform_activation_quantizer(self): min_range=min_range, max_range=max_range) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 50, 50, 3) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 50, 50, 3) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs quantized_tensor = quantizer(input_tensor.to(get_working_device())) # The maximal threshold is 4 using a signed quantization, so we expect all values to be in this range @@ -209,8 +219,10 @@ def test_illegal_range_uniform_activation_quantizer(self): min_range=min_range, max_range=max_range) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 50, 50, 3) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 50, 50, 3) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs quantized_tensor = quantizer(input_tensor.to(get_working_device())) # The maximal threshold is 4 using a signed quantization, so we expect all values to be in this range diff --git a/tests/pytorch_tests/quantizers_tests/test_illegal_weights_inferable_quantizer.py b/tests/pytorch_tests/quantizers_tests/test_illegal_weights_inferable_quantizer.py index a6c8e86..af65203 100644 --- a/tests/pytorch_tests/quantizers_tests/test_illegal_weights_inferable_quantizer.py +++ b/tests/pytorch_tests/quantizers_tests/test_illegal_weights_inferable_quantizer.py @@ -61,8 +61,10 @@ def test_zero_not_in_range_uniform_quantizer(self): max_range=max_range, channel_axis=2) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 50, 4, 50) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 50, 4, 50) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 50, 4, 50)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs fake_quantized_tensor = quantizer(input_tensor.to(get_working_device())) # We expect each channel values to be between min_range to max_range for each channel diff --git a/tests/pytorch_tests/quantizers_tests/test_weights_inferable_quantizer.py b/tests/pytorch_tests/quantizers_tests/test_weights_inferable_quantizer.py index 5856e54..fadb24b 100644 --- a/tests/pytorch_tests/quantizers_tests/test_weights_inferable_quantizer.py +++ b/tests/pytorch_tests/quantizers_tests/test_weights_inferable_quantizer.py @@ -35,8 +35,10 @@ def test_symmetric_weights_quantizer_per_tensor(self): per_channel=False, threshold=thresholds) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 50, 50, 3) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 50, 50, 3) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs # Quantize tensor quantized_tensor = quantizer(input_tensor.to(get_working_device())) @@ -69,8 +71,10 @@ def test_symmetric_weights_quantizer_per_channel(self): threshold=thresholds, channel_axis=3) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 50, 50, 3) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 50, 50, 3) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs # Quantize tensor quantized_tensor = quantizer(input_tensor.to(get_working_device())) fake_quantized_tensor = quantized_tensor @@ -110,8 +114,10 @@ def test_pot_weights_quantizer_per_channel(self): is_pot_scales = torch.all(quantizer.scales.log2().int() == quantizer.scales.log2()) self.assertTrue(is_pot_scales, f'Expected scales to be POT but: {quantizer.scales}') - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 50, 50, 3) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 50, 50, 3) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs fake_quantized_tensor = quantizer(input_tensor.to(get_working_device())) # We expect each channel values to be between -threshold to threshold since it's a signed quantization @@ -150,8 +156,10 @@ def test_pot_weights_quantizer_per_tensor(self): f'Expected to have one scale in per-tensor quantization but found ' f'{len(quantizer.scales)} scales') - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 50, 50, 3) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 50, 50, 3) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 50, 50, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs fake_quantized_tensor = quantizer(input_tensor.to(get_working_device())) assert torch.max(fake_quantized_tensor) < thresholds[ @@ -182,8 +190,10 @@ def test_uniform_weights_quantizer_per_channel(self): max_range=max_range, channel_axis=2) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 50, 4, 50) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 50, 4, 50) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 50, 4, 50)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs fake_quantized_tensor = quantizer(input_tensor.to(get_working_device())) # We expect each channel values to be between min_range to max_range for each channel @@ -222,8 +232,10 @@ def test_uniform_weights_quantizer_per_tensor(self): min_range=min_range, max_range=max_range) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 50, 4, 50) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 50, 4, 50) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 50, 4, 50)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs fake_quantized_tensor = quantizer(input_tensor.to(get_working_device())) # We expect tensor values values to be between min_range to max_range diff --git a/tests/pytorch_tests/quantizers_tests/test_weights_lut_inferable_quantizer.py b/tests/pytorch_tests/quantizers_tests/test_weights_lut_inferable_quantizer.py index 484faa7..14df0dd 100644 --- a/tests/pytorch_tests/quantizers_tests/test_weights_lut_inferable_quantizer.py +++ b/tests/pytorch_tests/quantizers_tests/test_weights_lut_inferable_quantizer.py @@ -34,8 +34,10 @@ def _weights_lut_quantizer_test(self, inferable_quantizer, num_bits, threshold, lut_values_bitwidth=lut_values_bitwidth, input_rank=input_rank) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.rand(1, 3, 3, 3) * 100 - 50 + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 3, 3, 3) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 3, 3, 3)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs fake_quantized_tensor = quantizer(input_tensor.to(get_working_device())) # Using a signed quantization, so we expect all values to be between -abs(max(threshold)) diff --git a/tests/pytorch_tests/test_activation_quantizer_holder.py b/tests/pytorch_tests/test_activation_quantizer_holder.py index 084129a..6ff55f6 100644 --- a/tests/pytorch_tests/test_activation_quantizer_holder.py +++ b/tests/pytorch_tests/test_activation_quantizer_holder.py @@ -36,8 +36,10 @@ def test_activation_quantization_holder_inference(self): signed=signed) model = PytorchActivationQuantizationHolder(quantizer) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.from_numpy(np.random.rand(1, 3, 50, 50). astype(np.float32) * 100 - 50, ) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 3, 50, 50) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 3, 50, 50)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs # Quantize tensor quantized_tensor = model(input_tensor) @@ -70,8 +72,10 @@ def test_activation_quantization_holder_save_and_load(self): quantizer = quantizer_class(**quantizer_args) model = PytorchActivationQuantizationHolder(quantizer) - # Initialize a random input to quantize between -50 to 50. - x = torch.from_numpy(np.random.rand(1, 3, 50, 50). astype(np.float32) * 100 - 50, ) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + x = torch.rand(1, 3, 50, 50) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 3, 50, 50)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + x = x * signs exp_output_tensor = model(x) fx_model = symbolic_trace(model) diff --git a/tests/pytorch_tests/test_fln_activation_quantizer_holder.py b/tests/pytorch_tests/test_fln_activation_quantizer_holder.py index 6c69cfc..424ff1b 100644 --- a/tests/pytorch_tests/test_fln_activation_quantizer_holder.py +++ b/tests/pytorch_tests/test_fln_activation_quantizer_holder.py @@ -51,8 +51,10 @@ def test_fln_activation_quantization_holder_inference(self): quantizer = quantizer_class(**quantizer_args) model = PytorchFLNActivationQuantizationHolder(quantizer, quantization_bypass) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.from_numpy(np.random.rand(1, 3, 50, 50). astype(np.float32) * 100 - 50, ) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 3, 50, 50) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 3, 50, 50)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs # Quantize tensor quantized_tensor = model(input_tensor) @@ -108,8 +110,10 @@ def test_fln_activation_quantization_holder_save_and_load(self): quantizer = quantizer_class(**quantizer_args) model = PytorchFLNActivationQuantizationHolder(quantizer, quantization_bypass) - # Initialize a random input to quantize between -50 to 50. - x = torch.from_numpy(np.random.rand(1, 3, 50, 50). astype(np.float32) * 100 - 50, ) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + x = torch.rand(1, 3, 50, 50) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 3, 50, 50)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + x = x * signs exp_output_tensor = model(x) fx_model = symbolic_trace(model) diff --git a/tests/pytorch_tests/test_preserving_activation_quantizer_holder.py b/tests/pytorch_tests/test_preserving_activation_quantizer_holder.py index a994410..829faa9 100644 --- a/tests/pytorch_tests/test_preserving_activation_quantizer_holder.py +++ b/tests/pytorch_tests/test_preserving_activation_quantizer_holder.py @@ -51,8 +51,10 @@ def test_preserving_activation_quantization_holder_inference(self): quantizer = quantizer_class(**quantizer_args) model = PytorchPreservingActivationQuantizationHolder(quantizer, quantization_bypass) - # Initialize a random input to quantize between -50 to 50. - input_tensor = torch.from_numpy(np.random.rand(1, 3, 50, 50). astype(np.float32) * 100 - 50, ) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + input_tensor = torch.rand(1, 3, 50, 50) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 3, 50, 50)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + input_tensor = input_tensor * signs # Quantize tensor quantized_tensor = model(input_tensor) @@ -108,8 +110,10 @@ def test_preserving_activation_quantization_holder_save_and_load(self): quantizer = quantizer_class(**quantizer_args) model = PytorchPreservingActivationQuantizationHolder(quantizer, quantization_bypass) - # Initialize a random input to quantize between -50 to 50. - x = torch.from_numpy(np.random.rand(1, 3, 50, 50). astype(np.float32) * 100 - 50, ) + # Initialize a random input to quantize between -50 to 50. Input includes positive and negative values. + x = torch.rand(1, 3, 50, 50) * 50 + signs = torch.from_numpy(np.where(np.indices((1, 3, 50, 50)).sum(axis=0) % 2 == 0, 1, -1).astype(np.int8)) + x = x * signs exp_output_tensor = model(x) fx_model = symbolic_trace(model)