Repository navigation
Conversation
Modified unproxy_arg function to unproxy list elements in place, preserving changes made by callee functions.
This branch has not been deployed
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 #176.
unproxy_argrebuilt every list argument as a new list. Callees that write results back into a list argument then wrote into the copy, and the caller never saw them.torch.optim.SGD.stepdoes exactly this: it passesmomentum_buffer_listto the functionalsgd, which stores the new buffers in that list, and then reads them back from the same list intooptimizer.state. With the copy, every momentum buffer stayedNone, so momentum was reset on every step.As suggested in the issue, this change unproxies list elements in place and returns the same list object. Tuples are unchanged (they are immutable, so there is nothing to write back).
One behaviour change to be aware of: after a wrapped call, the caller's list holds the unproxied objects instead of the proxies. For the per-step temporary lists that optimizers build this has no effect; a caller that passes a long-lived list of proxied objects would now see those entries unwrapped.
Tests.
tests/test_unproxy_list_inplace.pyadds six tests: list identity (flat and nested), write-back through positional and keyword arguments, tuples, and an end-to-end check that wrapstorch.optim.sgd.sgdwithunproxy_funcand asserts that every parameter has a momentum buffer after twoSGD.step()calls. All six fail onmainand pass with the fix.I ran the full suite with
python -m unittest discover -s tests -t .(torch 2.13.0+cu126, Python 3.11, WSL Ubuntu 24.04). Onmain: 369 tests, 10 failures and 3 errors. With this change: 369 tests, 4 failures and 3 errors. The 7 remaining failures and errors are identical on both and unrelated to this change; they look environment-specific:test_type_nulls_extensions_and_custom_comparison,tests.test_display_names,tests.test_violation_summary,test_empty_native_frame_and_extreme_identity_keys,test_inject_eval_loop,test_repeated_query_avoids_type_isin_rescan,test_strict_bounds_ties_reverse_and_empty.