Unit test of catching exceptions in TF2 DCHECK_XX

32 views
Skip to first unread message

cookie

unread,
Oct 28, 2021, 4:54:01 AM10/28/21
to Discuss
Dear sir or madam:

I'm learning on writing custom opkernel with TF2.0, and use DCHECK_EQ to ensure the dim size of my input. When I begin to write the test_case using the tf.test, I found it behaves different with how TF1 treats with the DCHECK.
In TF1, I use the code below and it works:
def test_raisesExceptionWithIncompatibleDimensions(self):
with tf.Session(''):
with self.assertRaises(ValueError):
      inner_product_module.inner_product([1, 2], [[1, 2], [3, 4]]).eval()
with self.assertRaises(ValueError):
      self.assertRaises(inner_product_module.inner_product([1, 2], [1, 2, 3, 4]).eval(), ValueError)
with self.assertRaises(ValueError):
      self.assertRaises(inner_product_module.inner_product([1, 2, 3], [[1, 2], [3, 4]]).eval(), ValueError)

But in TF2,  though I change the tf.Session("") into self.session(), the DCHECK_EQ exception in my custom op can not be caught by this assertRaises, and it just exits mu test program with signal 6 and logs the stack trace.

How to assert the DCHECK_XX in TF2? Hope someone can offer an example. 
Many thanks

t kevin

unread,
Oct 28, 2021, 5:02:44 AM10/28/21
to cookie, Discuss
DCHECK aka Debug CHECK only works with debug version of tensorflow.
you need to compile tensorflow with bazel compile options -cdbg.
Since you are debugging, simply replace DCHECK_XX with CHECK_XX will
do the work.

cookie <quqi...@shopee.com> 于2021年10月28日周四 下午4:54写道:
> --
> You received this message because you are subscribed to the Google Groups "Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.
> To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/678eaf8d-4d1d-4e6f-8382-a227ebcebca0n%40tensorflow.org.

cookie

unread,
Oct 28, 2021, 6:50:41 AM10/28/21
to Discuss, kevi...@gmail.com, Discuss, cookie
Well I tried change DCHECK to CHECK but the result is still not promising, the log is the same as below:
[ RUN      ] InnerProductOpTest.test_raisesExceptionWithIncompatibleDimensions
2021-10-28 18:36:59.075467: F inner_product.cc:64] Check failed: input_shape.dims() == 2 (1 vs. 2)
Fatal Python error: Aborted

Current thread 0x0000000106f48e00 (most recent call first):
  File "/Library/Python/3.8/lib/python/site-packages/tensorflow/python/eager/execute.py", line 59 in quick_execute
  File "<string>", line 87 in inner_product_eager_fallback
  File "<string>", line 48 in inner_product
  File "inner_product_test.py", line 15 in test_raisesExceptionWithIncompatibleDimensions

But the same code works well in TF1, which can catch the check fail raised from DCHECK_EQ and pass the unit test when it happens.
I'm confused how to implement the same target, which is construct an error case and use the assertRaises to catch it and pass this unit test. 
Can anyone help? An example of this is appreciated. Thanks

Reply all
Reply to author
Forward
0 new messages