Repository: pytorch
Issue: nn.functional.pad accepts bool values but raises internal assert when converted to JIT
Labels: oncall: jit

### 🐛 Describe the bug

While working on Longformer in `transformers`, I came across this comment: https://github.com/huggingface/transformers/issues/13126#issuecomment-993645323. An incorrect implementation passes a bool as `value` into `nn.functional.pad`, which normally works. However, it raises an internal assert when used with `torch.jit`:

Example:

```python
import torch
from torch import nn

class MyModule(nn.Module):
    def forward(self, inputs):
        return nn.functional.pad(
            inputs, (0, inputs.size(1) + 1), value=False # works if value=0
        )
```


`torch.jit.trace_module(MyModule(), {"forward": torch.zeros(3, 4)})` results in
```
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/Users/patrick/Projects/open-source/transformers/notebooks/torch-jit-pad.ipynb Cell 3' in <cell line: 1>()
----> 1 torch.jit.trace_module(MyModule(), {"forward": torch.zeros(3, 4)})

File ~/.pyenv-x86/versions/transformers-x86/lib/python3.9/site-packages/torch/jit/_trace.py:958, in trace_module(mod, inputs, optimize, check_trace, check_inputs, check_tolerance, strict, _force_outplace, _module_class, _compilation_unit)
    954     argument_names = get_callable_argument_names(func)
    956 example_inputs = make_tuple(example_inputs)
--> 958 module._c._create_method_from_trace(
    959     method_name,
    960     func,
    961     example_inputs,
    962     var_lookup_fn,
    963     strict,
    964     _force_outplace,
    965     argument_names,
    966 )
    967 check_trace_method = module._c._get_method(method_name)
    969 # Check the trace against new traces created from user-specified inputs

RuntimeError: 0INTERNAL ASSERT FAILED at "/Users/distiller/project/pytorch/torch/csrc/jit/ir/alias_analysis.cpp":607, please report a bug to PyTorch. We don't have an op for aten::constant_pad_nd but it isn't a special case.  Argument types: Tensor, int[], bool, 

Candidates:
	aten::constant_pad_nd(Tensor self, int[] pad, Scalar value=0) -> (Tensor)
```

`torch.jit.script(MyModule())` results in

```
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/Users/patrick/Projects/open-source/transformers/notebooks/torch-jit-pad.ipynb Cell 3' in <cell line: 1>()
----> 1 torch.jit.script(MyModule())

File ~/.pyenv/versions/torch/lib/python3.9/site-packages/torch/jit/_script.py:1265, in script(obj, optimize, _frames_up, _rcb, example_inputs)
   1263 if isinstance(obj, torch.nn.Module):
   1264     obj = call_prepare_scriptable_func(obj)
-> 1265     return torch.jit._recursive.create_script_module(
   1266         obj, torch.jit._recursive.infer_methods_to_compile
   1267     )
   1269 if isinstance(obj, dict):
   1270     return create_script_dict(obj)

File ~/.pyenv/versions/torch/lib/python3.9/site-packages/torch/jit/_recursive.py:454, in create_script_module(nn_module, stubs_fn, share_types, is_tracing)
    452 if not is_tracing:
    453     AttributeTypeIsSupportedChecker().check(nn_module)
--> 454 return create_script_module_impl(nn_module, concrete_type, stubs_fn)

File ~/.pyenv/versions/torch/lib/python3.9/site-packages/torch/jit/_recursive.py:520, in create_script_module_impl(nn_module, concrete_type, stubs_fn)
    518 # Compile methods if necessary
    519 if concrete_type not in concrete_type_store.methods_compiled:
--> 520     create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
    521     # Create hooks after methods to ensure no name collisions between hooks and methods.
    522     # If done before, hooks can overshadow methods that aren't exported.
    523     create_hooks_from_stubs(concrete_type, hook_stubs, pre_hook_stubs)

File ~/.pyenv/versions/torch/lib/python3.9/site-packages/torch/jit/_recursive.py:371, in create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
    368 property_defs = [p.def_ for p in property_stubs]
    369 property_rcbs = [p.resolution_callback for p in property_stubs]
--> 371 concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)

RuntimeError: 

_pad(Tensor input, int[] pad, str mode="constant", float value=0.) -> (Tensor):
Expected a value of type 'float' for argument 'value' but instead found type 'bool'.
:
  File "/var/folders/qm/r50_76fn105g_18z8w81058c0000gn/T/ipykernel_98988/2039179918.py", line 3
    def forward(self, inputs):
        return nn.functional.pad(
               ~~~~~~~~~~~~~~~~~ <--- HERE
            inputs, (0, inputs.size(1) + 1), value=False # works if value=0
        )
```

Is this considered a user error, or should it be addressed? Maybe we could at least improve the error message for `torch.jit.trace_module`.

Best,
Patrick

### Versions

Collecting environment information...
PyTorch version: 1.11.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.3.1 (arm64)
GCC version: Could not collect
Clang version: 13.1.6 (clang-1316.0.21.2.3)
CMake version: version 3.23.1
Libc version: N/A

Python version: 3.9.11 (main, May  4 2022, 09:48:34)  [Clang 13.1.6 (clang-1316.0.21.2.3)] (64-bit runtime)
Python platform: macOS-12.3.1-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.22.3
[pip3] torch==1.11.0
[pip3] torchaudio==0.11.0
[pip3] torchvision==0.12.0
[conda] Could not collect