Skip to content

Unproxy list arguments in place so callees can write results back (fixes #176) - #177

Open
hersh11 wants to merge 3 commits into
OrderLab:mainfrom
hersh11:fix-unproxy-list-inplace
Open

hersh11 wants to merge 3 commits into
OrderLab:mainfrom
hersh11:fix-unproxy-list-inplace

Conversation

@hersh11

@hersh11 hersh11 commented Nov 25, 2026

Copy link
Copy Markdown

Fixes #176.

unproxy_arg rebuilt 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.step does exactly this: it passes momentum_buffer_list to the functional sgd, which stores the new buffers in that list, and then reads them back from the same list into optimizer.state. With the copy, every momentum buffer stayed None, 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.py adds six tests: list identity (flat and nested), write-back through positional and keyword arguments, tuples, and an end-to-end check that wraps torch.optim.sgd.sgd with unproxy_func and asserts that every parameter has a momentum buffer after two SGD.step() calls. All six fail on main and 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). On main: 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.

Modified unproxy_arg function to unproxy list elements in place, preserving changes made by callee functions.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Model tracking (proxy, and subclass in 0.2.0) silently turns off SGD momentum: unproxy_arg copies momentum_buffer_list

1 participant