RuntimeError: !needs_dynamic_casting<func_t>::check(iter) INTERNAL ASSERT FAILED at "../aten/src/ATen/native/cpu/Loops.h":349, ... please report a bug to PyTorch.
Labels: module: optimizer, triaged

### 🐛 Describe the bug

I am running the AnyPrecision optimizer on cpu as part of my unit tests.  This optimizer has variance and momentum states in bfloat16 rather than fp32. 
While calculating the uncentered variance, 
~~~
# update uncentered variance
exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2)
~~~

I hit this assert:
RuntimeError: !needs_dynamic_casting<func_t>::check(iter) INTERNAL ASSERT FAILED at "../aten/src/ATen/native/cpu/Loops.h":349, please report a bug to PyTorch.

../../../torchmultimodal/modules/optimizers/anyprecision.py:157: RuntimeError

This works fine if the model is on GPU.  However, for unit tests trying to run on cpu as we don't have GPU for TorchMM CI. 
This issue will also come up for cpu_offload in the future for FSDP where we want to run the optimizer on cpu.

You can repro this by running the unit tests in this PR:
https://github.com/facebookresearch/multimodal/pull/477
and remove the @gpu_test decorator from the test_bfloat16_states test and set device='cpu' ( to force it to cpu).


 











### Versions

PyTorch version: 2.2.0.dev20230927+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.6 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Clang version: Could not collect
CMake version: version 3.25.0
Libc version: glibc-2.31

Python version: 3.9.16 | packaged by conda-forge | (main, Feb  1 2023, 21:39:03)  [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-5.15.0-1036-aws-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: 11.7.99
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA A10G
GPU 1: NVIDIA A10G
GPU 2: NVIDIA A10G
GPU 3: NVIDIA A10G

Nvidia driver version: 525.85.12
cuDNN version: Could not collect
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
Address sizes:                   48 bits physical, 48 bits virtual
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:                       AuthenticAMD
CPU family:                      23
Model:                           49
Model name:                      AMD EPYC 7R32
Stepping:                        0
CPU MHz:                         2800.000
BogoMIPS:                        5600.00
Hypervisor vendor:               KVM
Virtualization type:             full
L1d cache:                       768 KiB
L1i cache:                       768 KiB
L2 cache:                        12 MiB
L3 cache:                        96 MiB
NUMA node0 CPU(s):               0-47
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 store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
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
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected
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

Versions of relevant libraries:
[pip3] flake8==4.0.1
[pip3] flake8-bugbear==22.4.25
[pip3] flake8-polyfill==1.0.2
[pip3] memory-efficient-attention-pytorch==0.1.6
[pip3] mypy==1.0.1
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.3
[pip3] pytorch-triton==2.1.0+6e4932cda8
[pip3] torch==2.2.0.dev20230927+cu121
[pip3] torch-model-archiver==0.5.3b20220226
[pip3] torch-workflow-archiver==0.2.8b20230512
[pip3] torchaudio==2.2.0.dev20230927+cu121
[pip3] torchmultimodal==0.1.0b0
[pip3] torchserve==0.6.0b20220513
[pip3] torchtext==0.14.1
[pip3] torchvision==0.17.0.dev20230927+cu121
[pip3] triton==2.0.0.dev20221202
[pip3] triton-nightly==2.1.0.dev20230822000928
[pip3] vit-pytorch==1.2.2
[conda] blas                      1.0                         mkl    conda-forge
[conda] captum                    0.5.0                         0    pytorch
[conda] cudatoolkit               11.7.0              hd8887f6_11    conda-forge
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] libblas                   3.9.0            16_linux64_mkl    conda-forge
[conda] libcblas                  3.9.0            16_linux64_mkl    conda-forge
[conda] liblapack                 3.9.0            16_linux64_mkl    conda-forge
[conda] magma-cuda117             2.6.1                         1    pytorch
[conda] mkl                       2022.2.1         h84fe81f_16997    conda-forge
[conda] numpy                     1.24.3           py39h6183b62_0    conda-forge
[conda] pytorch-cuda              11.7                 h778d358_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] pytorch-triton            2.1.0+6e4932cda8          pypi_0    pypi
[conda] torch                     2.2.0.dev20230927+cu121          pypi_0    pypi
[conda] torch-model-archiver      0.5.3                    py39_0    pytorch
[conda] torch-workflow-archiver   0.2.8                    py39_0    pytorch
[conda] torchaudio                2.2.0.dev20230927+cu121          pypi_0    pypi
[conda] torchmultimodal           0.1.0b0                  pypi_0    pypi
[conda] torchserve                0.6.0                    py39_0    pytorch
[conda] torchtext                 0.14.1                     py39    pytorch
[conda] torchvision               0.17.0.dev20230927+cu121          pypi_0    pypi
[conda] triton                    2.0.0.dev20221202          pypi_0    pypi
[conda] triton-nightly            2.1.0.dev20230822000928          pypi_0    pypi
[conda] vit-pytorch               1.2.2                    pypi_0    pypi

cc @vincentqb @jbschlosser @albanD @janeyx99 @crcrpar