Skip to content

Commit 5cd1dff

Browse files
committed
hope that everything goes well
- update release notes - remove Include - remove NewDisposeScope and rename InnerNewDisposeScope - reorder members in DisposeScopeManager - make the paramter of RegisterOnCurrentDisposeScope to be torch.Tensor only
1 parent 9c27e03 commit 5cd1dff

File tree

5 files changed

+14
-32
lines changed

5 files changed

+14
-32
lines changed

RELEASENOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ __Breaking Changes__:
88

99
- `torchvision.dataset.MNIST` will try more mirrors.
1010
- The thrown exception might be changed when it fails to download `MNIST`, `FashionMNIST` or `KMNIST`.
11+
- `ObjectDisposedException` will now be thrown when trying to use the disposed dispose scopes.
12+
- The constructor of dispose scopes is no longer `public`. Use `torch.NewDisposeScope` instead.
1113

1214
__API Changes__:
1315

1416
- #1291 `Tensor.grad()` and `Tensor.set_grad()` have been replaced by a new property `Tensor.grad`.
1517
- A potential memory leak caused by `set_grad` has been resolved.
18+
- `Include` method of dispose scopes has been removed. Use `Attach` instead.
1619

1720
__Bug Fixes__:
1821

src/TorchSharp/DataLoader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ private bool MoveNextValue()
277277
public bool MoveNext()
278278
{
279279
DisposeCurrent();
280-
using (var scope = DisposeScopeManager.NewDisposeScope()) {
280+
using (var scope = torch.NewDisposeScope()) {
281281
if (!MoveNextValue()) return false;
282282

283283
var tensorIndexList = new List<long> { currentVal };
@@ -300,7 +300,7 @@ public bool MoveNext()
300300
foreach (var task in tasks)
301301
task.Wait();
302302

303-
using (var collate_scope = DisposeScopeManager.NewDisposeScope()) {
303+
using (var collate_scope = torch.NewDisposeScope()) {
304304
Current = collate_fn(items, device);
305305
currentDisposables = collate_scope.DisposablesView.ToList();
306306
collate_scope.Detach(currentDisposables);

src/TorchSharp/DisposeScope.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,6 @@ public int DisposablesCount {
5656
}
5757
}
5858

59-
/// <summary>
60-
/// Includes a disposable in the scope - for tensors this is done automatically once the scope has been
61-
/// created. Use this method to add additional disposables that should be disposed, but you typically
62-
/// don't need to call this method.
63-
/// </summary>
64-
/// <param name="disposable">The disposable to keep in the scope</param>
65-
/// <returns></returns>
66-
public T Include<T>(T disposable) where T : IDisposable
67-
{
68-
if (this._disposeScopeManager is null)
69-
throw new ObjectDisposedException("The dispose scope has been disposed.");
70-
Disposables.Add(disposable);
71-
return disposable;
72-
}
73-
7459
/// <summary>
7560
/// Excludes a set of tensors/disposables from the current dispose scope, and moves it up to the outer
7661
/// dispose scope, if one exists. See overloaded methods. If you wish to exclude a tensor from all sccopes,

src/TorchSharp/DisposeScopeManager.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. See LICENSE in the project root for license information.
22

33
using System;
4-
using System.Collections.Generic;
5-
using System.Diagnostics;
64

75
#nullable enable
86
namespace TorchSharp
@@ -15,30 +13,23 @@ namespace TorchSharp
1513
public class DisposeScopeManager
1614
{
1715
[ThreadStatic] private static DisposeScopeManager? _threadSingleton;
18-
internal ThreadDisposeScopeStatistics StatisticsInstance { get; } = new ThreadDisposeScopeStatistics();
19-
2016
internal static DisposeScopeManager ThreadSingleton => (_threadSingleton ??= new DisposeScopeManager());
21-
internal DisposeScope? CurrentDisposeScope { get; set; } = null;
2217

23-
public static ThreadDisposeScopeStatistics Statistics => ThreadSingleton.StatisticsInstance;
18+
internal ThreadDisposeScopeStatistics StatisticsInstance { get; } = new ThreadDisposeScopeStatistics();
19+
internal DisposeScope? CurrentDisposeScope { get; private set; } = null;
2420

25-
internal DisposeScope? RegisterOnCurrentDisposeScope(IDisposable disposable)
21+
internal DisposeScope? RegisterOnCurrentDisposeScope(torch.Tensor tensor)
2622
{
2723
if (this.CurrentDisposeScope is null) {
2824
StatisticsInstance.CreatedOutsideScopeCount++;
2925
return null;
3026
}
3127

3228
StatisticsInstance.CreatedInScopeCount++;
33-
this.CurrentDisposeScope.Include(disposable);
29+
this.CurrentDisposeScope.Disposables.Add(tensor);
3430
return CurrentDisposeScope;
3531
}
3632

37-
internal static DisposeScope NewDisposeScope()
38-
{
39-
return ThreadSingleton.InnerNewDisposeScope();
40-
}
41-
4233
internal void RemoveDisposeScope(DisposeScope disposeScope)
4334
{
4435
var scope = this.CurrentDisposeScope;
@@ -64,10 +55,12 @@ internal void RemoveDisposeScope(DisposeScope disposeScope)
6455
}
6556
}
6657

67-
private DisposeScope InnerNewDisposeScope()
58+
internal DisposeScope NewDisposeScope()
6859
{
6960
this.CurrentDisposeScope = new DisposeScope(this);
7061
return this.CurrentDisposeScope;
7162
}
63+
64+
public static ThreadDisposeScopeStatistics Statistics => ThreadSingleton.StatisticsInstance;
7265
}
7366
}

src/TorchSharp/Tensor/Tensor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7374,7 +7374,8 @@ public static long max_int_value(ScalarType type)
73747374
/// Creates a new dispose scope for the current thread. Any tensor created within the dispose scope will
73757375
/// be automatically disposed once the dispose scope is disposed.
73767376
/// </summary>
7377-
public static DisposeScope NewDisposeScope() => DisposeScopeManager.NewDisposeScope();
7377+
public static DisposeScope NewDisposeScope() =>
7378+
DisposeScopeManager.ThreadSingleton.NewDisposeScope();
73787379

73797380
/// <summary>
73807381
/// Creates a new dispose scope for the current thread, wrapping an expression.

0 commit comments

Comments
 (0)