pad#

Pad operators along a dimension. The returned operator will have the same rank as the input operator. The sizes of the unpadded dimensions will be the same as the input operator and the padded dimension will increase by the sum of the pre- and post-padding sizes.

The currently suported padding modes are MATX_PAD_MODE_CONSTANT and MATX_PAD_MODE_EDGE. The constant padding mode uses the specified pad value for all padded elements. The edge padding mode uses the edge values of the input operator for the padded elements (i.e., pre-padding will use the value of the first element and post-padding will use the value of the last element).

enum matx::PadMode#

Padding mode.

The padding mode to use for the pad operator. The default value is MATX_PAD_MODE_CONSTANT.

Values:

enumerator MATX_PAD_MODE_CONSTANT#

Constant padding mode. All padding elements will be set to the user-provided pad_value.

enumerator MATX_PAD_MODE_EDGE#

Edge padding mode. All padding elements will be set to the edge values of the original operator.

template<typename T>
__MATX_INLINE__ __MATX_HOST__ auto matx::pad(const T &op, int axis, const std::array<index_t, 2> &pad_sizes, const typename T::value_type &pad_value, PadMode mode = MATX_PAD_MODE_CONSTANT)#

Pad an operator along a single dimension.

Creates a new operator that pads the input operator along the specified dimension with a constant value or edge replication.

Template Parameters:

T – Input operator type

Parameters:
  • op – Input operator to pad

  • axis – Dimension along which to pad

  • pad_sizes – std::array containing {before, after} padding sizes. This operator will add before elements before the original operator and after elements after the original operator.

  • pad_value – Value to use for padding (constant padding mode only)

  • mode – Padding mode. Defaults to MATX_PAD_MODE_CONSTANT if not provided.

Returns:

Padded operator

template<typename T>
__MATX_INLINE__ __MATX_HOST__ auto matx::pad(const T &op, int axis, const index_t (&pad_sizes)[2], const typename T::value_type &pad_value, PadMode mode = MATX_PAD_MODE_CONSTANT)#

Pad an operator along a single dimension.

Creates a new operator that pads the input operator along the specified dimension with a constant value or edge replication.

Template Parameters:

T – Input operator type

Parameters:
  • op – Input operator to pad

  • axis – Dimension along which to pad

  • pad_sizes – C-style array containing {before, after} padding sizes. This operator will add before elements before the original operator and after elements after the original operator.

  • pad_value – Value to use for padding (constant padding mode only)

  • mode – Padding mode. Defaults to MATX_PAD_MODE_CONSTANT if not provided.

Returns:

Padded operator

Examples#

const int N = 5;
const int before = 2;
const int after = 3;

auto t1 = make_tensor<TestType>({N});
auto t1_padded = make_tensor<TestType>({N+before+after});

for (int i = 0; i < N; i++) {
  t1(i) = TestType(i+1);
}

// Pad tensor t1 along dimension 0 using value 42. This will included two padding
// elements before the original data and three padding elements after the original data.
(t1_padded = pad(t1, 0, {before, after}, TestType{42})).run(exec);

exec.sync();
// t1_padded is {42, 42, 1, 2, 3, 4, 5, 42, 42, 42}
const int N = 5;
const int before = 2;
const int after = 3;

auto t1 = make_tensor<TestType>({N});
auto t1_padded = make_tensor<TestType>({N+before+after});

for (int i = 0; i < N; i++) {
  t1(i) = TestType(i+1);
}

// Pad tensor t1 along dimension 0 using edge padding. This will included two padding
// elements before the original data and three padding elements after the original data.
(t1_padded = pad(t1, 0, {before, after}, TestType{42}, matx::MATX_PAD_MODE_EDGE)).run(exec);

exec.sync();
// t1_padded is {1, 1, 1, 2, 3, 4, 5, 5, 5, 5}