@@ -73,5 +73,26 @@ public void DisposingScopeAfterDetachingDoesNothing()
73
73
AssertTensorCounts ( 0 , 0 , 1 , 0 , 0 , 1 , 1 ) ;
74
74
AssertTotalsCounts ( 0 , 0 , 1 , 0 , 0 , 1 , 1 ) ;
75
75
}
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
+ }
76
97
}
77
98
}
0 commit comments