Skip to content

torch.nn.ZeroPad2d is not scriptable #47528

@BowenBao

Description

@BowenBao

🐛 Bug

Runtime error thrown when trying to script model that calls nn.ZeroPad2d.

To Reproduce

Steps to reproduce the behavior:

  1. Run the below script in python.
import torch
from torch import nn

class Pad(torch.nn.Module):
    def forward(self, x):
        pad_op =  nn.ZeroPad2d(padding=(10, 20, 0, 0))
        return pad_op(x)

m = torch.jit.script(Pad())
  1. Observe error
RuntimeError:
Unknown type name '_size_4_t':
  File "/home/bowbao/repos/pytorch/torch/nn/modules/padding.py", line 453
    def __init__(self, padding: _size_4_t) -> None:
                                ~~~~~~~~~ <--- HERE
        super(ZeroPad2d, self).__init__(padding, 0.)
'ZeroPad2d.__init__' is being compiled since it was called from '__torch__.torch.nn.modules.padding.ZeroPad2d'
  File "repro_1p_rnn_fairseq.py", line 6
    def forward(self, x):
        pad_op =  nn.ZeroPad2d(padding=(10, 20, 0, 0))
                  ~~~~~~~~~~~~ <--- HERE
        return pad_op(x)
'__torch__.torch.nn.modules.padding.ZeroPad2d' is being compiled since it was called from 'Pad.forward'
  File "repro_1p_rnn_fairseq.py", line 6
    def forward(self, x):
        pad_op =  nn.ZeroPad2d(padding=(10, 20, 0, 0))
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        return pad_op(x)

Expected behavior

Create scripted module correctly.

cc @gmagogsfm

Metadata

Metadata

Assignees

Labels

oncall: jitAdd this issue/PR to JIT oncall triage queue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions