Questions about compiling and exporting a model to TensorRT

pierisk: <p>I am trying understand the differences between the various ways to compile/export a PyTorch model.</p>
<p><strong>Background:</strong> My end goal is to export and use my detectron2 trained model as a TensorRT .engine file in NVIDIA frameworks, which got me into reading about TorchScript, torch.fx, torch.export, torch.compile, TorchDynamo with different backends e.g. TorchInductor and torch_tensorrt.</p>
<p>Since the model (mask2former with SWIN transformer backend) and the detectron2 framework in general have complex code constructs and dynamic control flow, I’ve ruled out torch.fx and all tracing methods (please correct me if my thinking is wrong).</p>
<p>I’m now left with these questions:</p>
<ol>
<li>Since my code has conditionals and dynamic control flow, is torch.jit.script the only “easy” option due to graph breaks and wanting to use it outside Python runtime?</li>
<li>Is torch.compile with the PyTorch model as input suitable for my goal (eventually serializing to a TensorRT engine file and using it outside Python), or should I first convert the model to TorchScript?</li>
<li>After compiling the model with any of the above methods, my understanding is I still need to use torch_tensorrt to serialize the model. Is there another way? I’ve stumbled upon the <a href="https://github.com/NVIDIA-AI-IOT/torch2trt" rel="noopener nofollow ugc">torch2rt</a> project by NVIDIA but I’m not certain if it’s a better option.</li>
</ol>
<p>Sorry for the long post and appreciate any help!</p>

lime: <p>Pieri’s mate here,</p>
<p>We made some progress,</p>
<ol>
<li>The other more “beta” option right now would be to follow any of those links <a href="https://pytorch.org/docs/stable/onnx_dynamo.html" rel="noopener nofollow ugc">1</a> <a href="https://pytorch.org/tutorials/beginner/onnx/export_simple_model_to_onnx_tutorial.html" rel="noopener nofollow ugc">2</a> <a href="https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html" rel="noopener nofollow ugc">3</a> that compiles to FX IR (with nodes and all) together with Dynamo, dynamic tracing and all the arcane magic spells.</li>
<li>Even though we have successfully compiled the model with the inductor backbone we’re still facing issues on possible paths to serialize optimized module type output by inductor. I really doubt there’s any paths to serialization from this IR.</li>
<li>NVIDIA’s efforts to this have been mainly for <a href="https://github.com/NVIDIA-AI-IOT/jetbot" rel="noopener nofollow ugc">JetBot</a> and using torch_tensorrt backend with dynamo seems like a plausible compromise but we’d still face the serialization issues (i.e. get a trt engine out of this).</li>
</ol>
<p>We’d really appreciate some feedback in that and the general usability of Dynamo right now. I really want to embrace this project but it feels like it’s not there yet and we’re left with good old scripting <img src="https://discuss.pytorch.org/images/emoji/apple/melting_face.png?v=12" title=":melting_face:" class="emoji" alt=":melting_face:" loading="lazy" width="20" height="20"></p>
<p>I am shamelessly tagging <a class="mention" href="/u/ezyang">@ezyang</a> on of the major celebs of this project <img src="https://discuss.pytorch.org/images/emoji/apple/upside_down_face.png?v=12" title=":upside_down_face:" class="emoji" alt=":upside_down_face:" loading="lazy" width="20" height="20"></p>