[DTensor] parallelize_module failed with nn.Transformer and the PairwiseParallel plan
Labels: oncall: distributed, triaged

### 🐛 Describe the bug

`parallelize_module` failed with `nn.Transformer` and the `PairwiseParallel` plan, which is unexpected according to [the doc](https://pytorch.org/docs/stable/distributed.tensor.parallel.html#torch.distributed.tensor.parallel.style.PairwiseParallel) of `PairwiseParallel`.

```python
import torch
import torch.nn as nn
from torch.distributed._tensor import DeviceMesh
from torch.distributed.tensor.parallel import (
    PairwiseParallel,
    parallelize_module,
)
from torch.testing._internal.common_utils import run_tests
from torch.testing._internal.distributed._tensor.common_dtensor import (
    DTensorTestBase,
    NUM_DEVICES,
    skip_unless_torch_gpu,
    with_comms,
)

class TransformerWrap(nn.Module):
    def __init__(self, embed_dim, num_heads, device=None):
        super().__init__()
        self.transformer = nn.Transformer(
            d_model=embed_dim*num_heads, nhead=num_heads, device=device
        )

    def forward(self, src, tgt):
        return self.transformer(src, tgt)

class DistTensorParallelExampleTest(DTensorTestBase):

    @with_comms
    @skip_unless_torch_gpu
    def test_transformer_megatron_e2e(self):

        torch.manual_seed(0)
        src = torch.rand((10, 16, 128), device=self.device_type)
        tgt = torch.rand((20, 16, 128), device=self.device_type)

        torch.manual_seed(5)
        model_tp = TransformerWrap(embed_dim=16, num_heads=8, device=self.device_type)

        # Shard module and initialize optimizer.
        device_mesh = DeviceMesh(self.device_type, list(range(NUM_DEVICES)))
        parallelize_module(model_tp, device_mesh, PairwiseParallel())

        LR = 0.25
        optim_tp = torch.optim.SGD(model_tp.parameters(), lr=LR)

        output_tp = model_tp(src, tgt)

        output_tp.sum().backward()

        optim_tp.step()

if __name__ == "__main__":
    run_tests()
```

Errors:
[test_tp_transformer.log](https://github.com/pytorch/pytorch/files/11264608/test_tp_transformer.log)


### Versions

Collecting environment information...
PyTorch version: 2.1.0.dev20230417
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.6 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: version 3.22.1
Libc version: glibc-2.27

Python version: 3.10.10 (main, Mar 21 2023, 18:45:11) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-1030-aws-x86_64-with-glibc2.27
Is CUDA available: True
CUDA runtime version: 11.7.99
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: Tesla T4
GPU 1: Tesla T4
GPU 2: Tesla T4
GPU 3: Tesla T4

Nvidia driver version: 515.65.01
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.5.0
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              48
On-line CPU(s) list: 0-47
Thread(s) per core:  2
Core(s) per socket:  24
Socket(s):           1
NUMA node(s):        1
Vendor ID:           GenuineIntel
CPU family:          6
Model:               85
Model name:          Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
Stepping:            7
CPU MHz:             2499.996
BogoMIPS:            4999.99
Hypervisor vendor:   KVM
Virtualization type: full
L1d cache:           32K
L1i cache:           32K
L2 cache:            1024K
L3 cache:            36608K
NUMA node0 CPU(s):   0-47
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single pti fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves ida arat pku ospke avx512_vnni

Versions of relevant libraries:
[pip3] numpy==1.23.5
[pip3] torch==2.1.0.dev20230417
[pip3] torchaudio==2.1.0.dev20230417
[pip3] torchdata==0.7.0.dev20230417
[pip3] torchelastic==0.2.2
[pip3] torchtext==0.16.0.dev20230417
[pip3] torchvision==0.16.0.dev20230417
[pip3] triton==2.1.0
[conda] blas                      1.0                         mkl  
[conda] mkl                       2021.4.0           h06a4308_640  
[conda] mkl-service               2.4.0           py310h7f8727e_0  
[conda] mkl_fft                   1.3.1           py310hd6ae3a3_0  
[conda] mkl_random                1.2.2           py310h00e6091_0  
[conda] numpy                     1.23.5          py310hd5efca6_0  
[conda] numpy-base                1.23.5          py310h8e6c178_0  
[conda] pytorch                   2.1.0.dev20230417 py3.10_cuda11.7_cudnn8.5.0_0    pytorch-nightly
[conda] pytorch-cuda              11.7                 h778d358_3    pytorch-nightly
[conda] pytorch-mutex             1.0                        cuda    pytorch-nightly
[conda] torchaudio                2.1.0.dev20230417     py310_cu117    pytorch-nightly
[conda] torchdata                 0.7.0.dev20230417           py310    pytorch-nightly
[conda] torchelastic              0.2.2                    pypi_0    pypi
[conda] torchtext                 0.16.0.dev20230417           py310    pytorch-nightly
[conda] torchtriton               2.1.0+46672772b4           py310    pytorch-nightly
[conda] torchvision               0.16.0.dev20230417     py310_cu117    pytorch-nightly

cc @mrshenli @pritamdamania87 @zhaojuanmao @satgera @rohan-varma @gqchen @aazzolini @osalpekar @jiayisuse @H-Huang @kwen2501 @awgu