Dataloader extremely slow on in-memory datasets
Labels: module: dataloader, triaged, module: data

### 🐛 Describe the bug

I am not sure this is a bug but it is a major performance issue and there seems to be no suitable issue category for that.

Torch dataloaders are the natural entry point for many applications but they apparently are extremely slow when working on in-memory data. Here a full example.

```python
from torch.utils.data import DataLoader, TensorDataset
import torch
import time

X = torch.ones(20000, 20)
Y = torch.ones(20000, 5)

ds = TensorDataset(X, Y)

dl_torch = DataLoader(ds, batch_size=256, shuffle=False, drop_last=True)

def get_custom_dl(batch_size=256):
    i_low = 0
    for i_high in range(batch_size, len(ds), batch_size):
        yield ds[i_low:i_high]
        i_low = i_high

# Evaluate speed of dl_torch
now = time.time()
for _ in range(100):
    list(dl_torch)
print(time.time() - now)

# >>> gives 4.5 secs on my system

# Now custom_dl

now = time.time()
for _ in range(100):
    list(get_custom_dl())
print(time.time() - now)

# >>> 0.014 secs
```

After some profiling, I found one of the problems to be in `_MapDatasetFetcher`, which calls the slow `data = [self.dataset[idx] for idx in possibly_batched_index]` and then collates it. This can be bypassed by using the reserved `__getitems__` method
which is used in `_MapDatasetFetcher`:


```python
from torch.utils.data import DataLoader, TensorDataset
import torch
import time

X = torch.ones(20000, 20)
Y = torch.ones(20000, 5)

class ExtendedTensorDataset(TensorDataset):
    def __getitems__(self, item):
        return self.__getitem__(item)

ds_ext = ExtendedTensorDataset(X, Y)
dl_ext = DataLoader(dl_ext, batch_size=256, shuffle=False, collate_fn=lambda x:x)

now = time.time()
for _ in range(100):
    list(dl_ext)
print(time.time() - now)

# >>> 0.58 secs
```

This feels very hacky and the performance is still off  by a factor of 40 from pure python (much better than the original factor 400 though).

I feel like something here is missing. I searched the torch package for the usage of `__getitems__` but found nothing, is this mechanism being deprecated?

I would be happy to work on a fix in a PR, as I feel that the default performance of `DataLoader` should not be so bad. But maybe I am just missing some way of improving it? 

I understand that I can pass a custom sampler to dataloader which would alleviate these issues, but I feel like the default SequenceSampler shouldn't be so slow

### Versions

PyTorch version: 2.0.1+cu117
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.2 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.26.4
Libc version: glibc-2.35

Python version: 3.11.4 (main, Jun  7 2023, 12:45:48) [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-6.2.0-10018-tuxedo-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
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

CPU:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Address sizes:                   39 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          16
On-line CPU(s) list:             0-15
Vendor ID:                       GenuineIntel
Model name:                      12th Gen Intel(R) Core(TM) i7-1260P
CPU family:                      6
Model:                           154
Thread(s) per core:              2
Core(s) per socket:              12
Socket(s):                       1
Stepping:                        3
CPU max MHz:                     4700,0000
CPU min MHz:                     400,0000
BogoMIPS:                        4992.00
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities
Virtualization:                  VT-x
L1d cache:                       448 KiB (12 instances)
L1i cache:                       640 KiB (12 instances)
L2 cache:                        9 MiB (6 instances)
L3 cache:                        18 MiB (1 instance)
NUMA node(s):                    1
NUMA node0 CPU(s):               0-15
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:          Not affected
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; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] mypy==1.4.1
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.4
[pip3] pytorch-lightning==2.0.6
[pip3] torch==2.0.1
[pip3] torchdata==0.6.1
[pip3] torchmetrics==1.0.1
[pip3] triton==2.0.0
[conda] Could not collect


cc @SsnL @VitalyFedyunin @ejguan @dzhulgakov