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

NotImplementedError: (Unimplemented) Delete Weight Dequant Linear Op Pass is not supported for per-channel quantization #13083

Open
nyh3174680732 opened this issue Jun 16, 2024 · 4 comments

Comments

@nyh3174680732
Copy link

问题描述 / Problem Description

模型在自动压缩之前可以正常运行,自动压缩之后开始报错,错误大概是这个错误表明你在尝试使用不支持的操作。根据错误消息,可能是在尝试使用Delete Weight Dequant Linear Op Pass来处理权重去量化线性操作,但该操作不支持通道量化,不知道怎么解决 完全按照https://github.com/PaddlePaddle/PaddleSlim/tree/develop/example/auto_compression/detection#4%E9%A2%84%E6%B5%8B%E9%83%A8%E7%BD%B2教程走的。

运行环境 / Runtime Environment

  • Paddle:PaddlePaddle 2.5.2

复现代码 / Reproduction Code

完整报错 / Complete Error Message

#predict.py
%cd ~
!python predict.py /home/aistudio/work/data/val/test.txt result.json
/home/aistudio
Model Arch: DETR
--transform op: Resize
--transform op: NormalizeImage
--transform op: Permute
Traceback (most recent call last):
File "/home/aistudio/predict.py", line 221, in
main(infer_txt, result_path, det_model_path, threshold)
File "/home/aistudio/predict.py", line 204, in main
detector = Detector(pred_config, det_model_path)
File "/home/aistudio/predict.py", line 126, in init
self.predictor, self.config = load_predictor(model_dir)
File "/home/aistudio/predict.py", line 78, in load_predictor
predictor = create_predictor(config)
NotImplementedError: (Unimplemented) Delete Weight Dequant Linear Op Pass is not supported for per-channel quantization (at ../paddle/fluid/framework/ir/delete_weight_dequant_linear_op_pass.cc:130)

可能解决方案 / Possible solutions

附件 / Appendix

@nyh3174680732 nyh3174680732 changed the title https://github.com/PaddlePaddle/PaddleOCR/issues/new?assignees=&labels=Bug&projects=&template=bug.md&title= NotImplementedError: (Unimplemented) Delete Weight Dequant Linear Op Pass is not supported for per-channel quantization Jun 16, 2024
@nyh3174680732
Copy link
Author

nyh3174680732 commented Jun 16, 2024

predict.py如下

@nyh3174680732
Copy link
Author

import os
import time
import sys
sys.path.append('PaddleDetection')
parent_path = os.path.abspath(os.path.join(file, *(['..'] * 2)))
sys.path.insert(0, parent_path)
import json
import yaml
from functools import reduce
import multiprocessing
from concurrent.futures import ThreadPoolExecutor, as_completed

from PIL import Image
import cv2
import numpy as np
import paddle
from paddle.inference import Config
from paddle.inference import create_predictor
from deploy.python.preprocess import preprocess, Resize, NormalizeImage, Permute, PadStride
from deploy.python.utils import argsparser, Timer, get_current_memory_mb

class PredictConfig():
"""set config of preprocess, postprocess and visualize
Args:
model_dir (str): root path of model.yml
"""

def __init__(self, model_dir):
    # parsing Yaml config for Preprocess
    deploy_file = os.path.join(model_dir, 'infer_cfg.yml')
    with open(deploy_file) as f:
        yml_conf = yaml.safe_load(f)
    self.arch = yml_conf['arch']
    self.preprocess_infos = yml_conf['Preprocess']
    self.min_subgraph_size = yml_conf['min_subgraph_size']
    self.labels = yml_conf['label_list']
    self.mask = False
    self.use_dynamic_shape = yml_conf['use_dynamic_shape']
    if 'mask' in yml_conf:
        self.mask = yml_conf['mask']
    self.tracker = None
    if 'tracker' in yml_conf:
        self.tracker = yml_conf['tracker']
    if 'NMS' in yml_conf:
        self.nms = yml_conf['NMS']
    if 'fpn_stride' in yml_conf:
        self.fpn_stride = yml_conf['fpn_stride']
    self.print_config()

def print_config(self):
    print('%s: %s' % ('Model Arch', self.arch))
    for op_info in self.preprocess_infos:
        print('--%s: %s' % ('transform op', op_info['type']))

def get_test_images(infer_file):
with open(infer_file, 'r') as f:
dirs = f.readlines()
images = [eval(repr(dir.replace('\n', ''))).replace('\', '/') for dir in dirs]
assert len(images) > 0, "no image found in {}".format(infer_file)
return images

def load_predictor(model_dir):
config = Config(
os.path.join(model_dir, 'model.pdmodel'),
os.path.join(model_dir, 'model.pdiparams'))
# initial GPU memory(M), device ID
config.enable_use_gpu(2000, 0)
# optimize graph and fuse op
config.switch_ir_optim(True)
# disable print log when predict
config.disable_glog_info()
# enable shared memory
config.enable_memory_optim()
# disable feed, fetch OP, needed by zero_copy_run
config.switch_use_feed_fetch_ops(False)
predictor = create_predictor(config)
return predictor, config

def create_inputs(imgs, im_info):
inputs = {}

im_shape = [np.array((e['im_shape'], )).astype('float32') for e in im_info]
scale_factor = [np.array((e['scale_factor'], )).astype('float32') for e in im_info]

origin_scale_factor = np.concatenate(scale_factor, axis=0)

imgs_shape = [[e.shape[1], e.shape[2]] for e in imgs]
max_shape_h = max([e[0] for e in imgs_shape])
max_shape_w = max([e[1] for e in imgs_shape])
padding_imgs = []
padding_imgs_shape = []
padding_imgs_scale = []
for img in imgs:
    im_c, im_h, im_w = img.shape[:]
    padding_im = np.zeros(
        (im_c, max_shape_h, max_shape_w), dtype=np.float32)
    padding_im[:, :im_h, :im_w] = np.array(img, dtype=np.float32)
    padding_imgs.append(padding_im)
    padding_imgs_shape.append(
        np.array([max_shape_h, max_shape_w]).astype('float32'))
    rescale = [float(max_shape_h) / float(im_h), float(max_shape_w) / float(im_w)]
    padding_imgs_scale.append(np.array(rescale).astype('float32'))
inputs['image'] = np.stack(padding_imgs, axis=0)
inputs['im_shape'] = np.stack(padding_imgs_shape, axis=0)
inputs['scale_factor'] = origin_scale_factor
return inputs

class Detector(object):

def __init__(self,
             pred_config,
             model_dir,
             device='CPU',
             run_mode='paddle',
             batch_size=1,
             trt_min_shape=1,
             trt_max_shape=1280,
             trt_opt_shape=640,
             trt_calib_mode=False,
             cpu_threads=1,
             enable_mkldnn=False):
    self.pred_config = pred_config
    self.predictor, self.config = load_predictor(model_dir)
    self.det_times = Timer()
    self.cpu_mem, self.gpu_mem, self.gpu_util = 0, 0, 0
    self.preprocess_ops = self.get_ops()

def get_ops(self):
    preprocess_ops = []
    for op_info in self.pred_config.preprocess_infos:
        new_op_info = op_info.copy()
        op_type = new_op_info.pop('type')
        preprocess_ops.append(eval(op_type)(**new_op_info))
    return preprocess_ops

def predict(self, inputs):
    # preprocess
    input_names = self.predictor.get_input_names()
    for i in range(len(input_names)):
        input_tensor = self.predictor.get_input_handle(input_names[i])
        input_tensor.copy_from_cpu(inputs[input_names[i]])

    np_boxes, np_boxes_num = [], []

    # model_prediction
    self.predictor.run()
    np_boxes.clear()
    np_boxes_num.clear()
    output_names = self.predictor.get_output_names()
    num_outs = int(len(output_names) / 2)

    for out_idx in range(num_outs):
        np_boxes.append(
            self.predictor.get_output_handle(output_names[out_idx])
            .copy_to_cpu())
        np_boxes_num.append(
            self.predictor.get_output_handle(output_names[
                out_idx + num_outs]).copy_to_cpu())

    np_boxes, np_boxes_num = np.array(np_boxes[0]), np.array(np_boxes_num[0])
    return dict(boxes=np_boxes, boxes_num=np_boxes_num)

def process_image(detector, im_path, threshold):
c_result = []
image_id = os.path.basename(im_path).split('.')[0]
im, im_info = preprocess(im_path, detector.preprocess_ops)
inputs = create_inputs([im], [im_info])
det_results = detector.predict(inputs)

im_bboxes_num = det_results['boxes_num'][0]
if im_bboxes_num > 0:
    bbox_results = det_results['boxes'][0:im_bboxes_num, 2:]
    id_results = det_results['boxes'][0:im_bboxes_num, 0]
    score_results = det_results['boxes'][0:im_bboxes_num, 1]

    for idx in range(im_bboxes_num):
        if float(score_results[idx]) >= threshold:
            c_result.append({"image_id": image_id,
                             "type": int(id_results[idx]) + 1,
                             "x": float(bbox_results[idx][0]),
                             "y": float(bbox_results[idx][1]),
                             "width": float(bbox_results[idx][2]) - float(bbox_results[idx][0]),
                             "height": float(bbox_results[idx][3]) - float(bbox_results[idx][1]),
                             "segmentation": []})
return c_result

def predict_image(detector, image_list, result_path, threshold):
c_results = {"result": []}

with ThreadPoolExecutor(max_workers=2) as executor:
    future_to_image = {executor.submit(process_image, detector, image, threshold): image for image in image_list}
    for future in as_completed(future_to_image):
        c_results["result"].extend(future.result())

# 写文件
with open(result_path, 'w') as ft:
    json.dump(c_results, ft)

def main(infer_txt, result_path, det_model_path, threshold):
pred_config = PredictConfig(det_model_path)
detector = Detector(pred_config, det_model_path)

# predict from image
img_list = get_test_images(infer_txt)
predict_image(detector, img_list, result_path, threshold)

if name == 'main':
start_time = time.time()
det_model_path = "/home/aistudio/PaddleSlim/example/auto_compression/detection/output"
#det_model_path = "/home/aistudio/PaddleDetection/output_inference/rtdetr_r101vd_6x_coco"

threshold = 0.5

paddle.enable_static()
infer_txt = sys.argv[1]
result_path = sys.argv[2]

main(infer_txt, result_path, det_model_path, threshold)
print('total time:', time.time() - start_time)
print('FPS:',len(get_test_images(infer_txt))/(time.time() - start_time))

@GreatV
Copy link
Collaborator

GreatV commented Jun 16, 2024

This is a Paddle framework issue. You can try updating your PaddlePaddle version or submit an issue to the paddlepaddle/paddle repository.

@nyh3174680732
Copy link
Author

This is a Paddle framework issue. You can try updating your PaddlePaddle version or submit an issue to the paddlepaddle/paddle repository.
升级到PaddlePaddle 2.6.1最新版本之后又变成这个错误了
/home/aistudio
Model Arch: DETR
--transform op: Resize
--transform op: NormalizeImage
--transform op: Permute
Traceback (most recent call last):
File "/home/aistudio/predict.py", line 221, in
main(infer_txt, result_path, det_model_path, threshold)
File "/home/aistudio/predict.py", line 208, in main
predict_image(detector, img_list, result_path, threshold)
File "/home/aistudio/predict.py", line 196, in predict_image
c_results["result"].extend(future.result())
File "/opt/conda/envs/python35-paddle120-env/lib/python3.10/concurrent/futures/_base.py", line 451, in result
return self.__get_result()
File "/opt/conda/envs/python35-paddle120-env/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
raise self._exception
File "/opt/conda/envs/python35-paddle120-env/lib/python3.10/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/aistudio/predict.py", line 171, in process_image
det_results = detector.predict(inputs)
File "/home/aistudio/predict.py", line 149, in predict
self.predictor.run()
ValueError: (InvalidArgument) The type of data we are trying to retrieve (float32) does not match the type of data (int8) currently contained in the container.
[Hint: Expected dtype() == phi::CppTypeToDataType::Type(), but received dtype():3 != phi::CppTypeToDataType::Type():10.] (at /paddle/paddle/phi/core/dense_tensor.cc:171)
[operator < fused_fc_elementwise_layernorm > error]

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

No branches or pull requests

2 participants