pytorch / pytorch

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

_pickle.UnpicklingError: invalid load key, '\x00'. during torch load #132991

Open Desperado17 opened 2 months ago

Desperado17 commented 2 months ago

🐛 Describe the bug

I try to convert a pytorch bin file to onnx format with the following script:

import torch import sys

sys.path.insert(0, '/media/user/SSD1TB/arcoreAI/YOLOPv2-ncnn')

device = torch.device('cpu') model = torch.load('yolopv2.bin', map_location=device)['model'].float() torch.onnx.export(model, torch.zeros((1, 3, 640, 640)), 'yolopv2.onnx', opset_version=12)

But it fails with the following error:

user@user-Precision-7510:/media/user/SSD1TB/arcoreAI/YOLOPv2-ncnn/models(main)$ python3 ./exportonnx.py /media/user/SSD1TB/arcoreAI/YOLOPv2-ncnn/models/./exportonnx.py:7: FutureWarning: You are using torch.load with weights_only=False (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for weights_only will be flipped to True. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via torch.serialization.add_safe_globals. We recommend you start setting weights_only=True for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature. model = torch.load('yolopv2.bin', map_location=device)['model'].float() Traceback (most recent call last): File "/media/user/SSD1TB/arcoreAI/YOLOPv2-ncnn/models/./exportonnx.py", line 7, in model = torch.load('yolopv2.bin', map_location=device)['model'].float() File "/home/user/.local/lib/python3.10/site-packages/torch/serialization.py", line 1114, in load return _legacy_load( File "/home/user/.local/lib/python3.10/site-packages/torch/serialization.py", line 1338, in _legacy_load magic_number = pickle_module.load(f, **pickle_load_args) _pickle.UnpicklingError: invalid load key, '\x00'.

Model is from here: https://github.com/FeiGeChuanShu/YOLOPv2-ncnn/tree/main/models

Is this a bug or am I doing something wrong? Regards

Versions

user@user-Precision-7510:/media/user/SSD1TB/arcoreAI/ncnn(master)$ wget https://raw.githubusercontent.com/pytorch/pytorch/main/torch/utils/collect_env.py

For security purposes, please check the contents of collect_env.py before running it.

python collect_env.py --2024-08-08 13:13:12-- https://raw.githubusercontent.com/pytorch/pytorch/main/torch/utils/collect_env.py Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.110.133, 185.199.109.133, 185.199.108.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 23357 (23K) [text/plain] Saving to: ‘collect_env.py’

collect_env.py 100%[===================>] 22,81K --.-KB/s in 0,03s

2024-08-08 13:13:13 (837 KB/s) - ‘collect_env.py’ saved [23357/23357]

Collecting environment information... PyTorch version: 2.4.0+cu121 Is debug build: False CUDA used to build PyTorch: 12.1 ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.3 LTS (x86_64) GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 Clang version: 17.0.6 (++20231209124227+6009708b4367-1~exp1~20231209124336.77) CMake version: version 3.28.1 Libc version: glibc-2.35

Python version: 3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0] (64-bit runtime) Python platform: Linux-6.5.0-44-generic-x86_64-with-glibc2.35 Is CUDA available: True CUDA runtime version: Could not collect CUDA_MODULE_LOADING set to: LAZY GPU models and configuration: GPU 0: Quadro M1000M Nvidia driver version: 535.183.01 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 Address sizes: 39 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 8 On-line CPU(s) list: 0-7 Vendor ID: GenuineIntel Model name: Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz CPU family: 6 Model: 94 Thread(s) per core: 2 Core(s) per socket: 4 Socket(s): 1 Stepping: 3 CPU max MHz: 3600,0000 CPU min MHz: 800,0000 BogoMIPS: 5399.81 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 smx 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 epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow 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 vnmi md_clear flush_l1d arch_capabilities Virtualisation: VT-x L1d cache: 128 KiB (4 instances) L1i cache: 128 KiB (4 instances) L2 cache: 1 MiB (4 instances) L3 cache: 8 MiB (1 instance) NUMA node(s): 1 NUMA node0 CPU(s): 0-7 Vulnerability Gather data sampling: Vulnerable: No microcode 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: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Retbleed: Mitigation; IBRS Vulnerability Spec rstack overflow: 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; IBRS; IBPB conditional; STIBP conditional; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected Vulnerability Srbds: Mitigation; Microcode Vulnerability Tsx async abort: Mitigation; TSX disabled

Versions of relevant libraries: [pip3] mypy-extensions==1.0.0 [pip3] numpy==1.26.4 [pip3] onnx==1.13.0 [pip3] onnx-tool==0.8.1 [pip3] torch==2.4.0 [pip3] torchaudio==2.4.0 [pip3] torchvision==0.19.0 [pip3] triton==3.0.0 [conda] Could not collect

cc @mruberry @mikaylagawarecki

BoyuanFeng commented 2 months ago

The error happens at model = torch.load('yolopv2.bin', map_location=device)['model'].float(), which is before torch.onnx.export() call. I suspect that 'yolopv2.bin' is broken. If you have access to the original weight, could you try torch.save and torch.load? example

Desperado17 commented 2 months ago

The error happens at model = torch.load('yolopv2.bin', map_location=device)['model'].float(), which is before torch.onnx.export() call. I suspect that 'yolopv2.bin' is broken. If you have access to the original weight, could you try torch.save and torch.load? example

Yeah, I corrected the title. The yolopv2.bin is in https://github.com/FeiGeChuanShu/YOLOPv2-ncnn/tree/main/models linked up there if you want to conduct a sanity check.

We use the model in C++ code and there it is loaded. The load is similar to the official one: https://github.com/FeiGeChuanShu/YOLOPv2-ncnn/blob/98cb071a348fbe00ee6b96d59a9e22a0854b8a69/src/yolopv2.cpp#L323

Do you see a reason this might not be a pytorch compatible bin?