# RuntimeError: undefined value, when covert Pytorch to Torchscript

**URL:** <https://discuss.pytorch.org/t/runtimeerror-undefined-value-when-covert-pytorch-to-torchscript/124589>\
**Category:** jit\
**Created:** [June 20, 2021, 11:07pm UTC](https://discuss.pytorch.org/t/runtimeerror-undefined-value-when-covert-pytorch-to-torchscript/124589 "2021-06-20T23:07:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen\_K](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stephen_k/32/39492_2.png) [@Stephen\_K](https://discuss.pytorch.org/u/Stephen_K)\
**Post date:** [June 20, 2021, 11:07pm UTC](https://discuss.pytorch.org/t/runtimeerror-undefined-value-when-covert-pytorch-to-torchscript/124589/1 "2021-06-20T23:07:16Z")

</div>

RuntimeError:  
undefined value hidden\_t:  
File “/workspace/OpenNMT-py-1.2.0\_torchscript\_testfuncwork/onmt/encoders/image\_encoder.py”, line 143  
out = torch.cat(all\_outputs, 0)

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

```

when I tried to convert Pytorch to Torchscript

full error message :  
Traceback (most recent call last):  
File “translate.py”, line 22, in   
print(“my\_module(imgr) :”,my\_module(imgr))  
File “/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py”, line 889, in \_call\_impl  
result = self.forward(\*input, \*\*kwargs)  
File “translate.py”, line 18, in forward  
result = main(imgr)  
File “/workspace/OpenNMT-py-1.2.0\_torchscript\_testfuncwork/onmt/bin/translate.py”, line 74, in main  
return translate(opt, imgr)  
File “/workspace/OpenNMT-py-1.2.0\_torchscript\_testfuncwork/onmt/bin/translate.py”, line 18, in translate  
translator = build\_translator(opt, logger=logger, report\_score=True)  
File “/workspace/OpenNMT-py-1.2.0\_torchscript\_testfuncwork/onmt/translate/translator.py”, line 29, in build\_translator  
fields, model, model\_opt = load\_test\_model(opt)  
File “/workspace/OpenNMT-py-1.2.0\_torchscript\_testfuncwork/onmt/model\_builder.py”, 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/model\_builder.py”, line 184, in build\_base\_model  
sm = torch.jit.script(model)  
File “/opt/conda/lib/python3.8/site-packages/torch/jit/\_script.py”, line 942, in script  
return torch.jit.\_recursive.create\_script\_module(  
File “/opt/conda/lib/python3.8/site-packages/torch/jit/\_recursive.py”, 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/\_recursive.py”, 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/\_script.py”, line 391, in \_construct  
init\_fn(script\_module)  
File “/opt/conda/lib/python3.8/site-packages/torch/jit/\_recursive.py”, 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/\_recursive.py”, 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/\_recursive.py”, 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)  
RuntimeError:  
undefined value hidden\_t:  
File “/workspace/OpenNMT-py-1.2.0\_torchscript\_testfuncwork/onmt/encoders/image\_encoder.py”, line 143  
out = torch.cat(all\_outputs, 0)

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

```

what I tried to convert :  
model.py

“”" 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.

```
Args:
  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.

    Args:
        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.

    Returns:
        (FloatTensor, dict[str, FloatTensor]):

        * decoder output ``(tgt_len, batch, hidden)``
        * dictionary attention dists of ``(tgt_len, batch, src_len)``
    """
    print("model.py")
    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,
                                  memory_lengths=lengths,
                                  with_align=with_align)
    return dec_out, attns

def update_dropout(self, dropout):
    self.encoder.update_dropout(dropout)
    self.decoder.update_dropout(dropout)

```

where I got Undefined value error  
image\_encoder.py

 ![image](https://discuss.pytorch.org/uploads/default/original/3X/1/d/1d8e5dcc300eeef16e18c0135a4c8136bab11b8e.png)

Can anyone help?..

---

<div class="post-metadata">

**Author:** ![AlphaBetaGamma96](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@AlphaBetaGamma96](https://discuss.pytorch.org/u/AlphaBetaGamma96)\
**Post date:** [June 21, 2021, 12:35am UTC](https://discuss.pytorch.org/t/runtimeerror-undefined-value-when-covert-pytorch-to-torchscript/124589/2 "2021-06-21T00:35:15Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Stephen\_K](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stephen_k/32/39492_2.png) [@Stephen\_K](https://discuss.pytorch.org/u/Stephen_K)\
**Post date:** [June 21, 2021, 12:46am UTC](https://discuss.pytorch.org/t/runtimeerror-undefined-value-when-covert-pytorch-to-torchscript/124589/3 "2021-06-21T00:46:52Z")

</div>

self.rnn was defined at image\_encoder.py **init**

```
def __init__ (self, num_layers, bidirectional, rnn_size, dropout,
             image_chanel_size=3):
    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),
                       num_layers=num_layers,
                       dropout=dropout,
                       bidirectional=bidirectional)
    self.pos_lut = nn.Embedding(1000, src_size)
```

---

<div class="post-metadata">

**Author:** ![AlphaBetaGamma96](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@AlphaBetaGamma96](https://discuss.pytorch.org/u/AlphaBetaGamma96)\
**Post date:** [June 21, 2021, 12:54am UTC](https://discuss.pytorch.org/t/runtimeerror-undefined-value-when-covert-pytorch-to-torchscript/124589/4 "2021-06-21T00:54:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Stephen\_K](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stephen_k/32/39492_2.png) [@Stephen\_K](https://discuss.pytorch.org/u/Stephen_K)\
**Post date:** [June 21, 2021, 5:40am UTC](https://discuss.pytorch.org/t/runtimeerror-undefined-value-when-covert-pytorch-to-torchscript/124589/5 "2021-06-21T05:40:33Z")

</div>

[https://pytorch.org/docs/stable/generated/torch.nn.LSTM.html](https://pytorch.org/docs/stable/generated/torch.nn.LSTM.html)

yes, I tried print hidden\_t.
