Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isNan: elementwise check for NaN elements in a matrix #768

Closed
pdamme opened this issue Jun 26, 2024 · 0 comments
Closed

isNan: elementwise check for NaN elements in a matrix #768

pdamme opened this issue Jun 26, 2024 · 0 comments
Labels
good first issue Non-urgent, simple task with limited scope, suitable for getting started as a contributor.

Comments

@pdamme
Copy link
Collaborator

pdamme commented Jun 26, 2024

Operations on matrices of floating-point value types (f32, f64) can result in nan (not a number) elements in certain cases. nan elements can be problematic for follow-up calculations as nan typically dominates every arithmetic operation. For instance nan + 1 yields nan, nan * 0 yields nan, and even nan == nan yields nan. Therefore, users need to be able to detect nan elements and do something with them. DAPHNE already offers related operations like replace() (which can replace any given value and is nan-aware) and hasSpecialValue(), (which returns true if the given value is contained in the given matrix, also nan-aware). However, sometimes, one wants to know which elements in a matrix are nan. This is essentially an elementwise unary operation.

Task: Ensure that DAPHNE offers an isNan-function to DaphneDSL and DaphneLib users. This function should get a matrix of any value type as its only argument and return a single matrix of the same shape and value type as its only result. In the result, each element should be 1 if the corresponding element in the argument was nan, and 0 otherwise.

Example (DaphneDSL):

X = t([1, nan, 0, inf, 99.9, nan]);
print(isNan(X));

Expected output:

DenseMatrix(1x6, double)
0 1 0 0 0 1

Hints:

  • Create an IsNanOp operation in DaphneIR (DAPHNE's MLIR-based intermediate representation) as a subclass of EwUnaryOp (elementwise unary operation) (see src/ir/daphneir/DaphneOps.td).
  • Create a DaphneDSL built-in function isNan (see src/parser/daphnedsl/DaphneDSLBuiltins.cpp) and document it (see doc/DaphneDSL/Builtins.md).
  • Create a DaphneLib function isNan on matrices (see src/api/python/daphne/operator/nodes/matrix.py) and document it (see doc/DaphneLib/APIRef.md).
  • Create a runtime kernel for isNan. Instead of writing a completely new kernel, please extend the existing ewUnaryMat-kernel by a new op code (see src/runtime/local/kernels/EwUnaryMat.h).
  • Add unit test cases for the kernel and script-level test cases for DaphneDSL and DaphneLib (see the developer documentation on testing).
  • Have a look at the online documentation in general, e.g., on DaphneDSL, DaphneLib, implementing built-in kernels, writing documentation, testing, etc.

Possible task extensions:

  • In fact, nan only exists for floating-point value types. Thus, when isNan is applied to a matrix of some integer value type, we already know that the result matrix will only contain zeros. Thus, we don't even need to scan the input matrix. The DAPHNE compiler could have a simplification rewrite that replaces isNan(X) by fill(0, nrow(X), ncol(X)). This could be implemented as a canonicalization (see src/ir/daphneir/DaphneDialect.cpp for simplifcation rewrites of other DaphneIR operations).
@pdamme pdamme added the good first issue Non-urgent, simple task with limited scope, suitable for getting started as a contributor. label Jun 26, 2024
@pdamme pdamme closed this as completed in 29fd7b9 Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Non-urgent, simple task with limited scope, suitable for getting started as a contributor.
Projects
None yet
Development

No branches or pull requests

1 participant