pytorch模型转onnx Exporting the operator _thnn_fused_lstm_cell to ONNX opset version 9 is not supported

11 篇文章 0 订阅

转换onnx模型的时候遇到_thnn_fused_lstm_cell的报错,刚开始的报错是提示Keyerror:_thnn_fused_lstm_cell,然后在https://github.com/pytorch/pytorch/issues/25533看到有人说是pytorch和CUDA版本太低的原因以及当使用CPU就不会报错,然后我首先尝试将pytorch升级到最新,CUDA也升级到10.2,最终还是报错Exporting the operator _thnn_fused_lstm_cell to ONNX opset version 9 is not supported. Please open a bug to request ONNX export support for the missing operator.

之后我定位到这个错误的产生是转换onnx模型时使用了torch.nn.LSTMCell()这个方法,如果使用torch.nn.LSTM()就不会报错,但是如果在CPU环境下将torch.nn.LSTMCell()转换成onnx则不会报错。总的来说就是torch.nn.LSTMCell()方法目前不支持在GPU环境下转换成onnx模型。目前已经给pytorch的GitHub提交issue,等待官方出解决方法。

下面是报错复现的列子:

import torch
import torch.nn as nn

class StackedLSTM(nn.Module):
    def __init__(self, num_layers, input_size, rnn_size, dropout):
        super(StackedLSTM, self).__init__()
        self.dropout = nn.Dropout(dropout)
        self.num_layers = num_layers
        self.layers = nn.ModuleList()

        for _ in range(num_layers):
            self.layers.append(nn.LSTMCell(input_size, rnn_size))
            input_size = rnn_size

    def forward(self, input_feed, hidden):
        h_0, c_0 = hidden
        h_1, c_1 = [], []
        for i, layer in enumerate(self.layers):
            h_1_i, c_1_i = layer(input_feed, (h_0[i], c_0[i]))
            input_feed = h_1_i
            if i + 1 != self.num_layers:
                input_feed = self.dropout(input_feed)
            h_1 += [h_1_i]
            c_1 += [c_1_i]
        h_1 = torch.stack(h_1)
        c_1 = torch.stack(c_1)
        return input_feed, (h_1, c_1)

#下面是在CPU环境下转换onnx模型,可以成功转换
lstm = StackedLSTM(2, 580, 500, 0.3)  #实例化StackedLSTM
h_t = torch.randn(2, 10, 500)  #隐状态h
h_c = torch.randn(2, 10, 500)  #隐状态c
hidden = (h_t, h_c)
a = torch.randn(10, 580)  #输入
torch.onnx.export(lstm, (a, hidden), f='LSTM.onnx', input_names=['input', 'hidden'], output_names=['output', 'h_t', 'h_c'])  #转换onnx模型

下面是在GPU环境下转换onnx模型,报错!
lstm = StackedLSTM(2, 580, 500, 0.3).cuda()  #实例化StackedLSTM
h_t = torch.randn(2, 10, 500).cuda()  #隐状态h
h_c = torch.randn(2, 10, 500).cuda()  #隐状态c
hidden = (h_t, h_c)
a = torch.randn(10, 580).cuda()  #输入
torch.onnx.export(lstm, (a, hidden), f='LSTM.onnx', input_names=['input', 'hidden'], output_names=['output', 'h_t', 'h_c'])  #转换onnx模型

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值