How to get around pad error when doing torch.jit.save?

username: JimW
cooked: <p>I tried to use torch.jit.save to save my PyTorch model as a jit checkpoint file. But I came across the following errors:</p>
<pre><code class="lang-auto"> File "/opt/homebrew/lib/python3.11/site-packages/torch/jit/_script.py", line 1429, in script
    ret = _script_impl(
          ^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/torch/jit/_script.py", line 1147, in _script_impl
    return torch.jit._recursive.create_script_module(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/torch/jit/_recursive.py", line 557, in create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/torch/jit/_recursive.py", line 630, in create_script_module_impl
    script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/torch/jit/_script.py", line 650, in _construct
    init_fn(script_module)
  File "/opt/homebrew/lib/python3.11/site-packages/torch/jit/_recursive.py", line 606, in init_fn
    scripted = create_script_module_impl(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/torch/jit/_recursive.py", line 630, in create_script_module_impl
    script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/torch/jit/_script.py", line 650, in _construct
    init_fn(script_module)
  File "/opt/homebrew/lib/python3.11/site-packages/torch/jit/_recursive.py", line 606, in init_fn
    scripted = create_script_module_impl(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/torch/jit/_recursive.py", line 630, in create_script_module_impl
    script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/torch/jit/_script.py", line 650, in _construct
    init_fn(script_module)
  File "/opt/homebrew/lib/python3.11/site-packages/torch/jit/_recursive.py", line 606, in init_fn
    scripted = create_script_module_impl(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/torch/jit/_recursive.py", line 634, in create_script_module_impl
    create_methods_and_properties_from_stubs(
  File "/opt/homebrew/lib/python3.11/site-packages/torch/jit/_recursive.py", line 466, in create_methods_and_properties_from_stubs
    concrete_type._create_methods_and_properties(
RuntimeError: 

pad(Tensor input, int[] pad, str mode="constant", float? value=None) -&gt; Tensor:
Expected a value of type 'Optional[float]' for argument 'value' but instead found type 'int'.
:
  File "/opt/homebrew/lib/python3.11/site-packages/torch/nn/modules/padding.py", line 215
        return F.pad(input, self.padding, "constant", self.value)
               ~~~~~ &lt;--- HERE
</code></pre>
<p>This error is related to pad operation inside torch/nn/modules/padding.py.  Could someone provide some guidance how to get around this error?</p>

username: benjamin-perry-duke
cooked: <p>It is hard to diagnose without the architecture of the model you are trying to jit.save.</p>
<p>From a naive scan of the error, it seems that the value you have set as the padding value is an integer; whereas the jit script for saving is expecting a floating point number for the pad value. I would try switching that value in your source code to a float and see if that solves the error.</p>