Skip to content
fn

pad_op

→TensorImplPtr
int pad_op(const int & a, int)

Python wrappers

Public Python APIs implemented by this engine symbol.

Pad a tensor on each side of each dimension with a constant fill value.

pad_width is a per-dimension list of (before, after) widths whose length must equal a.rank(). The output preserves the input's dtype and device; newly added elements are filled with constant.

Math

\begin{cases} x_{[i_0 - b_0, \ldots, i_{n-1} - b_{n-1}]} & \text{if } b_d \le i_d < b_d + D_d \text{ for all } d, \\ c & \text{otherwise}, \end{cases} $$ where $b_d$ is the leading pad width, $D_d$ is the input size at axis $d$, and $c$ is `constant`. **Shape** For each dimension $d$: $$ \text{out.shape}[d] = \text{a.shape}[d] + \text{pad\_width}[d].\text{first} + \text{pad\_width}[d].\text{second}. $$ **See Also** `concatenate_op` — pad is conceptually concat with constant tensors.

Parameters

aTensorImplPtr
Input tensor.
pad_widthvector<pair<int64_t, int64_t>>
One (before, after) pair per dimension of a. Both widths must be non-negative.
constantdouble
Fill value for the padded region. Cast to the input's dtype before storing.

Returns

TensorImplPtr

Padded tensor with shape out.shape[d] = a.shape[d] + before_d + after_d.

Raises

InvalidArgument
If any width in pad_width is negative.
LucidError
If pad_width.size() != a.rank().

Notes

Backward is implemented in the .cpp via PadBackward (schema name "pad", AmpPolicy::KeepInput). It slices the gradient sequentially per dimension, discarding pad_width[d].first leading and pad_width[d].second trailing elements to recover the original input region. Only the constant-fill mode is supported here; reflect / replicate modes live in higher-level Python wrappers.

Examples

Padding a ``(2, 2)`` tensor with ``pad_width = {(1, 0), (0, 2)}`` and
``constant = 0`` yields a ``(3, 4)`` output whose first row and last two
columns are zeros.