Struct melior::dialect::ods::sparse_tensor::UnaryOperation
source · pub struct UnaryOperation<'c> { /* private fields */ }
Expand description
An unary
operation. Unary set operation utilized within linalg.generic.
Defines a computation with a linalg.generic
operation that takes a single
operand and executes one of two regions depending on whether the operand is
nonzero (i.e. stored explicitly in the sparse storage format).
Two regions are defined for the operation must appear in this order:
- present (elements present in the sparse tensor)
- absent (elements not present in the sparse tensor)
Each region contains a single block describing the computation and result.
A non-empty block must end with a sparse_tensor.yield and the return type
must match the type of output
. The primary region’s block has one
argument, while the missing region’s block has zero arguments.
A region may also be declared empty (i.e. absent={}
), indicating that the
region does not contribute to the output.
Due to the possibility of empty regions, i.e. lack of a value for certain
cases, the result of this operation may only feed directly into the output
of the linalg.generic
operation or into into a custom reduction
sparse_tensor.reduce
operation that follows in the same region.
Example of A+1, restricted to existing elements:
%C = bufferization.alloc_tensor...
%0 = linalg.generic #trait
ins(%A: tensor<?xf64, #SparseVector>)
outs(%C: tensor<?xf64, #SparseVector>) {
^bb0(%a: f64, %c: f64) :
%result = sparse_tensor.unary %a : f64 to f64
present={
^bb0(%arg0: f64):
%cf1 = arith.constant 1.0 : f64
%ret = arith.addf %arg0, %cf1 : f64
sparse_tensor.yield %ret : f64
}
absent={}
linalg.yield %result : f64
} -> tensor<?xf64, #SparseVector>
Example returning +1 for existing values and -1 for missing values:
%C = bufferization.alloc_tensor...
%1 = linalg.generic #trait
ins(%A: tensor<?xf64, #SparseVector>)
outs(%C: tensor<?xf64, #SparseVector>) {
^bb0(%a: f64, %c: f64) :
%result = sparse_tensor.unary %a : f64 to i32
present={
^bb0(%x: f64):
%ret = arith.constant 1 : i32
sparse_tensor.yield %ret : i32
}
absent={
%ret = arith.constant -1 : i32
sparse_tensor.yield %ret : i32
}
linalg.yield %result : f64
} -> tensor<?xf64, #SparseVector>
Example showing a structural inversion (existing values become missing in the output, while missing values are filled with 1):
%C = bufferization.alloc_tensor...
%2 = linalg.generic #trait
ins(%A: tensor<?xf64, #SparseVector>)
outs(%C: tensor<?xf64, #SparseVector>) {
%result = sparse_tensor.unary %a : f64 to i64
present={}
absent={
%ret = arith.constant 1 : i64
sparse_tensor.yield %ret : i64
}
linalg.yield %result : f64
} -> tensor<?xf64, #SparseVector>
Implementations§
source§impl<'c> UnaryOperation<'c>
impl<'c> UnaryOperation<'c>
sourcepub fn as_operation(&self) -> &Operation<'c>
pub fn as_operation(&self) -> &Operation<'c>
Returns a generic operation.
sourcepub fn builder(
context: &'c Context,
location: Location<'c>
) -> UnaryOperationBuilder<'c, Unset, Unset, Unset, Unset>
pub fn builder( context: &'c Context, location: Location<'c> ) -> UnaryOperationBuilder<'c, Unset, Unset, Unset, Unset>
Creates a builder.