Using TorchTRT-FX backend on C++

daniellevi: <p>I read about the torch-trt package, the ability to convert a torch model to torchscript and then running it on C++. There are examples and I understand it.</p>
<p>Anyway, we have the other option, to trace the model using torch_tensorrt.fx, and then saving it to the disk. The .pt file contains the whole sub-graphs which are TRTModules, or GraphModule.<br>
Can I load this .pt(or any other file type) to the torch_tensorrt C++ API, and use it there?<br>
The torchscript doesn’t provide an API for splitting the model to sub-graphs and so on. So I want to use the fx flexibility for my runtime environment(on C++)</p>
<p>The package should work on C++, but the fx(as I understood) is a python only tool. And I can’t find an example for loading the .pt which created by torch_tensorrt.fx.</p>

ptrblck: <p>CC <a class="mention" href="/u/narendasan">@narendasan</a> as the expert on TorchTRT</p>

narendasan: <p><a class="mention" href="/u/daniellevi">@daniellevi</a> With the new version of Torch-TensorRT (1.3.0) you can use the FX frontend to compile your module to target the torchscript/tensorrt runtime using <code>use_experimental_fx_rt</code>. It produces a representation which can be passed to torch.jit.trace which can be saved and loaded in C++ and run in the same way as a module compiled with the TorchScript frontend.</p>
<p>For example:</p>
<pre><code class="lang-python">model_fx = model_fx.cuda()
inputs_fx = [i.cuda() for i in inputs_fx]
trt_fx_module_f16 = torch_tensorrt.compile(
    model_fx,
    ir="fx",
    inputs=inputs_fx,
    enabled_precisions={torch.float16},
    use_experimental_fx_rt=True,
    explicit_batch_dimension=True
)

# Save model using torch.save 

torch.save(trt_fx_module_f16, "trt.pt")
reload_trt_mod = torch.load("trt.pt")

# Trace and save the FX module in TorchScript
scripted_fx_module = torch.jit.trace(trt_fx_module_f16, example_inputs=inputs_fx)
scripted_fx_module.save("/tmp/scripted_fx_module.ts")
scripted_fx_module = torch.jit.load("/tmp/scripted_fx_module.ts") #This can also be loaded in C++
</code></pre>

daniellevi: <aside class="quote no-group" data-username="narendasan" data-post="3" data-topic="170639">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/narendasan/48/44133_2.png" class="avatar"> narendasan:</div>
<blockquote>
<p><code>use_experimental_fx_rt</code></p>
</blockquote>
</aside>
<p>I will try it.<br>
Thank you!</p>

daniellevi: <p>Hi<br>
I tried what you wrote, but the loading fails.<br>
The way I’m trying to load the model is by using this line-<br>
torch::jit::Module m_module = torch::jit::load(m_modelFile)</p>
<p>Is it correct? or is there other way to load it?<br>
Maybe you know about an example of a script which saves a model after torch.fx processing, and loads it into the C++ API?</p>
<p>Thanks</p>

narendasan: <p>We dont have a specific example for this but this is correct. Once the fx traced torchscript is saved to disk following the workflow above, the process is the same as other torchscript so you can follow this tutorial: <a href="https://github.com/pytorch/TensorRT/blob/main/notebooks/Resnet50-CPP.ipynb" class="inline-onebox" rel="noopener nofollow ugc">TensorRT/Resnet50-CPP.ipynb at main · pytorch/TensorRT · GitHub</a></p>