How to deploy torchscript module when original data structure class is not avaliable

username: BobChen
cooked: <p>Take detectron2 as an example. I have transformed a model from detectron2 with torch.jit.script to torchscript, the input of which is annotated with <code>detectron2.structures.box</code>. Because there is no detectron2 python package in the deployment environment of the torchscript module , I define another input data class <code>x.box</code> which has totally the same codes as <code>detectron2.structures.box</code>. But when this new input class is sent to the torchscript module, an error happened:<br>
<code>RuntimeError: classType INTERNAL ASSERT FAILED at /pytorch/torch/csrc/jit/python/pybind_utils.h:742, please report a bug to PyTorch.</code></p>
<p>Is this mean that I must send the same input data class(detectron2.structures.box) to the model? Or, is there any ways to make it work?</p>
<p>Thanks a lot:)</p>

username: BobChen
cooked: <p>I have figured out the reason. It is because the data class of the output of torchscript is not supported in the deployment environment.</p>
<p>So is there any way to register a new data class which consist of the same codes of the original one in torchscript in the deployment environment and construct links between them?</p>

username: driazati
cooked: <p>Are you able to post a repro for the internal assert failure?</p>
<p>It sounds like you’re asking for <a href="https://en.wikipedia.org/wiki/Structural_type_system">structural typing</a> instead of <a href="https://en.wikipedia.org/wiki/Nominal_type_system">nominal typing</a>. TorchScript is nominally typed, so we don’t have a blessed way to do exactly what you’re asking. (It does look like some version of structural typing is supported, but that is <a href="https://github.com/pytorch/pytorch/issues/35788">likely an accident</a>).</p>
<p><code>detectron2.structures.box</code> should be included with the serialized code of your TorchScript model, so you should be able to reconstruct it inside the JIT (though the API to do this may not exist/be pretty). Once we fix the above bug we can probably figure out some hack to make this still work.</p>

username: BobChen
cooked: <p>The codes are still being modified, but I can illustrate this problem with some sample codes：</p>
<p>In the development environment where torchscript is exported, we have:</p>
<pre><code class="lang-auto">from detectron2.structures import Box

class MyModule(nn.Module) -&gt; Box:
    some_of_codes
</code></pre>
<p>then we export torchscript:</p>
<pre><code class="lang-auto">model = MyModule()
torch.jit.script(model).save('scripted_model.pt')
</code></pre>
<p>In the deployment environment, we have:</p>
<pre><code class="lang-auto">model = torch.jit.load('scripted_model.pt')
input = some_data_of_model
output = model(input)
</code></pre>
<p>Since the data type ‘detectron2.structures.Box’ is not available here, the RuntimeError mentioned above will appear.</p>
<p>I can see that the ‘detectron2.structures.Box’ class is available in the serialized code of my TorchScript, but it seems that I can not use it to construct an instance which can receive the result from the output of torchscript.</p>