RuntimeError: undefined value, when covert Pytorch to Torchscript

undefined value hidden_t:
File “/workspace/OpenNMT-py-1.2.0_torchscript_testfuncwork/onmt/encoders/”, line 143
out =, 0)

    return hidden_t, out, lengths
           ~~~~~~~~ <--- HERE

when I tried to convert Pytorch to Torchscript

full error message :
Traceback (most recent call last):
File “”, line 22, in
print(“my_module(imgr) :”,my_module(imgr))
File “/opt/conda/lib/python3.8/site-packages/torch/nn/modules/”, line 889, in _call_impl
result = self.forward(*input, **kwargs)
File “”, line 18, in forward
result = main(imgr)
File “/workspace/OpenNMT-py-1.2.0_torchscript_testfuncwork/onmt/bin/”, line 74, in main
return translate(opt, imgr)
File “/workspace/OpenNMT-py-1.2.0_torchscript_testfuncwork/onmt/bin/”, line 18, in translate
translator = build_translator(opt, logger=logger, report_score=True)
File “/workspace/OpenNMT-py-1.2.0_torchscript_testfuncwork/onmt/translate/”, line 29, in build_translator
fields, model, model_opt = load_test_model(opt)
File “/workspace/OpenNMT-py-1.2.0_torchscript_testfuncwork/onmt/”, line 112, in load_test_model
model = build_base_model(model_opt, fields, use_gpu(opt), checkpoint,
File “/workspace/OpenNMT-py-1.2.0_torchscript_testfuncwork/onmt/”, line 184, in build_base_model
sm = torch.jit.script(model)
File “/opt/conda/lib/python3.8/site-packages/torch/jit/”, line 942, in script
return torch.jit._recursive.create_script_module(
File “/opt/conda/lib/python3.8/site-packages/torch/jit/”, line 391, in create_script_module
return create_script_module_impl(nn_module, concrete_type, stubs_fn)
File “/opt/conda/lib/python3.8/site-packages/torch/jit/”, line 448, in create_script_module_impl
script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
File “/opt/conda/lib/python3.8/site-packages/torch/jit/”, line 391, in _construct
File “/opt/conda/lib/python3.8/site-packages/torch/jit/”, line 428, in init_fn
scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)
File “/opt/conda/lib/python3.8/site-packages/torch/jit/”, line 452, in create_script_module_impl
create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
File “/opt/conda/lib/python3.8/site-packages/torch/jit/”, line 335, in create_methods_and_properties_from_stubs
concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
undefined value hidden_t:
File “/workspace/OpenNMT-py-1.2.0_torchscript_testfuncwork/onmt/encoders/”, line 143
out =, 0)

    return hidden_t, out, lengths
           ~~~~~~~~ <--- HERE

what I tried to convert :

“”" Onmt NMT Model base class definition “”"
import torch.nn as nn

#import os

class NMTModel(nn.Module):
full_path = os.path.realpath(file)
path, filename = os.path.split(full_path)
print(path + ’ → ’ + filename + " NMTModel" + “\n”)
Core trainable object in OpenNMT. Implements a trainable interface
for a simple, generic encoder + decoder model.

  encoder (onmt.encoders.EncoderBase): an encoder object
  decoder (onmt.decoders.DecoderBase): a decoder object

def __init__(self, encoder, decoder):
    super(NMTModel, self).__init__()
    self.encoder = encoder
    self.decoder = decoder

def forward(self, src, tgt, lengths, bptt=False, with_align=False):
    """Forward propagate a `src` and `tgt` pair for training.
    Possible initialized with a beginning decoder state.

        src (Tensor): A source sequence passed to encoder.
            typically for inputs this will be a padded `LongTensor`
            of size ``(len, batch, features)``. However, may be an
            image or other generic input depending on encoder.
        tgt (LongTensor): A target sequence passed to decoder.
            Size ``(tgt_len, batch, features)``.
        lengths(LongTensor): The src lengths, pre-padding ``(batch,)``.
        bptt (Boolean): A flag indicating if truncated bptt is set.
            If reset then init_state
        with_align (Boolean): A flag indicating whether output alignment,
            Only valid for transformer decoder.

        (FloatTensor, dict[str, FloatTensor]):

        * decoder output ``(tgt_len, batch, hidden)``
        * dictionary attention dists of ``(tgt_len, batch, src_len)``
    dec_in = tgt[:-1]  # exclude last target from inputs

    enc_state, memory_bank, lengths = self.encoder(src, lengths)

    if bptt is False:
        self.decoder.init_state(src, memory_bank, enc_state)
    dec_out, attns = self.decoder(dec_in, memory_bank,
    return dec_out, attns

def update_dropout(self, dropout):

where I got Undefined value error

Can anyone help?..

It’d be better to get a dev’s opinion on this, but I think this issue is due to the self.rnn. Whereabouts is self.rnn defined? Is it something that’s inferred? Because pytorch is saying hidden_t isn’t defined for some reason!

self.rnn was defined at init

def __init__(self, num_layers, bidirectional, rnn_size, dropout,
    super(ImageEncoder, self).__init__()
    self.num_layers = num_layers
    self.num_directions = 2 if bidirectional else 1
    self.hidden_size = rnn_size

    self.layer1 = nn.Conv2d(image_chanel_size, 64, kernel_size=(3, 3),
                            padding=(1, 1), stride=(1, 1))
    self.layer2 = nn.Conv2d(64, 128, kernel_size=(3, 3),
                            padding=(1, 1), stride=(1, 1))
    self.layer3 = nn.Conv2d(128, 256, kernel_size=(3, 3),
                            padding=(1, 1), stride=(1, 1))
    self.layer4 = nn.Conv2d(256, 256, kernel_size=(3, 3),
                            padding=(1, 1), stride=(1, 1))
    self.layer5 = nn.Conv2d(256, 512, kernel_size=(3, 3),
                            padding=(1, 1), stride=(1, 1))
    self.layer6 = nn.Conv2d(512, 512, kernel_size=(3, 3),
                            padding=(0, 0), stride=(1, 1))

    self.batch_norm1 = nn.BatchNorm2d(256)
    self.batch_norm2 = nn.BatchNorm2d(512)
    self.batch_norm3 = nn.BatchNorm2d(512)

    src_size = 512
    dropout = dropout[0] if type(dropout) is list else dropout
    self.rnn = nn.LSTM(src_size, int(rnn_size / self.num_directions),
    self.pos_lut = nn.Embedding(1000, src_size)

Strange, does self.rnn return 2 arguments? For some reason, Torchscript can’t find the hidden_t tensor generated by self.rnn.

yes, I tried print hidden_t.