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

Implementation of new SparsityOp #790

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/DaphneDSL/Builtins.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ The following built-in functions allow to find out the shape/dimensions of matri

Returns the number of cells in `arg`.
This is the product of the number of rows and the number of columns.

- **`sparsity`**`(arg:matrix)`

Returns the DAPHNE compiler's *estimate* of the argument's sparsity.
Note that this value may deviate from the *actual* sparsity of the data at run-time.

## Elementwise unary

Expand Down
22 changes: 22 additions & 0 deletions src/ir/daphneir/DaphneDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,28 @@ mlir::LogicalResult mlir::daphne::NumCellsOp::canonicalize(
return mlir::failure();
}

/**
* @brief Replaces SparsityOp by a constant, if the sparsity of the input is known
* (e.g., due to sparsity inference).
*/
mlir::LogicalResult mlir::daphne::SparsityOp::canonicalize(
mlir::daphne::SparsityOp op, PatternRewriter &rewriter
) {
double sparsity = -1.0;

mlir::Type inTy = op.getArg().getType();
if(auto t = inTy.dyn_cast<mlir::daphne::MatrixType>())
sparsity = t.getSparsity();

if(sparsity != -1) {
rewriter.replaceOpWithNewOp<mlir::daphne::ConstantOp>(
op, sparsity
);
return mlir::success();
}
return mlir::failure();
}

/**
* @brief Replaces a `DistributeOp` by a `DistributedReadOp`, if its input
* value (a) is defined by a `ReadOp`, and (b) is not used elsewhere.
Expand Down
7 changes: 7 additions & 0 deletions src/ir/daphneir/DaphneOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ def Daphne_NumRowsOp : Daphne_NumOp<"numRows">;
def Daphne_NumColsOp : Daphne_NumOp<"numCols">;
def Daphne_NumCellsOp : Daphne_NumOp<"numCells">;

def SparsityOp : Daphne_Op<"sparsity", [DataTypeSca]> {
let arguments = (ins MatrixOf<[AnyScalar]>:$arg);
let results = (outs FloatScalar:$res);

let hasCanonicalizeMethod = 1;
}

// ****************************************************************************
// Matrix multiplication
// ****************************************************************************
Expand Down
5 changes: 5 additions & 0 deletions src/parser/daphnedsl/DaphneDSLBuiltins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,11 @@ antlrcpp::Any DaphneDSLBuiltins::build(mlir::Location loc, const std::string & f
return createNumOp<NumColsOp>(loc, func, args);
if(func == "ncell")
return createNumOp<NumCellsOp>(loc, func, args);
if(func == "sparsity") {
checkNumArgsExact(loc, func, numArgs, 1);
mlir::Value arg = args[0];
return static_cast<mlir::Value>(builder.create<SparsityOp>(loc, builder.getF64Type(), arg));
}

// ********************************************************************
// Elementwise unary
Expand Down
31 changes: 31 additions & 0 deletions src/runtime/local/kernels/Sparsity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2021 The DAPHNE Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef SRC_RUNTIME_LOCAL_KERNELS_SPARSITY_H
#define SRC_RUNTIME_LOCAL_KERNELS_SPARSITY_H

#include <runtime/local/context/DaphneContext.h>

// ****************************************************************************
// Convenience function
// ****************************************************************************

template<class DTArg>
double sparsity(const DTArg * arg, DCTX(ctx)) {
return -1.0;
}

#endif //SRC_RUNTIME_LOCAL_KERNELS_SPARSITY_H
35 changes: 35 additions & 0 deletions src/runtime/local/kernels/kernels.json
Original file line number Diff line number Diff line change
Expand Up @@ -2946,6 +2946,41 @@
"opCodes": ["MINUS", "SIGN", "SQRT", "EXP", "ABS", "FLOOR", "CEIL", "ROUND", "LN",
"SIN", "COS", "TAN", "ASIN", "ACOS", "ATAN", "SINH", "COSH", "TANH", "ISNAN"]
},
{
"kernelTemplate": {
"header": "Sparsity.h",
"opName": "sparsity",
"returnType": "double",
"templateParams": [
{
"name": "DTArg",
"isDataType": true
}
],
"runtimeParams": [
{
"type": "const DTArg *",
"name": "arg"
}
]
},
"instantiations": [
[["DenseMatrix", "double"]],
[["DenseMatrix", "float"]],
[["DenseMatrix", "int64_t"]],
[["DenseMatrix", "int32_t"]],
[["DenseMatrix", "int8_t"]],
[["DenseMatrix", "uint64_t"]],
[["DenseMatrix", "uint32_t"]],
[["DenseMatrix", "uint8_t"]],
[["DenseMatrix", "bool"]],
[["DenseMatrix", "size_t"]],
[["CSRMatrix", "double"]],
[["CSRMatrix", "float"]],
[["CSRMatrix", "int64_t"]],
[["CSRMatrix", "uint8_t"]]
]
},
{
"kernelTemplate": {
"header": "NumCols.h",
Expand Down
Loading