Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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[
Expand Down Expand Up @@ -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[
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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[
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions tests/keras_tests/test_activation_quantizer_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading