|
14 | 14 | from itertools import product
|
15 | 15 |
|
16 | 16 | import numpy as np
|
| 17 | +from numpy.testing import assert_almost_equal |
17 | 18 | import tensorflow as tf
|
18 | 19 |
|
19 | 20 | from tensorflow.python.ops import lookup_ops
|
|
69 | 70 | is_inf = tf.math.is_inf
|
70 | 71 | floormod = tf.math.floormod
|
71 | 72 | matrix_diag_part = tf.compat.v1.matrix_diag_part
|
| 73 | + fake_quant_with_min_max_args = tf.quantization.fake_quant_with_min_max_args |
72 | 74 | elif LooseVersion(tf.__version__) >= "1.13":
|
73 | 75 | conv2d_backprop_input = tf.compat.v1.nn.conv2d_backprop_input
|
74 | 76 | multinomial = tf.compat.v1.random.multinomial
|
|
88 | 90 | is_inf = tf.math.is_inf
|
89 | 91 | floormod = tf.floormod
|
90 | 92 | matrix_diag_part = tf.compat.v1.matrix_diag_part
|
| 93 | + fake_quant_with_min_max_args = tf.compat.v1.quantization.fake_quant_with_min_max_args |
91 | 94 | else:
|
92 | 95 | conv2d_backprop_input = tf.nn.conv2d_backprop_input
|
93 | 96 | multinomial = tf.multinomial
|
@@ -3352,6 +3355,65 @@ def func(base_matrix, diag, k):
|
3352 | 3355 |
|
3353 | 3356 | self._run_test_case(func, [_OUTPUT], {_INPUT: input_val, _INPUT1: diag_val, _INPUT2: k_val})
|
3354 | 3357 |
|
| 3358 | + @check_opset_min_version(10) |
| 3359 | + @check_tf_min_version("1.14") |
| 3360 | + def test_fakequant_with_min_max(self): |
| 3361 | + def func(x): |
| 3362 | + ret = fake_quant_with_min_max_args( |
| 3363 | + x, min=-1024, max=1023, num_bits=8, narrow_range=False, name=None) |
| 3364 | + return tf.identity(ret, name=_TFOUTPUT) |
| 3365 | + |
| 3366 | + x_val = np.random.random(size=[4, 3]).astype(np.float32) * 2048. - 1024. |
| 3367 | + x_val0 = np.abs(x_val) |
| 3368 | + self._run_test_case(func, [_OUTPUT], {_INPUT: x_val0}, rtol=1e-6, atol=1e-4) |
| 3369 | + self._run_test_case(func, [_OUTPUT], {_INPUT: x_val}, rtol=1e-6, atol=1e-4) |
| 3370 | + |
| 3371 | + x_val = np.random.random(size=[4, 3]).astype(np.float32) * 2048. - 1024 |
| 3372 | + x_val[0, 0] = -1024 |
| 3373 | + x_val[0, 1] = -1023 |
| 3374 | + x_val[0, 2] = 1024 |
| 3375 | + x_val[1, 0] = 1023 |
| 3376 | + x_val[1, 1] = 1025 |
| 3377 | + x_val[1, 2] = -1025 |
| 3378 | + self._run_test_case(func, [_OUTPUT], {_INPUT: x_val}, rtol=1e-6, atol=1e-4) |
| 3379 | + |
| 3380 | + @check_opset_min_version(10) |
| 3381 | + @check_tf_min_version("1.14") |
| 3382 | + def test_fakequant_with_min_max_same_sign(self): |
| 3383 | + def func_neg(x): |
| 3384 | + ret = fake_quant_with_min_max_args( |
| 3385 | + x, min=-1024*3, max=-1024, num_bits=8, narrow_range=False, name=None) |
| 3386 | + return tf.identity(ret, name=_TFOUTPUT) |
| 3387 | + |
| 3388 | + x_val = np.random.random(size=[4, 3]).astype(np.float32) * 2048. - 1024 * 3. |
| 3389 | + try: |
| 3390 | + self._run_test_case(func_neg, [_OUTPUT], {_INPUT: x_val}, rtol=1e-6, atol=1e-4) |
| 3391 | + except ValueError: |
| 3392 | + pass |
| 3393 | + |
| 3394 | + @check_opset_min_version(9, "atan2") |
| 3395 | + def test_atan2(self): |
| 3396 | + # Test all possible pairs of pos, neg, zero for x and y. |
| 3397 | + |
| 3398 | + def atan2(y, x): |
| 3399 | + sx = np.sign(x) |
| 3400 | + sy = np.sign(y) |
| 3401 | + pi_part = (sy + sx * (sy ** 2 - 1)) * (sx - 1) * (-np.pi/2) |
| 3402 | + atan_part = np.arctan(y / (x + (1 - sx ** 2))) * sx ** 2 |
| 3403 | + return atan_part + pi_part |
| 3404 | + |
| 3405 | + test_pairs = [[y, x] for x in [3., -4., 0.] for y in [5., -6., 0.]] |
| 3406 | + y_val = np.array([y for y, x in test_pairs], dtype=np.float32) |
| 3407 | + x_val = np.array([x for y, x in test_pairs], dtype=np.float32) |
| 3408 | + assert_almost_equal(np.arctan2(y_val, x_val), atan2(y_val, x_val)) |
| 3409 | + |
| 3410 | + def func(y, x): |
| 3411 | + atan2_ = tf.math.atan2(y, x) |
| 3412 | + return tf.identity(atan2_, name=_TFOUTPUT) |
| 3413 | + |
| 3414 | + self._run_test_case( |
| 3415 | + func, [_OUTPUT], {_INPUT: y_val, _INPUT2: x_val}, rtol=1e-06) |
| 3416 | + |
3355 | 3417 |
|
3356 | 3418 | if __name__ == '__main__':
|
3357 | 3419 | unittest_main()
|
0 commit comments