intel / intel-extension-for-pytorch

A Python package for extending the official PyTorch that can easily obtain performance on Intel platform
Apache License 2.0
1.58k stars 244 forks source link

model weight is lost after ipex inplace optimize in version 2.0.100 #356

Open rnwang04 opened 1 year ago

rnwang04 commented 1 year ago

Describe the bug

below code works well in ipex version 2.0.0 but fails in 2.0.100

import os
import numpy as np
from torch import nn
import torch
import torch.nn.functional as F
import intel_extension_for_pytorch as ipex

class Net(nn.Module):
    def __init__(self, l1=8, l2=16):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, l1)
        self.fc2 = nn.Linear(l1, l2)
        self.fc3 = nn.Linear(l2, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.reshape(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

class CannotCopyNet(Net):
    def __deepcopy__(self, memo):
        invalidOperationError(False, "The `deepcopy` function shouldn't be called")

model = CannotCopyNet()
model.eval()
ipex_model = ipex.optimize(model, inplace=True)
state = ipex_model.state_dict()
ipex_model = model.load_state_dict(state)
print("works")

error message is:

RuntimeError: Error(s) in loading state_dict for CannotCopyNet:
        Unexpected key(s) in state_dict: "conv1.weight", "conv2.weight", "fc1.weight", "fc2.weight", "fc3.weight".

I wonder why all weight of model are lost after inplace ipex optimization.

Versions

Collecting environment information... PyTorch version: 2.0.0+cu117 PyTorch CXX11 ABI: No IPEX version: 2.0.100+cpu IPEX commit: 6a341a3 Build type: Release

OS: Ubuntu 20.04.3 LTS (x86_64) GCC version: (Ubuntu 11.1.0-1ubuntu1~20.04) 11.1.0 Clang version: 10.0.0-4ubuntu1 IGC version: N/A CMake version: version 3.26.3 Libc version: glibc-2.31

Python version: 3.9.16 (main, Mar 8 2023, 14:00:05) [GCC 11.2.0] (64-bit runtime) Python platform: Linux-5.15.0-60-generic-x86_64-with-glibc2.31 Is XPU available: False DPCPP runtime version: N/A MKL version: N/A GPU models and configuration:

Intel OpenCL ICD version: N/A Level Zero version: N/A

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): 224 On-line CPU(s) list: 0-223 Thread(s) per core: 2 Core(s) per socket: 28 Socket(s): 4 NUMA node(s): 4 Vendor ID: GenuineIntel CPU family: 6 Model: 85 Model name: Intel(R) Xeon(R) Platinum 8380H CPU @ 2.90GHz Stepping: 11 CPU MHz: 3489.909 CPU max MHz: 4300.0000 CPU min MHz: 1000.0000 BogoMIPS: 5800.00 Virtualization: VT-x L1d cache: 3.5 MiB L1i cache: 3.5 MiB L2 cache: 112 MiB L3 cache: 154 MiB NUMA node0 CPU(s): 0-27,112-139 NUMA node1 CPU(s): 28-55,140-167 NUMA node2 CPU(s): 56-83,168-195 NUMA node3 CPU(s): 84-111,196-223 Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Retbleed: Mitigation; Enhanced IBRS 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; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence 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 art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local avx512_bf16 dtherm ida arat pln pts pku ospke avx512_vnni md_clear flush_l1d arch_capabilities

Versions of relevant libraries: [pip3] intel-extension-for-pytorch==2.0.100 [pip3] numpy==1.23.4 [pip3] pytorch-lightning==1.6.4 [pip3] torch==2.0.0 [pip3] torchmetrics==0.11.0 [pip3] torchvision==0.15.1 [conda] intel-extension-for-pytorch 2.0.100 pypi_0 pypi [conda] numpy 1.23.4 pypi_0 pypi [conda] pytorch-lightning 1.6.4 pypi_0 pypi [conda] torch 2.0.0 pypi_0 pypi [conda] torchmetrics 0.11.0 pypi_0 pypi [conda] torchvision 0.15.1 pypi_0 pypi

jingxu10 commented 1 year ago

@zhuhaozhe @XiaobingSuper

zhuhaozhe commented 1 year ago

Hi, @rnwang04. Thanks for reporting this, May I know if there is any special case need to do:

ipex_model = ipex.optimize(model, inplace=True)
state = ipex_model.state_dict()
ipex_model = model.load_state_dict(state)

If inplace is set to True, the model is modified inplaced and should not operate model anymore.