Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

How to prune part of model with QAT pruner #5037

Open
2730gf opened this issue Aug 1, 2022 · 9 comments
Open

How to prune part of model with QAT pruner #5037

2730gf opened this issue Aug 1, 2022 · 9 comments
Assignees
Labels

Comments

@2730gf
Copy link

2730gf commented Aug 1, 2022

Describe the issue:
Hello, I have a model that needs to be quantized. This model consists of backbone, neck and head. I only want to quantify the neck part. There are many model parameters here, I don't want to specify each tensor name. Here is the method I use:
quantizer = QAT_Quantizer(model.neck, config_list, optimizer)
But the optimizer saves the parameters of the entire model. Will it have any effect if I use it like this? At the same time, what is the role of optimizer in QAT, and why does QAT need to pass in an optimizer?
If there is something wrong with my approach, what should I do?

Environment:

  • NNI version:
  • Training service (local|remote|pai|aml|etc):
  • Client OS:
  • Server OS (for remote mode only):
  • Python version:
  • PyTorch/TensorFlow version:
  • Is conda/virtualenv/venv used?:
  • Is running in Docker?:

Configuration:

  • Experiment config (remember to remove secrets!):
  • Search space:

Log message:

  • nnimanager.log:
  • dispatcher.log:
  • nnictl stdout and stderr:

How to reproduce it?:

@J-shang
Copy link
Contributor

J-shang commented Aug 1, 2022

I think it's OK.

optimizer.step will be replaced in QATQuantizer by another step:

def new_step(optimizer):
    optimizer.step()
    model.steps.add_(1)

So QATQuantizer don't need the parameters bounded in your optimizer, just use it to count steps.

But please pay attention to the config_list and the calibration_config, the module name in these dict will be xxx instead of neck.xxx, so if you want to use the calibration_config to do the real speedup (i.e., build a TensorRT engine), remember to add neck. prefix for the module name.

@2730gf
Copy link
Author

2730gf commented Aug 2, 2022

Thank you for your reply. I am using the mmdetection framework, and the optimizer used is not torch.nn.optim, so an error will be reported. How can I solve this?

@2730gf
Copy link
Author

2730gf commented Aug 3, 2022

Hello, can anyone provide a solution?

@QuanluZhang QuanluZhang assigned J-shang and unassigned QuanluZhang Aug 3, 2022
@J-shang
Copy link
Contributor

J-shang commented Aug 4, 2022

Thank you for your reply. I am using the mmdetection framework, and the optimizer used is not torch.nn.optim, so an error will be reported. How can I solve this?

could you show the error and the optimizer you used?

@2730gf
Copy link
Author

2730gf commented Aug 4, 2022

Hello, I am reading the source code to try to solve the problem of optimizer; but I found another problem, if QAT does not specify dummy_input, there will be this bug below, is this normal? I see dummy_input=None can be specified
AssertionError: Could not found shapes for layer conv1

@2730gf
Copy link
Author

2730gf commented Aug 4, 2022

Add my code here:
quantizer = QAT_Quantizer(model, config_list, optimizer) quantizer.compress()

@J-shang
Copy link
Contributor

J-shang commented Aug 5, 2022

Hello, I am reading the source code to try to solve the problem of optimizer; but I found another problem, if QAT does not specify dummy_input, there will be this bug below, is this normal? I see dummy_input=None can be specified AssertionError: Could not found shapes for layer conv1

I check the QAT logic, I think dummy_input should not be None, it's a required parameter for QAT. You could random generate a dummy input by torch.rand(...).to(device)

@2730gf
Copy link
Author

2730gf commented Aug 5, 2022

The link I refer to is as follows:

quantizer = QAT_Quantizer(model, config_list, optimizer)

Dummy_input is not used here, and whether the shape of this dummy_input needs to be the same as the input size of the model. If the shape is inconsistent, it will cause the QAT to fail, right?

@J-shang
Copy link
Contributor

J-shang commented Aug 5, 2022

The link I refer to is as follows:

quantizer = QAT_Quantizer(model, config_list, optimizer)

Dummy_input is not used here, and whether the shape of this dummy_input needs to be the same as the input size of the model. If the shape is inconsistent, it will cause the QAT to fail, right?

Seems an old version example, I will update it.

yes, QAT need to know the input/output shape of each quantized layer.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants