Mojo module

# elementwise

Ops that perform element-wise computations/comparisons.

Operations in this module are split into either unary or binary operations.

### 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 broadcasting rules.
- If the input tensor types have different element types, they will each
be
*promoted*to the same dtype according to the dtype promotion rules*before*executing the operation. This may involve a cast that changes the representation (including precision) of the data values. - 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
`broadcast(lhs, rhs)`

- dtype depending on the op and the
- 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.

### DType Promotion Rulesβ

The Graph API splits dtype promotion into two pieces: bit width and category.
Bit width is simply the number of bits that are needed to represent a dtype.
Category is an order hierarchy: `bool < unsigned int < signed int < float`

.

A promotion candidate is calculated between two dtypes (`a`

and `b`

) as:
`(max(category(a), category(b)), max(bitwidth(a), bitwidth(b)))`

.

An exception will be raised if a either input dtype might contain a value that is
unrepresentable by the promotion candidate (e.g `u32 -> i32`

or `i32 -> f32`

).

An exception will be raised if the input has the same bit width but a different format
than the promotion candidate (e.g. `f16 -> bf16`

or `f32 -> tf32`

).

If no exception is raised, the promotion candidate is accepted. All inputs will be cast to the promotion candidate before the underlying operation is run.

### Broadcasting Rulesβ

Given two input tensor shapes, broadcasting works as following:

- Prepend static 1 dimensions onto the tensor with lower rank to make it so that both tensors have the same rank.
- If a dimension is a static 1 dimension, it will broadcast to the size of the dimension in the other tensor.
- All other dimensions will be asserted to be equivalent. If they are not, an exception will be raised.

## Functionsβ

- β
`abs`

: Computes the elementwise absolute value of a symbolic tensor. - β
`add`

: Adds two symbolic tensors. - β
`cos`

: Computes the elementwise cosine of a symbolic tensor. - β
`div`

: Divides two symbolic tensors. - β
`equal`

: Computes the elementwise equality comparison between two symbolic tensors. - β
`erf`

: Computes the elementwise error function of a symbolic tensor. - β
`exp`

: Computes the elementwise exp function of a symbolic tensor. - β
`floor`

: Computes the elementwise floor of a symbolic tensor. - β
`gelu`

: Computes the elementwise gelu function of a symbolic tensor. - β
`greater`

: Computes the elementwise greater than comparison between two symbolic tensors. - β
`greater_equal`

: Computes the elementwise greater-or-equal comparison between two symbolic tensors. - β
`is_inf`

: Computes the elementwise is_inf of a symbolic tensor. - β
`is_nan`

: Computes the elementwise is_nan of a symbolic tensor. - β
`log`

: Computes the elementwise natural logarithm of a symbolic tensor. - β
`log1p`

: Computes the elementwise logarithm of 1 plus a symbolic tensor. - β
`logsoftmax`

: Computes the elementwise logsoftmax of a symbolic tensor. - β
`max`

: Computes the elementwise maximum of two symbolic tensors. - β
`min`

: Computes the elementwise minimum of two symbolic tensors. - β
`mod`

: Computes the elementwise maximum of two symbolic tensors. - β
`mul`

: Computes the elementwise multiplication of two symbolic tensors. - β
`not_equal`

: Computes the elementwise inequality comparison between two symbolic tensors. - β
`pow`

: Computes the elementwise exponentiation of two symbolic tensors. - β
`relu`

: Computes the elementwise relu of a symbolic tensor. - β
`round`

: Computes the elementwise round of a symbolic tensor. - β
`roundeven`

: Computes the elementwise roundeven of a symbolic tensor. - β
`rsqrt`

: Computes the elementwise inverse-square-root of a symbolic tensor. - β
`sigmoid`

: Computes the elementwise sigmoid of a symbolic tensor. - β
`silu`

: Computes the elementwise silu of a symbolic tensor. - β
`sin`

: Computes the elementwise sine of a symbolic tensor. - β
`softmax`

: Computes the elementwise softmax of a symbolic tensor. - β
`sqrt`

: Computes the elementwise sqrt of a symbolic tensor. - β
`sub`

: Computes the elementwise subtraction of two symbolic tensors. - β
`tanh`

: Computes the elementwise tanh of a symbolic tensor. - β
`trunc`

: Computes the elementwise truncation of a symbolic tensor.

Was this page helpful?

Thank you! We'll create more content like this.

Thank you for helping us improve!

π What went wrong?