def test_unpack_from_int(self, test_values, dtype): original_bitrange, packed_value = test_values packed_value = tf.constant(packed_value, dtype) unpacked_value = tf_utils.unpack_from_int(packed_value, original_bitrange, target_bitrange=28, shape=(5, )) self.assertEqual(dtype, unpacked_value.dtype) self.assertAllEqual([1, 0, 1, 1, 0], self.evaluate(unpacked_value))
def test_unpack_from_int_special_case_8_28(self): packed_value = tf.constant([[151098150], [162680028], [6464334]]) unpacked_value = tf_utils.unpack_from_int(packed_value, original_bitrange=8, target_bitrange=28, shape=(10, )) expected_unpacked_value = tf.constant( [38, 147, 1, 201, 205, 36, 155, 78, 163, 98]) self.assertAllEqual(self.evaluate(expected_unpacked_value), self.evaluate(unpacked_value))
def test_unpack_from_int_special_case_7_28(self): packed_value = tf.constant([[145402741], [17867529], [14960]]) unpacked_value = tf_utils.unpack_from_int(packed_value, original_bitrange=7, target_bitrange=28, shape=(10, )) expected_unpacked_value = tf.constant( [117, 86, 42, 69, 9, 70, 66, 8, 112, 116]) self.assertAllEqual(self.evaluate(expected_unpacked_value), self.evaluate(unpacked_value))
def test_unpack_from_int_special_case_12_28(self): packed_value = tf.constant([[147589877], [153981074], [187904011], [112475767], [150]]) unpacked_value = tf_utils.unpack_from_int(packed_value, original_bitrange=12, target_bitrange=28, shape=(10, )) expected_unpacked_value = tf.constant( [2805, 3264, 2344, 3472, 2962, 768, 2867, 3703, 2883, 2406]) self.assertAllEqual(self.evaluate(expected_unpacked_value), self.evaluate(unpacked_value))
def test_unpack_from_int_special_case_6_28(self): packed_value = tf.constant([[183448818], [33236180], [236923387], [146481]]) unpacked_value = tf_utils.unpack_from_int(packed_value, original_bitrange=6, target_bitrange=28, shape=(17, )) expected_unpacked_value = tf.constant([ 50, 19, 51, 59, 10, 53, 36, 44, 31, 44, 31, 10, 31, 56, 49, 48, 35 ]) self.assertAllEqual(self.evaluate(expected_unpacked_value), self.evaluate(unpacked_value))
def test_unpack_from_int_different_outputs(self): packed_value = tf.constant([[1 + 2**3]], tf.int32) unpacked_value = tf_utils.unpack_from_int(packed_value, original_bitrange=1, target_bitrange=28, shape=(4, )) self.assertAllEqual([1, 0, 0, 1], self.evaluate(unpacked_value)) unpacked_value = tf_utils.unpack_from_int(packed_value, original_bitrange=1, target_bitrange=28, shape=(5, )) self.assertAllEqual([1, 0, 0, 1, 0], self.evaluate(unpacked_value)) unpacked_value = tf_utils.unpack_from_int(packed_value, original_bitrange=2, target_bitrange=28, shape=(2, )) self.assertAllEqual([1, 2], self.evaluate(unpacked_value)) unpacked_value = tf_utils.unpack_from_int(packed_value, original_bitrange=2, target_bitrange=28, shape=(3, )) self.assertAllEqual([1, 2, 0], self.evaluate(unpacked_value)) unpacked_value = tf_utils.unpack_from_int(packed_value, original_bitrange=3, target_bitrange=28, shape=(2, )) self.assertAllEqual([1, 1], self.evaluate(unpacked_value)) unpacked_value = tf_utils.unpack_from_int(packed_value, original_bitrange=3, target_bitrange=28, shape=(3, )) self.assertAllEqual([1, 1, 0], self.evaluate(unpacked_value)) unpacked_value = tf_utils.unpack_from_int(packed_value, original_bitrange=4, target_bitrange=28, shape=(1, )) self.assertAllEqual([9], self.evaluate(unpacked_value)) unpacked_value = tf_utils.unpack_from_int(packed_value, original_bitrange=4, target_bitrange=28, shape=(2, )) self.assertAllEqual([9, 0], self.evaluate(unpacked_value))
def test_random_input(self, input_bitrange, target_bitrange): # Tests that packing/unpacking amounts to identity, regardless of the input. num_elements = np.random.randint(low=1, high=50) value = tf.constant( np.random.randint(low=0, high=2**input_bitrange, size=num_elements)) packed_value = tf_utils.pack_into_int(value, input_bitrange, target_bitrange) unpacked_value = tf_utils.unpack_from_int(packed_value, input_bitrange, target_bitrange, value.shape) value, unpacked_value = self.evaluate((value, unpacked_value)) try: self.assertAllEqual(value, unpacked_value) except: # pylint: disable=bare-except self.fail(f'Random input test failed with input value: {value}')
def test_boundary_conditions(self, input_bitrange, target_bitrange): max_v = 2**input_bitrange - 1 input_value = tf.constant( [0, 0, 0, max_v, max_v, max_v, 0, 0, max_v, max_v, 0, max_v] * 20) for length in [1, 6, 7, 8, 20 * 6, 20 * 7, 20 * 8, 20 * 12]: value = input_value[:length] packed_value = tf_utils.pack_into_int(value, input_bitrange, target_bitrange) unpacked_value = tf_utils.unpack_from_int(packed_value, input_bitrange, target_bitrange, value.shape) self.assertAllEqual(self.evaluate(value), self.evaluate(unpacked_value))
def decode(self, encoded_tensors, decode_params, num_summands=None, shape=None): """See base class.""" del decode_params, num_summands # Unused. unpacked_x = tf_utils.unpack_from_int( encoded_tensors[self.ENCODED_VALUES_KEY], self._input_bits, self._target_bitrange, shape) dummy_type_value = encoded_tensors.get(self.DUMMY_TYPE_VALUES_KEY) if dummy_type_value is not None: return tf.cast(unpacked_x, dummy_type_value.dtype) else: return tf.cast(unpacked_x, tf.float32)