Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add MMRazor quantization #513

Merged
merged 44 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
85ed93e
[FEATURE] add quant algo `Learned Step Size Quantization` (#346)
fpshuang Nov 11, 2022
09f943c
[Features]Quantize pipeline (#350)
HIT-cwh Nov 25, 2022
c7e2c9a
[Feature] Add `prepare_for_mmdeploy` interface (#365)
pppppM Dec 1, 2022
486e315
CodeCamp #132 add MinMaxFloorObserver (#376)
L-Icarus Dec 22, 2022
b57a11a
Quant go (#409)
humu789 Dec 29, 2022
0f209c2
[Refactor & Doc] Refactor graph_utils and add docstring and pytest (#…
HIT-cwh Jan 9, 2023
82056fc
Merge dev-1.x into quantize (#430)
humu789 Jan 12, 2023
c0d16ce
[Docs] Add docstring and unittest about backendconfig & observer & fa…
humu789 Jan 16, 2023
4dcf3f1
[Docs] Add docstring for `MMArchitectureQuant` & `NativeQuantizer` (#…
Niko-zyf Jan 17, 2023
854f269
[Docs] Add docstring and unitest about custom tracer (#427)
humu789 Jan 17, 2023
2eea077
[Docs & Refactor] Add docstring and UT of other quantizers (#439)
humu789 Jan 17, 2023
e78ac79
[Feature&Doc]Modify ptq pipeline and support lsq (#435)
HIT-cwh Jan 18, 2023
2a61cd0
[Docs] Add customize_quantization_tutorial (#440)
Niko-zyf Jan 18, 2023
6cdb394
[Docs] Add quantization user guide (#441)
humu789 Jan 30, 2023
0cd361d
[Bug] Fix del redundant fakequant (#447)
HIT-cwh Feb 7, 2023
d6a7ea5
[Feature] Add onnx exporters (#475)
HIT-cwh Mar 15, 2023
0a75547
[Feature]Rewrite the origin model during prepare (#488)
HIT-cwh Mar 27, 2023
019e43b
[Feature] Using rewriter in mmrazor when building qmodels. (#490)
HIT-cwh Mar 29, 2023
70a91b3
[Feature] Quantization global optimization (#491)
humu789 Apr 7, 2023
a339aed
Fix code syntax in UT (#470)
wm901115nwpu Apr 10, 2023
8dad357
passed lint and pytest
Apr 11, 2023
b046908
try to fix ci
Apr 11, 2023
64a39e1
[Bug] Try to fix CI (#502)
humu789 Apr 11, 2023
9ff2b74
[Feature] Support lsq (#501)
HIT-cwh Apr 11, 2023
5711ed9
[Feature] Add exporter pytest (#504)
HIT-cwh Apr 12, 2023
8f26f5b
[Bug] Fix ci converage setting (#508)
humu789 Apr 13, 2023
dbf3398
[Bug] Fix codecov (#509)
humu789 Apr 16, 2023
8ec3655
[BUG] Fix quantization loop (#507)
HIT-cwh Apr 17, 2023
72dc61b
add test ptq
Apr 11, 2023
e307823
opt ptq pipeline
Apr 13, 2023
e16cb64
refactor quant configs
Apr 13, 2023
6bfff52
update config path
Apr 14, 2023
b56cc5d
add summary analyse tool
Apr 14, 2023
1c95740
fix benchmark_test:detnas_frcnn_shufflenet_subnet_coco_1x.py
Apr 14, 2023
bae160a
update quantization README.md
Apr 17, 2023
700bfbd
update quantization metafile, readme, config path
Apr 17, 2023
d431b7d
update quantization docs
Apr 17, 2023
0608dd4
update git main link in workflow
Apr 17, 2023
bd3449a
update benchmark_summary_analyse.py
Apr 17, 2023
07987a6
del dmcp results
Apr 17, 2023
2ba02c7
[Bug] fix a rebase error (#514)
humu789 Apr 17, 2023
f9c2cb0
[Bug] Fix CI (#515)
humu789 Apr 17, 2023
7282a15
Update CI config and Passed (#516)
humu789 Apr 17, 2023
ab19a46
[Docs] Fix cwd test accuary (#517)
humu789 Apr 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .circleci/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ jobs:
name: Clone Repos
command: |
git clone -b main --depth 1 https://github.com/open-mmlab/mmengine.git /home/circleci/mmengine
git clone -b dev-3.x --depth 1 https://github.com/open-mmlab/mmdetection.git /home/circleci/mmdetection
git clone -b dev-1.x --depth 1 https://github.com/open-mmlab/mmclassification.git /home/circleci/mmclassification
git clone -b dev-1.x --depth 1 https://github.com/open-mmlab/mmsegmentation.git /home/circleci/mmsegmentation
git clone -b main --depth 1 https://github.com/open-mmlab/mmdetection.git /home/circleci/mmdetection
git clone -b 1.x --depth 1 https://github.com/open-mmlab/mmclassification.git /home/circleci/mmclassification
git clone -b main --depth 1 https://github.com/open-mmlab/mmsegmentation.git /home/circleci/mmsegmentation
- run:
name: Build Docker image
command: |
Expand Down Expand Up @@ -153,15 +153,15 @@ workflows:
- dev-1.x
- build_cpu:
name: minimum_version_cpu
torch: 1.6.0
torchvision: 0.7.0
python: 3.7.9
torch: 1.8.1
torchvision: 0.9.1
python: 3.7.4
requires:
- lint
- build_cpu:
name: maximum_version_cpu
torch: 1.12.1
torchvision: 0.13.1
torch: 1.13.1
torchvision: 0.14.1
python: 3.9.0
requires:
- lint
Expand All @@ -183,7 +183,7 @@ workflows:
jobs:
- build_cuda:
name: minimum_version_gpu
torch: 1.6.0
torch: 1.8.1
# Use double quotation mark to explicitly specify its type
# as string instead of number
cuda: "10.1"
Expand Down
67 changes: 67 additions & 0 deletions .dev_scripts/benchmark_summary_analyse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import argparse
import os

import mmengine


def parse_args():
parser = argparse.ArgumentParser(
description='Analyse summary.yml generated by benchmark test')
parser.add_argument('file_path', help='Summary.yml path')
args = parser.parse_args()
return args


metric_mapping = {
'Top 1 Accuracy': 'accuracy/top1',
'Top 5 Accuracy': 'accuracy/top5',
'box AP': 'coco/bbox_mAP',
'mIoU': 'mIoU'
}


def compare_metric(result, metric):
expect_val = result['expect'][metric]
actual_val = result['actual'].get(metric_mapping[metric], None)
if actual_val is None:
return None, None
if metric == 'box AP':
actual_val *= 100
decimal_bit = len(str(expect_val).split('.')[-1])
actual_val = round(actual_val, decimal_bit)
error = round(actual_val - expect_val, decimal_bit)
error_percent = round(abs(error) * 100 / expect_val, 3)
return error, error_percent


def main():
args = parse_args()
file_path = args.file_path
results = mmengine.load(file_path, 'yml')
miss_models = dict()
sort_by_error = dict()
for k, v in results.items():
valid_keys = v['expect'].keys()
compare_res = dict()
for m in valid_keys:
error, error_percent = compare_metric(v, m)
if error is None:
continue
compare_res[m] = {'error': error, 'error_percent': error_percent}
if error != 0:
miss_models[k] = compare_res
sort_by_error[k] = error
sort_by_error = sorted(
sort_by_error.items(), key=lambda x: abs(x[1]), reverse=True)
miss_models_sort = dict()
miss_models_sort['total error models'] = len(sort_by_error)
for k_v in sort_by_error:
index = k_v[0]
miss_models_sort[index] = miss_models[index]
save_path = os.path.join(os.path.dirname(file_path), 'summary_error.yml')
mmengine.fileio.dump(miss_models_sort, save_path, sort_keys=False)
print(f'Summary analysis result saved in {save_path}')


if __name__ == '__main__':
main()
5 changes: 3 additions & 2 deletions .dev_scripts/benchmark_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
def parse_args():
parser = argparse.ArgumentParser(
description="Test all models' accuracy in model-index.yml")
parser.add_argument(
'partition', type=str, help='Cluster partition to use.')
parser.add_argument('checkpoint_root', help='Checkpoint file root path.')
parser.add_argument(
'--partition', type=str, help='Cluster partition to use.')
parser.add_argument(
'--job-name',
type=str,
Expand Down Expand Up @@ -148,6 +148,7 @@ def create_test_job_batch(commands, model_info, args, port):
if exists:
print(f'{checkpoint} already exists.')
else:
print(f'start downloading {fname}')
wget.download(model_info.weights, str(checkpoint))
print(f'\nSaved in {checkpoint}.')

Expand Down
24 changes: 7 additions & 17 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,12 @@ concurrency:

jobs:
test_linux:
runs-on: ubuntu-18.04
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: [3.7]
torch: [1.6.0, 1.7.0, 1.8.0, 1.9.0, 1.10.0, 1.11.0, 1.12.0, 1.13.0]
torch: [1.8.0, 1.9.0, 1.10.0, 1.11.0, 1.12.0, 1.13.0]
include:
- torch: 1.6.0
torch_version: 1.6
torchvision: 0.7.0
- torch: 1.7.0
torch_version: 1.7
torchvision: 0.8.1
- torch: 1.7.0
torch_version: 1.7
torchvision: 0.8.1
python-version: 3.8
- torch: 1.8.0
torch_version: 1.8
torchvision: 0.9.0
Expand Down Expand Up @@ -103,11 +93,11 @@ jobs:
pip install -U openmim
mim install 'mmcv >= 2.0.0rc1'
- name: Install MMCls
run: pip install git+https://github.com/open-mmlab/mmclassification.git@dev-1.x
run: pip install 'mmcls>=1.0.0rc0'
- name: Install MMDet
run: pip install git+https://github.com/open-mmlab/mmdetection.git@dev-3.x
run: pip install git+https://github.com/open-mmlab/mmdetection.git@main
- name: Install MMSeg
run: pip install git+https://github.com/open-mmlab/mmsegmentation.git@dev-1.x
run: pip install git+https://github.com/open-mmlab/mmsegmentation.git@main
- name: Install other dependencies
run: pip install -r requirements.txt
- name: Build and install
Expand All @@ -119,8 +109,8 @@ jobs:
coverage report -m
# Upload coverage report for python3.8 && pytorch1.12.0 cpu
- name: Upload coverage to Codecov
if: ${{matrix.torch == '1.12.0' && matrix.python-version == '3.8'}}
uses: codecov/codecov-action@v2
if: ${{matrix.torch == '1.13.0' && matrix.python-version == '3.8'}}
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
flags: unittests
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<!--算法库 Badges-->

[![PyPI](https://img.shields.io/pypi/v/mmrazor)](https://pypi.org/project/mmrazor)
[![docs](https://img.shields.io/badge/docs-latest-blue)](https://mmrazor.readthedocs.io/en/dev-1.x/)
[![docs](https://img.shields.io/badge/docs-latest-blue)](https://mmrazor.readthedocs.io/en/quantize/)
[![badge](https://github.com/open-mmlab/mmrazor/workflows/build/badge.svg)](https://github.com/open-mmlab/mmrazor/actions)
[![codecov](https://codecov.io/gh/open-mmlab/mmrazor/branch/master/graph/badge.svg)](https://codecov.io/gh/open-mmlab/mmrazor)
[![license](https://img.shields.io/github/license/open-mmlab/mmrazor.svg)](https://github.com/open-mmlab/mmrazor/blob/master/LICENSE)
Expand All @@ -32,9 +32,9 @@

<!--Note:请根据各算法库自身情况设置项目和链接-->

[📘Documentation](https://mmrazor.readthedocs.io/en/dev-1.x/) |
[🛠️Installation](https://mmrazor.readthedocs.io/en/dev-1.x/get_started/installation.html) |
[👀Model Zoo](https://mmrazor.readthedocs.io/en/dev-1.x/get_started/model_zoo.html) |
[📘Documentation](https://mmrazor.readthedocs.io/en/quantize/) |
[🛠️Installation](https://mmrazor.readthedocs.io/en/quantize/get_started/installation.html) |
[👀Model Zoo](https://mmrazor.readthedocs.io/en/quantize/get_started/model_zoo.html) |
[🤔Reporting Issues](https://github.com/open-mmlab/mmrazor/issues/new/choose)

</div>
Expand Down Expand Up @@ -68,7 +68,7 @@ MMRazor is a model compression toolkit for model slimming and AutoML, which incl
- Neural Architecture Search (NAS)
- Pruning
- Knowledge Distillation (KD)
- Quantization (come soon)
- Quantization

It is a part of the [OpenMMLab](https://openmmlab.com/) project.

Expand All @@ -86,7 +86,7 @@ Major features:

With better modular design, developers can implement new model compression algorithms with only a few codes, or even by simply modifying config files.

Below is an overview of MMRazor's design and implementation, please refer to [tutorials](https://mmrazor.readthedocs.io/en/dev-1.x/get_started/overview.html) for more details.
Below is an overview of MMRazor's design and implementation, please refer to [tutorials](https://mmrazor.readthedocs.io/en/quantize/get_started/overview.html) for more details.

<div align="center">
<img src="resources/design_and_implement.png" style="zoom:100%"/>
Expand Down Expand Up @@ -164,7 +164,7 @@ Please refer to [installation.md](/docs/en/get_started/installation.md) for more

## Getting Started

Please refer to [user guides](https://mmrazor.readthedocs.io/en/dev-1.x/user_guides/index.html) for the basic usage of MMRazor. There are also [advanced guides](https://mmrazor.readthedocs.io/en/dev-1.x/advanced_guides/index.html):
Please refer to [user guides](https://mmrazor.readthedocs.io/en/quantize/user_guides/index.html) for the basic usage of MMRazor. There are also [advanced guides](https://mmrazor.readthedocs.io/en/quantize/advanced_guides/index.html):

## Contributing

Expand Down
14 changes: 7 additions & 7 deletions configs/distill/mmcls/ofd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ We investigate the design aspects of feature distillation methods achieving netw

#### Vanilla

| Dataset | Model | Top-1 (%) | Top-5 (%) | Download |
| ------- | ----------------------------------------------------------------------- | --------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| CIFAR10 | [WRN16-2](../../../vanilla/mmcls/wide-resnet/wrn16-w2_b16x8_cifar10.py) | 93.43 | 99.75 | [model](https://download.openmmlab.com/mmrazor/v1/wide_resnet/wrn16_2_b16x8_cifar10_20220831_204709-446b466e.pth) \| [log](https://download.openmmlab.com/mmrazor/v1/wide_resnet/wrn16_2_b16x8_cifar10_20220831_204709-446b466e.json) |
| CIFAR10 | [WRN28-4](../../../vanilla/mmcls/wide-resnet/wrn28-w4_b16x8_cifar10.py) | 95.49 | 99.81 | [model](https://download.openmmlab.com/mmrazor/v1/wide_resnet/wrn28_4_b16x8_cifar10_20220831_173536-d6f8725c.pth) \| [log](https://download.openmmlab.com/mmrazor/v1/wide_resnet/wrn28_4_b16x8_cifar10_20220831_173536-d6f8725c.json) |
| Dataset | Model | Top-1 (%) | Download |
| ------- | ----------------------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| CIFAR10 | [WRN16-2](../../../vanilla/mmcls/wide-resnet/wrn16-w2_b16x8_cifar10.py) | 93.43 | [model](https://download.openmmlab.com/mmrazor/v1/wide_resnet/wrn16_2_b16x8_cifar10_20220831_204709-446b466e.pth) \| [log](https://download.openmmlab.com/mmrazor/v1/wide_resnet/wrn16_2_b16x8_cifar10_20220831_204709-446b466e.json) |
| CIFAR10 | [WRN28-4](../../../vanilla/mmcls/wide-resnet/wrn28-w4_b16x8_cifar10.py) | 95.49 | [model](https://download.openmmlab.com/mmrazor/v1/wide_resnet/wrn28_4_b16x8_cifar10_20220831_173536-d6f8725c.pth) \| [log](https://download.openmmlab.com/mmrazor/v1/wide_resnet/wrn28_4_b16x8_cifar10_20220831_173536-d6f8725c.json) |

#### Distillation

| Dataset | Model | Flops(M) | Teacher | Top-1 (%) | Top-5 (%) | Configs | Download |
| ------- | ------- | -------- | ------- | --------- | --------- | ----------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| CIFAR10 | WRN16-2 | 101 | WRN28-4 | 95.23 | 99.79 | [config](./ofd_backbone_resnet50_resnet18_8xb16_cifar10.py) | [model](https://download.openmmlab.com/mmrazor/v1/overhaul/ofd_backbone_resnet50_resnet18_8xb16_cifar10_20220831_220553-f5d12e61.pth) \| [log](https://download.openmmlab.com/mmrazor/v1/overhaul/ofd_backbone_resnet50_resnet18_8xb16_cifar10_20220831_220553-f5d12e61.json) |
| Dataset | Model | Flops(M) | Teacher | Top-1 (%) | Configs | Download |
| ------- | ------- | -------- | ------- | --------- | ----------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| CIFAR10 | WRN16-2 | 101 | WRN28-4 | 94.21 | [config](./ofd_backbone_resnet50_resnet18_8xb16_cifar10.py) | [model](https://download.openmmlab.com/mmrazor/v1/overhaul/ofd_backbone_resnet50_resnet18_8xb16_cifar10_20230417_192216-ace2908f.pth) \| [log](https://download.openmmlab.com/mmrazor/v1/overhaul/ofd_backbone_resnet50_resnet18_8xb16_cifar10_20230417_192216-ace2908f.log) |

## Getting Started

Expand Down
4 changes: 2 additions & 2 deletions configs/distill/mmcls/ofd/metafile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ Models:
- Task: Image Classification
Dataset: CIFAR-10
Metrics:
Top 1 Accuracy: 95.4400
Top 1 Accuracy: 94.21
Config: configs/distill/mmcls/ofd/ofd_backbone_resnet50_resnet18_8xb16_cifar10.py
Weights: https://download.openmmlab.com/mmrazor/v1/overhaul/ofd_backbone_resnet50_resnet18_8xb16_cifar10_20220831_220553-f5d12e61.pth
Weights: https://download.openmmlab.com/mmrazor/v1/overhaul/ofd_backbone_resnet50_resnet18_8xb16_cifar10_20230417_192216-ace2908f.pth
2 changes: 1 addition & 1 deletion configs/nas/mmcls/darts/darts_subnet_1xb96_cifar10_2.0.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
init_cfg=dict(
type='Pretrained',
checkpoint= # noqa: E251
'https://openmmlab-share.oss-cn-hangzhou.aliyuncs.com/mmrazor/v1/darts/darts_subnetnet_1xb96_cifar10_acc-97.27_20211222-17e42600_latest.pth', # noqa: E501
'https://download.openmmlab.com/mmrazor/v1/darts/darts_subnetnet_1xb96_cifar10_acc-97.27_20211222-17e42600_latest.pth', # noqa: E501
prefix='architecture.'))

model_wrapper_cfg = None
Expand Down
2 changes: 1 addition & 1 deletion configs/nas/mmcls/darts/metafile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ Models:
Top 1 Accuracy: 97.32
Top 5 Accuracy: 99.94
Config: configs/nas/mmcls/darts/darts_subnet_1xb96_cifar10_2.0.py
Weights: https://download.openmmlab.com/mmrazor/v1/darts/darts_subnetnet_1xb96_cifar10_acc-97.32_20211222-23ca1e10.pth
Weights: https://download.openmmlab.com/mmrazor/v1/darts/darts_subnetnet_1xb96_cifar10_acc-97.32_20211222-e5727921_latest.pth
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
init_cfg=dict(
type='Pretrained',
checkpoint= # noqa: E251
'detnas_subnet_frcnn_shufflenetv2_fpn_1x_coco_bbox_backbone_flops-0.34M_mAP-37.5_20220715-61d2e900_v1.pth', # noqa: E501
'https://download.openmmlab.com/mmrazor/v1/detnas/detnas_subnet_frcnn_shufflenetv2_fpn_1x_coco_bbox_backbone_flops-0.34M_mAP-37.5_20220715-61d2e900_v1.pth', # noqa: E501
prefix='architecture.'))

find_unused_parameters = False
Loading