This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] PROCESSOR-SDK-AM68A:导入 onnx 时、PSDK 9.2中的 QATv2出现问题

Guru**** 2540720 points


请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/processors-group/processors/f/processors-forum/1398344/processor-sdk-am68a-issue-with-qatv2-in-psdk-9-2-when-importing-onnx

器件型号:PROCESSOR-SDK-AM68A

工具与软件:

你(们)好

我看到了使用 QATv2绕过训练错误的方法、使用该方法使用 QATv2训练网络。
不过、 在转换为 onnx 后导入网络时、我遇到了与票证(https://e2e.ti.com/support/processors-group/processors/f/processors-forum/1355757/am68a-tidl-import-with-edgeai_torchmodelopt-qatv2-model)相同的问题

培训网络和 ONX 出口流程与上述 TT 相同(https://e2e.ti.com/support/processors-group/processors/f/processors-forum/1355757/am68a-tidl-import-with-edgeai_torchmodelopt-qatv2-model)

我使用了如下导入选项。

modelType          = 2
numParamBits       = 8
numFeatureBits     = 8
quantizationStyle  = 6
inQuantFactor      = 1
inElementType      = 1
inWidth            = 640
inHeight           = 384
inNumChannels      = 3
calibrationOption = 0
biasCalibrationIterations = 0

误差如下所示。

如何导入 onnx 文件?

谢谢你。
林亨达

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    林亨达先生:

    您使用的是最新的工具吗? 对于9.2、您可以使用 https://github.com/TexasInstruments/edgeai-tidl-tools/releases/tag/09_02_09_00

    谢谢!

    Fabiana

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    感谢您的答复。

    我尝试的时间是09_02_06_00。

    我将尝试使用09_02_09_00。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Hyuntak:

    如果您可以使用09_02_09_00验证问题的行为、这些内容可能会有所帮助。 由于您使用的是 edgeai-tidl-tools、因此我还建议您尝试使用 Python 脚本(而不是此处所述的配置文件)导入模型。 您可以指定此处文档中指定的预量化模型。 通过这种方法、我建议使用 debug_level = 2来发送编译日志或使用配置文件方法 debugTraceLevel = 2来发送编译日志。  

    此致!

    Asha

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    你好、Asha。

    很抱歉迟到了响应。

    我尝试使用 PSDK 09_02_09_00、但结果相同。

    但是、我在将 QATv2手电筒模型导出到 onnx 时发现了一些问题。

    如文档所述,我使用 QATv2训练了火炬模型,并尝试导出到 onnx wiht model.convert ()(代码如下)

    model = edgeai_torchmodelopt.xmodelopt.quantization.v2.QATFxModule(model, total_epochs=config.epochs)
    model.train()
    
    # train
    
    model.eval()
    model.convert()
    
    dummy_input = torch.rand((1,3,384,640))
        torch.onnx.export(model.module, dummy_input, './qatv2_model.onnx', export_params=True, verbose=False, do_constant_folding=True, opset_version=18)

    但是、 我 收到一个错误、上面显示

    File "/workspace/unified_model/task/tidl_qat_v2_func.py", line 300, in create_unified_student_model
        torch.onnx.export(model.module, dummy_input, './qatv2_model.onnx', export_params=True, verbose=False, do_constant_folding=True, opset_version=18)
      File "/opt/conda/lib/python3.10/site-packages/torch/onnx/utils.py", line 506, in export
        _export(
      File "/opt/conda/lib/python3.10/site-packages/torch/onnx/utils.py", line 1548, in _export
        graph, params_dict, torch_out = _model_to_graph(
      File "/opt/conda/lib/python3.10/site-packages/torch/onnx/utils.py", line 1113, in _model_to_graph
        graph, params, torch_out, module = _create_jit_graph(model, args)
      File "/opt/conda/lib/python3.10/site-packages/torch/onnx/utils.py", line 989, in _create_jit_graph
        graph, torch_out = _trace_and_get_graph_from_model(model, args)
      File "/opt/conda/lib/python3.10/site-packages/torch/onnx/utils.py", line 885, in _trace_and_get_graph_from_model
        orig_state_dict_keys = torch.jit._unique_state_dict(model).keys()
      File "/opt/conda/lib/python3.10/site-packages/torch/jit/_trace.py", line 71, in _unique_state_dict
        filtered_dict[k] = v.detach()
    AttributeError: 'torch.qscheme' object has no attribute 'detach'

    跳过 model.convert()时未发生。 并在使用 PSDK 9.2进行导入时出现错误。

    (第一次试验时,我错过了 model.convert (),这是我的错误)

    此外、我在写响应时发现了新的。

    我在上面编写了代码 model.convert()、但文档中显示的模型= model.convert。

    所以我尝试了 model=model.convert (),但我得到了新的错误...

      File "/workspace/unified_model/task/tidl_qat_v2_func.py", line 300, in create_unified_student_model
        torch.onnx.export(model.module, dummy_input, './qatv2_model.onnx', export_params=True, verbose=False, do_constant_folding=True, opset_version=18)
      File "/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1614, in __getattr__
        raise AttributeError("'{}' object has no attribute '{}'".format(
    AttributeError: 'GraphModule' object has no attribute 'module'. Did you mean: 'modules'?

    此致!

    Hyuntak

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Hyuntak:

    我很抱歉在我出差的时候,我最后的反应很晚。

    如果我的理解正确,在你的原始文章中,你能够导出的模型是当你离开 model.convert ()(用户错误)  

    现在、如果包含它、也就是 这里的脚本 所描述的内容、您会在最近发布的文章中得到最后一个错误。  

    我会将您的票交给一位工程师、以便更好地为您提供  QATv2手电筒型号方面的支持、将其转化为 onx 脚本、而不是模型导入。 很抱歉耽误你的时间。  

    此致!

    Asha

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    据我所知、错误是在 edgeai-modeloptimization 中使用 v2量化包装程序创建的 PyTorch QAT 模型的 onnx epxort 中。

    您能否为我们提供一个简单的 python 脚本来复制该问题。 您可以创建一个简单的模型、并执行您遵循的步骤来复制错误-您不需要包含训练代码。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好、Manu。

    我使用该脚本测试了 QATv2。 如果出现问题、请告知我。

    import torch
    import torch.nn as nn
    import torch.nn.functional as F
    import edgeai_torchmodelopt
    
    
    class testnet(nn.Module):
        def __init__(self):
            super().__init__()
            self.conv0 = nn.Conv2d(3, 32, 3, padding=1, stride=2)
            self.bn0 = nn.BatchNorm2d(32)
            self.relu0 = nn.ReLU()
            
            self.conv1 = nn.Conv2d(32, 32, 3, padding=1, stride=2)
            self.bn1 = nn.BatchNorm2d(32)
            self.relu1 = nn.ReLU()
            
            self.conv2 = nn.Conv2d(32, 32, 3, padding=1, stride=2)
            self.bn2 = nn.BatchNorm2d(32)
            self.relu2 = nn.ReLU()
            
            self.conv3 = nn.Conv2d(32, 32, 3, padding=1, stride=2)
            self.bn3 = nn.BatchNorm2d(32)
            self.relu3 = nn.ReLU()
            
            self.conv4 = nn.Conv2d(32, 32, 3, padding=1, stride=2)
            self.bn4 = nn.BatchNorm2d(32)
            self.relu4 = nn.ReLU()
            
            self.conv5 = nn.Conv2d(32, 32, 3, padding=1, stride=2)
            self.bn5 = nn.BatchNorm2d(32)
            self.relu5 = nn.ReLU()
            
            self.conv6 = nn.Conv2d(32, 32, 3, padding=1, stride=2)
            self.bn6 = nn.BatchNorm2d(32)
            self.relu6 = nn.ReLU()
            
            self.pad = torch.nn.ZeroPad2d(((0,0,1,1)))
            
            self.conv7 = nn.Conv2d(32, 32, 3, stride=1, padding=0)
            self.bn7 = nn.BatchNorm2d(32)
            self.relu7 = nn.ReLU()
            
            self.conv8 = nn.Conv2d(32, 2, 3, stride=1, padding=0)
            self.bn8 = nn.BatchNorm2d(2)
            self.relu8 = nn.ReLU()
    
            
        def forward(self, x):
            # 640x384 -> 320x192
            x = self.conv0(x)
            x = self.bn0(x)
            x = self.relu0(x)
            
            # 320x192 -> 160x96
            x = self.conv1(x)
            x = self.bn1(x)
            x = self.relu1(x)
            
            # 160x96 -> 80x48
            x = self.conv2(x)
            x = self.bn2(x)
            x = self.relu2(x)
            
            # 80x48 -> 40x24
            x = self.conv3(x)
            x = self.bn3(x)
            x = self.relu3(x)
            
            # 40x24 -> 20x12
            x = self.conv4(x)
            x = self.bn4(x)
            x = self.relu4(x)
            
            # 20x12 -> 10x6
            x = self.conv5(x)
            x = self.bn5(x)
            x = self.relu5(x)
            # x = self.avg_pool5(x)
            
            # 10x6 -> 5x3
            x = self.conv6(x)
            x = self.bn6(x)
            x = self.relu6(x)
            
            # 5x3 -> 5x5
            x = self.pad(x)
            
            # 5x5 -> 3x3
            x = self.conv7(x)
            x = self.bn7(x)
            x = self.relu7(x)
            
            # 3x3 -> 1x1
            x = self.conv8(x)
            x = self.bn8(x)
            x = self.relu8(x)
            
            return x
        
    
    model = testnet().cuda()
    model = edgeai_torchmodelopt.xmodelopt.quantization.v2.QATFxModule(model, total_epochs=config.epochs)
    optimizer = torch.optim.SGD(
                model.parameters(),
                lr=0.00001, momentum=0.9, weight_decay=0.005)
    
    model.train()
    dummy_input = torch.rand((1,3,384,640)).cuda()
    dummy_gt = torch.zeros(1).long().cuda()
    
    output = model(dummy_input)
    loss = nn.CrossEntropyLoss()(output.squeeze(-1).squeeze(-1), dummy_gt)
    loss.backward()
    optimizer.step()
    
    model.eval()
    model = model.cpu()
    
    torch.save(model.module.state_dict(), './qatv2_model.pth')
    
    print('before model\n', model)
    model.convert()
    print('after model\n', model)
    
    dummy_input = torch.rand((1,3,384,640)).cpu()
    torch.onnx.export(model.module, dummy_input, './qatv2_model.onnx', export_params=True, verbose=False, do_constant_folding=True, opset_version=18)

    此致!
    Hyuntak

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    感谢您分享该脚本。 我们将重新审视一下它。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Hyuntak:

    您是否可以尝试导出模型本身而不是 model.module、即

    torch.onnx.export(model, dummy_input, './qatv2_model.onnx', export_params=True, verbose=False, do_constant_folding=True, opset_version=18)

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    你好、Parakh。

    感谢您的答复。

    首先、它可以在您回复时使用此代码。

    model = model.convert()
    dummy_input = torch.rand((1,3,384,640))
    torch.onnx.export(model, dummy_input, './qatv2_model.onnx', export_params=True, verbose=False, do_constant_folding=True, opset_version=18)

    但是,在应用上述代码之前,我在 model.train()、model.eval()中遇到了一些错误

        model.train()
      File "/opt/conda/lib/python3.10/site-packages/edgeai_torchmodelopt/xmodelopt/quantization/v2/quant_fx_base.py", line 53, in train
        return quant_fx_func.train(self.module, *args, **kwargs)
      File "/opt/conda/lib/python3.10/site-packages/edgeai_torchmodelopt/xmodelopt/quantization/v2/quant_fx_func.py", line 174, in train
        self.__train_backup__(mode=mode)
      File "/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1614, in __getattr__
        raise AttributeError("'{}' object has no attribute '{}'".format(
    AttributeError: 'GraphModule' object has no attribute '__train_backup__'

    model.train()和 model.eval()位于示例代码中、因此我按原样使用它。

    但它不起作用。 它使用了 model.module.train()、model.module.eval()。

    转换为 onnx 后、将 onnx 导入到 tidl 二进制文件时、我遇到了和以前一样的问题。

    ONNX Model (Proto) File  : ./qatv2_model.onnx  
    TIDL Network File      : /workspace/qatv2_test_AL_PSDK_09_02_09_00_output/qatv2_test_AL_PSDK_09_02_09_00.bin  
    TIDL IO Info File      : /workspace/qatv2_test_AL_PSDK_09_02_09_00_output/qatv2_test_AL_PSDK_09_02_09_00_io_  
    Current ONNX OpSet Version   : 18  
    Unsupported data type for onnxImport
    Unsupported data type for onnxImport
    Unsupported data type for onnxImport
    Could not find const or initializer of layer /conv7/DequantizeLinear_2 !!!
    Could not find const or initializer of layer /conv7/DequantizeLinear !!!
    Could not find const or initializer of layer /conv7/DequantizeLinear !!!
     ONNX operator Identity is not suported now..  By passing
     ONNX operator Identity is not suported now..  By passing
    Could not find const or initializer of layer /QuantizeLinear_1 !!!
     ONNX operator Identity is not suported now..  By passing
    Could not find const or initializer of layer /pad/Pad !!!
    
    Unsupported Onnx import data type : 0 
    Segmentation fault (core dumped)

    PSDK 为09_02_09_00、输入文件为-128~128 μ s 归一化图像二进制文件。 我附上导入文本。 (还尝试使用10_00_04_00、得到相同的结果)

    请告诉我如何修复它。

    modelType          = 2
    inputNetFile       = "./qatv2_model.onnx"
    outputNetFile      = "/workspace/qatv2_test_AL_PSDK_10_00_04_00_output/qatv2_test_AL_PSDK_10_00_04_00.bin"
    outputParamsFile   = "/workspace/qatv2_test_AL_PSDK_10_00_04_00_output/qatv2_test_AL_PSDK_10_00_04_00_io_"
    numParamBits       = 8
    numFeatureBits     = 8
    quantizationStyle  = 6
    inQuantFactor      = 1
    inElementType      = 1
    inWidth            = 640
    inHeight           = 384
    inNumChannels      = 3
    calibrationOption = 0
    biasCalibrationIterations = 0
    
    # Configuration Parameters for Input Pre-Processing
    inDataNorm  = 0
    inDataFormat = 1
    
    # Configuration Parameters for path of different modules
    tidlStatsTool = "/workspace/edgeai-tidl-tools_AL_10_00_04_00/tidl_tools/PC_dsp_test_dl_algo.out"
    perfSimTool = "/workspace/edgeai-tidl-tools_AL_10_00_04_00/tidl_tools/ti_cnnperfsim.out"
    graphVizTool = "/workspace/edgeai-tidl-tools_AL_10_00_04_00/tidl_tools/tidl_graphVisualiser.out"
    modelDumpTool = "/workspace/edgeai-tidl-tools_AL_10_00_04_00/tidl_tools/tidl_dump.out"
    
    # Configuration Parameters related to TIDL-RT inference for quantization
    inData             = "/workspace/tools/tidl_import_tools/tidl_import_data/clear_0.bin"
    inFileFormat = 1
    numFrames = 1
    postProcType = 0
    
    # Configuration Parameters for Graph Compiler
    perfSimConfig = "/workspace/edgeai-tidl-tools_AL_10_00_04_00/tidl_tools/device_config.cfg"
    
    debugTraceLevel = 2
    advanced_options:prequantized_model = 1

    此致!
    hyuntak

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好!

    对于问题的第一部分、我想代码中有一个小错误、您只需更改 edgeai-modeloptimization/torchmodelopt/edgeai_torchmodelopt/xquantization /v2/quant_base.py/quant_fx_base.py 文件内的第46行、其中 add_methods 变量可以设置为 True。 那么您将能够按原样使用 model.train()和 model.eval()功能。

    self.module = quant_fx_func.init(model, *args, add_methods=True, **kwargs)

    对于问题的第二部分、首先确保您在尝试使用之前已使用 onnx-simplifier 软件包简化了导出的网络、之后、第36行的标志似乎已关闭、您可以使用以下标志。

    preQuantizedModel = 1

    我猜您要将量化输入传递到网络、如果要将浮点输入传递到网络、则可以设置以下标志。

    addDataConvertToNet = 3