Instead of that, you could also look into

Inject your code into linecache under different module names like IPython does. This isn’t exactly documented, but clean as far as the JIT is concerned.
Get the AST using git_jit_def, meddle with the AST, and use _jit_script_compile to compile yourself (you probably need the resolver too), so you basically mimic torch.jit.script except the caching. This is fairly invasive into JIT internals, but at least it is straightforward to your cause.

Best regards
Thomas