Skip to content

Commit

Permalink
Minor improvements.
Browse files Browse the repository at this point in the history
- Renamed the new category of unary ops to "Comparison".
- Removed special floating-point values like nan and inf from test cases with integer value types.
- Removed comment on missing isNan() from guidelines on porting from Numpy to DaphneLib.
- Some more minor changes.
  • Loading branch information
pdamme committed Jul 11, 2024
1 parent b830d8b commit c854d95
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 35 deletions.
6 changes: 3 additions & 3 deletions doc/DaphneDSL/Builtins.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ The following built-in functions all follow the same scheme:
| **`floor`** | round down |
| **`ceil`** | round up |

### Other Utilities
### Comparison

| function | meaning |
| ----- | -------------------------------------- |
|**`isNan`**| returns 1 if argument is nan, 0 otherwise |
| ----- | ----- |
| **`isNan`** | `1` if argument is NaN, `0` otherwise |

## Elementwise binary

Expand Down
1 change: 1 addition & 0 deletions doc/DaphneLib/APIRef.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ In the following, we describe only the latter.
- **`sinh`**`()`
- **`cosh`**`()`
- **`tanh`**`()`
- **`isNan`**`()`

**Elementwise binary:**

Expand Down
2 changes: 0 additions & 2 deletions doc/DaphneLib/Numpy2DaphneLib.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,6 @@ Porting for other Numpy versions may be possible by following similar lines of t

- `numpy.`**`isnan`**`(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'isnan'>`

*Note: `isNan()` is not supported in DAPHNE yet (see #768).*

*Parameters*

- `x`: supported
Expand Down
6 changes: 3 additions & 3 deletions src/api/python/daphne/operator/nodes/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ def sqrt(self) -> 'OperationNode':
return Matrix(self.daphne_context,'sqrt', [self])

def isNan(self) -> 'OperationNode':
"""Checks if argument is of type Nan. Returns 1 if argument is Nan, 0 otherwise.
:return: `Matrix` representing operation
"""Elementwise check for NaN values in this matrix (resulting in 1 if the element is NaN, 0 otherwise).
:return: `Matrix` A node representing the isNan operation.
"""
return Matrix(self.daphne_context,'isNan', [self])
return Matrix(self.daphne_context, 'isNan', [self])

def round(self) -> 'OperationNode':
return Matrix(self.daphne_context, 'round', [self])
Expand Down
7 changes: 6 additions & 1 deletion src/ir/daphneir/DaphneOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ def Daphne_EwSignOp : Daphne_EwUnaryOp<"ewSign", NumScalar, [ValueTypeFromFirstA
def Daphne_EwExpOp : Daphne_EwUnaryOp<"ewExp", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_EwLnOp : Daphne_EwUnaryOp<"ewLn", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_EwSqrtOp : Daphne_EwUnaryOp<"ewSqrt", NumScalar, [ValueTypeFromArgsFP, DeclareOpInterfaceMethods<VectorizableOpInterface>]>;
def Daphne_EwIsNanOp : Daphne_EwUnaryOp<"ewIsnan", NumScalar, [ValueTypeFromFirstArg]>;

// ----------------------------------------------------------------------------
// Logical
Expand Down Expand Up @@ -245,6 +244,12 @@ def Daphne_EwAsinOp : Daphne_EwUnaryOp<"ewAsin", NumScalar, [ValueTypeFromArgsFP
def Daphne_EwAcosOp : Daphne_EwUnaryOp<"ewAcos", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_EwAtanOp : Daphne_EwUnaryOp<"ewAtan", NumScalar, [ValueTypeFromArgsFP]>;

// ----------------------------------------------------------------------------
// Comparison
// ----------------------------------------------------------------------------

def Daphne_EwIsNanOp : Daphne_EwUnaryOp<"ewIsnan", NumScalar, [ValueTypeFromFirstArg]>;

// ****************************************************************************
// Elementwise binary
// ****************************************************************************
Expand Down
4 changes: 2 additions & 2 deletions src/parser/daphnedsl/DaphneDSLBuiltins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,12 @@ antlrcpp::Any DaphneDSLBuiltins::build(mlir::Location loc, const std::string & f
return createUnaryOp<EwAtanOp>(loc, func, args);

// --------------------------------------------------------------------
// Other Utility Functions
// Comparison
// --------------------------------------------------------------------

if (func == "isNan")
return createUnaryOp<EwIsNanOp>(loc, func, args);


// ********************************************************************
// Elementwise binary
// ********************************************************************
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/local/kernels/EwUnarySca.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ EwUnaryScaFuncPtr<VTRes, VTArg> getEwUnaryScaFuncPtr(UnaryOpCode opCode) {
MAKE_CASE(UnaryOpCode::FLOOR)
MAKE_CASE(UnaryOpCode::CEIL)
MAKE_CASE(UnaryOpCode::ROUND)
// Other utilities
// Comparison.
MAKE_CASE(UnaryOpCode::ISNAN)
#undef MAKE_CASE
default:
Expand Down Expand Up @@ -169,7 +169,7 @@ MAKE_EW_UNARY_SCA(UnaryOpCode::TANH, tanh(arg));
MAKE_EW_UNARY_SCA(UnaryOpCode::FLOOR, floor(arg));
MAKE_EW_UNARY_SCA(UnaryOpCode::CEIL, std::ceil(arg));
MAKE_EW_UNARY_SCA(UnaryOpCode::ROUND, round(arg));
// Other Utilities
// Comparison.
MAKE_EW_UNARY_SCA(UnaryOpCode::ISNAN, std::isnan(arg));

#undef MAKE_EW_UNARY_SCA_CLOSED_DOMAIN_ERROR
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/local/kernels/UnaryOpCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enum class UnaryOpCode {
FLOOR,
CEIL,
ROUND,
// Other Utilities
// Comparison.
ISNAN
};

Expand Down
4 changes: 2 additions & 2 deletions test/api/cli/operations/OperationsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ MAKE_TEST_CASE("ctable", 1)
MAKE_TEST_CASE("gemv", 1)
MAKE_TEST_CASE("idxMax", 1)
MAKE_TEST_CASE("idxMin", 1)
MAKE_TEST_CASE("isNan", 1)
MAKE_TEST_CASE("mean", 1)
MAKE_TEST_CASE("operator_at", 2)
MAKE_TEST_CASE("operator_eq", 2)
Expand All @@ -58,5 +59,4 @@ MAKE_TEST_CASE("seq", 2)
MAKE_TEST_CASE("solve", 1)
MAKE_TEST_CASE("sqrt", 1)
MAKE_TEST_CASE("sum", 1)
MAKE_TEST_CASE("syrk", 1)
MAKE_TEST_CASE("isNan", 1)
MAKE_TEST_CASE("syrk", 1)
4 changes: 3 additions & 1 deletion test/api/python/matrix_ewunary.daphne
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ print(atan(m));
print(sinh(m));
print(cosh(m));
print(tanh(m));
print(isNan(m));

print(isNan(m));
print(isNan([nan]));
5 changes: 4 additions & 1 deletion test/api/python/matrix_ewunary.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from daphne.context.daphne_context import DaphneContext
import math

dc = DaphneContext()

Expand Down Expand Up @@ -43,4 +44,6 @@
m.sinh().print().compute()
m.cosh().print().compute()
m.tanh().print().compute()
m.isNan().print().compute()

m.isNan().print().compute()
dc.fill(math.nan, 1, 1).isNan().print().compute()
4 changes: 3 additions & 1 deletion test/api/python/scalar_ewunary.daphne
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ print(atan(s));
print(sinh(s));
print(cosh(s));
print(tanh(s));
print(isNan(s));

print(isNan(s));
print(isNan(nan));
5 changes: 4 additions & 1 deletion test/api/python/scalar_ewunary.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from daphne.context.daphne_context import DaphneContext
import math

dc = DaphneContext()

Expand Down Expand Up @@ -46,4 +47,6 @@
s.sum().sinh().print().compute()
s.sum().cosh().print().compute()
s.sum().tanh().print().compute()
s.sum().isNan().print().compute()

s.sum().isNan().print().compute()
dc.fill(math.nan, 1, 1).cbind(dc.fill(1.0, 1, 1)).sum().isNan().print().compute()
13 changes: 3 additions & 10 deletions test/runtime/local/kernels/EwUnaryMatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,27 +549,20 @@ TEMPLATE_PRODUCT_TEST_CASE(TEST_NAME("round, floating-point-specific"), TAG_KERN
}

// ****************************************************************************
// Other Utilities
// Comparison
// ****************************************************************************

TEMPLATE_PRODUCT_TEST_CASE(TEST_NAME("isNan"), TAG_KERNELS, (DATA_TYPES), (int32_t)) {
using DT = TestType;
using VT = typename DT::VT;

auto arg = genGivenVals<DT>(7, {
auto arg = genGivenVals<DT>(4, {
1,
std::numeric_limits<VT>::quiet_NaN(),
0,
std::numeric_limits<VT>::infinity(),
-std::numeric_limits<VT>::infinity(),
99,
-99,
});

auto exp = genGivenVals<DT>(7, {
0,
0,
0,
auto exp = genGivenVals<DT>(4, {
0,
0,
0,
Expand Down
7 changes: 2 additions & 5 deletions test/runtime/local/kernels/EwUnaryScaTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,15 @@ TEMPLATE_TEST_CASE(TEST_NAME("round, floating-point-specific"), TAG_KERNELS, FP_
}

// ****************************************************************************
// Other Utilities
// Comparison
// ****************************************************************************

TEMPLATE_TEST_CASE(TEST_NAME("isNan"), TAG_KERNELS, SI_VALUE_TYPES) {
using VT = TestType;
checkEwUnarySca<UnaryOpCode::ISNAN, VT>(1, 0);
checkEwUnarySca<UnaryOpCode::ISNAN, VT>(std::numeric_limits<VT>::quiet_NaN(), 0);
checkEwUnarySca<UnaryOpCode::ISNAN, VT>(std::numeric_limits<VT>::infinity(), 0);
checkEwUnarySca<UnaryOpCode::ISNAN, VT>(-std::numeric_limits<VT>::infinity(), 0);
checkEwUnarySca<UnaryOpCode::ISNAN, VT>(99, 0);
checkEwUnarySca<UnaryOpCode::ISNAN, VT>(-99, 0);
checkEwUnarySca<UnaryOpCode::ISNAN, VT>(0, 0);
checkEwUnarySca<UnaryOpCode::ISNAN, VT>(std::numeric_limits<VT>::denorm_min(), 0);
}

TEMPLATE_TEST_CASE(TEST_NAME("isNan, floating-point-specific"), TAG_KERNELS, FP_VALUE_TYPES) {
Expand Down

0 comments on commit c854d95

Please sign in to comment.