open-mmlab / mmrazor

OpenMMLab Model Compression Toolbox and Benchmark.
https://mmrazor.readthedocs.io/en/latest/
Apache License 2.0
1.46k stars 228 forks source link

TypeError: 'ABCMeta' Object is not subscriptable when configuring mmseg model for PTQ and QAT #652

Closed ktadoulas closed 3 days ago

ktadoulas commented 1 week ago

Hi,

I am working on configuring a custom config file for the stdc1 model in MMsegmentation using the Cityscapes dataset, with the aim of performing both PTQ and QAT. For reference, I have looked at the following MMRazor config files:

However, I am encountering the following error during both PTQ and QAT: Opera Snapshot_2024-09-12_124929_Screenshot20124418 png

Opera Snapshot_2024-09-12_124608_Screenshot20124137 png

Since the provided config is limited for mmcls, could you please guide me on how to properly modify the config file for the stdc1 model and Cityscapes dataset?

Below is the config files I wrote: For ptq_openvino_stdc1_4xb12-80k_cityscapes-512x1024.py

_base_ = [
    'mmseg::stdc/stdc1_4xb12-80k_cityscapes-512x1024.py' ,
    # '../../deploy_cfgs/mmseg/segmentation_openvino_dynamic-1024x2048.py'
]

val_dataloader = dict(batch_size=32)

test_cfg = dict(
    type='mmrazor.PTQLoop',
    calibrate_dataloader=val_dataloader,
    calibrate_steps=32,
)

global_qconfig = dict(
    w_observer=dict(type='mmrazor.PerChannelMinMaxObserver'),
    a_observer=dict(type='mmrazor.MovingAverageMinMaxObserver'),
    w_fake_quant=dict(type='mmrazor.FakeQuantize'),
    a_fake_quant=dict(type='mmrazor.FakeQuantize'),
    w_qscheme=dict(
        qdtype='qint8', bit=8, is_symmetry=True, is_symmetric_range=True),
    a_qscheme=dict(
        qdtype='quint8', bit=8, is_symmetry=True, averaging_constant=0.1),
)

float_checkpoint = 'mmsegmentation/work_dirs/stdc1_4xb12-80k_cityscapes-512x1024/best_mIoU_epoch_0.pth'  # path to my local model

model = dict(
    _delete_=True,
    type='mmrazor.MMArchitectureQuant',
    data_preprocessor = dict(
        type='mmseg.SegDataPreProcessor',
        mean=[123.675, 116.28, 103.53],
        std=[58.395, 57.12, 57.375],
        bgr_to_rgb=True,
        pad_val=0,
        seg_pad_val=255),
    architecture=_base_.model,
    float_checkpoint=float_checkpoint,
    quantizer=dict(
        type='mmrazor.OpenVINOQuantizer',
        global_qconfig=global_qconfig,
        tracer=dict(
            type='mmrazor.CustomTracer',
        skipped_methods=[
                'mmseg.models.decode_heads.decode_head.BaseDecodeHead.predict_by_feat',
                'mmseg.models.decode_heads.decode_head.BaseDecodeHead.loss_by_feat'
            )
            ))

model_wrapper_cfg = dict(type='mmrazor.MMArchitectureQuantDDP', ) 

For qat_openvino_stdc1_4xb12-80k_cityscapes-512x1024.py:


_base_ = ['mmseg::stdc/stdc1_4xb12-80k_cityscapes-512x1024.py' ]

float_checkpoint = 'mmsegmentation/work_dirs/stdc1_4xb12-80k_cityscapes-512x1024/best_mIoU_epoch_0.pth'  # path to my local model

global_qconfig = dict(
    w_observer=dict(type='mmrazor.PerChannelMinMaxObserver'),
    a_observer=dict(type='mmrazor.MovingAverageMinMaxObserver'),
    w_fake_quant=dict(type='mmrazor.FakeQuantize'),
    a_fake_quant=dict(type='mmrazor.FakeQuantize'),
    w_qscheme=dict(
        qdtype='qint8', bit=8, is_symmetry=True, is_symmetric_range=True),
    a_qscheme=dict(
        qdtype='quint8', bit=8, is_symmetry=True, averaging_constant=0.1),
)

model = dict(
    _delete_=True,
    _scope_='mmrazor',
    type='MMArchitectureQuant',
    data_preprocessor = dict(
        type='mmseg.SegDataPreProcessor',
        mean=[123.675, 116.28, 103.53],
        std=[58.395, 57.12, 57.375],
        bgr_to_rgb=True,
        pad_val=0,
        seg_pad_val=255),
    architecture=_base_.model,
    float_checkpoint=float_checkpoint,
    quantizer=dict(
        type='mmrazor.OpenVINOQuantizer',
        global_qconfig=global_qconfig,
        tracer=dict(
            type='mmrazor.CustomTracer',
        skipped_methods=[
                'mmseg.models.decode_heads.decode_head.BaseDecodeHead.predict_by_feat',
                'mmseg.models.decode_heads.decode_head.BaseDecodeHead.loss_by_feat']
            )))

optim_wrapper = dict(
    optimizer=dict(type='SGD', lr=0.0001, momentum=0.9, weight_decay=0.0001))

# learning policy
param_scheduler = dict(
    _delete_=True, type='ConstantLR', factor=1.0, by_epoch=True)

model_wrapper_cfg = dict(
    type='mmrazor.MMArchitectureQuantDDP',
    broadcast_buffers=False,
    find_unused_parameters=False)

# train, val, test setting
train_cfg = dict(
    _delete_=True,
    type='mmrazor.QATEpochBasedLoop',
    max_epochs=10,
    val_interval=1)
val_cfg = dict(_delete_=True, type='mmrazor.QATValLoop')

# Make sure the buffer such as min_val/max_val in saved checkpoint is the same
# among different rank.
default_hooks = dict(sync=dict(type='SyncBuffersHook')) ```
Veccoy commented 1 week ago

Hi, I remember having this issue at first when using MMRazor, but I can't remember the exact solution. I think it was coming from the Python version. I was using Python 3.8, now I'm using Python 3.10 and I don't have this error anymore. I hope this can help

ktadoulas commented 1 week ago

Hi, I remember having this issue at first when using MMRazor, but I can't remember the exact solution. I think it was coming from the Python version. I was using Python 3.8, now I'm using Python 3.10 and I don't have this error anymore. I hope this can help

@Veccoy Upgrading Python to version 3.10, while keeping the rest of the package versions unchanged, resulted in the following error: ImportError: torch>=1.13 is not installed properly

Here is my current setup:

mmcv: 2.1.0 mmengine: 0.10.4 mmrazor: 1.0.0 mmsegmentation: 1.2.2 torch: 2.4.1+cu118

Could you share the specific versions of Python and other packages in your working environment? Thanks!

Veccoy commented 4 days ago

Hi, MMRazor does not work with PyTorch 2.x unfortunately, or you have to modified the code deeply. You can see in several script that some code parts are not run if you don't have the right Torch version (I don't know why they did this...)

Try downgrading your PyTorch version to 1.13.1. You can find below my requirement file for the experiments I made, there may be useless dependencies in it.

# Base requirements
ftfy
future>=0.18.2
joblib>=1.4.2
numpy==1.26.4
onnx>=1.13.0
onnx-graphsurgeon
onnxscript
regex
setuptools==60.5.0
tensorboard>=2.16.2
torch_tb_profiler  # for model profiling visualization
torch==1.13.1
torchtext==0.14.1
torchvision==0.14.1

# MMLab requirements
openmim
mmengine
mmcv>=2.0.0
mmpretrain>=1.0.0
# Already installed from source
mmcls>=1.0.0rc6
mmdet>=3.0.0
mmsegmentation>1.0.0
mmrazor>=1.0.0
ktadoulas commented 3 days ago

@Veccoy Thank you ! Indeed, downgrading PyTorch version to 1.13.1 works.