Xilinx / Vitis-AI

Vitis AI is Xilinx’s development stack for AI inference on Xilinx hardware platforms, including both edge devices and Alveo cards.
https://www.xilinx.com/ai
Apache License 2.0
1.42k stars 620 forks source link

Import slim model from pruning to quantization #1039

Open Sinelemouk opened 1 year ago

Sinelemouk commented 1 year ago

Hello Xilinx,

Thank you for alowing me to play with your tool,

I succesfully create a slim_model with the vitis-ai-optimizer_pytorch. However, if i try to to the quantization with this conda env, i get the folowing error :

  File "/opt/vitis_ai/conda/envs/vitis-ai-optimizer_pytorch/lib/python3.7/site-packages/nndct_shared/compile/xgraph.py", line 24, in <module>
    raise Exception('please install xir package')

In the reverse, i can't load the slim_model in the vitis-ai-pytorch as describe in the ug1333 because of the folowing error:

ImportError: cannot import name 'get_pruning_runner' from 'pytorch_nndct' (/opt/vitis_ai/conda/envs/vitis-ai-pytorch/lib/python3.7/site-packages/pytorch_nndct/__init__.py)

How can i load the pruned model for the quantization step? Is there any exemple?

I am using the "With Pruning API" for loading the slim_model.

method = 'iterative' # or 'one_step'
runner = get_pruning_runner(model, input_signature, method)
slim_model = runner.prune(removal_ratio=0.2, mode='slim')
slim_model.load_state_dict(torch.load('model_pruned.pth'))

The Without Pruning API doesent work for me. The model.json file does not fit the slim_model_from_state_dict_and_pruning_info function you provided.

{
  "Vdsr::input_0": {
    "node": "Vdsr::input_0",
    "ratio": null,
    "removed_outputs": [],
    "out_dim": 0,
    "removed_inputs": [],
    "in_dim": 0,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/Conv2d[0]/input.3": {
    "node": "Vdsr::Vdsr/Sequential[layers]/Conv2d[0]/input.3",
    "ratio": 0.4,
    "removed_outputs": [
      25,
      49,
      39,
      46,
      3,
      13,
      31,
      56,
      45,
      60,
      9,
      2,
      47,
      7,
      59,
      15,
      12,
      22,
      48,
      19,
      35,
      21,
      57,
      29,
      32,
      63
    ],
    "out_dim": 38,
    "removed_inputs": [],
    "in_dim": 0,
    "master": true
  },
  "Vdsr::Vdsr/Sequential[layers]/ReLU[1]/input.5": {
    "node": "Vdsr::Vdsr/Sequential[layers]/ReLU[1]/input.5",
    "ratio": null,
    "removed_outputs": [
      25,
      49,
      39,
      46,
      3,
      13,
      31,
      56,
      45,
      60,
      9,
      2,
      47,
      7,
      59,
      15,
      12,
      22,
      48,
      19,
      35,
      21,
      57,
      29,
      32,
      63
    ],
    "out_dim": 38,
    "removed_inputs": [
      25,
      49,
      39,
      46,
      3,
      13,
      31,
      56,
      45,
      60,
      9,
      2,
      47,
      7,
      59,
      15,
      12,
      22,
      48,
      19,
      35,
      21,
      57,
      29,
      32,
      63
    ],
    "in_dim": 38,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/Conv2d[2]/input.7": {
    "node": "Vdsr::Vdsr/Sequential[layers]/Conv2d[2]/input.7",
    "ratio": null,
    "removed_outputs": [],
    "out_dim": 64,
    "removed_inputs": [
      25,
      49,
      39,
      46,
      3,
      13,
      31,
      56,
      45,
      60,
      9,
      2,
      47,
      7,
      59,
      15,
      12,
      22,
      48,
      19,
      35,
      21,
      57,
      29,
      32,
      63
    ],
    "in_dim": 38,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/ReLU[3]/input.9": {
    "node": "Vdsr::Vdsr/Sequential[layers]/ReLU[3]/input.9",
    "ratio": null,
    "removed_outputs": [],
    "out_dim": 64,
    "removed_inputs": [],
    "in_dim": 64,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/Conv2d[4]/input.11": {
    "node": "Vdsr::Vdsr/Sequential[layers]/Conv2d[4]/input.11",
    "ratio": 0.2,
    "removed_outputs": [
      14,
      4,
      6,
      29,
      23,
      9,
      19,
      13,
      3,
      43,
      17,
      58
    ],
    "out_dim": 52,
    "removed_inputs": [],
    "in_dim": 64,
    "master": true
  },
  "Vdsr::Vdsr/Sequential[layers]/ReLU[5]/input.13": {
    "node": "Vdsr::Vdsr/Sequential[layers]/ReLU[5]/input.13",
    "ratio": null,
    "removed_outputs": [
      14,
      4,
      6,
      29,
      23,
      9,
      19,
      13,
      3,
      43,
      17,
      58
    ],
    "out_dim": 52,
    "removed_inputs": [
      14,
      4,
      6,
      29,
      23,
      9,
      19,
      13,
      3,
      43,
      17,
      58
    ],
    "in_dim": 52,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/Conv2d[6]/input.15": {
    "node": "Vdsr::Vdsr/Sequential[layers]/Conv2d[6]/input.15",
    "ratio": 0.1,
    "removed_outputs": [
      8,
      57,
      33,
      61,
      16,
      12
    ],
    "out_dim": 58,
    "removed_inputs": [
      14,
      4,
      6,
      29,
      23,
      9,
      19,
      13,
      3,
      43,
      17,
      58
    ],
    "in_dim": 52,
    "master": true
  },
  "Vdsr::Vdsr/Sequential[layers]/ReLU[7]/input.17": {
    "node": "Vdsr::Vdsr/Sequential[layers]/ReLU[7]/input.17",
    "ratio": null,
    "removed_outputs": [
      8,
      57,
      33,
      61,
      16,
      12
    ],
    "out_dim": 58,
    "removed_inputs": [
      8,
      57,
      33,
      61,
      16,
      12
    ],
    "in_dim": 58,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/Conv2d[8]/input.19": {
    "node": "Vdsr::Vdsr/Sequential[layers]/Conv2d[8]/input.19",
    "ratio": 0.1,
    "removed_outputs": [
      48,
      36,
      39,
      61,
      62,
      58
    ],
    "out_dim": 58,
    "removed_inputs": [
      8,
      57,
      33,
      61,
      16,
      12
    ],
    "in_dim": 58,
    "master": true
  },
  "Vdsr::Vdsr/Sequential[layers]/ReLU[9]/input.21": {
    "node": "Vdsr::Vdsr/Sequential[layers]/ReLU[9]/input.21",
    "ratio": null,
    "removed_outputs": [
      48,
      36,
      39,
      61,
      62,
      58
    ],
    "out_dim": 58,
    "removed_inputs": [
      48,
      36,
      39,
      61,
      62,
      58
    ],
    "in_dim": 58,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/Conv2d[10]/input.23": {
    "node": "Vdsr::Vdsr/Sequential[layers]/Conv2d[10]/input.23",
    "ratio": 0.2,
    "removed_outputs": [
      25,
      5,
      36,
      10,
      0,
      56,
      14,
      41,
      61,
      27,
      4,
      26
    ],
    "out_dim": 52,
    "removed_inputs": [
      48,
      36,
      39,
      61,
      62,
      58
    ],
    "in_dim": 58,
    "master": true
  },
  "Vdsr::Vdsr/Sequential[layers]/ReLU[11]/input.25": {
    "node": "Vdsr::Vdsr/Sequential[layers]/ReLU[11]/input.25",
    "ratio": null,
    "removed_outputs": [
      25,
      5,
      36,
      10,
      0,
      56,
      14,
      41,
      61,
      27,
      4,
      26
    ],
    "out_dim": 52,
    "removed_inputs": [
      25,
      5,
      36,
      10,
      0,
      56,
      14,
      41,
      61,
      27,
      4,
      26
    ],
    "in_dim": 52,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/Conv2d[12]/input.27": {
    "node": "Vdsr::Vdsr/Sequential[layers]/Conv2d[12]/input.27",
    "ratio": 0.1,
    "removed_outputs": [
      9,
      22,
      28,
      6,
      11,
      14
    ],
    "out_dim": 58,
    "removed_inputs": [
      25,
      5,
      36,
      10,
      0,
      56,
      14,
      41,
      61,
      27,
      4,
      26
    ],
    "in_dim": 52,
    "master": true
  },
  "Vdsr::Vdsr/Sequential[layers]/ReLU[13]/input.29": {
    "node": "Vdsr::Vdsr/Sequential[layers]/ReLU[13]/input.29",
    "ratio": null,
    "removed_outputs": [
      9,
      22,
      28,
      6,
      11,
      14
    ],
    "out_dim": 58,
    "removed_inputs": [
      9,
      22,
      28,
      6,
      11,
      14
    ],
    "in_dim": 58,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/Conv2d[14]/input.31": {
    "node": "Vdsr::Vdsr/Sequential[layers]/Conv2d[14]/input.31",
    "ratio": 0.1,
    "removed_outputs": [
      47,
      49,
      50,
      31,
      46,
      38
    ],
    "out_dim": 58,
    "removed_inputs": [
      9,
      22,
      28,
      6,
      11,
      14
    ],
    "in_dim": 58,
    "master": true
  },
  "Vdsr::Vdsr/Sequential[layers]/ReLU[15]/input.33": {
    "node": "Vdsr::Vdsr/Sequential[layers]/ReLU[15]/input.33",
    "ratio": null,
    "removed_outputs": [
      47,
      49,
      50,
      31,
      46,
      38
    ],
    "out_dim": 58,
    "removed_inputs": [
      47,
      49,
      50,
      31,
      46,
      38
    ],
    "in_dim": 58,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/Conv2d[16]/input.35": {
    "node": "Vdsr::Vdsr/Sequential[layers]/Conv2d[16]/input.35",
    "ratio": null,
    "removed_outputs": [],
    "out_dim": 64,
    "removed_inputs": [
      47,
      49,
      50,
      31,
      46,
      38
    ],
    "in_dim": 58,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/ReLU[17]/input.37": {
    "node": "Vdsr::Vdsr/Sequential[layers]/ReLU[17]/input.37",
    "ratio": null,
    "removed_outputs": [],
    "out_dim": 64,
    "removed_inputs": [],
    "in_dim": 64,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/Conv2d[18]/input.39": {
    "node": "Vdsr::Vdsr/Sequential[layers]/Conv2d[18]/input.39",
    "ratio": 0.1,
    "removed_outputs": [
      59,
      51,
      2,
      16,
      3,
      22
    ],
    "out_dim": 58,
    "removed_inputs": [],
    "in_dim": 64,
    "master": true
  },
  "Vdsr::Vdsr/Sequential[layers]/ReLU[19]/input.41": {
    "node": "Vdsr::Vdsr/Sequential[layers]/ReLU[19]/input.41",
    "ratio": null,
    "removed_outputs": [
      59,
      51,
      2,
      16,
      3,
      22
    ],
    "out_dim": 58,
    "removed_inputs": [
      59,
      51,
      2,
      16,
      3,
      22
    ],
    "in_dim": 58,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/Conv2d[20]/input.43": {
    "node": "Vdsr::Vdsr/Sequential[layers]/Conv2d[20]/input.43",
    "ratio": 0.1,
    "removed_outputs": [
      58,
      20,
      41,
      46,
      37,
      47
    ],
    "out_dim": 58,
    "removed_inputs": [
      59,
      51,
      2,
      16,
      3,
      22
    ],
    "in_dim": 58,
    "master": true
  },
  "Vdsr::Vdsr/Sequential[layers]/ReLU[21]/input.45": {
    "node": "Vdsr::Vdsr/Sequential[layers]/ReLU[21]/input.45",
    "ratio": null,
    "removed_outputs": [
      58,
      20,
      41,
      46,
      37,
      47
    ],
    "out_dim": 58,
    "removed_inputs": [
      58,
      20,
      41,
      46,
      37,
      47
    ],
    "in_dim": 58,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/Conv2d[22]/input.47": {
    "node": "Vdsr::Vdsr/Sequential[layers]/Conv2d[22]/input.47",
    "ratio": 0.1,
    "removed_outputs": [
      22,
      1,
      31,
      6,
      8,
      59
    ],
    "out_dim": 58,
    "removed_inputs": [
      58,
      20,
      41,
      46,
      37,
      47
    ],
    "in_dim": 58,
    "master": true
  },
  "Vdsr::Vdsr/Sequential[layers]/ReLU[23]/input.49": {
    "node": "Vdsr::Vdsr/Sequential[layers]/ReLU[23]/input.49",
    "ratio": null,
    "removed_outputs": [
      22,
      1,
      31,
      6,
      8,
      59
    ],
    "out_dim": 58,
    "removed_inputs": [
      22,
      1,
      31,
      6,
      8,
      59
    ],
    "in_dim": 58,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/Conv2d[24]/input.51": {
    "node": "Vdsr::Vdsr/Sequential[layers]/Conv2d[24]/input.51",
    "ratio": 0.1,
    "removed_outputs": [
      49,
      52,
      20,
      25,
      55,
      53
    ],
    "out_dim": 58,
    "removed_inputs": [
      22,
      1,
      31,
      6,
      8,
      59
    ],
    "in_dim": 58,
    "master": true
  },
  "Vdsr::Vdsr/Sequential[layers]/ReLU[25]/input": {
    "node": "Vdsr::Vdsr/Sequential[layers]/ReLU[25]/input",
    "ratio": null,
    "removed_outputs": [
      49,
      52,
      20,
      25,
      55,
      53
    ],
    "out_dim": 58,
    "removed_inputs": [
      49,
      52,
      20,
      25,
      55,
      53
    ],
    "in_dim": 58,
    "master": false
  },
  "Vdsr::Vdsr/Sequential[layers]/Conv2d[26]/1247": {
    "node": "Vdsr::Vdsr/Sequential[layers]/Conv2d[26]/1247",
    "ratio": null,
    "removed_outputs": [],
    "out_dim": 3,
    "removed_inputs": [
      49,
      52,
      20,
      25,
      55,
      53
    ],
    "in_dim": 58,
    "master": false
  },
  "Vdsr::Vdsr/1249": {
    "node": "Vdsr::Vdsr/1249",
    "ratio": null,
    "removed_outputs": [],
    "out_dim": 3,
    "removed_inputs": [],
    "in_dim": 3,
    "master": false
  },
  "Vdsr::return_0": {
    "node": "Vdsr::return_0",
    "ratio": null,
    "removed_outputs": [],
    "out_dim": 3,
    "removed_inputs": [],
    "in_dim": 3,
    "master": false
  }
}

Thanks

akuzminski commented 1 year ago

Is there any update on this? I have the same problem because weight shapes changed.