Skip to content

Commit c69c4a0

Browse files
working on memory issue during tests for TorchSharp (#7022)
1 parent 8b483f4 commit c69c4a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+825
-56
lines changed

src/Microsoft.ML.TorchSharp/AutoFormerV2/Attention.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class Attention : Module<Tensor, Tensor, Tensor>
3131
private readonly Parameter attention_biases;
3232
private readonly TensorIndex attention_bias_idxs;
3333
private readonly Softmax softmax;
34+
private bool _disposedValue;
3435
#pragma warning restore MSML_PrivateFieldName
3536

3637

@@ -131,5 +132,23 @@ public override Tensor forward(Tensor x, Tensor mask)
131132
return x.MoveToOuterDisposeScope();
132133
}
133134
}
135+
136+
protected override void Dispose(bool disposing)
137+
{
138+
if (!_disposedValue)
139+
{
140+
if (disposing)
141+
{
142+
norm.Dispose();
143+
qkv.Dispose();
144+
proj.Dispose();
145+
attention_biases.Dispose();
146+
softmax.Dispose();
147+
_disposedValue = true;
148+
}
149+
}
150+
151+
base.Dispose(disposing);
152+
}
134153
}
135154
}

src/Microsoft.ML.TorchSharp/AutoFormerV2/AutoFormerV2Backbone.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class AutoFormerV2Backbone : Module<Tensor, List<Tensor>>
2424
private readonly LayerNorm norm1;
2525
private readonly LayerNorm norm2;
2626
private readonly LayerNorm norm3;
27+
private bool _disposedValue;
2728
#pragma warning restore MSML_PrivateFieldName
2829

2930
/// <summary>
@@ -155,5 +156,23 @@ public override List<Tensor> forward(Tensor imgBatch)
155156
return outs;
156157
}
157158
}
159+
160+
protected override void Dispose(bool disposing)
161+
{
162+
if (!_disposedValue)
163+
{
164+
if (disposing)
165+
{
166+
patch_embed.Dispose();
167+
layers.Dispose();
168+
norm1.Dispose();
169+
norm2.Dispose();
170+
norm3.Dispose();
171+
_disposedValue = true;
172+
}
173+
}
174+
175+
base.Dispose(disposing);
176+
}
158177
}
159178
}

src/Microsoft.ML.TorchSharp/AutoFormerV2/AutoFormerV2Block.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class AutoFormerV2Block : Module<Tensor, int, int, Tensor, Tensor>
2222
private readonly Attention attn;
2323
private readonly MLP mlp;
2424
private readonly Conv2dBN local_conv;
25+
private bool _disposedValue;
2526
#pragma warning restore MSML_PrivateFieldName
2627

2728
/// <summary>
@@ -182,5 +183,21 @@ private static Tensor WindowPartition(Tensor x, int windowSize)
182183
return windows.MoveToOuterDisposeScope();
183184
}
184185
}
186+
187+
protected override void Dispose(bool disposing)
188+
{
189+
if (!_disposedValue)
190+
{
191+
if (disposing)
192+
{
193+
attn.Dispose();
194+
mlp.Dispose();
195+
local_conv.Dispose();
196+
_disposedValue = true;
197+
}
198+
}
199+
200+
base.Dispose(disposing);
201+
}
185202
}
186203
}

src/Microsoft.ML.TorchSharp/AutoFormerV2/AutoformerV2.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class AutoFormerV2 : Module<Tensor, (Tensor, Tensor, Tensor)>
2323
private readonly FPN neck;
2424
private readonly RetinaHead bbox_head;
2525
private readonly Anchors anchors;
26+
private bool _disposedValue;
2627
#pragma warning restore MSML_PrivateFieldName
2728

2829
/// <summary>
@@ -134,5 +135,22 @@ private void InitializeWeight()
134135
}
135136
}
136137
}
138+
139+
protected override void Dispose(bool disposing)
140+
{
141+
if (!_disposedValue)
142+
{
143+
if (disposing)
144+
{
145+
backbone.Dispose();
146+
neck.Dispose();
147+
bbox_head.Dispose();
148+
anchors.Dispose();
149+
_disposedValue = true;
150+
}
151+
}
152+
153+
base.Dispose(disposing);
154+
}
137155
}
138156
}

src/Microsoft.ML.TorchSharp/AutoFormerV2/BasicLayer.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class BasicLayer : Module<Tensor, int, int, (Tensor, int, int, Tensor, in
2222
private readonly int shiftSize;
2323
private readonly ModuleList<AutoFormerV2Block> blocks;
2424
private readonly PatchMerging downsample;
25+
private bool _disposedValue;
2526
#pragma warning restore MSML_PrivateFieldName
2627

2728
/// <summary>
@@ -133,5 +134,20 @@ private static Tensor WindowPartition(Tensor x, int windowSize)
133134
return windows.MoveToOuterDisposeScope();
134135
}
135136
}
137+
138+
protected override void Dispose(bool disposing)
139+
{
140+
if (!_disposedValue)
141+
{
142+
if (disposing)
143+
{
144+
blocks.Dispose();
145+
downsample.Dispose();
146+
_disposedValue = true;
147+
}
148+
}
149+
150+
base.Dispose(disposing);
151+
}
136152
}
137153
}

src/Microsoft.ML.TorchSharp/AutoFormerV2/Conv2dBN.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Conv2dBN : Module<Tensor, Tensor>
1717
#pragma warning disable MSML_PrivateFieldName // Need to match TorchSharp model names.
1818
private readonly Conv2d c;
1919
private readonly BatchNorm2d bn;
20+
private bool _disposedValue;
2021
#pragma warning restore MSML_PrivateFieldName
2122

2223
/// <summary>
@@ -48,5 +49,20 @@ public override Tensor forward(Tensor x)
4849
return x2.MoveToOuterDisposeScope();
4950
}
5051
}
52+
53+
protected override void Dispose(bool disposing)
54+
{
55+
if (!_disposedValue)
56+
{
57+
if (disposing)
58+
{
59+
c.Dispose();
60+
bn.Dispose();
61+
_disposedValue = true;
62+
}
63+
}
64+
65+
base.Dispose(disposing);
66+
}
5167
}
5268
}

src/Microsoft.ML.TorchSharp/AutoFormerV2/ConvLayer.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class ConvLayer : Module<Tensor, int, int, (Tensor, int, int, Tensor, int
1717
#pragma warning disable MSML_PrivateFieldName // Need to match TorchSharp model names.
1818
private readonly ModuleList<MBConv> blocks;
1919
private readonly PatchMerging downsample;
20+
private bool _disposedValue;
2021
#pragma warning restore MSML_PrivateFieldName
2122

2223
/// <summary>
@@ -56,5 +57,20 @@ public override (Tensor, int, int, Tensor, int, int) forward(Tensor x, int h, in
5657
return (xOut.MoveToOuterDisposeScope(), h, w, x.MoveToOuterDisposeScope(), nH, nW);
5758
}
5859
}
60+
61+
protected override void Dispose(bool disposing)
62+
{
63+
if (!_disposedValue)
64+
{
65+
if (disposing)
66+
{
67+
blocks.Dispose();
68+
downsample.Dispose();
69+
_disposedValue = true;
70+
}
71+
}
72+
73+
base.Dispose(disposing);
74+
}
5975
}
6076
}

src/Microsoft.ML.TorchSharp/AutoFormerV2/ConvModule.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class ConvModule : Module<Tensor, Tensor>
1818
private readonly Conv2d conv;
1919
private readonly ReLU activation;
2020
private readonly bool useRelu;
21+
private bool _disposedValue;
2122
#pragma warning restore MSML_PrivateFieldName
2223

2324
/// <summary>
@@ -57,5 +58,20 @@ public override Tensor forward(Tensor x)
5758
return x.MoveToOuterDisposeScope();
5859
}
5960
}
61+
62+
protected override void Dispose(bool disposing)
63+
{
64+
if (!_disposedValue)
65+
{
66+
if (disposing)
67+
{
68+
conv.Dispose();
69+
activation?.Dispose();
70+
_disposedValue = true;
71+
}
72+
}
73+
74+
base.Dispose(disposing);
75+
}
6076
}
6177
}

src/Microsoft.ML.TorchSharp/AutoFormerV2/FPN.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class FPN : Module<List<Tensor>, List<Tensor>>
2020
private readonly ModuleList<Module<Tensor, Tensor>> lateral_convs;
2121
private readonly ModuleList<Module<Tensor, Tensor>> fpn_convs;
2222
private readonly int numOuts;
23+
private bool _disposedValue;
2324
#pragma warning restore MSML_PrivateFieldName
2425

2526
/// <summary>
@@ -100,5 +101,20 @@ public override List<Tensor> forward(List<Tensor> inputs)
100101
return outs;
101102
}
102103
}
104+
105+
protected override void Dispose(bool disposing)
106+
{
107+
if (!_disposedValue)
108+
{
109+
if (disposing)
110+
{
111+
lateral_convs.Dispose();
112+
fpn_convs.Dispose();
113+
_disposedValue = true;
114+
}
115+
}
116+
117+
base.Dispose(disposing);
118+
}
103119
}
104120
}

src/Microsoft.ML.TorchSharp/AutoFormerV2/MBConv.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class MBConv : Module<Tensor, Tensor>
2121
private readonly GELU act2;
2222
private readonly Conv2dBN conv3;
2323
private readonly GELU act3;
24+
private bool _disposedValue;
2425
#pragma warning restore MSML_PrivateFieldName
2526

2627
/// <summary>
@@ -59,5 +60,24 @@ public override Tensor forward(Tensor x0)
5960
return x.MoveToOuterDisposeScope();
6061
}
6162
}
63+
64+
protected override void Dispose(bool disposing)
65+
{
66+
if (!_disposedValue)
67+
{
68+
if (disposing)
69+
{
70+
conv1.Dispose();
71+
act1.Dispose();
72+
conv2.Dispose();
73+
act2.Dispose();
74+
conv3.Dispose();
75+
act3.Dispose();
76+
_disposedValue = true;
77+
}
78+
}
79+
80+
base.Dispose(disposing);
81+
}
6282
}
6383
}

0 commit comments

Comments
 (0)