Pad#

Pad - 13#

Version

  • name: Pad (GitHub)

  • domain: main

  • since_version: 13

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

This version of the operator has been available since version 13.

Summary

Given a tensor containing the data to be padded (data), a tensor containing the number of start and end pad values for axis (pads), (optionally) a mode, and (optionally) constant_value, a padded tensor (output) is generated.

The three supported modes are (similar to corresponding modes supported by numpy.pad):

  1. constant`(default) - pads with a given constant value as specified by `constant_value (which defaults to 0, empty string, or False)

  2. reflect - pads with the reflection of the vector mirrored on the first and last values of the vector along each axis

  3. edge - pads with the edge values of array

Example 1 (constant mode):

Insert 0 pads to the beginning of the second dimension.

data = [

[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],

]

pads = [0, 2, 0, 0]

mode = ‘constant’

constant_value = 0.0

output = [

[0.0, 0.0, 1.0, 1.2], [0.0, 0.0, 2.3, 3.4], [0.0, 0.0, 4.5, 5.7],

]

Example 2 (reflect mode):

data = [

[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],

]

pads = [0, 2, 0, 0]

mode = ‘reflect’

output = [

[1.0, 1.2, 1.0, 1.2], [2.3, 3.4, 2.3, 3.4], [4.5, 5.7, 4.5, 5.7],

]

Example 3 (edge mode):

data = [

[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],

]

pads = [0, 2, 0, 0]

mode = ‘edge’

output = [

[1.0, 1.0, 1.0, 1.2], [2.3, 2.3, 2.3, 3.4], [4.5, 4.5, 4.5, 5.7],

]

Attributes

  • mode: Supported modes: constant`(default), `reflect, edge Default value is 'constant'.

Inputs

Between 2 and 3 inputs.

  • data (heterogeneous) - T: Input tensor.

  • pads (heterogeneous) - tensor(int64): Tensor of integers indicating the number of padding elements to add or remove (if negative) at the beginning and end of each axis. For 2D input tensor, it is the number of pixels. pads should be a 1D tensor of shape [2 * input_rank]. pads format should be: [x1_begin, x2_begin,…,x1_end, x2_end,…], where xi_begin is the number of pad values added at the beginning of axis i and xi_end, the number of pad values added at the end of axis i.

  • constant_value (optional, heterogeneous) - T: (Optional) A scalar value to be used if the mode chosen is constant (by default it is 0, empty string or False).

Outputs

  • output (heterogeneous) - T: Tensor after padding.

Type Constraints

  • T in ( tensor(bfloat16), tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain input and output types to all tensor types.

Examples

constant_pad

node = onnx.helper.make_node(
    'Pad',
    inputs=['x', 'pads', 'value'],
    outputs=['y'],
    mode='constant'
)
x = np.random.randn(1, 3, 4, 5).astype(np.float32)
pads = np.array([0, 0, 1, 3, 0, 0, 2, 4]).astype(np.int64)  # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]
value = np.float32(1.2)
y = pad_impl(
    x,
    pads,
    'constant',
    1.2
)

expect(node, inputs=[x, pads, value], outputs=[y],
       name='test_constant_pad')

reflection_and_edge_pad

for mode in ['edge', 'reflect']:
    node = onnx.helper.make_node(
        'Pad',
        inputs=['x', 'pads'],
        outputs=['y'],
        mode=mode
    )
    x = np.random.randn(1, 3, 4, 5).astype(np.int32)
    pads = np.array([0, 0, 1, 1, 0, 0, 1, 1]).astype(np.int64)  # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]
    y = pad_impl(
        x,
        pads,
        mode
    )

    expect(node, inputs=[x, pads], outputs=[y],
           name='test_{}_pad'.format(mode))

Differences

00Given a tensor containing the data to be padded (data), a tensor containing the number of start and end pad values for axis (pads), (optionally) a mode, and (optionally) constant_value,Given a tensor containing the data to be padded (data), a tensor containing the number of start and end pad values for axis (pads), (optionally) a mode, and (optionally) constant_value,
11a padded tensor (output) is generated.a padded tensor (output) is generated.
22
33The three supported modes are (similar to corresponding modes supported by numpy.pad):The three supported modes are (similar to corresponding modes supported by numpy.pad):
44
551) constant(default) - pads with a given constant value as specified by constant_value (which defaults to 0)1) constant(default) - pads with a given constant value as specified by constant_value (which defaults to 0, empty string, or False)
66
772) reflect - pads with the reflection of the vector mirrored on the first and last values of the vector along each axis2) reflect - pads with the reflection of the vector mirrored on the first and last values of the vector along each axis
88
993) edge - pads with the edge values of array3) edge - pads with the edge values of array
1010
1111Example 1 (constant mode):Example 1 (constant mode):
1212 Insert 0 pads to the beginning of the second dimension. Insert 0 pads to the beginning of the second dimension.
1313
1414 data = data =
1515 [ [
1616 [1.0, 1.2], [1.0, 1.2],
1717 [2.3, 3.4], [2.3, 3.4],
1818 [4.5, 5.7], [4.5, 5.7],
1919 ] ]
2020
2121 pads = [0, 2, 0, 0] pads = [0, 2, 0, 0]
2222
2323 mode = 'constant' mode = 'constant'
2424
2525 constant_value = 0.0 constant_value = 0.0
2626
2727 output = output =
2828 [ [
2929 [0.0, 0.0, 1.0, 1.2], [0.0, 0.0, 1.0, 1.2],
3030 [0.0, 0.0, 2.3, 3.4], [0.0, 0.0, 2.3, 3.4],
3131 [0.0, 0.0, 4.5, 5.7], [0.0, 0.0, 4.5, 5.7],
3232 ] ]
3333
3434Example 2 (reflect mode):Example 2 (reflect mode):
3535 data = data =
3636 [ [
3737 [1.0, 1.2], [1.0, 1.2],
3838 [2.3, 3.4], [2.3, 3.4],
3939 [4.5, 5.7], [4.5, 5.7],
4040 ] ]
4141
4242 pads = [0, 2, 0, 0] pads = [0, 2, 0, 0]
4343
4444 mode = 'reflect' mode = 'reflect'
4545
4646 output = output =
4747 [ [
4848 [1.0, 1.2, 1.0, 1.2], [1.0, 1.2, 1.0, 1.2],
4949 [2.3, 3.4, 2.3, 3.4], [2.3, 3.4, 2.3, 3.4],
5050 [4.5, 5.7, 4.5, 5.7], [4.5, 5.7, 4.5, 5.7],
5151 ] ]
5252
5353Example 3 (edge mode):Example 3 (edge mode):
5454 data = data =
5555 [ [
5656 [1.0, 1.2], [1.0, 1.2],
5757 [2.3, 3.4], [2.3, 3.4],
5858 [4.5, 5.7], [4.5, 5.7],
5959 ] ]
6060
6161 pads = [0, 2, 0, 0] pads = [0, 2, 0, 0]
6262
6363 mode = 'edge' mode = 'edge'
6464
6565 output = output =
6666 [ [
6767 [1.0, 1.0, 1.0, 1.2], [1.0, 1.0, 1.0, 1.2],
6868 [2.3, 2.3, 2.3, 3.4], [2.3, 2.3, 2.3, 3.4],
6969 [4.5, 4.5, 4.5, 5.7], [4.5, 4.5, 4.5, 5.7],
7070 ] ]
7171
7272**Attributes****Attributes**
7373
7474* **mode**:* **mode**:
7575 Supported modes: constant(default), reflect, edge Default value is 'constant'. Supported modes: constant(default), reflect, edge Default value is 'constant'.
7676
7777**Inputs****Inputs**
7878
7979Between 2 and 3 inputs.Between 2 and 3 inputs.
8080
8181* **data** (heterogeneous) - **T**:* **data** (heterogeneous) - **T**:
8282 Input tensor. Input tensor.
8383* **pads** (heterogeneous) - **tensor(int64)**:* **pads** (heterogeneous) - **tensor(int64)**:
8484 Tensor of integers indicating the number of padding elements to add Tensor of integers indicating the number of padding elements to add
8585 or remove (if negative) at the beginning and end of each axis. For or remove (if negative) at the beginning and end of each axis. For
8686 2D input tensor, it is the number of pixels. pads should be a 1D 2D input tensor, it is the number of pixels. pads should be a 1D
8787 tensor of shape [2 * input_rank]. pads format should be: tensor of shape [2 * input_rank]. pads format should be:
8888 [x1_begin, x2_begin,...,x1_end, x2_end,...], where xi_begin is the [x1_begin, x2_begin,...,x1_end, x2_end,...], where xi_begin is the
8989 number of pad values added at the beginning of axis i and xi_end, number of pad values added at the beginning of axis i and xi_end,
9090 the number of pad values added at the end of axis i. the number of pad values added at the end of axis i.
9191* **constant_value** (optional, heterogeneous) - **T**:* **constant_value** (optional, heterogeneous) - **T**:
9292 (Optional) A scalar value to be used if the mode chosen is (Optional) A scalar value to be used if the mode chosen is
9393 constant (by default it is 0). constant (by default it is 0, empty string or False).
9494
9595**Outputs****Outputs**
9696
9797* **output** (heterogeneous) - **T**:* **output** (heterogeneous) - **T**:
9898 Tensor after padding. Tensor after padding.
9999
100100**Type Constraints****Type Constraints**
101101
102102* **T** in (* **T** in (
103 tensor(bfloat16),
104 tensor(bool),
105 tensor(complex128),
106 tensor(complex64),
103107 tensor(double), tensor(double),
104108 tensor(float), tensor(float),
105109 tensor(float16), tensor(float16),
106110 tensor(int16), tensor(int16),
107111 tensor(int32), tensor(int32),
108112 tensor(int64), tensor(int64),
109113 tensor(int8), tensor(int8),
114 tensor(string),
110115 tensor(uint16), tensor(uint16),
111116 tensor(uint32), tensor(uint32),
112117 tensor(uint64), tensor(uint64),
113118 tensor(uint8) tensor(uint8)
114119 ): ):
115120 Constrains input and output to only numeric types. Constrain input and output types to all tensor types.

Pad - 11#

Version

  • name: Pad (GitHub)

  • domain: main

  • since_version: 11

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

This version of the operator has been available since version 11.

Summary

Given a tensor containing the data to be padded (data), a tensor containing the number of start and end pad values for axis (pads), (optionally) a mode, and (optionally) constant_value, a padded tensor (output) is generated.

The three supported modes are (similar to corresponding modes supported by numpy.pad):

  1. constant`(default) - pads with a given constant value as specified by `constant_value (which defaults to 0)

  2. reflect - pads with the reflection of the vector mirrored on the first and last values of the vector along each axis

  3. edge - pads with the edge values of array

Example 1 (constant mode):

Insert 0 pads to the beginning of the second dimension.

data = [

[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],

]

pads = [0, 2, 0, 0]

mode = ‘constant’

constant_value = 0.0

output = [

[0.0, 0.0, 1.0, 1.2], [0.0, 0.0, 2.3, 3.4], [0.0, 0.0, 4.5, 5.7],

]

Example 2 (reflect mode):

data = [

[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],

]

pads = [0, 2, 0, 0]

mode = ‘reflect’

output = [

[1.0, 1.2, 1.0, 1.2], [2.3, 3.4, 2.3, 3.4], [4.5, 5.7, 4.5, 5.7],

]

Example 3 (edge mode):

data = [

[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],

]

pads = [0, 2, 0, 0]

mode = ‘edge’

output = [

[1.0, 1.0, 1.0, 1.2], [2.3, 2.3, 2.3, 3.4], [4.5, 4.5, 4.5, 5.7],

]

Attributes

  • mode: Supported modes: constant`(default), `reflect, edge Default value is 'constant'.

Inputs

Between 2 and 3 inputs.

  • data (heterogeneous) - T: Input tensor.

  • pads (heterogeneous) - tensor(int64): Tensor of integers indicating the number of padding elements to add or remove (if negative) at the beginning and end of each axis. For 2D input tensor, it is the number of pixels. pads should be a 1D tensor of shape [2 * input_rank]. pads format should be: [x1_begin, x2_begin,…,x1_end, x2_end,…], where xi_begin is the number of pad values added at the beginning of axis i and xi_end, the number of pad values added at the end of axis i.

  • constant_value (optional, heterogeneous) - T: (Optional) A scalar value to be used if the mode chosen is constant (by default it is 0).

Outputs

  • output (heterogeneous) - T: Tensor after padding.

Type Constraints

  • T in ( tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrains input and output to only numeric types.

Differences

0Given a tensor containing the data to be padded (data), a tensor containing the number of start and end pad values for axis (pads), (optionally) a mode, and (optionally) constant_value,
1a padded tensor (output) is generated.
2
03Given data tensor, pads, mode, and value.The three supported modes are (similar to corresponding modes supported by numpy.pad):
1Example:
4
51) constant(default) - pads with a given constant value as specified by constant_value (which defaults to 0)
6
27 Insert 0 pads to the beginning of the second dimension.2) reflect - pads with the reflection of the vector mirrored on the first and last values of the vector along each axis
3 data = [
8
49 [1.0, 1.2],3) edge - pads with the edge values of array
10
511 [2.3, 3.4],Example 1 (constant mode):
612 [4.5, 5.7], Insert 0 pads to the beginning of the second dimension.
13
14 data =
15 [
16 [1.0, 1.2],
17 [2.3, 3.4],
18 [4.5, 5.7],
719 ] ]
20
821 pads = [0, 2, 0, 0] pads = [0, 2, 0, 0]
22
23 mode = 'constant'
24
25 constant_value = 0.0
26
927 output = [ output =
10 [
28 [
1129 [0.0, 0.0, 1.0, 1.2], [0.0, 0.0, 1.0, 1.2],
1230 [0.0, 0.0, 2.3, 3.4], [0.0, 0.0, 2.3, 3.4],
1331 [0.0, 0.0, 4.5, 5.7], [0.0, 0.0, 4.5, 5.7],
14 ],
1532 ] ]
1633
34Example 2 (reflect mode):
35 data =
36 [
37 [1.0, 1.2],
38 [2.3, 3.4],
39 [4.5, 5.7],
40 ]
41
42 pads = [0, 2, 0, 0]
43
44 mode = 'reflect'
45
46 output =
47 [
48 [1.0, 1.2, 1.0, 1.2],
49 [2.3, 3.4, 2.3, 3.4],
50 [4.5, 5.7, 4.5, 5.7],
51 ]
52
53Example 3 (edge mode):
54 data =
55 [
56 [1.0, 1.2],
57 [2.3, 3.4],
58 [4.5, 5.7],
59 ]
60
61 pads = [0, 2, 0, 0]
62
63 mode = 'edge'
64
65 output =
66 [
67 [1.0, 1.0, 1.0, 1.2],
68 [2.3, 2.3, 2.3, 3.4],
69 [4.5, 4.5, 4.5, 5.7],
70 ]
71
1772**Attributes****Attributes**
1873
1974* **mode**:* **mode**:
2075 Three modes: constant(default), reflect, edge Default value is 'constant'. Supported modes: constant(default), reflect, edge Default value is 'constant'.
76
77**Inputs**
78
79Between 2 and 3 inputs.
80
81* **data** (heterogeneous) - **T**:
82 Input tensor.
2183* **pads** (required):* **pads** (heterogeneous) - **tensor(int64)**:
2284 List of integers indicating the number of padding elements to add or Tensor of integers indicating the number of padding elements to add
2385 remove (if negative) at the beginning and end of each axis. For 2D or remove (if negative) at the beginning and end of each axis. For
2486 it is the number of pixels. pads rank should be double of the 2D input tensor, it is the number of pixels. pads should be a 1D
2587 input's rank. pads format should be as follow [x1_begin, tensor of shape [2 * input_rank]. pads format should be:
2688 x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels [x1_begin, x2_begin,...,x1_end, x2_end,...], where xi_begin is the
2789 added at the beginning of axis i and xi_end, the number of pixels number of pad values added at the beginning of axis i and xi_end,
2890 added at the end of axis i. the number of pad values added at the end of axis i.
2991* **value**:* **constant_value** (optional, heterogeneous) - **T**:
92 (Optional) A scalar value to be used if the mode chosen is
3093 One float, indicates the value to be filled. Default value is 0.0. constant (by default it is 0).
3194
32**Inputs**
33
34* **data** (heterogeneous) - **T**:
35 Input tensor.
36
3795**Outputs****Outputs**
3896
3997* **output** (heterogeneous) - **T**:* **output** (heterogeneous) - **T**:
4098 Tensor after padding. Tensor after padding.
4199
42100**Type Constraints****Type Constraints**
43101
44102* **T** in (* **T** in (
45103 tensor(double), tensor(double),
46104 tensor(float), tensor(float),
47105 tensor(float16) tensor(float16),
106 tensor(int16),
107 tensor(int32),
108 tensor(int64),
109 tensor(int8),
110 tensor(uint16),
111 tensor(uint32),
112 tensor(uint64),
113 tensor(uint8)
48114 ): ):
49115 Constrain input and output types to float tensors. Constrains input and output to only numeric types.

Pad - 2#

Version

  • name: Pad (GitHub)

  • domain: main

  • since_version: 2

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

This version of the operator has been available since version 2.

Summary

Given data tensor, pads, mode, and value. Example:

Insert 0 pads to the beginning of the second dimension. data = [

[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],

] pads = [0, 2, 0, 0] output = [

[

[0.0, 0.0, 1.0, 1.2], [0.0, 0.0, 2.3, 3.4], [0.0, 0.0, 4.5, 5.7],

],

]

Attributes

  • mode: Three modes: constant(default), reflect, edge Default value is 'constant'.

  • pads (required): List of integers indicating the number of padding elements to add or remove (if negative) at the beginning and end of each axis. For 2D it is the number of pixels. pads rank should be double of the input’s rank. pads format should be as follow [x1_begin, x2_begin…x1_end, x2_end,…], where xi_begin the number of pixels added at the beginning of axis i and xi_end, the number of pixels added at the end of axis i.

  • value: One float, indicates the value to be filled. Default value is 0.0.

Inputs

  • data (heterogeneous) - T: Input tensor.

Outputs

  • output (heterogeneous) - T: Tensor after padding.

Type Constraints

  • T in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.

Differences

00Given data tensor, paddings, mode, and value.Given data tensor, pads, mode, and value.
11Example:Example:
22 Insert 0 paddings to the beginning of the second dimension. Insert 0 pads to the beginning of the second dimension.
33 data = [ data = [
44 [1.0, 1.2], [1.0, 1.2],
55 [2.3, 3.4], [2.3, 3.4],
66 [4.5, 5.7], [4.5, 5.7],
77 ] ]
88 paddings = [0, 0, 2, 0] pads = [0, 2, 0, 0]
99 output = [ output = [
1010 [ [
1111 [0.0, 0.0, 1.0, 1.2], [0.0, 0.0, 1.0, 1.2],
1212 [0.0, 0.0, 2.3, 3.4], [0.0, 0.0, 2.3, 3.4],
1313 [0.0, 0.0, 4.5, 5.7], [0.0, 0.0, 4.5, 5.7],
1414 ], ],
1515 ] ]
1616
1717**Attributes****Attributes**
1818
1919* **mode**:* **mode**:
2020 Three modes: constant(default), reflect, edge Default value is 'constant'. Three modes: constant(default), reflect, edge Default value is 'constant'.
2121* **paddings** (required):* **pads** (required):
2222 List of integers indicate the padding element count at the beginning List of integers indicating the number of padding elements to add or
23 remove (if negative) at the beginning and end of each axis. For 2D
2324 and end of each axis, for 2D it is the number of pixel. paddings it is the number of pixels. pads rank should be double of the
2425 rank should be double of the input's rank. paddings format should input's rank. pads format should be as follow [x1_begin,
2526 be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels
2627 xi_begin the number of pixels added at the beginning of axis i and added at the beginning of axis i and xi_end, the number of pixels
2728 xi_end, the number of pixels added at the end of axis i. added at the end of axis i.
2829* **value**:* **value**:
2930 One float, indicates the value to be filled, default is 0 Default value is 0.0. One float, indicates the value to be filled. Default value is 0.0.
3031
3132**Inputs****Inputs**
3233
3334* **data** (heterogeneous) - **T**:* **data** (heterogeneous) - **T**:
3435 Input tensor. Input tensor.
3536
3637**Outputs****Outputs**
3738
3839* **output** (heterogeneous) - **T**:* **output** (heterogeneous) - **T**:
3940 Tensor after padding. Tensor after padding.
4041
4142**Type Constraints****Type Constraints**
4243
4344* **T** in (* **T** in (
4445 tensor(double), tensor(double),
4546 tensor(float), tensor(float),
4647 tensor(float16) tensor(float16)
4748 ): ):
4849 Constrain input and output types to float tensors. Constrain input and output types to float tensors.

Pad - 1#

Version

  • name: Pad (GitHub)

  • domain: main

  • since_version: 1

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: False

This version of the operator has been available since version 1.

Summary

Given data tensor, paddings, mode, and value. Example:

Insert 0 paddings to the beginning of the second dimension. data = [

[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],

] paddings = [0, 0, 2, 0] output = [

[

[0.0, 0.0, 1.0, 1.2], [0.0, 0.0, 2.3, 3.4], [0.0, 0.0, 4.5, 5.7],

],

]

Attributes

  • mode: Three modes: constant(default), reflect, edge Default value is 'constant'.

  • paddings (required): List of integers indicate the padding element count at the beginning and end of each axis, for 2D it is the number of pixel. paddings rank should be double of the input’s rank. paddings format should be as follow [x1_begin, x2_begin…x1_end, x2_end,…], where xi_begin the number of pixels added at the beginning of axis i and xi_end, the number of pixels added at the end of axis i.

  • value: One float, indicates the value to be filled, default is 0 Default value is 0.0.

Inputs

  • data (heterogeneous) - T: Input tensor.

Outputs

  • output (heterogeneous) - T: Tensor after padding.

Type Constraints

  • T in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.