[TorchScript] Better support for non-nn.Module classes defined in different file #125950
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.
Stack from ghstack (oldest at bottom):
Motivating Issue:
We had this example:
This would fail to script:
Fix details:
In TorchScript, we use "resolution callbacks" - python callables that will map a string to an object. In this case, the resolution callback was unable to identify that
sqrt
was actuallymath.sqrt
.We have multiple types of resolution callbacks: two are
createResolutionCallbackFromFrame
andcreateResolutionCallbackFromClosure
.In this case, we were using
createResolutionCallbackFromFrame
. But this doesn't work ifjit.script
is called from a different file than the class definition.Fix:
createResolutionCallbackFromClosure
for each of the methods on a class (which is similar to what we do for an nn.Module). Pass this into the C++ function so that we can use the individual RCBs for each of the methods. If a method doesn't have an RCB for some reason, then fallback to the old RCB method where we look at the current frame.Differential Revision: D57224315