binary

Binary operations for the Tricycle framework.

This module contains binary operations that can be applied to tensors of the same shape. These operations include element-wise addition, subtraction, multiplication, division, and comparison operations like maximum and minimum.

Note

In Tricycle, binary operations are only allowed on matrices of the same shape to simplify gradient computations.

class BinaryAdd[source]

Bases: Op

Element-wise addition of two tensors.

This class implements the forward pass for element-wise addition of two tensors.

_out

The output of the forward pass.

Type:

numpy._typing._array_like._SupportsArray[numpy.dtype[Any]] | numpy._typing._nested_sequence._NestedSequence[numpy._typing._array_like._SupportsArray[numpy.dtype[Any]]] | bool | int | float | complex | str | bytes | numpy._typing._nested_sequence._NestedSequence[bool | int | float | complex | str | bytes] | None

forward(tensor_1, tensor_2)[source]

Add two tensors element-wise.

Parameters:
  • tensor_1 (Tensor) – First input tensor.

  • tensor_2 (Tensor) – Second input tensor.

Returns:

A Tensor representing the element-wise sum of the input tensors.

Raises:

AssertionError – If the shapes of the input tensors do not match.

Return type:

Tensor

class BinaryDivide[source]

Bases: Op

Element-wise division of two tensors.

This class implements the forward pass for element-wise division of two tensors.

TODO: we should probably fuse these into a single op

forward(tensor_1, tensor_2)[source]

Divide the elements of two tensors together, element-wise.

The two tensors must have the same shape.

Parameters:
  • tensor_1 (Tensor) – First input tensor (numerator).

  • tensor_2 (Tensor) – Second input tensor (denominator).

Returns:

A Tensor representing the element-wise division of the input tensors.

Raises:

AssertionError – If the shapes of the input tensors do not match.

Return type:

Tensor

class BinaryMax[source]

Bases: Op

Element-wise maximum of two tensors.

This class implements the forward and backward passes for element-wise maximum of two tensors.

_is_bigger_1

Boolean array indicating where the first tensor is larger.

_is_bigger_2

Boolean array indicating where the second tensor is larger or equal.

_out

The output of the forward pass.

Type:

numpy._typing._array_like._SupportsArray[numpy.dtype[Any]] | numpy._typing._nested_sequence._NestedSequence[numpy._typing._array_like._SupportsArray[numpy.dtype[Any]]] | bool | int | float | complex | str | bytes | numpy._typing._nested_sequence._NestedSequence[bool | int | float | complex | str | bytes] | None

_grad_1

The gradient for the first tensor.

_grad_2

The gradient for the second tensor.

back_fn_1(grad)[source]

Compute the gradient for the first tensor in the maximum operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

A Tensor representing the gradient for the first tensor.

Return type:

Tensor

back_fn_2(grad)[source]

Compute the gradient for the second tensor in the maximum operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

A Tensor representing the gradient for the second tensor.

Return type:

Tensor

forward(tensor_1, tensor_2)[source]

Compare two tensors element-wise, returning the maximum of each pair of elements.

The two tensors must have the same shape. If elements are equal, return the first.

Parameters:
  • tensor_1 (Tensor) – First input tensor.

  • tensor_2 (Tensor) – Second input tensor.

Returns:

A Tensor representing the element-wise maximum of the input tensors.

Raises:

AssertionError – If the shapes of the input tensors do not match.

Return type:

Tensor

class BinaryMin[source]

Bases: Op

Element-wise minimum of two tensors.

This class implements the forward and backward passes for element-wise minimum of two tensors.

_is_smaller_1

Boolean array indicating where the first tensor is smaller.

_is_smaller_2

Boolean array indicating where the second tensor is smaller or equal.

_out

The output of the forward pass.

Type:

numpy._typing._array_like._SupportsArray[numpy.dtype[Any]] | numpy._typing._nested_sequence._NestedSequence[numpy._typing._array_like._SupportsArray[numpy.dtype[Any]]] | bool | int | float | complex | str | bytes | numpy._typing._nested_sequence._NestedSequence[bool | int | float | complex | str | bytes] | None

_grad_1

The gradient for the first tensor.

_grad_2

The gradient for the second tensor.

back_fn_1(grad)[source]

Compute the gradient for the first tensor in the minimum operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

A Tensor representing the gradient for the first tensor.

Return type:

Tensor

back_fn_2(grad)[source]

Compute the gradient for the second tensor in the minimum operation.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

A Tensor representing the gradient for the second tensor.

Return type:

Tensor

forward(tensor_1, tensor_2)[source]

Compare two tensors element-wise, returning the minimum of each pair of elements.

The two tensors must have the same shape. If elements are equal, return the first.

Parameters:
  • tensor_1 (Tensor) – First input tensor.

  • tensor_2 (Tensor) – Second input tensor.

Returns:

A Tensor representing the element-wise minimum of the input tensors.

Raises:

AssertionError – If the shapes of the input tensors do not match.

Return type:

Tensor

class BinaryMultiply[source]

Bases: Op

Element-wise multiplication of two tensors.

This class implements the forward pass for element-wise multiplication of two tensors.

forward(tensor_1, tensor_2)[source]

Multiply the elements of two tensors together, element-wise.

The two tensors must have the same shape.

Parameters:
  • tensor_1 (Tensor) – First input tensor.

  • tensor_2 (Tensor) – Second input tensor.

Returns:

A Tensor representing the element-wise product of the input tensors.

Raises:

AssertionError – If the shapes of the input tensors do not match.

Return type:

Tensor

class BinarySubtract[source]

Bases: Op

Element-wise subtraction of two tensors.

This class implements the forward and backward passes for element-wise subtraction of two tensors.

_grad

The gradient of the backward pass for the second tensor.

_out

The output of the forward pass.

Type:

numpy._typing._array_like._SupportsArray[numpy.dtype[Any]] | numpy._typing._nested_sequence._NestedSequence[numpy._typing._array_like._SupportsArray[numpy.dtype[Any]]] | bool | int | float | complex | str | bytes | numpy._typing._nested_sequence._NestedSequence[bool | int | float | complex | str | bytes] | None

back_fn_2(grad)[source]

Compute the gradient for the second tensor in the subtraction.

Parameters:

grad (Tensor) – The gradient tensor.

Returns:

A Tensor representing the gradient for the second tensor.

Return type:

Tensor

forward(tensor_1, tensor_2)[source]

Subtract one tensor from another element-wise.

The two tensors must have the same shape.

Parameters:
  • tensor_1 (Tensor) – First input tensor.

  • tensor_2 (Tensor) – Second input tensor to be subtracted from the first.

Returns:

A Tensor representing the element-wise difference of the input tensors.

Raises:

AssertionError – If the shapes of the input tensors do not match.

Return type:

Tensor