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>

source

pub fn name() -> &'static str

Returns a name.

source

pub fn as_operation(&self) -> &Operation<'c>

Returns a generic operation.

source

pub fn builder( context: &'c Context, location: Location<'c> ) -> UnaryOperationBuilder<'c, Unset, Unset, Unset, Unset>

Creates a builder.

source

pub fn output(&self) -> Result<OperationResult<'c, '_>, Error>

source

pub fn x(&self) -> Result<Value<'c, '_>, Error>

source

pub fn present_region(&self) -> Result<RegionRef<'c, '_>, Error>

source

pub fn absent_region(&self) -> Result<RegionRef<'c, '_>, Error>

Trait Implementations§

source§

impl<'c> From<UnaryOperation<'c>> for Operation<'c>

source§

fn from(operation: UnaryOperation<'c>) -> Self

Converts to this type from the input type.
source§

impl<'c> TryFrom<Operation<'c>> for UnaryOperation<'c>

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(operation: Operation<'c>) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<'c> RefUnwindSafe for UnaryOperation<'c>

§

impl<'c> !Send for UnaryOperation<'c>

§

impl<'c> !Sync for UnaryOperation<'c>

§

impl<'c> Unpin for UnaryOperation<'c>

§

impl<'c> UnwindSafe for UnaryOperation<'c>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.