xformers icon indicating copy to clipboard operation
xformers copied to clipboard

Can not execute the cifar VIT example with new pl - patch_embedding invoked multiple times

Open jakubMitura14 opened this issue 9 months ago • 2 comments

🐛 Bug

Command

I am trying to execute the VIT example [1] from the examples folder with new pytorch lightning and I get an error as below

I also had some small issues like

AttributeError: 'CIFAR10DataModule' object has no attribute 'size'

So i resolved those manually

 image_size = 32#dm.size(-1)  # 32 for CIFAR
 num_classes = 10#dm.num_classes  # 10 for CIFAR

And aded task to accuracy object

self.val_accuracy = Accuracy(task="multiclass", num_classes=10)

During debugging I had inspected class xFormer(torch.nn.Module) and in the forward function numer of encoders is 6 (although it seems to me that from configuration it should be 1). Additionally I see that patch encoding is invoked multiple times in a single case - so when patchembedding gets executed second time will get incorrect shape and will not work.

1)https://github.com/facebookresearch/xformers/blob/main/examples/cifar_ViT.py

Traceback (most recent call last):
  File "/workspaces/konwersjaJsonData/nnunet/nnunetv2pl/nnUNet/nnunetv2/training/nnUNetTrainer/swin_unetr/play_xformers.py", line 341, in <module>
    trainer.fit(lm, dm)
  File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/trainer/trainer.py", line 608, in fit
    call._call_and_handle_interrupt(
  File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/trainer/call.py", line 38, in _call_and_handle_interrupt
    return trainer_fn(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/trainer/trainer.py", line 650, in _fit_impl
    self._run(model, ckpt_path=self.ckpt_path)
  File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/trainer/trainer.py", line 1112, in _run
    results = self._run_stage()
  File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/trainer/trainer.py", line 1191, in _run_stage
    self._run_train()
  File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/trainer/trainer.py", line 1204, in _run_train
    self._run_sanity_check()
  File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/trainer/trainer.py", line 1276, in _run_sanity_check
    val_loop.run()
  File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/loops/loop.py", line 199, in run
    self.advance(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/loops/dataloader/evaluation_loop.py", line 152, in advance
    dl_outputs = self.epoch_loop.run(self._data_fetcher, dl_max_batches, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/loops/loop.py", line 199, in run
    self.advance(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/loops/epoch/evaluation_epoch_loop.py", line 137, in advance
    output = self._evaluation_step(**kwargs)
  File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/loops/epoch/evaluation_epoch_loop.py", line 234, in _evaluation_step
    output = self.trainer._call_strategy_hook(hook_name, *kwargs.values())
  File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/trainer/trainer.py", line 1494, in _call_strategy_hook
    output = fn(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/pytorch_lightning/strategies/strategy.py", line 390, in validation_step
    return self.model.validation_step(*args, **kwargs)
  File "/workspaces/konwersjaJsonData/nnunet/nnunetv2pl/nnUNet/nnunetv2/training/nnUNetTrainer/swin_unetr/play_xformers.py", line 290, in validation_step
    self.evaluate(batch, "val")
  File "/workspaces/konwersjaJsonData/nnunet/nnunetv2pl/nnUNet/nnunetv2/training/nnUNetTrainer/swin_unetr/play_xformers.py", line 281, in evaluate
    y_hat = self(x)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/workspaces/konwersjaJsonData/nnunet/nnunetv2pl/nnUNet/nnunetv2/training/nnUNetTrainer/swin_unetr/play_xformers.py", line 253, in forward
    x = self.vit(x)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/xformers/factory/model_factory.py", line 276, in forward
    memory = encoder(memory, input_mask=encoder_input_mask)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/xformers/factory/block_factory.py", line 214, in forward
    x = self.patch_emb(x)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/container.py", line 217, in forward
    input = module(input)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/xformers/components/patch_embedding.py", line 49, in forward
    assert H * H == HW, f"{H, HW}"
AssertionError: (16, 257)

Environment

PyTorch version: 2.0.1+cu118
Is debug build: False
CUDA used to build PyTorch: 11.8
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.25.0
Libc version: glibc-2.35

Python version: 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-6.2.0-31-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 11.8.89
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA GeForce RTX 3090
GPU 1: NVIDIA GeForce RTX 3090

Nvidia driver version: 535.86.05
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.0
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):                             20
On-line CPU(s) list:                0-19
Vendor ID:                          GenuineIntel
Model name:                         Intel(R) Core(TM) i9-10850K CPU @ 3.60GHz
CPU family:                         6
Model:                              165
Thread(s) per core:                 2
Core(s) per socket:                 10
Socket(s):                          1
Stepping:                           5
CPU max MHz:                        5200.0000
CPU min MHz:                        800.0000
BogoMIPS:                           7200.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 pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp pku ospke md_clear flush_l1d arch_capabilities
Virtualization:                     VT-x
L1d cache:                          320 KiB (10 instances)
L1i cache:                          320 KiB (10 instances)
L2 cache:                           2.5 MiB (10 instances)
L3 cache:                           20 MiB (1 instance)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-19

Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.4
[pip3] numpydoc==1.5.0
[pip3] pytorch-ignite==0.4.12
[pip3] pytorch-lightning==1.9.5
[pip3] torch==2.0.1+cu118
[pip3] torchaudio==2.0.2+cu118
[pip3] torchio==0.19.1
[pip3] torchmetrics==1.1.0
[pip3] torchvision==0.15.2+cu118
[conda] Could not collect

jakubMitura14 avatar Sep 03 '23 05:09 jakubMitura14

@jakubMitura14 had same issue, look like factory has now been deemed unmaintained. Please see https://github.com/facebookresearch/xformers/pull/850

TLDR: you need to write your own (Vit) module now. my aim is to raise a PR for the ViT example. will update here once I'm done

chriseviparker avatar Sep 21 '23 10:09 chriseviparker

@chriseviparker Did you raise a PR?

johan-sightic avatar Dec 20 '23 10:12 johan-sightic