-
Notifications
You must be signed in to change notification settings - Fork 218
Add items() method to nn.Module for state_dict iteration #1556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3309,6 +3309,75 @@ public void TestCustomComponentName() | |||||||||||||||||||||
| Assert.True(sd.ContainsKey("_linear2.weight")); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| [Fact] | ||||||||||||||||||||||
| public void TestModuleItems() | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| var lin = Linear(10, 5, true); | ||||||||||||||||||||||
| var sd = lin.state_dict(); | ||||||||||||||||||||||
| var items = new List<(string, Tensor)>(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| using (var enumerator = lin.items()) { | ||||||||||||||||||||||
| while (enumerator.MoveNext()) { | ||||||||||||||||||||||
| items.Add(enumerator.Current); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // items() should return the same entries as state_dict() | ||||||||||||||||||||||
| Assert.Equal(sd.Count, items.Count); | ||||||||||||||||||||||
| foreach (var (name, value) in items) { | ||||||||||||||||||||||
| Assert.True(sd.ContainsKey(name)); | ||||||||||||||||||||||
| Assert.Equal(sd[name].shape, value.shape); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+3312
to
+3331
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| [Fact] | ||||||||||||||||||||||
| public void TestModuleItemsWithSubmodules() | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| var seq = Sequential( | ||||||||||||||||||||||
| ("lin1", Linear(10, 5)), | ||||||||||||||||||||||
| ("lin2", Linear(5, 2))); | ||||||||||||||||||||||
| var sd = seq.state_dict(); | ||||||||||||||||||||||
| var items = new List<(string, Tensor)>(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| using (var enumerator = seq.items()) { | ||||||||||||||||||||||
| while (enumerator.MoveNext()) { | ||||||||||||||||||||||
| items.Add(enumerator.Current); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Assert.Equal(sd.Count, items.Count); | ||||||||||||||||||||||
| Assert.Contains(items, i => i.Item1 == "lin1.weight"); | ||||||||||||||||||||||
| Assert.Contains(items, i => i.Item1 == "lin2.weight"); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| [Fact] | ||||||||||||||||||||||
| public void TestModelMergeUsingItemsAndStateDict() | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| // Demonstrate model merging pattern using items() + state_dict() + load_state_dict() | ||||||||||||||||||||||
| // This is how users would merge models, matching the PyTorch pattern | ||||||||||||||||||||||
| var model1 = Linear(10, 5, true); | ||||||||||||||||||||||
| var model2 = Linear(10, 5, true); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| var sd1 = model1.state_dict(); | ||||||||||||||||||||||
| var sd2 = model2.state_dict(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| var merged = new Dictionary<string, Tensor>(); | ||||||||||||||||||||||
| using (var enumerator = model1.items()) { | ||||||||||||||||||||||
| while (enumerator.MoveNext()) { | ||||||||||||||||||||||
| var (name, _) = enumerator.Current; | ||||||||||||||||||||||
| merged[name] = (sd1[name] + sd2[name]) / 2; | ||||||||||||||||||||||
|
Comment on lines
+3365
to
+3368
|
||||||||||||||||||||||
| using (var enumerator = model1.items()) { | |
| while (enumerator.MoveNext()) { | |
| var (name, _) = enumerator.Current; | |
| merged[name] = (sd1[name] + sd2[name]) / 2; | |
| using (var _ = no_grad()) { | |
| using (var enumerator = model1.items()) { | |
| while (enumerator.MoveNext()) { | |
| var (name, _) = enumerator.Current; | |
| merged[name] = (sd1[name] + sd2[name]) / 2; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Module.items()is implemented by callingstate_dict(), which allocates and populates a newDictionary<string, Tensor>on every enumeration. Since this API is intended for iteration, consider yielding directly fromnamed_parameters(recurse: true)andnamed_buffers(recurse: true, include_nonpersistent: false)to avoid the intermediate dictionary allocation and duplicate traversal work.