Skip to Content

pad_op

View the code on GitHub

Structs

Struct: Pad

Fields

Methods

fwd(arg0: Array, target_shape: ArrayShape, slices_in_target: List[Slice]) -> Array
Pads an array to a target shape.
Args
  • arg0: Array The input array to be padded.

  • target_shape: ArrayShape The target shape to pad the input array to.

  • slices_in_target: List[Slice] A list of slices specifying the region in the target shape where the input array is copied.

Returns
  • Array - An array containing the input array padded to the target shape.

Pads the input array to the target shape by copying the input array to the target shape. The target shape must be larger than the input array shape. The slices in the target shape specify the region where the input array is copied.

Examples:

a = Array([[1, 2], [3, 4]]) target_shape = ArrayShape([2, 3]) slices_in_target = [Slice(0, 2), Slice(0, 2)] result = pad(a, target_shape, slices_in_target) print(result)

Note: This function supports:

  • Automatic differentiation (reverse mode only).
  • Complex valued arguments.
vjp(primals: List[Array], grad: Array, out: Array) -> List[Array]
Computes the vector-Jacobian product for the padding operation.
Args
  • primals: List[Array] A list containing the primal input array and the target shape.

  • grad: Array The gradient of the output with respect to some scalar function.

  • out: Array The output of the forward pass (unused in this function).

Returns
  • List[Array] - A list containing the gradient with respect to the input.

Implements reverse-mode automatic differentiation for the padding operation.

Note: The vector-Jacobian product for padding is computed as the gradient of the output array sliced to the target shape.

padded_shape(mut curr: ArrayShape, args: List[ArrayShape])
Computes the shape of an array after padding.
Args
  • curr: ArrayShape The ArrayShape to store the result of the computation.

  • args: List[ArrayShape] The ArrayShape to pad, the target ArrayShape.

__call__(mut curr: Array, args: List[Array])
Performs the forward pass for padding an array to a target shape.
Args
  • curr: Array The current array to store the result (modified in-place).

  • args: List[Array] A list containing the input array and the target shape.

Pads the input array to the target shape and stores the result in the current array. Initializes the current array if not already set up.

Note: This function assumes that the shape and data of the args are already set up. If the current array (curr) is not initialized, it computes the shape based on the target shape and sets up the data accordingly.

Functions

pad

pad(arg0: Array, target_shape: ArrayShape, slices_in_target: List[Slice]) -> Array
Pads an array to a target shape.
Args
  • arg0: Array The input array to be padded.

  • target_shape: ArrayShape The target shape to pad the input array to.

  • slices_in_target: List[Slice] A list of slices specifying the region in the target shape where the input array is copied.

Returns
  • Array - An array containing the input array padded to the target shape.

Pads the input array to the target shape by copying the input array to the target shape. The target shape must be larger than the input array shape. The slices in the target shape specify the region where the input array is copied.

Examples:

a = Array([[1, 2], [3, 4]]) target_shape = ArrayShape([2, 3]) slices_in_target = [Slice(0, 2), Slice(0, 2)] result = pad(a, target_shape, slices_in_target) print(result)

Note: This function supports:

  • Automatic differentiation (reverse mode only).
  • Complex valued arguments.
Last updated on