Repository: pytorch
Issue: DataLoader: `pin_memory` should respect object attributes before object collection type
Labels: module: dataloader,triaged

### 🐛 Describe the bug

Consider a custom DataLoader that returns an object of class `X(MutableMapping)`, which defines its own `pin_memory` method. It would be expected that setting `pin_memory=True` would call `X`'s `pin_memory` method, but instead the PyTorch DataLoader first checks the type of `X` and applies a default `pin_memory` to each member of `X` (see [these lines](https://github.com/pytorch/pytorch/blob/master/torch/utils/data/_utils/pin_memory.py#L53-L58)). 

This situation is not contrived; it arose in implementing [data loading logic in PyTorch Geometric](https://github.com/pyg-team/pytorch_geometric/pull/5051), where the returned objects subclass `MutableMapping` but also define their own custom `pin_memory` implementation. A simple fix here would be to move the [`hasattr` check](https://github.com/pytorch/pytorch/blob/master/torch/utils/data/_utils/pin_memory.py#L69-L70) above collection type-based checks. Would be curious if there are use-cases that prefer checking for collection types before `hasattr`.

Happy to contribute the fix if it makes sense. Thank you!

### Versions

PyTorch version: 1.11.0+cu102
Is debug build: False
CUDA used to build PyTorch: 10.2
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.3
Libc version: glibc-2.27

Python version: 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:18)  [GCC 10.3.0] (64-bit runtime)
Python platform: Linux-5.4.0-1069-aws-x86_64-with-glibc2.10
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: Tesla T4
Nvidia driver version: 510.47.03
cuDNN version: Probably one of the following:
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.0.5
/usr/local/cuda-11.2/targets/x86_64-linux/lib/libcudnn.so.8.1.1
/usr/local/cuda-11.2/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.1.1
/usr/local/cuda-11.2/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.1.1
/usr/local/cuda-11.2/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.1.1
/usr/local/cuda-11.2/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.1.1
/usr/local/cuda-11.2/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.1.1
/usr/local/cuda-11.2/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.1.1
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.22.3
[pip3] pytorch-lightning==1.6.3
[pip3] pytorch-memlab==0.2.4
[pip3] torch==1.11.0
[pip3] torch-geometric==2.0.5
[pip3] torch-scatter==2.0.9
[pip3] torch-sparse==0.6.13
[pip3] torchmetrics==0.8.1
[pip3] torchtext==0.12.0
[conda] numpy                     1.22.3                   pypi_0    pypi
[conda] pytorch-lightning         1.6.3                    pypi_0    pypi
[conda] pytorch-memlab            0.2.4                    pypi_0    pypi
[conda] torch                     1.11.0                   pypi_0    pypi
[conda] torch-geometric           2.0.5                     dev_0    <develop>
[conda] torch-scatter             2.0.9                    pypi_0    pypi
[conda] torch-sparse              0.6.13                   pypi_0    pypi
[conda] torchmetrics              0.8.1                    pypi_0    pypi
[conda] torchtext                 0.12.0                   pypi_0    pypi

cc @SsnL @VitalyFedyunin @ejguan @NivekT