-
Notifications
You must be signed in to change notification settings - Fork 214
Fixed Module.to() bugs with ParameterList and ParameterDict, and with autograd tracking movements between CPU & GPU
#1181
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4d373ed
attempt to rewrite _toEpilog in one function
shaltielshmid 05d0997
Added override _to in ParameterDict & List
shaltielshmid d909ec2
Added unit tests
shaltielshmid 61255bd
Fixed test for backwards on cpu
shaltielshmid eafb8cc
Brought back the memo for device
shaltielshmid 81ea7f7
Added toWillCopy function
shaltielshmid 42a7467
Added release notes
shaltielshmid e5c3498
Saved release notes
shaltielshmid c0c598f
Added <br/> to bug fixes
shaltielshmid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -163,66 +163,6 @@ protected internal virtual Module _to(Device device, ScalarType dtype) | |
| return this; | ||
| } | ||
|
|
||
| protected void _toEpilog(Device device, ScalarType dtype) | ||
| { | ||
| foreach (var (_, sm) in named_children()) sm._to(device, dtype); | ||
|
|
||
| var alreadyHandled = new HashSet<IntPtr>(); | ||
|
|
||
| foreach (var field in GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { | ||
|
|
||
| var fieldName = field.ComponentName(); | ||
| var value = field.GetValue(this); | ||
|
|
||
| switch (value) { | ||
| // This order in which these cases are arranged is significant. | ||
| case Parameter param when dtype == param.dtype && device.type == param.device_type && device.index == param.device_index: | ||
| alreadyHandled.Add(param.handle); | ||
| continue; | ||
|
|
||
| case Parameter param: { | ||
| var t = param.to(dtype, device); | ||
| t.retain_grad(); | ||
| var p = new Parameter(t, param.requires_grad); | ||
| field.SetValue(this, p); | ||
| ConditionallyRegisterParameter(fieldName, p); | ||
| alreadyHandled.Add(p.handle); | ||
| break; | ||
| } | ||
|
|
||
| case Tensor tensor when (device.type != tensor.device_type || device.index != tensor.device_index): { | ||
| var t = tensor.to(dtype, device); | ||
| field.SetValue(this, t); | ||
| ConditionallyRegisterBuffer(fieldName, t); | ||
| alreadyHandled.Add(t.handle); | ||
| break; | ||
| } | ||
|
|
||
| case Tensor tensor: | ||
| alreadyHandled.Add(tensor.handle); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| foreach (var (name, param) in named_parameters(false).ToList()) { | ||
| if (alreadyHandled.Contains(param.handle)) continue; | ||
| var t = param.to(dtype, device); | ||
| ConditionallyRegisterParameter(name, t); | ||
| } | ||
|
|
||
| foreach (var (name, buffer) in named_buffers(false).ToList()) { | ||
| if (alreadyHandled.Contains(buffer.handle)) continue; | ||
| var t = buffer.to(dtype, device); | ||
| ConditionallyRegisterBuffer(name, t); | ||
| } | ||
|
|
||
| _deviceType = device.type; | ||
| _deviceIndex = device.index; | ||
|
|
||
| Debug.Assert(_deviceType == DeviceType.CUDA || _deviceIndex == -1); | ||
| } | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Moves the parameters and buffers. | ||
| /// </summary> | ||
|
|
@@ -249,63 +189,6 @@ protected internal virtual Module _to(DeviceType deviceType, int deviceIndex = - | |
| return this; | ||
| } | ||
|
|
||
| protected void _toEpilog(DeviceType deviceType, int deviceIndex) | ||
| { | ||
| foreach (var (_, sm) in named_children()) sm._to(deviceType, deviceIndex); | ||
|
|
||
| var alreadyHandled = new HashSet<IntPtr>(); | ||
|
|
||
| foreach (var field in GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { | ||
|
|
||
| var fieldName = field.ComponentName(); | ||
| var value = field.GetValue(this); | ||
|
|
||
| switch (value) { | ||
| // This order in which these cases are arranged is significant. | ||
| case Parameter param when deviceType == param.device_type && deviceIndex == param.device_index: | ||
| alreadyHandled.Add(param.handle); | ||
| continue; | ||
|
|
||
| case Parameter param: { | ||
| var t = param.to(deviceType, deviceIndex); | ||
| t.retain_grad(); | ||
| var p = new Parameter(t, param.requires_grad); | ||
| field.SetValue(this, p); | ||
| ConditionallyRegisterParameter(fieldName, p); | ||
| alreadyHandled.Add(p.handle); | ||
| break; | ||
| } | ||
|
|
||
| case Tensor tensor when (deviceType != tensor.device_type || deviceIndex != tensor.device_index): { | ||
| var t = tensor.to(deviceType, deviceIndex); | ||
| field.SetValue(this, t); | ||
| ConditionallyRegisterBuffer(fieldName, t); | ||
| alreadyHandled.Add(t.handle); | ||
| break; | ||
| } | ||
|
|
||
| case Tensor tensor: | ||
| alreadyHandled.Add(tensor.handle); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| foreach (var (name, param) in named_parameters(false).ToList()) { | ||
| if (alreadyHandled.Contains(param.handle)) continue; | ||
| var t = param.to(deviceType, deviceIndex); | ||
| ConditionallyRegisterParameter(name, t); | ||
| } | ||
|
|
||
| foreach (var (name, buffer) in named_buffers(false).ToList()) { | ||
| if (alreadyHandled.Contains(buffer.handle)) continue; | ||
| var t = buffer.to(deviceType, deviceIndex); | ||
| ConditionallyRegisterBuffer(name, t); | ||
| } | ||
|
|
||
| _deviceType = deviceType; | ||
| _deviceIndex = deviceIndex; | ||
| } | ||
|
|
||
| private DeviceType _deviceType = DeviceType.CPU; | ||
| private int _deviceIndex = -1; | ||
|
|
||
|
|
@@ -325,55 +208,62 @@ protected internal virtual Module _to(ScalarType dtype) | |
|
|
||
| protected void _toEpilog(ScalarType dtype) | ||
| { | ||
| foreach (var (_, sm) in named_children()) sm._to(dtype); | ||
| _toEpilog(dtype, null); | ||
| } | ||
|
|
||
| var alreadyHandled = new HashSet<IntPtr>(); | ||
| protected void _toEpilog(Device device, ScalarType dtype) | ||
| { | ||
| _toEpilog(dtype, device); | ||
| } | ||
|
|
||
| foreach (var field in GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { | ||
| protected void _toEpilog(DeviceType deviceType, int deviceIndex) | ||
| { | ||
| _toEpilog(null, new Device(deviceType, deviceIndex)); | ||
| } | ||
|
|
||
| var fieldName = field.ComponentName(); | ||
| var value = field.GetValue(this); | ||
| private void _toEpilog(ScalarType? dtype, Device device) | ||
| { | ||
| foreach (var (_, sm) in named_children()) { | ||
| if (device is null) sm._to(dtype.Value); | ||
| else if (dtype is null) sm._to(device.type, device.index); | ||
| else sm._to(device, dtype.Value); | ||
| } | ||
|
|
||
| switch (value) { | ||
| // This order in which these cases are arranged is significant. | ||
| case Parameter param when dtype == param.dtype: | ||
| alreadyHandled.Add(param.handle); | ||
| continue; | ||
|
|
||
| case Parameter param: { | ||
| var t = param.to(dtype); | ||
| t.retain_grad(); | ||
| var p = new Parameter(t, param.requires_grad); | ||
| field.SetValue(this, p); | ||
| ConditionallyRegisterParameter(fieldName, p); | ||
| alreadyHandled.Add(p.handle); | ||
| break; | ||
| } | ||
| var fieldsByComponentName = GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) | ||
| .ToDictionary(field => field.ComponentName()); | ||
|
|
||
| case Tensor tensor when dtype == tensor.dtype: | ||
| alreadyHandled.Add(tensor.handle); | ||
| continue; | ||
| foreach (var (name, param) in named_parameters(false).ToList()) { | ||
| if (!param.toWillCopy(dtype ?? param.dtype, device ?? param.device)) continue; | ||
|
|
||
| case Tensor tensor: { | ||
| var t = tensor.to(dtype); | ||
| field.SetValue(this, t); | ||
| ConditionallyRegisterBuffer(fieldName, t); | ||
| alreadyHandled.Add(t.handle); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| // Store the requires_grad flag ahead, since we dispose the parameter after moving | ||
| bool requiresGrad = param.requires_grad; | ||
| Parameter p; | ||
| // When moving the parameter, we don't want the autograd to track this movement on the graph. | ||
| // In addition, we need the new tensor to be a leaf to accumulate gradients, so if we didn't | ||
| // disable grad we would need to call .detach() on the moved tensor. | ||
| using (var d = torch.no_grad()) | ||
| p = new Parameter(param.to(dtype ?? param.dtype, device ?? param.device, disposeAfter: true), requiresGrad); | ||
| ConditionallyRegisterParameter(name, p); | ||
|
|
||
| foreach (var (name, param) in named_parameters(false).ToList()) { | ||
| if (alreadyHandled.Contains(param.handle)) continue; | ||
| var t = param.to(dtype); | ||
| ConditionallyRegisterParameter(name, t); | ||
| // If this parameter is a field, set it | ||
| if (fieldsByComponentName.TryGetValue(name, out var field)) | ||
| field.SetValue(this, p); | ||
| } | ||
|
|
||
| foreach (var (name, buffer) in named_buffers(false).ToList()) { | ||
| if (alreadyHandled.Contains(buffer.handle)) continue; | ||
| var t = buffer.to(dtype); | ||
| if (!buffer.toWillCopy(dtype ?? buffer.dtype, device ?? buffer.device)) continue; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are old buffers/parameters disposed anywhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. In the ".to()" call I use the |
||
|
|
||
| // Buffers don't get grads so we don't need to detach them afterwards | ||
| var t = buffer.to(dtype ?? buffer.dtype, device ?? buffer.device, disposeAfter: true); | ||
| ConditionallyRegisterBuffer(name, t); | ||
|
|
||
| if (fieldsByComponentName.TryGetValue(name, out var field)) | ||
| field.SetValue(this, t); | ||
| } | ||
|
|
||
| if (device is not null) { | ||
| _deviceType = device.type; | ||
| _deviceIndex = device.index; | ||
| } | ||
| } | ||
|
|
||
|
|
||
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
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
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
Oops, something went wrong.
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.
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.
Does this have any performance implications?
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.
Under the assumption that the costly operation is the reflection, then there shouldn't be.
We're creating a hash collection of the same size (alreadyHandled vs the Dictionary), so I guess the cost would be a the lookups in the dictionary, which should be very minimal.
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.
The brings up a functional concern -- the alreadyHandled set is there to make sure that we don't accidentally deal with the same tensor twice. Is that no longer a possibility?
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.
I don't think so.
The reason it was needed before was because the function was iterating through two different lists of parameters. One using reflection (the parameters registered through the
RegisterComponents()function), and the other is using the internal list of registered parameters.In the proposed code we only go through the list of registered parameters, so there isn't a concern of dealing with the same tensor twice.
Unless there is a case where someone can register the same tensor twice?
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.
If someone registers the same parameter under two different names, then we have an issue - the first encounter will dispose the parameter, and then the second time it will have a null tensor error.
Should this be a use case we should handle?