Try to use torchscript on Dict[str, List[float]]

username: huoge
cooked: <p>Hello everyone. I attempt to use</p>
<p>torch.jit.script on the Dict[str,List[float]], but it doesn’t work。<br>
Has anyone ever done any related work?</p>
<p>by the way, I build the pytorch from source and the torch version is 1.4.0a0+93db2b8</p>
<p>I’d appreciate if anybody can help me! Thanks in advance!</p>
<p>here is the code:</p>
<pre><code class="lang-auto">import torch
from typing import Dict, List
class dictlist(torch.nn.Module):
    item:Dict[str, List[float]]
    hyps:List[Dict[str, List[float]]]
    def __init__(self):
        torch.nn.Module.__init__(self)
        self.item = {'score': [0.0], 'ys': [0, 1]}
        self.hyps = [self.item]
    def forward(self):
        new_item:Dict[str, List[float]] = {'score':[1.0], 'ys': [1,2,3]}
        self.hyps.append(new_item)
model = dictlist()
script_model = torch.jit.script(model)
</code></pre>
<p>and here is the log:</p>
<p><code> RuntimeError: values[i]-&gt;type()-&gt;isSubtypeOf(value_type) INTERNAL ASSERT FAILED at /pytorch/torch/csrc/jit/ir.cpp:1527, please report a bug to PyTorch.  (createDict at pytorch/torch/csrc/jit/ir.cpp:1527)</code></p>

username: zdevito
cooked: <p>The problem here is that [1,2,3] is a list of integers not a list of floats. If it is changed to [1.0, 2.0, 3.0], it should work. I submitted <a href="https://github.com/pytorch/pytorch/pull/31375" rel="nofollow noopener">https://github.com/pytorch/pytorch/pull/31375</a> to make the error message report correctly.</p>

username: huoge
cooked: <p>oh, thanks. I thought it would be converted automatically.<br>
when I change it to a list of floats, it works.</p>

username: Wajahat_Akhtar
cooked: <p>Actually, I am also facing the same issue as above : I am not able to use dictionary. I am using the following code to convert my decoder to Torch script.</p>
<pre><code class="lang-python">import numpy as np
import torch
import torch.nn as nn

from collections import OrderedDict
from layers import *

class DepthDecoder(nn.Module):

def  **init** (self, num_ch_enc, scales=range(4), num_output_channels=1, use_skips=True):
super(DepthDecoder, self). **init** ()

    self.num_output_channels = num_output_channels
    self.use_skips = use_skips
    self.upsample_mode = 'nearest'
    self.scales = scales

    self.num_ch_enc = num_ch_enc
    self.num_ch_dec = np.array([16, 32, 64, 128, 256])

    # decoder
    self.convs = OrderedDict()
    for i in range(4, -1, -1):
        # upconv_0
        num_ch_in = self.num_ch_enc[-1] if i == 4 else self.num_ch_dec[i + 1]
        num_ch_out = self.num_ch_dec[i]
        self.convs[("upconv", i, 0)] = ConvBlock(num_ch_in, num_ch_out)

        # upconv_1
        num_ch_in = self.num_ch_dec[i]
        if self.use_skips and i &gt; 0:
            num_ch_in += self.num_ch_enc[i - 1]
        num_ch_out = self.num_ch_dec[i]
        self.convs[("upconv", i, 1)] = ConvBlock(num_ch_in, num_ch_out)

    for s in self.scales:
        self.convs[("dispconv", s)] = Conv3x3(self.num_ch_dec[s], self.num_output_channels)

    self.decoder = nn.ModuleList(list(self.convs.values()))
    self.sigmoid = nn.Sigmoid()

def forward(self, input_features):
    outputs = {}

    # decoder
    x = input_features[-1]
    for i in range(4, -1, -1):
        x = self.convs[("upconv", i, 0)](x)
        x = [upsample(x)]
        if self.use_skips and i &gt; 0:
            x += [input_features[i - 1]]
        x = torch.cat(x, 1)
        x = self.convs[("upconv", i, 1)](x)
        if i in self.scales:
            outputs[("disp", i)] = self.sigmoid(self.convs[("dispconv", i)](x))

    return outputs

num_enc_channels = np.array([ 64, 64, 128, 256, 512])
depth_decoder = DepthDecoder( num_ch_enc= num_enc_channels , scales=range(4))
traced_script_module_decoder = torch.jit.script(depth_decoder)
traced_script_module_decoder.save(‘new-decoder.pt’)
</code></pre>
<p><strong>Error :</strong></p>
<pre><code class="lang-auto">File “C:\Users\lib\site-packages\torch\jit_recursive.py”, line 259, in create_methods_from_stubs
concrete_type._create_methods(defs, rcbs, defaults)
RuntimeError:
Module ‘DepthDecoder’ has no attribute ‘convs’ ( **This attribute exists on the Python module, but we failed to convert Python type: ‘OrderedDict’ to a TorchScript type** .):
File “C:\Users\networks\depth_decoder.py”, line 55
x = input_features[-1]
for i in range(4, -1, -1):
x = self.convs(“upconv”, i, 0)
~~~~~~~~~~ &lt;— HERE
x = [upsample(x)]
if self.use_skips and i &gt; 0:
</code></pre>
<p><strong>More Specifically I am trying to convert this nn.Module using TorchScript</strong></p>
<p>Link : <a href="https://github.com/nianticlabs/monodepth2/blob/master/networks/depth_decoder.py" rel="nofollow noopener">https://github.com/nianticlabs/monodepth2/blob/master/networks/depth_decoder.py</a></p>