isNan
: elementwise check for NaN elements in a matrix
#768
Labels
good first issue
Non-urgent, simple task with limited scope, suitable for getting started as a contributor.
Operations on matrices of floating-point value types (
f32
,f64
) can result innan
(not a number) elements in certain cases.nan
elements can be problematic for follow-up calculations asnan
typically dominates every arithmetic operation. For instancenan + 1
yieldsnan
,nan * 0
yieldsnan
, and evennan == nan
yieldsnan
. Therefore, users need to be able to detectnan
elements and do something with them. DAPHNE already offers related operations likereplace()
(which can replace any given value and isnan
-aware) andhasSpecialValue()
, (which returnstrue
if the given value is contained in the given matrix, alsonan
-aware). However, sometimes, one wants to know which elements in a matrix arenan
. 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 be1
if the corresponding element in the argument wasnan
, and0
otherwise.Example (DaphneDSL):
Expected output:
Hints:
IsNanOp
operation in DaphneIR (DAPHNE's MLIR-based intermediate representation) as a subclass ofEwUnaryOp
(elementwise unary operation) (seesrc/ir/daphneir/DaphneOps.td
).isNan
(seesrc/parser/daphnedsl/DaphneDSLBuiltins.cpp
) and document it (seedoc/DaphneDSL/Builtins.md
).isNan
on matrices (seesrc/api/python/daphne/operator/nodes/matrix.py
) and document it (seedoc/DaphneLib/APIRef.md
).isNan
. Instead of writing a completely new kernel, please extend the existingewUnaryMat
-kernel by a new op code (seesrc/runtime/local/kernels/EwUnaryMat.h
).Possible task extensions:
nan
only exists for floating-point value types. Thus, whenisNan
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 replacesisNan(X)
byfill(0, nrow(X), ncol(X))
. This could be implemented as a canonicalization (seesrc/ir/daphneir/DaphneDialect.cpp
for simplifcation rewrites of other DaphneIR operations).The text was updated successfully, but these errors were encountered: