×

签到

分享到微信

打开微信,使用扫一扫进入页面后,点击右上角菜单,

点击“发送给朋友”或“分享到朋友圈”完成分享

MLU220的离线模型生成出现段错误失败 已解决 繁Sige2023-06-08 09:58:48 回复 2 查看 技术答疑 使用求助
MLU220的离线模型生成出现段错误失败
分享到:

官方提供的Docker模拟环境中尝试转换模型成为MLU220的离线推导模型,以下是代码

CNML 7.9.114 06a1920

CNRT 4.7.12 03ea1d9

Python 3.6.8

import torchvision.models as models

import torch

import torch_mlu

import torch_mlu.core.mlu_model as ct

import torch_mlu.core.mlu_quantize as mlu_quantize


net = models.resnet50()

# net = models.quantization.resnet50(quantize=True)


dtype = "int8"

per_channel = False

mean = [0.485, 0.456, 0.406]

std = [0.229, 0.224, 0.225]

net.eval().float()

use_avg = False

data_scale = 1.0


dszie = 224

batch_size = 32

half_input = True

mname = 'resnet50'


qconfig = {'iteration': 32, 'use_avg':use_avg, 'data_scale':data_scale, 'mean': mean, 'std': std, 'per_channel':per_channel}

quantized_weight = mlu_quantize.quantize_dynamic_mlu(net, qconfig, dtype=dtype, gen_quant = True)


quantized_model = mlu_quantize.quantize_dynamic_mlu(net)

quantized_model.load_state_dict(quantized_weight.state_dict())


quantized_model.eval().float()


# prepare input

example_mlu = torch.randn(batch_size, 3, dszie, dszie, dtype=torch.float)

randn_mlu = torch.randn(1, 3, dszie, dszie, dtype=torch.float)

if half_input:

    randn_mlu = randn_mlu.type(torch.HalfTensor)

    example_mlu = example_mlu.type(torch.HalfTensor)


# set offline flag

ct.set_core_number(4)

ct.set_core_version('MLU220')

ct.set_device(-1)

ct.save_as_cambricon(mname)

ct.set_input_format(0)


# run jit fuse

net_traced = torch.jit.trace(quantized_model.to(ct.mlu_device()),

                             randn_mlu.to(ct.mlu_device()),

                             check_trace=False)


# run inference and save cambricon

net_traced(example_mlu.to(ct.mlu_device()))


# unset offline flag

ct.save_as_cambricon("")


运行到net_traced(example_mlu.to(ct.mlu_device()))会出现报错,以下是报错信息

QQ图片20230608095130.png

最后程序会出现段错误结束程序,最终会产生MLU220的离线推理文件,但是文件大小存在问题,可能只有几兆

版权所有 © 2024 寒武纪 Cambricon.com 备案/许可证号:京ICP备17003415号-1
关闭