question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tracing to torchscript

See original GitHub issue

Has anyone been able to successfully convert the generator model to torchscript?

I receive a bizarre error: while tracing works

zero = torch.full((1, 80, 10), -11.52).cuda()
with open("hifi-gan/config.json") as f:
    data = f.read()
h = env.AttrDict(json.loads(data))
vocoder = models.Generator(h).cuda()
vocoder.load_state_dict(
    torch.load("hifi-gan/pretrained_universal/g_02500000")["generator"]
)
vocoder.remove_weight_norm()
vocoder.eval()
with torch.no_grad():
    traced_vocoder = torch.jit.trace(vocoder, zero)
    torch.jit.save(traced_vocoder, "vocoder.pth")

Trying to then load the model gives a weird error:

traced_vocoder = torch.jit.load("vocoder.pth")
/opt/conda/lib/python3.8/site-packages/torch/jit/_serialization.py in load(f, map_location, _extra_files)
    159     cu = torch._C.CompilationUnit()
    160     if isinstance(f, str) or isinstance(f, pathlib.Path):
--> 161         cpp_module = torch._C.import_ir_module(cu, f, map_location, _extra_files)
    162     else:
    163         cpp_module = torch._C.import_ir_module_from_buffer(

RuntimeError: Found character '45' in string, strings must be qualified Python identifiers

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:14

github_iconTop GitHub Comments

3reactions
ctlaltdefeatcommented, Feb 3, 2021

Have been working with TorchScript on another project, and stumbled across this issue with the exact same error message.

For me the issue was that I importing modules I was tracing with a dashes (character 45) in the paths. Maybe the dash in hifi-gan is the problem for you too? I don’t know why this information is incorporated into the TorchScript binary file, but changing the path to underscores fixed it the error when loading in C++ for me.

Thanks! I was indeed using dashes and loading using importlib, and when I instead just added those paths to the sys path the error goes away. Does seem like a weird Torchscript bug.

3reactions
ErenBalatkancommented, Jan 26, 2021

I were able to convert to torchscript via jit.script after some slight modifications, can share the repo tonight

Read more comments on GitHub >

github_iconTop Results From Across the Web

TorchScript — PyTorch 1.13 documentation
In many cases either tracing or scripting is an easier approach for converting a model to TorchScript. Tracing and scripting can be composed...
Read more >
TorchScript: Tracing vs. Scripting - Yuxin's Blog
Export: refers to the process that turns a model written in eager-mode Python code into a graph that describes the computation. · Tracing:...
Read more >
Mastering TorchScript: Tracing vs Scripting, Device Pinning ...
Tracing. When using torch.jit.trace you'll provide your model and sample input as arguments. The input will be fed through the model as ...
Read more >
How to convert your PyTorch model to TorchScript | djl
There are two ways to convert your model to TorchScript: tracing and scripting. We will only demonstrate the first one, tracing, but you...
Read more >
Export to TorchScript - Hugging Face
TorchScript is a way to create serializable and optimizable models from PyTorch code. There are two PyTorch modules, JIT and TRACE, that allow...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found