Skip to content

Commit 1b33637

Browse files
Adding dispose-scope unit test for Parameter creation
1 parent d328e84 commit 1b33637

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

test/TorchSharpTest/TestDisposeScopesStatisticsTensor.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,26 @@ public void DisposingScopeAfterDetachingDoesNothing()
7373
AssertTensorCounts(0, 0, 1, 0, 0, 1, 1);
7474
AssertTotalsCounts(0, 0, 1, 0, 0, 1, 1);
7575
}
76+
77+
[Fact]
78+
public void ParameterCreatedFromScopedTensorOnlyCountsDisposeForParameter()
79+
{
80+
var scope = torch.NewDisposeScope();
81+
var t = torch.tensor(3.0f);
82+
AssertTensorCounts(0, 0, 1, 0, 0, 0, 1);
83+
var p = new TorchSharp.Modules.Parameter(t);
84+
//Stats should not change when converting the tensor to a parameter
85+
AssertTensorCounts(0, 0, 1, 0, 0, 0, 1);
86+
t.Dispose();
87+
//The tensor doesn't own the lifetime now, the parameter does, so again no change.
88+
AssertTensorCounts(0, 0, 1, 0, 0, 0, 1);
89+
Assert.True(t.IsInvalid);
90+
Assert.False(p.IsInvalid);
91+
Assert.Equal(3.0f, p.ToSingle());
92+
scope.Dispose();
93+
//We can count the dispose when the parameter goes away.
94+
AssertTensorCounts(0, 0, 1, 1, 0, 0, 0);
95+
Assert.True(p.IsInvalid);
96+
}
7697
}
7798
}

0 commit comments

Comments
 (0)