cornell-zhang / heterocl

HeteroCL: A Multi-Paradigm Programming Infrastructure for Software-Defined Heterogeneous Computing
https://cornell-zhang.github.io/heterocl/
Apache License 2.0
322 stars 92 forks source link

[API] Add hcl.assert for Runtime Assertion #385

Closed ezw2 closed 3 years ago

ezw2 commented 3 years ago

This PR introduces a new API hcl.assert_, which evaluates whether a condition is true or false in HeteroCL programs. The user has the option to provide a customized message as well as print out a value or array of values for debugging purposes. If the condition is true, the assert statement has no effect on the program, and if it is false, the rest of the program will not be executed and the error message will be printed out.

Examples for hcl.assert_


A = hcl.compute((10,), lambda x: x + 1)
hcl.assert_(A[0] == 1)
hcl.print(A[0])

output: 1


A = hcl.compute((10,), lambda x: x + 1)
hcl.assert_(A[7] == 7 &&  A[1] == 1, “assert error, A[7]: %d (should be 7) and A[1]: %d (should be 1)”, [A[7], A[1]])
hcl.print(A[2])
hcl.assert_(A[3] == 4, “incorrect value of A[3]”)

output: assert error, A[7]: 8 (should be 7) and A[1]: 2 (should be 1)


A = hcl.compute((10,), lambda x: x + 1)
hcl.assert_(A[3] == 4, “A[3] should equal 4”)
hcl.print(A[0], "A[0]: %d\n")
hcl.assert_(A[4] == 6, “incorrect value of A[4], should be 6 but was %d”, A[4])
hcl.print(A[2], "A[2]: %d\n")

output:

A[0]: 1
incorrect value of A[4], should be 6 but was 5

The assert message and values to be printed are optional. As with hcl print: for integers, by default use "%d". For all other data types including floating points and fixed points, use "%f".

seanlatias commented 3 years ago

For the tests, you need to do the following things

  1. Our test system automatically tests the function starting with test_xxx. Thus, you need to rename the functions if you want our tool to test it (or not test it). Our tool would ignore the __main__ function.
  2. Similarly for the file names, you need to name it starting with test_ in order for it to be tested.
  3. We need to check the message printed out. Please take a look at how hcl.print is tested. Otherwise the passed tests do not mean anything.
seanlatias commented 3 years ago

Forgot to mention one thing. For Python, we usually use 4 spaces for the indentation.

ezw2 commented 3 years ago

Ok should I fix the python indentations to make them all 4 spaces?