1
1
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. See LICENSE in the project root for license information.
2
+
2
3
using System ;
3
4
using System . Runtime . InteropServices ;
4
5
using static TorchSharp . PInvoke . NativeMethods ;
@@ -18,6 +19,8 @@ public static partial class rnn
18
19
/// </summary>
19
20
public sealed class PackedSequence : IDisposable
20
21
{
22
+ internal DisposeScope OwningDisposeScope { get ; set ; }
23
+
21
24
/// <summary>
22
25
/// Class wrapping PyTorch's packedsequence object reference.
23
26
/// </summary>
@@ -39,6 +42,7 @@ internal HType() : base(IntPtr.Zero, true)
39
42
protected override bool ReleaseHandle ( )
40
43
{
41
44
THSNN_PackedSequence_dispose ( handle ) ;
45
+ handle = IntPtr . Zero ;
42
46
return true ;
43
47
}
44
48
}
@@ -62,6 +66,10 @@ protected override bool ReleaseHandle()
62
66
/// The original indices
63
67
/// </summary>
64
68
public readonly Tensor unsorted_indices ;
69
+ /// <summary>
70
+ /// Is true if the PackedSequence has been disposed, false otherwise.
71
+ /// </summary>
72
+ public bool IsInvalid => handle . IsInvalid ;
65
73
private HType handle ;
66
74
67
75
internal PackedSequence ( HType handle )
@@ -71,6 +79,11 @@ internal PackedSequence(HType handle)
71
79
this . batch_sizes = new Tensor ( THSNN_PackedSequence_batch_sizes ( handle ) ) ;
72
80
this . sorted_indices = new Tensor ( THSNN_PackedSequence_sorted_indices ( handle ) ) ;
73
81
this . unsorted_indices = new Tensor ( THSNN_PackedSequence_unsorted_indices ( handle ) ) ;
82
+ OwningDisposeScope = DisposeScopeManager . ThreadSingleton . RegisterOnCurrentDisposeScope ( this . data ) ;
83
+ OwningDisposeScope = DisposeScopeManager . ThreadSingleton . RegisterOnCurrentDisposeScope ( this . batch_sizes ) ;
84
+ OwningDisposeScope = DisposeScopeManager . ThreadSingleton . RegisterOnCurrentDisposeScope ( this . sorted_indices ) ;
85
+ OwningDisposeScope = DisposeScopeManager . ThreadSingleton . RegisterOnCurrentDisposeScope ( this . unsorted_indices ) ;
86
+ OwningDisposeScope = DisposeScopeManager . ThreadSingleton . RegisterOnCurrentDisposeScope ( this ) ;
74
87
}
75
88
76
89
internal HType Handle => handle ;
@@ -84,15 +97,69 @@ public void Dispose()
84
97
this . batch_sizes . Dispose ( ) ;
85
98
this . sorted_indices . Dispose ( ) ;
86
99
this . unsorted_indices . Dispose ( ) ;
100
+ OwningDisposeScope ? . MarkAsDisposed ( this ) ;
87
101
88
102
if ( handle != null && ! handle . IsInvalid ) {
89
103
handle . Dispose ( ) ;
90
104
handle . SetHandleAsInvalid ( ) ;
105
+
106
+ }
107
+ }
108
+ /// <summary>
109
+ /// Moves PackedSequence to the outer DisposeScope. If there is no outer DisposeScope, it's detached from the
110
+ /// DisposeScope system.
111
+ /// </summary>
112
+ /// <returns>The same PackedSequence that the method was called on</returns>
113
+ public PackedSequence MoveToOuterDisposeScope ( )
114
+ {
115
+ OwningDisposeScope ? . MoveToOuter ( this . data ) ;
116
+ OwningDisposeScope ? . MoveToOuter ( this . batch_sizes ) ;
117
+ OwningDisposeScope ? . MoveToOuter ( this . sorted_indices ) ;
118
+ OwningDisposeScope ? . MoveToOuter ( this . unsorted_indices ) ;
119
+ OwningDisposeScope ? . MoveToOuter ( this ) ;
120
+ return this ;
121
+ }
122
+
123
+ /// <summary>
124
+ /// Detaches the PackedSequence completely from the DisposeScope system.
125
+ /// </summary>
126
+ /// <returns>The same PackedSequence that the method was called on</returns>
127
+ public PackedSequence DetachFromDisposeScope ( )
128
+ {
129
+ OwningDisposeScope ? . Detach ( this . data ) ;
130
+ OwningDisposeScope ? . Detach ( this . batch_sizes ) ;
131
+ OwningDisposeScope ? . Detach ( this . sorted_indices ) ;
132
+ OwningDisposeScope ? . Detach ( this . unsorted_indices ) ;
133
+ OwningDisposeScope ? . Detach ( this ) ;
134
+ return this ;
135
+ }
136
+
137
+ public PackedSequence MoveToOtherDisposeScope ( PackedSequence other )
138
+ {
139
+ return MoveToOtherDisposeScope ( other . OwningDisposeScope ) ;
140
+ }
141
+
142
+ public PackedSequence MoveToOtherDisposeScope ( DisposeScope other )
143
+ {
144
+ if ( OwningDisposeScope == null && other != null ) {
145
+ other . Attach ( this . data ) ;
146
+ other . Attach ( this . batch_sizes ) ;
147
+ other . Attach ( this . sorted_indices ) ;
148
+ other . Attach ( this . unsorted_indices ) ;
149
+ other . Attach ( this ) ;
150
+ }
151
+ else {
152
+ OwningDisposeScope ? . MoveToOther ( other , this . data ) ;
153
+ OwningDisposeScope ? . MoveToOther ( other , this . batch_sizes ) ;
154
+ OwningDisposeScope ? . MoveToOther ( other , this . sorted_indices ) ;
155
+ OwningDisposeScope ? . MoveToOther ( other , this . unsorted_indices ) ;
156
+ OwningDisposeScope ? . MoveToOther ( other , this ) ;
91
157
}
158
+ return this ;
92
159
}
93
- }
160
+ }
94
161
}
95
162
}
96
163
}
97
164
}
98
- }
165
+ }
0 commit comments