-
Notifications
You must be signed in to change notification settings - Fork 216
Dispose Scalars implicitly created in Tensor operators#1434
Open
ds5678 wants to merge 2 commits intodotnet:main from
Open
Dispose Scalars implicitly created in Tensor operators #1434ds5678 wants to merge 2 commits intodotnet:main from
Scalars implicitly created in Tensor operators #1434ds5678 wants to merge 2 commits intodotnet:main from
Conversation
Contributor
Author
ds5678
commented
Jan 10, 2025
@dotnet-policy-service agree
yueyinqiu
yueyinqiu
approved these changes
Jan 10, 2025
@ds5678
ds5678
force-pushed
the
dispose-implicitly-converted-scalars
branch
from
April 4, 2025 19:52
273f174 to
ff6ffd7
Compare
@ds5678
ds5678
force-pushed
the
dispose-implicitly-converted-scalars
branch
from
May 3, 2025 20:00
ff6ffd7 to
3778ec6
Compare
Contributor
Author
ds5678
commented
May 27, 2025
Is there anything I need to do for this to be merged?
@ds5678
ds5678
force-pushed
the
dispose-implicitly-converted-scalars
branch
from
July 16, 2025 21:57
3778ec6 to
6c5156f
Compare
@ds5678
ds5678
force-pushed
the
dispose-implicitly-converted-scalars
branch
from
August 16, 2025 23:54
6c5156f to
a714ab7
Compare
Contributor
- FYI, let me share some my findings which are not covered by changes in this current MR, but related to missing
TorchSharp.Scalar.Dispose.- TorchSharp
torch.Tensor.add_may internally call anothertorch.Tensor.add_w/ implicitTorchSharp.Scalarconversions from hardcoded1.- return add_(target, 1);
- return add_(scalar, 1);
- consider in cases of a user intentionally call
torch.Tensor.add_for a inplace op. - update (2025年09月05日): TorchSharp
torch.Tensor.addalso have same symptom.- return add(target, 1);
- return add(scalar, 1);
- update (2025年09月05日): TorchSharp
torch.Tensor.addcdiv{,_}also have same symptom.- return addcdiv(tensor1, tensor2, 1);
- return addcdiv_(tensor1, tensor2, 1);
- update (2025年09月05日):
TorchSharp.Modules.BatchNorm.forwardalso has same symptom.- num_batches_tracked.add_(1);
- a possible workaround i can think of: carefully call
torch.Tensor.add_(or same kind method) w/usingdeclaredTorchSharp.Scalartypedalpha(or same kind argument) explicitly.
- TorchSharp
torch.nn.functional.leaky_reluinternally calltorch.Tensor.leaky_relu{,_}w/ implicitTorchSharp.Scalarconversions fromdoubletypednegative_slope.- return inplace ? input.leaky_relu_(negative_slope).alias() : input.leaky_relu(negative_slope);
- other {modules,functions} that directly use specified .NET typed arguments for forward calculation also have possible same symptom.
- a possible workaround i can think of: carefully call
torch.Tensor.leaky_relu{,_}(or other same kind method) w/usingdeclaredTorchSharp.Scalartypednegative_slope(or other same kind arguments) explicitly.
- TorchSharp
torch.arangemay internally call anothertorch.arangew/ implicitTorchSharp.Scalarconversions from hardcoded0and/or1.- return arange(start, stop, 1, dtype, device, requires_grad);
- return arange(0, stop, 1, dtype, device, requires_grad);
- a possible workaround i can think of: carefully call
torch.arangew/usingdeclaredTorchSharp.Scalartyped{start,stop,step}.
TorchSharp.Modules.SGD.stepcallstorch.Tensor.{add,add_,mul_}w/ implicitTorchSharp.Scalarconversions fromdoubletyped ones.TorchSharp/src/TorchSharp/Optimizers/SGD.cs
Line 158 in 6ceda53
grad = grad.add(param, alpha: weight_decay);TorchSharp/src/TorchSharp/Optimizers/SGD.cs
Line 168 in 6ceda53
buf.mul_(momentum).add_(grad, alpha: (1 - dampening));TorchSharp/src/TorchSharp/Optimizers/SGD.cs
Line 172 in 6ceda53
grad = grad.add(buf, alpha: momentum);TorchSharp/src/TorchSharp/Optimizers/SGD.cs
Line 181 in 6ceda53
param.add_(grad, alpha: alpha);- other TorchSharp optimizers also have possible same symptom.
- a possible workaround: derive from
TorchSharp.Modules.SGD(or other optimizer class) and overridestep.
- TorchSharp
Contributor
- IMHO, relying implict conversions to type inherits
IDisposable(i meanTorchSharp.Scalar) inside TorchSharp easily produces serious memory leak that won't be able to workaround by user side. - most probably, someone would have to do global screening (and hopefully fixing) for
TorchSharp.Scalar(andtorch.Tensoras well if possible) w/ temporary disabling their implicit conversions, otherwise TorchSharp definetly can not be used for any applications run long time.- and most of deep learning based applications usually run long time; e.g. long time training, FPS speed inference iteration.
- another possible solution i can think of is making
TorchSharp.DisposeScopeto coverTorchSharp.Scalaras well?
Contributor
Author
ds5678
commented
Sep 5, 2025
@hiyuh if you want to make a pull request onto my branch with your additions, I'd happily merge it so that they can be part of the review for this pull request.
Contributor
hiyuh
commented
Sep 5, 2025
@ds5678
thanks.
i'm grandually moving my task from "avoiding memory leak in my app code" to "fix leak in TorchSharp".
i'll let you know if my outcome does make something better.
@ds5678
ds5678
force-pushed
the
dispose-implicitly-converted-scalars
branch
5 times, most recently
from
February 15, 2026 00:15
3692c79 to
1a3a0a7
Compare
@ds5678
ds5678
force-pushed
the
dispose-implicitly-converted-scalars
branch
from
February 16, 2026 23:05
1a3a0a7 to
cdbfd62
Compare
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.
This pull request modifies the
Tensoroperators involving primitives to immediately dispose theScalarobject they create, rather than waiting for the garbage collector.