Stop ct.convert from rewriting the traced model's TorchScript graph - #2859
Open
Om-singhaI wants to merge 1 commit into
Open
Om-singhaI wants to merge 1 commit into
Om-singhaI wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2215
ct.convertchanges the traced model you pass in, and for some models that means you can't save and load it afterwards:_expand_and_optimize_irtakestorchscript.forward.graphand runs the inline, DCE and_jit_pass_canonicalize_graph_fuser_opspasses on it in place. That's the user's own graph, so their module ends up holding aprim::ConstantChunknode that TorchScript can't load back.The repro uses
convert_to="milinternal"so it runs without the compiled backend, but the default conversion goes through the same frontend code.The fix runs those passes on
torchscript.forward.graph.copy()instead. The converter only reads the graph this function returns, so conversion itself doesn't change. I checked traced and scripted models (chunk, nested modules with BatchNorm, a shared parameter, fork plus an if block), and the lowered graph, input names, params and buffers all come out identical to main. The user's graph is now left alone.I added
test_convert_does_not_modify_torchscript_model. On main it fails because the traced graph gains aprim::ConstantChunknode, and with the fix it passes:I ran it on torch 2.8.0. I didn't run the full frontend suite locally.