Skip to main content

elementwise

Elementwise operations on graph tensor Symbols.

Operations in this library are split into two main kinds:

Unary operations

Elementwise-unary-operations all have the following properties:

  • They operate on a single symbolic tensor value of any shape
  • Their output is a single symbolic tensor value with the same shape as their input
  • The computation they represent will be itemwise-independent, in other words the output value in any position of the output tensor at computation time will depend only on the input value at that same position, and no others.

Binary operations

Elementwise-binary-operations all have the following properties:

  • They operate on two symbolic tensor values, a left value and a right value.
  • The input tensor types must be compatible according to the _elementwise_broadcast()broadcasting rules.broadcasting` documentation for more details.
  • If the input tensor types have different element types, they will each be "promoted" to some dtype according to type_promotion.promote()before executing the operation. This may involve a cast that changes the representation (including precision) of the data values. See the type_promotion documentation for more details.
  • Their output is a single symbolic tensor value with
    • dtype depending on the op and the promoted dtype, ie. promote(lhs, rhs)
    • shape equal to the result of _elementwise_broadcast(lhs, rhs)
  • The computation they represent will be itemwise-independent, in other words after broadcasting the input values to the same shape, the output value in any position of the output tensor at computation time will depend only on the input position at the two broadcast input values at that same position, and no others.

add

add(owned lhs: Symbol, owned rhs: Symbol) -> Symbol

Adds two symbolic tensors.

Creates a new op node to compute the addition of two symbol tensor values and adds it to the graph, returning the symbolic result.

  • If lhs and rhs have different dtypes, they will be promoted according to type_promotion before the operation.
  • If lhs and rhs have different shapes, they will be broadcast to the same shape according to _elementwise_broadcast() before the operation.

Raises: - If the input values' shapes are not compatible for broadcasting. See _elementwise_broadcast() for more. - If one of the input values has an unsupported dtype. - If the two symbols are parts of different graphs.

Args:

  • lhs (Symbol): The symbol to use as left side of the addition.
  • rhs (Symbol): The symbol to use as left side of the addition.

Returns:

A symbolic tensor value representing the output of the addition. The result will have: - the same dtype as the type-promotion of the two input dtypes - the same shape as the broadcast of the two input shapes.

div

div(owned lhs: Symbol, owned rhs: Symbol) -> Symbol

Divides two symbolic tensors.

Creates a new op node to compute the division of two symbol tensor values and adds it to the graph, returning the symbolic result.

  • If lhs and rhs have different dtypes, they will be promoted according to type_promotion before the operation.
  • If lhs and rhs have different shapes, they will be broadcast to the same shape according to _elementwise_broadcast() before the operation.

Raises: - If the input values' shapes are not compatible for broadcasting. See _elementwise_broadcast() for more. - If one of the input values has an unsupported dtype. - If the two symbols are parts of different graphs.

Args:

  • lhs (Symbol): The symbol to use as left side of the division.
  • rhs (Symbol): The symbol to use as left side of the division.

Returns:

A symbolic tensor value representing the output of the division. The result will have: - the same dtype as the type-promotion of the two input dtypes - the same shape as the broadcast of the two input shapes.

max

max(owned lhs: Symbol, owned rhs: Symbol) -> Symbol

Computes the elementwise maximum of two symbolic tensors.

Creates a new op node to compute the maximum of two symbol tensor values and adds it to the graph, returning the symbolic result.

  • If lhs and rhs have different dtypes, they will be promoted according to type_promotion before the operation.
  • If lhs and rhs have different shapes, they will be broadcast to the same shape according to _elementwise_broadcast() before the operation.

Raises: - If the input values' shapes are not compatible for broadcasting. See _elementwise_broadcast() for more. - If one of the input values has an unsupported dtype. - If the two symbols are parts of different graphs.

Args:

  • lhs (Symbol): The symbol to use as left side of the maximum.
  • rhs (Symbol): The symbol to use as left side of the maximum.

Returns:

A symbolic tensor value representing the output of the maximum. The result will have: - the same dtype as the type-promotion of the two input dtypes - the same shape as the broadcast of the two input shapes.

min

min(owned lhs: Symbol, owned rhs: Symbol) -> Symbol

Computes the elementwise minimum of two symbolic tensors.

Creates a new op node to compute the minimum of two symbol tensor values and adds it to the graph, returning the symbolic result.

  • If lhs and rhs have different dtypes, they will be promoted according to type_promotion before the operation.
  • If lhs and rhs have different shapes, they will be broadcast to the same shape according to _elementwise_broadcast() before the operation.

Raises: - If the input values' shapes are not compatible for broadcasting. See _elementwise_broadcast() for more. - If one of the input values has an unsupported dtype. - If the two symbols are parts of different graphs.

Args:

  • lhs (Symbol): The symbol to use as left side of the minimum.
  • rhs (Symbol): The symbol to use as left side of the minimum.

Returns:

A symbolic tensor value representing the output of the minimum. The result will have: - the same dtype as the type-promotion of the two input dtypes - the same shape as the broadcast of the two input shapes.

mod

mod(owned lhs: Symbol, owned rhs: Symbol) -> Symbol

Computes the elementwise maximum of two symbolic tensors.

Creates a new op node to compute the maximum of two symbol tensor values and adds it to the graph, returning the symbolic result.

  • If lhs and rhs have different dtypes, they will be promoted according to type_promotion before the operation.
  • If lhs and rhs have different shapes, they will be broadcast to the same shape according to _elementwise_broadcast() before the operation.

Raises: - If the input values' shapes are not compatible for broadcasting. See _elementwise_broadcast() for more. - If one of the input values has an unsupported dtype. - If the two symbols are parts of different graphs.

Args:

  • lhs (Symbol): The symbol to use as left side of the maximum.
  • rhs (Symbol): The symbol to use as left side of the maximum.

Returns:

A symbolic tensor value representing the output of the maximum. The result will have: - the same dtype as the type-promotion of the two input dtypes - the same shape as the broadcast of the two input shapes.

mul

mul(owned lhs: Symbol, owned rhs: Symbol) -> Symbol

Computes the elementwise multiplication of two symbolic tensors.

Creates a new op node to compute the multiplication of two symbol tensor values and adds it to the graph, returning the symbolic result.

  • If lhs and rhs have different dtypes, they will be promoted according to type_promotion before the operation.
  • If lhs and rhs have different shapes, they will be broadcast to the same shape according to _elementwise_broadcast() before the operation.

Raises: - If the input values' shapes are not compatible for broadcasting. See _elementwise_broadcast() for more. - If one of the input values has an unsupported dtype. - If the two symbols are parts of different graphs.

Args:

  • lhs (Symbol): The symbol to use as left side of the multiplication.
  • rhs (Symbol): The symbol to use as left side of the multiplication.

Returns:

A symbolic tensor value representing the output of the multiplication. The result will have: - the same dtype as the type-promotion of the two input dtypes - the same shape as the broadcast of the two input shapes.

pow

pow(owned lhs: Symbol, owned rhs: Symbol) -> Symbol

Computes the elementwise exponentiation of two symbolic tensors.

Creates a new op node to compute the exponentiation of two symbol tensor values and adds it to the graph, returning the symbolic result.

  • If lhs and rhs have different dtypes, they will be promoted according to type_promotion before the operation.
  • If lhs and rhs have different shapes, they will be broadcast to the same shape according to _elementwise_broadcast() before the operation.

Raises: - If the input values' shapes are not compatible for broadcasting. See _elementwise_broadcast() for more. - If one of the input values has an unsupported dtype. - If the two symbols are parts of different graphs.

Args:

  • lhs (Symbol): The symbol to use as left side of the exponentiation.
  • rhs (Symbol): The symbol to use as left side of the exponentiation.

Returns:

A symbolic tensor value representing the output of the exponentiation. The result will have: - the same dtype as the type-promotion of the two input dtypes - the same shape as the broadcast of the two input shapes.

sub

sub(owned lhs: Symbol, owned rhs: Symbol) -> Symbol

Computes the elementwise subtraction of two symbolic tensors.

Creates a new op node to compute the subtraction of two symbol tensor values and adds it to the graph, returning the symbolic result.

  • If lhs and rhs have different dtypes, they will be promoted according to type_promotion before the operation.
  • If lhs and rhs have different shapes, they will be broadcast to the same shape according to _elementwise_broadcast() before the operation.

Raises: - If the input values' shapes are not compatible for broadcasting. See _elementwise_broadcast() for more. - If one of the input values has an unsupported dtype. - If the two symbols are parts of different graphs.

Args:

  • lhs (Symbol): The symbol to use as left side of the subtraction.
  • rhs (Symbol): The symbol to use as left side of the subtraction.

Returns:

A symbolic tensor value representing the output of the subtraction. The result will have: - the same dtype as the type-promotion of the two input dtypes - the same shape as the broadcast of the two input shapes.

equal

equal(owned lhs: Symbol, owned rhs: Symbol) -> Symbol

Computes the elementwise equality comparison between two symbolic tensors.

Creates a new op node to compute the equality comparison of two symbol tensor values and adds it to the graph, returning the symbolic result.

  • If lhs and rhs have different dtypes, they will be promoted according to type_promotion before the operation.
  • If lhs and rhs have different shapes, they will be broadcast to the same shape according to _elementwise_broadcast() before the operation.

Raises: - If the input values' shapes are not compatible for broadcasting. See _elementwise_broadcast() for more. - If one of the input values has an unsupported dtype. - If the two symbols are parts of different graphs.

Args:

  • lhs (Symbol): The symbol to use as left side of the equality comparison.
  • rhs (Symbol): The symbol to use as left side of the equality comparison.

Returns:

A symbolic tensor value representing the output of the equality comparison. The result will have: - element type bool, true if the left-hand-side value at a given position is equal to the right-hand-side value at that same position, and false otherwise. - the same shape as the broadcast of the two input shapes.

greater

greater(owned lhs: Symbol, owned rhs: Symbol) -> Symbol

Computes the elementwise greater than comparison between two symbolictensors.

Creates a new op node to compute the greater than comparison of two symbol tensor values and adds it to the graph, returning the symbolic result.

  • If lhs and rhs have different dtypes, they will be promoted according to type_promotion before the operation.
  • If lhs and rhs have different shapes, they will be broadcast to the same shape according to _elementwise_broadcast() before the operation.

Raises: - If the input values' shapes are not compatible for broadcasting. See _elementwise_broadcast() for more. - If one of the input values has an unsupported dtype. - If the two symbols are parts of different graphs.

Args:

  • lhs (Symbol): The symbol to use as left side of the greater than comparison.
  • rhs (Symbol): The symbol to use as left side of the greater than comparison.

Returns:

A symbolic tensor value representing the output of the greater than comparison. The result will have: - element type bool, true if the left-hand-side value at a given position is strictly greater than (not equal to) the right-hand-side at that same position, and false otherwise. - the same shape as the broadcast of the two input shapes.

greater_equal

greater_equal(owned lhs: Symbol, owned rhs: Symbol) -> Symbol

Computes the elementwise greater-or-equal comparison between two symbolic tensors.

Creates a new op node to compute the equality comparison of two symbol tensor values and adds it to the graph, returning the symbolic result.

  • If lhs and rhs have different dtypes, they will be promoted according to type_promotion before the operation.
  • If lhs and rhs have different shapes, they will be broadcast to the same shape according to _elementwise_broadcast() before the operation.

Raises: - If the input values' shapes are not compatible for broadcasting. See _elementwise_broadcast() for more. - If one of the input values has an unsupported dtype. - If the two symbols are parts of different graphs.

Args:

  • lhs (Symbol): The symbol to use as left side of the equality comparison.
  • rhs (Symbol): The symbol to use as left side of the equality comparison.

Returns:

A symbolic tensor value representing the output of the equality comparison. The result will have: - element type bool, true if the left-hand-side value at a given position is greater than or equal to the right hand side at that same position, and false otherwise. - the same shape as the broadcast of the two input shapes.

not_equal

not_equal(owned lhs: Symbol, owned rhs: Symbol) -> Symbol

Computes the elementwise inequality comparison between two symbolic tensors.

Creates a new op node to compute the inequality comparison of two symbol tensor values and adds it to the graph, returning the symbolic result.

  • If lhs and rhs have different dtypes, they will be promoted according to type_promotion before the operation.
  • If lhs and rhs have different shapes, they will be broadcast to the same shape according to _elementwise_broadcast() before the operation.

Raises: - If the input values' shapes are not compatible for broadcasting. See _elementwise_broadcast() for more. - If one of the input values has an unsupported dtype. - If the two symbols are parts of different graphs.

Args:

  • lhs (Symbol): The symbol to use as left side of the inequality comparison.
  • rhs (Symbol): The symbol to use as left side of the inequality comparison.

Returns:

A symbolic tensor value representing the output of the inequality comparison. The result will have: - element type bool, true if the elements at a given position are not equal and false otherwise. - the same shape as the broadcast of the two input shapes.

abs

abs(owned value: Symbol) -> Symbol

Computes the elementwise absolute value of a symbolic tensor.

Creates a new op node to compute the elementwise absolute value of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the absolute value computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

exp

exp(owned value: Symbol) -> Symbol

Computes the elementwise exp function of a symbolic tensor.

Creates a new op node to compute the elementwise exp function of a symbolic tensor and adds it to the graph, returning the symbolic result.

exp is defined as exp(x) = e^x, where e is Euler's number.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the exp function computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

erf

erf(owned value: Symbol) -> Symbol

Computes the elementwise error function of a symbolic tensor.

Creates a new op node to compute the elementwise error function of a symbolic tensor and adds it to the graph, returning the symbolic result.

The error function erf is defined as the probability that a randomly sampled normal distribution falls within a given range. See Error function for more details.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the error function computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

gelu

gelu(owned value: Symbol) -> Symbol

Computes the elementwise gelu function of a symbolic tensor.

Creates a new op node to compute the elementwise gelu function of a symbolic tensor and adds it to the graph, returning the symbolic result.

gelu is defined as gelu(x)=xΦ(x)gelu(x) = x \Phi(x) where Φ\Phi is the cumulative distribution function of the Gaussian distribution. See the paper for more details.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the gelu function computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

log

log(owned value: Symbol) -> Symbol

Computes the elementwise natural logarithm of a symbolic tensor.

Creates a new op node to compute the elementwise natural logarithm of a symbolic tensor and adds it to the graph, returning the symbolic result.

The natural logarithm function log is defined as the inverse of the exponential function exp(), ie. it computes the value y in the equation x = e^y where e is Euler's number.

log(x) is undefined for x <= 0 for real numbers. Complex numbers are currently unsupported.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the natural logarithm computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

log1p

log1p(owned value: Symbol) -> Symbol

Computes the elementwise logarithm of 1 plus a symbolic tensor.

Creates a new op node to compute the elementwise log1p of a symbolic tensor and adds it to the graph, returning the symbolic result.

The log1p function is defined as log1p(x) = log(1 + x), where log() is the natural logarithm.

Using log1p(x) rather than computing log(1 + x) can give greater numerical precision results.

log(x) is undefined for x <= 0 for real numbers. Complex numbers are currently unsupported.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the log1p computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

logsoftmax

logsoftmax(owned value: Symbol) -> Symbol

Computes the elementwise logsoftmax of a symbolic tensor.

Creates a new op node to compute the elementwise logsoftmax of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the logsoftmax computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

relu

relu(owned value: Symbol) -> Symbol

Computes the elementwise relu of a symbolic tensor.

Creates a new op node to compute the elementwise relu of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the relu computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

sigmoid

sigmoid(owned value: Symbol) -> Symbol

Computes the elementwise sigmoid of a symbolic tensor.

Creates a new op node to compute the elementwise sigmoid of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the sigmoid computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

silu

silu(owned value: Symbol) -> Symbol

Computes the elementwise silu of a symbolic tensor.

Creates a new op node to compute the elementwise silu of a symbolic tensor and adds it to the graph, returning the symbolic result.

silu is defined as silu(x) = x * [sigmoid](/engine/reference/mojo/graph/ops/elementwise.html#sigmoid)(x).

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the silu computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

softmax

softmax(owned value: Symbol) -> Symbol

Computes the elementwise softmax of a symbolic tensor.

Creates a new op node to compute the elementwise softmax of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the softmax computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

cos

cos(owned value: Symbol) -> Symbol

Computes the elementwise cosine of a symbolic tensor.

Creates a new op node to compute the elementwise cosine of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the trunc computation. If it's not a floating-point DType, it will be promoted to one according to type promotion rules before computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

floor

floor(owned value: Symbol) -> Symbol

Computes the elementwise floor of a symbolic tensor.

Creates a new op node to compute the elementwise floor of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the trunc computation. If it's not a floating-point DType, it will be promoted to one according to type promotion rules before computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

round

round(owned value: Symbol) -> Symbol

Computes the elementwise round of a symbolic tensor.

Creates a new op node to compute the elementwise round of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the trunc computation. If it's not a floating-point DType, it will be promoted to one according to type promotion rules before computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

roundeven

roundeven(owned value: Symbol) -> Symbol

Computes the elementwise roundeven of a symbolic tensor.

Creates a new op node to compute the elementwise roundeven of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the trunc computation. If it's not a floating-point DType, it will be promoted to one according to type promotion rules before computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

rsqrt

rsqrt(owned value: Symbol) -> Symbol

Computes the elementwise inverse-square-root of a symbolic tensor.

Creates a new op node to compute the elementwise rsqrt of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the trunc computation. If it's not a floating-point DType, it will be promoted to one according to type promotion rules before computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

sqrt

sqrt(owned value: Symbol) -> Symbol

Computes the elementwise sqrt of a symbolic tensor.

Creates a new op node to compute the elementwise sqrt of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the trunc computation. If it's not a floating-point DType, it will be promoted to one according to type promotion rules before computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

sin

sin(owned value: Symbol) -> Symbol

Computes the elementwise sine of a symbolic tensor.

Creates a new op node to compute the elementwise sine of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the trunc computation. If it's not a floating-point DType, it will be promoted to one according to type promotion rules before computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

tanh

tanh(owned value: Symbol) -> Symbol

Computes the elementwise tanh of a symbolic tensor.

Creates a new op node to compute the elementwise tanh of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the trunc computation. If it's not a floating-point DType, it will be promoted to one according to type promotion rules before computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

trunc

trunc(owned value: Symbol) -> Symbol

Computes the elementwise truncation of a symbolic tensor.

Creates a new op node to compute the elementwise trunc of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the trunc computation. If it's not a floating-point DType, it will be promoted to one according to type promotion rules before computation.

Returns:

A new symbolic tensor value representing the output of the absolute value computation.

is_nan

is_nan(owned value: Symbol) -> Symbol

Computes the elementwise is_nan of a symbolic tensor.

Creates a new op node to compute the elementwise is_nan of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the is_nan computation.

Returns:

The result will have: - element type bool, true if the element at a given position is NaN, false otherwise - the same shape as the input value.

is_inf

is_inf(owned value: Symbol) -> Symbol

Computes the elementwise is_inf of a symbolic tensor.

Creates a new op node to compute the elementwise is_inf of a symbolic tensor and adds it to the graph, returning the symbolic result.

Raises: If the symbol doesn't represent a tensor value.

Args:

  • value (Symbol): The symbolic tensor to use as the input to the is_inf computation.

Returns:

The result will have: - element type bool, true if the element at a given position is plus or minus infinity, false otherwise - the same shape as the input value.