onnx問題匯總


 

 

1.onnx在windows下使用

   時間節點:2021年7月

  基於Anaconda ,打開並激活pytorch環境,然后按順序安裝。

conda install -c conda-forge numpy protobuf==3.16.0 libprotobuf=3.16.0
conda install -c conda-forge onnx

  之后安裝onnxruntime或者onnxruntime-gpu

pip install onnxruntime
或者
pip install onnxruntime-gpu

  倆者差別是運行環境不同。根據自己運行onnx模式環境,適合就好。

 

 

2.onnx 不支持roll 操作符。 

    問題時間節點:2021年7月

 嘗試將swin Transformer模型從pytorch導給tensorflow。 運行遇到操作符不支持問題。

警告信息

Exporting the operator roll to ONNX opset version 9 is not supported. Please open a bug to request ONNX export support for the missing operator. 

 剛接觸onnx,不懂。問朋友之后,可能是onnx支持的操作集問題

torch.onnx.export(……
                  opset_version=13)

  

還是報錯。此處截取onnx的操作集適配機制,方便后來者理解該參數

_default_onnx_opset_version = 9
_onnx_main_opset = 13
_onnx_stable_opsets = [7, 8, 9, 10, 11, 12]
_export_onnx_opset_version = _default_onnx_opset_version


def _set_opset_version(opset_version):
    global _export_onnx_opset_version
    if opset_version == _default_onnx_opset_version:
        _export_onnx_opset_version = opset_version
        return
    if opset_version in _onnx_stable_opsets + [_onnx_main_opset]:
        _export_onnx_opset_version = opset_version
        return
    raise ValueError("Unsupported ONNX opset version: " + str(opset_version))

感興趣的可以搜索pytorch源碼。 默認=“9” 。 10,11,12 可以理解為基於9的代碼拓展。

經朋友提醒,在pytorch的開源代碼搜索到onnx

https://github.com/pytorch/pytorch/blob/master/test/onnx/test_pytorch_onnx_onnxruntime.py

  def test_roll(self):
        class M(torch.nn.Module):
            def __init__(self, shifts, dims):
                super(M, self).__init__()
                self.shifts = shifts
                self.dims = dims

            def forward(self, x):
                return torch.roll(x, self.shifts, self.dims)

  

  onnxruntime已經包含roll的測試。理論上應該支持該操作。嘗試在pytorch的官網搜索 roll關鍵詞

官方roll源碼

import torch.onnx.symbolic_helper as sym_help
from torch.onnx.symbolic_helper import parse_args, _parse_arg, _unimplemented

@parse_args('v', 'is', 'is')
def roll(g, self, shifts, dims):
    assert len(shifts) == len(dims)

    result = self
    for i in range(len(shifts)):
        shapes = []
        shape = sym_help._slice_helper(g,
                                       result,
                                       axes=[dims[i]],
                                       starts=[-shifts[i]],
                                       ends=[maxsize])
        shapes.append(shape)
        shape = sym_help._slice_helper(g,
                                       result,
                                       axes=[dims[i]],
                                       starts=[0],
                                       ends=[-shifts[i]])
        shapes.append(shape)
        result = g.op("Concat", *shapes, axis_i=dims[i])

    return result

 

     

 

      顯示torch.onnx的操作集9 已經包含roll操作。
      搜索最新發布版本pytorch。
     1)到pytorch官網搜索最近12天發布的pyotorch版本 。頁面顯示只到pytorch1.9.0。

      2)嘗試到pypi搜索已經發布打包好的pytorch 。 發現pytorch似乎沒有類似tensorflow的tf_nightly預覽版本 。

     最后,剩下直接修改調用roll的代碼,更改為支持onnx的算子。或者編譯最新版本pytorch源碼。

      -------------------------------------------------------------------------------------------------------

     后面進一步搜索發現如下信息

4月20號提出roll轉換問題 開發者說到已經記錄會加入支持 。 

roll的onnx代碼提交 顯示5月17號議題經提交支持onnx導出“roll”。pytorch1.9是6月份發布。 所以,感覺基於1.9 還會顯示不支持,有點滯后。

   后面再試試

torch.onnx.export的其余參數。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM