Repository: pytorch
Issue: CUDA failure with deterministic fancy indexed assignment with broadcasting
Labels: triaged,module: advanced indexing,module: determinism,module: edge cases

### 🐛 Describe the bug

I get an internal assert failure when using fancy indexed assignment on CUDA in deterministic mode. This appears to be the same as #96724 (and #105819), which was closed when #105833 was merged. But the problem seems to persist.

From my brief tests this seems to be triggered when the assignment is done in deterministic mode and the tensors are on CUDA. There's no issue on CPU or with deterministic mode off on CUDA. The dimensions of the tensor and the assignment value seem to play a role as well, though I haven't dug very far into it.

Some examples:
```python
import torch

test = torch.randn(2, 2, device='cpu')
idx = [0, 1]
torch.use_deterministic_algorithms(True)
test[:, idx] = torch.randn((test.shape[0], 1), device='cpu')
print(test[:, idx])
test = test.cuda()
torch.use_deterministic_algorithms(False)
test[:, idx] = torch.randn((test.shape[0], 1), device='cuda')
print(test[:, idx])
torch.use_deterministic_algorithms(True)
test[:, idx] = 5
print(test[:, idx])
test[:, idx] = torch.randn((test.shape[0], len(idx)), device='cuda')
print(test[:, idx])
test[:, idx] = torch.randn((test.shape[0], 1), device='cuda')
print(test[:, idx])
```

I get the following output where all assignments except the last succeed:
```
tensor([[ 0.4746,  0.4746],
        [-0.1332, -0.1332]])
tensor([[-0.8091, -0.8091],
        [-0.4685, -0.4685]], device='cuda:0')
tensor([[5., 5.],
        [5., 5.]], device='cuda:0')
tensor([[-3.3053, -0.2223],
        [-0.3461, -0.3334]], device='cuda:0')
Traceback (most recent call last):
  File "/home/ubuntu/cuda.py", line 17, in <module>
    test[:, idx] = torch.randn((test.shape[0], 1), device='cuda')
    ~~~~^^^^^^^^
RuntimeError: linearIndex.numel()*sliceSize*nElemBefore == expandedValue.numel() INTERNAL ASSERT FAILED at "../aten/src/ATen/native/cuda/Indexing.cu":490, please report a bug to PyTorch. number of flattened indices did not match number of elements in the value tensor: 4 vs 8
```
This is with PyTorch 2.4.0 on Python 3.12.2, but I initially discovered it with PyTorch 2.2.2 on Python 3.11.8.

I wasn't sure if I should comment on one of the previous issues or create a new one; apologies if I picked the wrong option.

### Versions

Collecting environment information...
PyTorch version: 2.4.0+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.35

Python version: 3.12.2 (main, Jul 26 2024, 14:15:59) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-6.5.0-1023-aws-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA A10G
Nvidia driver version: 555.42.06
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.2
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.2
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
Address sizes:                      48 bits physical, 48 bits virtual
Byte Order:                         Little Endian
CPU(s):                             4
On-line CPU(s) list:                0-3
Vendor ID:                          AuthenticAMD
Model name:                         AMD EPYC 7R32
CPU family:                         23
Model:                              49
Thread(s) per core:                 2
Core(s) per socket:                 2
Socket(s):                          1
Stepping:                           0
BogoMIPS:                           5599.99
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf tsc_known_freq pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy cr8_legacy abm sse4a misalignsse 3dnowprefetch topoext ssbd ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 clzero xsaveerptr rdpru wbnoinvd arat npt nrip_save rdpid
Hypervisor vendor:                  KVM
Virtualization type:                full
L1d cache:                          64 KiB (2 instances)
L1i cache:                          64 KiB (2 instances)
L2 cache:                           1 MiB (2 instances)
L3 cache:                           8 MiB (1 instance)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-3
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit:        Not affected
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Not affected
Vulnerability Retbleed:             Mitigation; untrained return thunk; SMT enabled with STIBP protection
Vulnerability Spec rstack overflow: Vulnerable: Safe RET, no microcode
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Retpolines; IBPB conditional; STIBP always-on; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] numpy==2.0.1
[pip3] torch==2.4.0
[pip3] triton==3.0.0
[conda] Could not collect

cc @mruberry @kurtamohler