OneRaynyDay commented on a change in pull request #11833: [MXNET-688] Fix 
quantization divide by zero errors
URL: https://github.com/apache/incubator-mxnet/pull/11833#discussion_r203913519
 
 

 ##########
 File path: tests/python/quantization/test_quantization.py
 ##########
 @@ -485,6 +486,27 @@ def test_quantize_sym_with_calib():
         assert_almost_equal(np.array([lhs]), np.array([rhs]), rtol=1e-3, 
atol=1e-4)
 
 
+@with_seed()
+def test_smooth_distribution():
+    assert_exception(lambda: 
mx.contrib.quant._smooth_distribution(np.zeros((2,)), eps=1e-3), ValueError)
+    dirac_delta = np.zeros((5,))
+    dirac_delta[2] = 1
+    smooth_dirac_delta = dirac_delta.copy()
+    smooth_dirac_delta += 1e-3
+    smooth_dirac_delta[2] -= 5e-3
+    assert_almost_equal(mx.contrib.quant._smooth_distribution(dirac_delta, 
eps=1e-3), smooth_dirac_delta)
+
+
+@with_seed()
+def test_optimal_threshold_adversarial_case():
+    # The worst case for the optimal_threshold function is when the values are 
concentrated
+    # at one edge: [0, 0, ..., 1000]. (histogram)
+    # We want to make sure that the optimal threshold in this case is the max.
+    arr = np.array([2]*1000)
+    res = mx.contrib.quant._get_optimal_threshold(arr, num_quantized_bins=5)
 
 Review comment:
   Using the incorrectly implemented code for `_get_optimal_threshold`, we 
would result in a divide by 0 error here.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to