pytorch / pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration
https://pytorch.org
Other
82.35k stars 22.15k forks source link

Pruning under channels_last format #96743

Open agonon opened 1 year ago

agonon commented 1 year ago

🐛 Describe the bug

Pruning do not seem to be compatible with models stored in channels_last format because torch.view() is currently used, and it requires a contiguous input tensor.

Minimal Working Example :

import torch
from torch.nn.utils import prune
model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet18', pretrained=True)
model.to(memory_format=torch.channels_last)
for name, module in model.named_modules():
            if isinstance(module, torch.nn.Conv2d):
                prune.l1_unstructured(module, name="weight", amount=0.5)
                break

Error log:

---------------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/user/scripts/mwe.py", line 8, in <module>
    prune.l1_unstructured(module, name="weight", amount=0.5)
  File "/home/user/.local/lib/python3.9/site-packages/torch/nn/utils/prune.py", line 923, in l1_unstructured
    L1Unstructured.apply(
  File "/home/user/.local/lib/python3.9/site-packages/torch/nn/utils/prune.py", line 562, in apply
    return super(L1Unstructured, cls).apply(
  File "/home/user/.local/lib/python3.9/site-packages/torch/nn/utils/prune.py", line 205, in apply
    raise e
  File "/home/user/.local/lib/python3.9/site-packages/torch/nn/utils/prune.py", line 191, in apply
    mask = method.compute_mask(importance_scores, default_mask=default_mask)
  File "/home/user/.local/lib/python3.9/site-packages/torch/nn/utils/prune.py", line 536, in compute_mask
    topk = torch.topk(torch.abs(t).view(-1), k=nparams_toprune, largest=False)
RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.

Versions

Collecting environment information... PyTorch version: 1.12.0+cu102 Is debug build: False CUDA used to build PyTorch: 10.2 ROCM used to build PyTorch: N/A

OS: Debian GNU/Linux 11 (bullseye) (x86_64) GCC version: (Debian 10.2.1-6) 10.2.1 20210110 Clang version: Could not collect CMake version: version 3.18.4 Libc version: glibc-2.31

Python version: 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110] (64-bit runtime) Python platform: Linux-5.10.0-20-amd64-x86_64-with-glibc2.31 Is CUDA available: True CUDA runtime version: 11.2.152 CUDA_MODULE_LOADING set to: GPU models and configuration: GPU 0: NVIDIA GeForce RTX 2080 SUPER GPU 1: NVIDIA GeForce RTX 2080 SUPER

Nvidia driver version: 470.161.03 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: 46 bits physical, 48 bits virtual CPU(s): 32 On-line CPU(s) list: 0-31 Thread(s) per core: 2 Core(s) per socket: 8 Socket(s): 2 NUMA node(s): 2 Vendor ID: GenuineIntel CPU family: 6 Model: 45 Model name: Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz Stepping: 7 CPU MHz: 1605.327 CPU max MHz: 3300.0000 CPU min MHz: 1200.0000 BogoMIPS: 5199.96 Virtualization: VT-x L1d cache: 512 KiB L1i cache: 512 KiB L2 cache: 4 MiB L3 cache: 40 MiB NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30 NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31 Vulnerability Itlb multihit: KVM: Mitigation: VMX disabled Vulnerability L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Meltdown: Mitigation; PTI Vulnerability Mmio stale data: Unknown: No mitigations Vulnerability Retbleed: Not affected 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, IBRS_FW, STIBP conditional, 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 dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx lahf_lm pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid xsaveopt dtherm ida arat pln pts md_clear flush_l1d

Versions of relevant libraries: [pip3] mypy-extensions==0.4.3 [pip3] numpy==1.23.1 [pip3] numpy-groupies==0+unknown [pip3] numpydoc==1.1.0 [pip3] torch==1.12.0 [pip3] torchvision==0.13.0 [conda] Could not collect

cc @jamesr66a

mokachocolat commented 1 year ago

Hello, I have exactly the same problem... Any news about how to solve it ?

agonon commented 1 year ago

Hi, the problem is still open, for now I simply do not use channels_last format when pruning.