Repository: pytorch
Issue: Cannot use mask and slice assignment together
Labels: triaged,module: advanced indexing

### 🐛 Describe the bug

I don't know whether it is a bug or just an unpleasant feature due to the underlying variable managing principles. Here's a minimal example for the bug: 
```python
x = torch.zeros(2, 3, 4, 6)
mask = torch.tensor([[ True, True, False], [True, False, True]])
y = torch.rand(2, 3, 4, 3)
x[mask, :, :3] = y[mask]
```
In which I hope to
- In dimension 0 and 1, only the 4x6/1x3 slices in `x`/`y` that whose corresponding element in `mask` is `True` are allowed to be assigned. 
- In dimension 3, only the first 3 elements in `x` are assigned with the 3-element tensor from `y`.
However I got the error below
```
RuntimeError: shape mismatch: value tensor of shape [4, 4, 3] cannot be broadcast to indexing result of shape [4, 3, 6]
```

### Versions

```
Collecting environment information...
PyTorch version: 2.1.1
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Microsoft Windows 10 专业版 (10.0.19045 64 位) # In English: Microsoft Windows 10 Pro (10.0.19045 64-bit)
GCC version: Could not collect
Clang version: Could not collect
CMake version: version 3.27.4
Libc version: N/A

Python version: 3.9.18 (main, Sep 11 2023, 14:09:26) [MSC v.1916 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.19045-SP0
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA GeForce RTX 4090
GPU 1: NVIDIA GeForce RTX 4090

Nvidia driver version: 560.94
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
----------------------
Name: Intel(R) Xeon(R) Platinum 8374B CPU @ 2.70GHz
Manufacturer: GenuineIntel
Family: 179
Architecture: 9
ProcessorType: 3
DeviceID: CPU0
CurrentClockSpeed: 2700
MaxClockSpeed: 2700
L2CacheSize: 48640
L2CacheSpeed: None
Revision: 27142
----------------------
Name: Intel(R) Xeon(R) Platinum 8374B CPU @ 2.70GHz
Manufacturer: GenuineIntel
Family: 179
Architecture: 9
ProcessorType: 3
DeviceID: CPU1
CurrentClockSpeed: 2700
MaxClockSpeed: 2700
L2CacheSize: 48640
L2CacheSpeed: None
Revision: 27142

Versions of relevant libraries:
[pip3] mypy==1.9.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.0
[pip3] torch==2.1.1
[pip3] torchaudio==2.1.1
[pip3] torchvision==0.16.1
[conda] blas                      1.0                         mkl    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
[conda] cuda-cudart               12.1.105                      0    nvidia
[conda] cuda-cudart-dev           12.1.105                      0    nvidia
[conda] cuda-cupti                12.1.105                      0    nvidia
[conda] cuda-libraries            12.1.0                        0    nvidia
[conda] cuda-libraries-dev        12.1.0                        0    nvidia
[conda] cuda-nvrtc                12.1.105                      0    nvidia
[conda] cuda-nvrtc-dev            12.1.105                      0    nvidia
[conda] cuda-nvtx                 12.1.105                      0    nvidia
[conda] cuda-opencl               12.3.101                      0    nvidia
[conda] cuda-opencl-dev           12.3.101                      0    nvidia
[conda] cuda-runtime              12.1.0                        0    nvidia
[conda] libcublas                 12.1.0.26                     0    nvidia
[conda] libcublas-dev             12.1.0.26                     0    nvidia
[conda] libcufft                  11.0.2.4                      0    nvidia
[conda] libcufft-dev              11.0.2.4                      0    nvidia
[conda] libcurand                 10.3.4.101                    0    nvidia
[conda] libcurand-dev             10.3.4.101                    0    nvidia
[conda] libcusolver               11.4.4.55                     0    nvidia
[conda] libcusolver-dev           11.4.4.55                     0    nvidia
[conda] libcusparse               12.0.2.55                     0    nvidia
[conda] libcusparse-dev           12.0.2.55                     0    nvidia
[conda] libnvjitlink              12.1.105                      0    nvidia
[conda] libnvjitlink-dev          12.1.105                      0    nvidia
[conda] mkl                       2023.1.0         h6b88ed4_46357    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
[conda] mkl-service               2.4.0            py39h2bbff1b_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
[conda] mkl_fft                   1.3.8            py39h2bbff1b_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
[conda] mkl_random                1.2.4            py39h59b6b97_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
[conda] numpy                     1.26.0           py39h055cbcc_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
[conda] numpy-base                1.26.0           py39h65a83cf_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
[conda] pytorch                   2.1.1           py3.9_cuda12.1_cudnn8_0    pytorch
[conda] pytorch-cuda              12.1                 hde6ce7c_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchaudio                2.1.1                    pypi_0    pypi
[conda] torchvision               0.16.1                   pypi_0    pypi
```