Skip to content

Commit 6ee70ba

Browse files
committed
Remove ILifetimeStatistics interface
1 parent dceef30 commit 6ee70ba

File tree

4 files changed

+117
-93
lines changed

4 files changed

+117
-93
lines changed

src/TorchSharp/DisposeScope.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ public void Detach(IEnumerable<IDisposable> disposables)
214214
foreach (var disposable in disposables) {
215215
if (Disposables.Remove(disposable)) {
216216
if (disposable is torch.Tensor tensor) {
217-
_disposeScopeManager.StatisticsInstance._TensorStatistics.DetachedFromScopeCount++;
217+
_disposeScopeManager.StatisticsInstance.TensorStatistics.DetachedFromScopeCount++;
218218
tensor.OwningDisposeScope = null;
219219
} else if (disposable is torch.nn.utils.rnn.PackedSequence sequence) {
220-
_disposeScopeManager.StatisticsInstance._PackedSequenceStatistics.DetachedFromScopeCount++;
220+
_disposeScopeManager.StatisticsInstance.PackedSequenceStatistics.DetachedFromScopeCount++;
221221
sequence.OwningDisposeScope = null;
222222
}
223223
}
@@ -243,9 +243,9 @@ public IReadOnlyList<IDisposable> Attach(IEnumerable<IDisposable> disposables)
243243
foreach (var disposable in disposables) {
244244
if (AddToOther(this, disposable)) {
245245
if (disposable is torch.Tensor tensor) {
246-
_disposeScopeManager.StatisticsInstance._TensorStatistics.AttachedToScopeCount++;
246+
_disposeScopeManager.StatisticsInstance.TensorStatistics.AttachedToScopeCount++;
247247
} else if (disposable is torch.nn.utils.rnn.PackedSequence sequence) {
248-
_disposeScopeManager.StatisticsInstance._PackedSequenceStatistics.AttachedToScopeCount++;
248+
_disposeScopeManager.StatisticsInstance.PackedSequenceStatistics.AttachedToScopeCount++;
249249
}
250250
}
251251
result.Add(disposable);
@@ -404,10 +404,10 @@ internal HashSet<IDisposable> DetachAllAndDispose()
404404
var disposables = this.Disposables;
405405
foreach (var disposable in this.Disposables) {
406406
if (disposable is torch.Tensor tensor) {
407-
this._disposeScopeManager!.StatisticsInstance._TensorStatistics.DetachedFromScopeCount++;
407+
this._disposeScopeManager!.StatisticsInstance.TensorStatistics.DetachedFromScopeCount++;
408408
tensor.OwningDisposeScope = null;
409409
} else if (disposable is torch.nn.utils.rnn.PackedSequence sequence) {
410-
this._disposeScopeManager!.StatisticsInstance._PackedSequenceStatistics.DetachedFromScopeCount++;
410+
this._disposeScopeManager!.StatisticsInstance.PackedSequenceStatistics.DetachedFromScopeCount++;
411411
sequence.OwningDisposeScope = null;
412412
}
413413
}

src/TorchSharp/DisposeScopeManager.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ public class DisposeScopeManager
2222
{
2323
if (this.CurrentDisposeScope is null) {
2424
if (item is torch.Tensor t) {
25-
StatisticsInstance._TensorStatistics.CreatedOutsideScopeCount++;
25+
StatisticsInstance.TensorStatistics.CreatedOutsideScopeCount++;
2626
} else if (item is torch.nn.utils.rnn.PackedSequence p) {
27-
StatisticsInstance._PackedSequenceStatistics.CreatedOutsideScopeCount++;
27+
StatisticsInstance.PackedSequenceStatistics.CreatedOutsideScopeCount++;
2828
}
2929
return null;
3030
} else {
3131
if (item is torch.Tensor t) {
32-
StatisticsInstance._TensorStatistics.CreatedInScopeCount++;
32+
StatisticsInstance.TensorStatistics.CreatedInScopeCount++;
3333
} else if (item is torch.nn.utils.rnn.PackedSequence p) {
34-
StatisticsInstance._PackedSequenceStatistics.CreatedInScopeCount++;
34+
StatisticsInstance.PackedSequenceStatistics.CreatedInScopeCount++;
3535
}
3636
this.CurrentDisposeScope.Disposables.Add(item);
3737
return CurrentDisposeScope;
@@ -41,17 +41,17 @@ public class DisposeScopeManager
4141
internal void DisposingOnCurrentScope(torch.Tensor item)
4242
{
4343
if (item.OwningDisposeScope == null) {
44-
StatisticsInstance._TensorStatistics.DisposedOutsideScopeCount++;
44+
StatisticsInstance.TensorStatistics.DisposedOutsideScopeCount++;
4545
} else {
46-
StatisticsInstance._TensorStatistics.DisposedInScopeCount++;
46+
StatisticsInstance.TensorStatistics.DisposedInScopeCount++;
4747
}
4848
}
4949
internal void DisposingOnCurrentScope(torch.nn.utils.rnn.PackedSequence item)
5050
{
5151
if (item.OwningDisposeScope == null) {
52-
StatisticsInstance._PackedSequenceStatistics.DisposedOutsideScopeCount++;
52+
StatisticsInstance.PackedSequenceStatistics.DisposedOutsideScopeCount++;
5353
} else {
54-
StatisticsInstance._PackedSequenceStatistics.DisposedInScopeCount++;
54+
StatisticsInstance.PackedSequenceStatistics.DisposedInScopeCount++;
5555
}
5656
}
5757
internal void RemoveDisposeScope(DisposeScope disposeScope)

src/TorchSharp/ThreadDisposeScopeStatistics.cs

Lines changed: 94 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,135 @@
11
#nullable enable
22
using System.Text;
3-
using TorchSharp;
43

54
namespace TorchSharp
65
{
7-
public interface ILifetimeStatistics
6+
7+
/// <summary>
8+
/// Keeps track of the combined Tensor and PackedSequence statistics for the current thread. Can be queried to figure out performance/memory issues.
9+
/// </summary>
10+
public class ThreadDisposeScopeStatistics
811
{
912
/// <summary>
10-
/// The number of disposables that were created on this thread, but weren't captured by a DisposeScope.
13+
/// The total number of Tensors and PackedSequence instances that were created on this thread, but weren't
14+
/// captured by a DisposeScope.
1115
/// </summary>
12-
long CreatedOutsideScopeCount { get; }
16+
public long CreatedOutsideScopeCount => TensorStatistics.CreatedOutsideScopeCount +
17+
PackedSequenceStatistics.CreatedOutsideScopeCount;
18+
1319
/// <summary>
14-
/// The number of disposables that were Disposed on this thread, but weren't captured by a DisposeScope.
20+
/// The number of Tensors and PackedSequence instances that were Disposed on this thread, but weren't
21+
/// captured by a DisposeScope.
1522
/// </summary>
16-
long DisposedOutsideScopeCount { get; }
23+
public long DisposedOutsideScopeCount => TensorStatistics.DisposedOutsideScopeCount +
24+
PackedSequenceStatistics.DisposedOutsideScopeCount;
1725
/// <summary>
18-
/// The number of disposables that were created on this thread and were captured by a DisposeScope.
26+
/// The number of Tensors and PackedSequence instances that were created on this thread and were captured
27+
/// by a DisposeScope.
1928
/// </summary>
20-
long CreatedInScopeCount { get; }
29+
public long CreatedInScopeCount => TensorStatistics.CreatedInScopeCount +
30+
PackedSequenceStatistics.CreatedInScopeCount;
31+
2132
/// <summary>
22-
/// The number of disposables that were disposed on this thread and were disposed while in a DisposeScope.
33+
/// The number of Tensors and PackedSequence instances that were disposed on this thread and were disposed
34+
/// while in a DisposeScope.
2335
/// </summary>
24-
long DisposedInScopeCount { get; }
36+
public long DisposedInScopeCount => TensorStatistics.DisposedInScopeCount +
37+
PackedSequenceStatistics.DisposedInScopeCount;
38+
2539
/// <summary>
26-
/// The number of disposables that were created on this thread outside a DisposeScope, and then
27-
/// eventually were attached to one.
40+
/// The number of Tensors and PackedSequence instances that were created on this thread outside a DisposeScope,
41+
/// and then eventually were attached to one.
2842
/// </summary>
29-
long AttachedToScopeCount { get; }
43+
public long AttachedToScopeCount=> TensorStatistics.AttachedToScopeCount +
44+
PackedSequenceStatistics.AttachedToScopeCount;
45+
3046
/// <summary>
31-
/// Number of disposables that were once included in the scope, but were subsequently detached.
47+
/// Number of Tensors and PackedSequence instances that were once included in a DisposeScope, but were
48+
/// subsequently detached.
3249
/// </summary>
33-
long DetachedFromScopeCount { get; }
50+
public long DetachedFromScopeCount=> TensorStatistics.DetachedFromScopeCount +
51+
PackedSequenceStatistics.DetachedFromScopeCount;
52+
3453
/// <summary>
35-
/// The number of disposables that are currently live on the current thread.
54+
/// Exact number of Tensors and PackedSequence instances that are still live. Is the difference of all
55+
/// created objects minus all disposed objects.
3656
/// </summary>
37-
long ThreadTotalLiveCount { get; }
57+
public long ThreadTotalLiveCount => TensorStatistics.ThreadTotalLiveCount +
58+
PackedSequenceStatistics.ThreadTotalLiveCount;
59+
3860
/// <summary>
39-
/// Resets the counts for the current thread. See ThreadTotalLiveCount etc. Mainly used in tests to make sure
40-
/// we get a clean slate on the thread.
61+
/// Resets the counts for the current thread. See ThreadTotalLiveCount etc. Mainly used in tests and memory
62+
/// leak debugging to make sure we get a clean slate on the thread.
4163
/// </summary>
42-
void Reset();
64+
public void Reset()
65+
{
66+
TensorStatistics.Reset();
67+
PackedSequenceStatistics.Reset();
68+
}
4369
/// <summary>
4470
/// A debug printout of all the properties and their values, suitable for a log or console output
4571
/// </summary>
4672
/// <returns></returns>
47-
string ToString();
48-
}
49-
50-
/// <summary>
51-
/// Keeps track of the combined Tensor and PackedSequence statistics for the current thread. Can be queried to figure out performance/memory issues.
52-
/// </summary>
53-
public class ThreadDisposeScopeStatistics: ILifetimeStatistics
54-
{
55-
public long CreatedOutsideScopeCount => _TensorStatistics.CreatedOutsideScopeCount +
56-
_PackedSequenceStatistics.CreatedOutsideScopeCount;
57-
58-
public long DisposedOutsideScopeCount => _TensorStatistics.DisposedOutsideScopeCount +
59-
_PackedSequenceStatistics.DisposedOutsideScopeCount;
60-
public long CreatedInScopeCount => _TensorStatistics.CreatedInScopeCount +
61-
_PackedSequenceStatistics.CreatedInScopeCount;
62-
63-
public long DisposedInScopeCount => _TensorStatistics.DisposedInScopeCount +
64-
_PackedSequenceStatistics.DisposedInScopeCount;
65-
66-
public long AttachedToScopeCount=> _TensorStatistics.AttachedToScopeCount +
67-
_PackedSequenceStatistics.AttachedToScopeCount;
68-
69-
public long DetachedFromScopeCount=> _TensorStatistics.DetachedFromScopeCount +
70-
_PackedSequenceStatistics.DetachedFromScopeCount;
71-
72-
public long ThreadTotalLiveCount => _TensorStatistics.ThreadTotalLiveCount +
73-
_PackedSequenceStatistics.ThreadTotalLiveCount;
74-
75-
public void Reset()
76-
{
77-
_TensorStatistics.Reset();
78-
_PackedSequenceStatistics.Reset();
79-
}
80-
8173
public override string ToString()
8274
{
83-
return LifetimeStatisticsUtil.DebugString(this);
75+
var sb = new StringBuilder();
76+
sb.Append("ThreadTotalLiveCount: " + ThreadTotalLiveCount);
77+
sb.Append("; CreatedOutsideScopeCount: " + CreatedOutsideScopeCount);
78+
sb.Append("; DisposedOutsideScopeCount: " + DisposedOutsideScopeCount);
79+
sb.Append("; CreatedInScopeCount: " + CreatedInScopeCount);
80+
sb.Append("; DisposedInScopeCount: " + DisposedInScopeCount);
81+
sb.Append("; AttachedToScopeCount: " + AttachedToScopeCount);
82+
sb.Append("; DetachedFromScopeCount: " + DetachedFromScopeCount);
83+
return sb.ToString();
8484
}
8585
/// <summary>
8686
/// Keeps track of the Tensor statistics for the current thread. Can be queried to figure out performance/memory issues.
8787
/// </summary>
88-
public ILifetimeStatistics TensorStatistics => _TensorStatistics;
89-
internal LifetimeStatistics _TensorStatistics { get; set; } = new LifetimeStatistics();
88+
public LifetimeStatistics TensorStatistics { get; } = new LifetimeStatistics();
9089
/// <summary>
9190
/// Keeps track of the PackedSequence statistics for the current thread. Can be queried to figure out performance/memory issues.
9291
/// </summary>
93-
public ILifetimeStatistics PackedSequenceStatistics => _PackedSequenceStatistics;
94-
internal LifetimeStatistics _PackedSequenceStatistics { get; set; } = new LifetimeStatistics();
92+
public LifetimeStatistics PackedSequenceStatistics { get; } = new LifetimeStatistics();
9593
}
9694

97-
public class LifetimeStatistics : ILifetimeStatistics
95+
public class LifetimeStatistics
9896
{
97+
/// <summary>
98+
/// The number of disposables that were created on this thread, but weren't captured by a DisposeScope.
99+
/// </summary>
99100
public long CreatedOutsideScopeCount { get; internal set; }
101+
/// <summary>
102+
/// The number of disposables that were Disposed on this thread, but weren't captured by a DisposeScope.
103+
/// </summary>
100104
public long DisposedOutsideScopeCount { get; internal set; }
105+
/// <summary>
106+
/// The number of disposables that were created on this thread and were captured by a DisposeScope.
107+
/// </summary>
101108
public long CreatedInScopeCount { get; internal set; }
109+
/// <summary>
110+
/// The number of disposables that were disposed on this thread and were disposed while in a DisposeScope.
111+
/// </summary>
102112
public long DisposedInScopeCount { get; internal set; }
113+
/// <summary>
114+
/// The number of disposables that were created on this thread outside a DisposeScope, and then
115+
/// eventually were attached to one.
116+
/// </summary>
103117
public long AttachedToScopeCount { get; internal set; }
118+
/// <summary>
119+
/// Number of disposables that were once included in a DisposeScope, but were subsequently detached.
120+
/// </summary>
104121
public long DetachedFromScopeCount { get; internal set; }
105122
/// <summary>
106123
/// Exact number of objects that are still live. Is the difference of all created objects
107124
/// minus all disposed objects.
108125
/// </summary>
109-
public long ThreadTotalLiveCount => (CreatedInScopeCount - DisposedInScopeCount) + (CreatedOutsideScopeCount - DisposedOutsideScopeCount);
126+
public long ThreadTotalLiveCount => (CreatedInScopeCount - DisposedInScopeCount) +
127+
(CreatedOutsideScopeCount - DisposedOutsideScopeCount);
110128

129+
/// <summary>
130+
/// Resets the counts for the current thread. See ThreadTotalLiveCount etc. Mainly used in tests to make sure
131+
/// we get a clean slate on the thread.
132+
/// </summary>
111133
public void Reset()
112134
{
113135
CreatedOutsideScopeCount = 0;
@@ -117,23 +139,20 @@ public void Reset()
117139
AttachedToScopeCount = 0;
118140
DetachedFromScopeCount = 0;
119141
}
142+
/// <summary>
143+
/// A debug printout of all the properties and their values, suitable for a log or console output
144+
/// </summary>
145+
/// <returns></returns>
120146
public override string ToString()
121-
{
122-
return LifetimeStatisticsUtil.DebugString(this);
123-
}
124-
}
125-
static class LifetimeStatisticsUtil
126-
{
127-
public static string DebugString(this ILifetimeStatistics statistics)
128147
{
129148
var sb = new StringBuilder();
130-
sb.Append("ThreadTotalLiveCount: " + statistics.ThreadTotalLiveCount);
131-
sb.Append("; CreatedOutsideScopeCount: " + statistics.CreatedOutsideScopeCount);
132-
sb.Append("; DisposedOutsideScopeCount: " + statistics.DisposedOutsideScopeCount);
133-
sb.Append("; CreatedInScopeCount: " + statistics.CreatedInScopeCount);
134-
sb.Append("; DisposedInScopeCount: " + statistics.DisposedInScopeCount);
135-
sb.Append("; AttachedToScopeCount: " + statistics.AttachedToScopeCount);
136-
sb.Append("; DetachedFromScopeCount: " + statistics.DetachedFromScopeCount);
149+
sb.Append("ThreadTotalLiveCount: " + ThreadTotalLiveCount);
150+
sb.Append("; CreatedOutsideScopeCount: " + CreatedOutsideScopeCount);
151+
sb.Append("; DisposedOutsideScopeCount: " + DisposedOutsideScopeCount);
152+
sb.Append("; CreatedInScopeCount: " + CreatedInScopeCount);
153+
sb.Append("; DisposedInScopeCount: " + DisposedInScopeCount);
154+
sb.Append("; AttachedToScopeCount: " + AttachedToScopeCount);
155+
sb.Append("; DetachedFromScopeCount: " + DetachedFromScopeCount);
137156
return sb.ToString();
138157
}
139158
}

test/TorchSharpTest/TestDisposeScopesStatisticsBase.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,20 @@ protected static void AssertTotalsCounts(long createdOutside,
3939
long detached,
4040
long threadTotalLive)
4141
{
42-
AssertStatCounts(createdOutside, disposedOutside,
43-
createdIn, disposedIn,
44-
attached, detached, threadTotalLive, DisposeScopeManager.Statistics);
42+
var stats = DisposeScopeManager.Statistics;
43+
Assert.True(createdOutside == stats.CreatedOutsideScopeCount, $"CreatedOutsideScopeCount: Expected({createdOutside})!={stats.CreatedOutsideScopeCount}");
44+
Assert.True(disposedOutside == stats.DisposedOutsideScopeCount, $"DisposedOutsideScopeCount: Expected({disposedOutside})!={stats.DisposedOutsideScopeCount}");
45+
Assert.True(createdIn == stats.CreatedInScopeCount, $"CreatedInScope: Expected({createdIn})!={stats.CreatedInScopeCount}");
46+
Assert.True(disposedIn == stats.DisposedInScopeCount, $"DisposedInScopeCount: Expected({disposedIn})!={stats.DisposedInScopeCount}");
47+
Assert.True(attached == stats.AttachedToScopeCount, $"AttachedToScopeCount: Expected({attached})!={stats.AttachedToScopeCount}");
48+
Assert.True(detached == stats.DetachedFromScopeCount, $"DetachedFromScopeCount: Expected({detached})!={stats.DetachedFromScopeCount}");
49+
Assert.True(threadTotalLive == stats.ThreadTotalLiveCount, $"ThreadTotalLiveCount: Expected({threadTotalLive})!={stats.ThreadTotalLiveCount}");
4550
}
4651

4752
protected static void AssertStatCounts(long createdOutside, long disposedOutside,
4853
long createdIn, long disposedIn,
4954
long attached, long detached,
50-
long threadTotalLive, ILifetimeStatistics stats)
55+
long threadTotalLive, LifetimeStatistics stats)
5156
{
5257
Assert.True(createdOutside == stats.CreatedOutsideScopeCount, $"CreatedOutsideScopeCount: Expected({createdOutside})!={stats.CreatedOutsideScopeCount}");
5358
Assert.True(disposedOutside == stats.DisposedOutsideScopeCount, $"DisposedOutsideScopeCount: Expected({disposedOutside})!={stats.DisposedOutsideScopeCount}");

0 commit comments

Comments
 (0)