@@ -15,8 +15,13 @@ public class ByteTensor : ITorchTensor<byte>
15
15
[ DllImport ( "LibTorchSharp" ) ]
16
16
extern static AtenSharp . ByteTensor . HType THS_getTHTensorUnsafe ( HType handle ) ;
17
17
18
+ [ DllImport ( "LibTorchSharp" ) ]
19
+ extern static void THS_Delete ( HType handle ) ;
20
+
18
21
internal sealed class HType : SafeHandle
19
22
{
23
+ internal bool shouldClean = true ;
24
+
20
25
public HType ( IntPtr preexistingHandle , bool ownsHandle ) : base ( IntPtr . Zero , ownsHandle )
21
26
{
22
27
SetHandle ( preexistingHandle ) ;
@@ -31,8 +36,12 @@ internal HType() : base(IntPtr.Zero, true)
31
36
32
37
protected override bool ReleaseHandle ( )
33
38
{
34
- var atenTensor = new AtenSharp . ByteTensor ( THS_getTHTensorUnsafe ( this ) ) ;
35
- atenTensor . Dispose ( ) ;
39
+ // var atenTensor = new AtenSharp.ByteTensor(THS_getTHTensorUnsafe(this));
40
+ // atenTensor.Dispose();
41
+ if ( shouldClean )
42
+ {
43
+ THS_Delete ( this ) ;
44
+ }
36
45
return true ;
37
46
}
38
47
@@ -47,9 +56,10 @@ protected override void Dispose(bool disposing)
47
56
48
57
internal HType handle ;
49
58
50
- internal ByteTensor ( HType handle )
59
+ internal ByteTensor ( HType handle , bool shouldClean = true )
51
60
{
52
61
this . handle = handle ;
62
+ this . handle . shouldClean = shouldClean ;
53
63
}
54
64
55
65
/// <summary>
@@ -313,8 +323,13 @@ public class ShortTensor : ITorchTensor<short>
313
323
[ DllImport ( "LibTorchSharp" ) ]
314
324
extern static AtenSharp . ShortTensor . HType THS_getTHTensorUnsafe ( HType handle ) ;
315
325
326
+ [ DllImport ( "LibTorchSharp" ) ]
327
+ extern static void THS_Delete ( HType handle ) ;
328
+
316
329
internal sealed class HType : SafeHandle
317
330
{
331
+ internal bool shouldClean = true ;
332
+
318
333
public HType ( IntPtr preexistingHandle , bool ownsHandle ) : base ( IntPtr . Zero , ownsHandle )
319
334
{
320
335
SetHandle ( preexistingHandle ) ;
@@ -329,8 +344,12 @@ internal HType() : base(IntPtr.Zero, true)
329
344
330
345
protected override bool ReleaseHandle ( )
331
346
{
332
- var atenTensor = new AtenSharp . ShortTensor ( THS_getTHTensorUnsafe ( this ) ) ;
333
- atenTensor . Dispose ( ) ;
347
+ // var atenTensor = new AtenSharp.ShortTensor(THS_getTHTensorUnsafe(this));
348
+ // atenTensor.Dispose();
349
+ if ( shouldClean )
350
+ {
351
+ THS_Delete ( this ) ;
352
+ }
334
353
return true ;
335
354
}
336
355
@@ -345,9 +364,10 @@ protected override void Dispose(bool disposing)
345
364
346
365
internal HType handle ;
347
366
348
- internal ShortTensor ( HType handle )
367
+ internal ShortTensor ( HType handle , bool shouldClean = true )
349
368
{
350
369
this . handle = handle ;
370
+ this . handle . shouldClean = shouldClean ;
351
371
}
352
372
353
373
/// <summary>
@@ -611,8 +631,13 @@ public class IntTensor : ITorchTensor<int>
611
631
[ DllImport ( "LibTorchSharp" ) ]
612
632
extern static AtenSharp . IntTensor . HType THS_getTHTensorUnsafe ( HType handle ) ;
613
633
634
+ [ DllImport ( "LibTorchSharp" ) ]
635
+ extern static void THS_Delete ( HType handle ) ;
636
+
614
637
internal sealed class HType : SafeHandle
615
638
{
639
+ internal bool shouldClean = true ;
640
+
616
641
public HType ( IntPtr preexistingHandle , bool ownsHandle ) : base ( IntPtr . Zero , ownsHandle )
617
642
{
618
643
SetHandle ( preexistingHandle ) ;
@@ -627,8 +652,12 @@ internal HType() : base(IntPtr.Zero, true)
627
652
628
653
protected override bool ReleaseHandle ( )
629
654
{
630
- var atenTensor = new AtenSharp . IntTensor ( THS_getTHTensorUnsafe ( this ) ) ;
631
- atenTensor . Dispose ( ) ;
655
+ // var atenTensor = new AtenSharp.IntTensor(THS_getTHTensorUnsafe(this));
656
+ // atenTensor.Dispose();
657
+ if ( shouldClean )
658
+ {
659
+ THS_Delete ( this ) ;
660
+ }
632
661
return true ;
633
662
}
634
663
@@ -643,9 +672,10 @@ protected override void Dispose(bool disposing)
643
672
644
673
internal HType handle ;
645
674
646
- internal IntTensor ( HType handle )
675
+ internal IntTensor ( HType handle , bool shouldClean = true )
647
676
{
648
677
this . handle = handle ;
678
+ this . handle . shouldClean = shouldClean ;
649
679
}
650
680
651
681
/// <summary>
@@ -909,8 +939,13 @@ public class LongTensor : ITorchTensor<long>
909
939
[ DllImport ( "LibTorchSharp" ) ]
910
940
extern static AtenSharp . LongTensor . HType THS_getTHTensorUnsafe ( HType handle ) ;
911
941
942
+ [ DllImport ( "LibTorchSharp" ) ]
943
+ extern static void THS_Delete ( HType handle ) ;
944
+
912
945
internal sealed class HType : SafeHandle
913
946
{
947
+ internal bool shouldClean = true ;
948
+
914
949
public HType ( IntPtr preexistingHandle , bool ownsHandle ) : base ( IntPtr . Zero , ownsHandle )
915
950
{
916
951
SetHandle ( preexistingHandle ) ;
@@ -925,8 +960,12 @@ internal HType() : base(IntPtr.Zero, true)
925
960
926
961
protected override bool ReleaseHandle ( )
927
962
{
928
- var atenTensor = new AtenSharp . LongTensor ( THS_getTHTensorUnsafe ( this ) ) ;
929
- atenTensor . Dispose ( ) ;
963
+ // var atenTensor = new AtenSharp.LongTensor(THS_getTHTensorUnsafe(this));
964
+ // atenTensor.Dispose();
965
+ if ( shouldClean )
966
+ {
967
+ THS_Delete ( this ) ;
968
+ }
930
969
return true ;
931
970
}
932
971
@@ -941,9 +980,10 @@ protected override void Dispose(bool disposing)
941
980
942
981
internal HType handle ;
943
982
944
- internal LongTensor ( HType handle )
983
+ internal LongTensor ( HType handle , bool shouldClean = true )
945
984
{
946
985
this . handle = handle ;
986
+ this . handle . shouldClean = shouldClean ;
947
987
}
948
988
949
989
/// <summary>
@@ -1207,8 +1247,13 @@ public class DoubleTensor : ITorchTensor<double>
1207
1247
[ DllImport ( "LibTorchSharp" ) ]
1208
1248
extern static AtenSharp . DoubleTensor . HType THS_getTHTensorUnsafe ( HType handle ) ;
1209
1249
1250
+ [ DllImport ( "LibTorchSharp" ) ]
1251
+ extern static void THS_Delete ( HType handle ) ;
1252
+
1210
1253
internal sealed class HType : SafeHandle
1211
1254
{
1255
+ internal bool shouldClean = true ;
1256
+
1212
1257
public HType ( IntPtr preexistingHandle , bool ownsHandle ) : base ( IntPtr . Zero , ownsHandle )
1213
1258
{
1214
1259
SetHandle ( preexistingHandle ) ;
@@ -1223,8 +1268,12 @@ internal HType() : base(IntPtr.Zero, true)
1223
1268
1224
1269
protected override bool ReleaseHandle ( )
1225
1270
{
1226
- var atenTensor = new AtenSharp . DoubleTensor ( THS_getTHTensorUnsafe ( this ) ) ;
1227
- atenTensor . Dispose ( ) ;
1271
+ // var atenTensor = new AtenSharp.DoubleTensor(THS_getTHTensorUnsafe(this));
1272
+ // atenTensor.Dispose();
1273
+ if ( shouldClean )
1274
+ {
1275
+ THS_Delete ( this ) ;
1276
+ }
1228
1277
return true ;
1229
1278
}
1230
1279
@@ -1239,9 +1288,10 @@ protected override void Dispose(bool disposing)
1239
1288
1240
1289
internal HType handle ;
1241
1290
1242
- internal DoubleTensor ( HType handle )
1291
+ internal DoubleTensor ( HType handle , bool shouldClean = true )
1243
1292
{
1244
1293
this . handle = handle ;
1294
+ this . handle . shouldClean = shouldClean ;
1245
1295
}
1246
1296
1247
1297
/// <summary>
@@ -1505,8 +1555,13 @@ public class FloatTensor : ITorchTensor<float>
1505
1555
[ DllImport ( "LibTorchSharp" ) ]
1506
1556
extern static AtenSharp . FloatTensor . HType THS_getTHTensorUnsafe ( HType handle ) ;
1507
1557
1558
+ [ DllImport ( "LibTorchSharp" ) ]
1559
+ extern static void THS_Delete ( HType handle ) ;
1560
+
1508
1561
internal sealed class HType : SafeHandle
1509
1562
{
1563
+ internal bool shouldClean = true ;
1564
+
1510
1565
public HType ( IntPtr preexistingHandle , bool ownsHandle ) : base ( IntPtr . Zero , ownsHandle )
1511
1566
{
1512
1567
SetHandle ( preexistingHandle ) ;
@@ -1521,8 +1576,12 @@ internal HType() : base(IntPtr.Zero, true)
1521
1576
1522
1577
protected override bool ReleaseHandle ( )
1523
1578
{
1524
- var atenTensor = new AtenSharp . FloatTensor ( THS_getTHTensorUnsafe ( this ) ) ;
1525
- atenTensor . Dispose ( ) ;
1579
+ // var atenTensor = new AtenSharp.FloatTensor(THS_getTHTensorUnsafe(this));
1580
+ // atenTensor.Dispose();
1581
+ if ( shouldClean )
1582
+ {
1583
+ THS_Delete ( this ) ;
1584
+ }
1526
1585
return true ;
1527
1586
}
1528
1587
@@ -1537,9 +1596,10 @@ protected override void Dispose(bool disposing)
1537
1596
1538
1597
internal HType handle ;
1539
1598
1540
- internal FloatTensor ( HType handle )
1599
+ internal FloatTensor ( HType handle , bool shouldClean = true )
1541
1600
{
1542
1601
this . handle = handle ;
1602
+ this . handle . shouldClean = shouldClean ;
1543
1603
}
1544
1604
1545
1605
/// <summary>
@@ -1806,33 +1866,33 @@ internal enum ATenScalarMapping : short
1806
1866
1807
1867
public static class TensorExtensionMethods
1808
1868
{
1809
- internal static ITorchTensor < T > ToTorchTensor < T > ( this IntPtr rawTensor )
1869
+ internal static ITorchTensor < T > ToTorchTensor < T > ( this IntPtr rawTensor , bool shouldClean = true )
1810
1870
{
1811
1871
switch ( true )
1812
1872
{
1813
1873
case bool _ when typeof ( T ) == typeof ( byte ) :
1814
1874
{
1815
- return ( ITorchTensor < T > ) new ByteTensor ( new ByteTensor . HType ( rawTensor , true ) ) ;
1875
+ return ( ITorchTensor < T > ) new ByteTensor ( new ByteTensor . HType ( rawTensor , true ) , shouldClean ) ;
1816
1876
}
1817
1877
case bool _ when typeof ( T ) == typeof ( short ) :
1818
1878
{
1819
- return ( ITorchTensor < T > ) new ShortTensor ( new ShortTensor . HType ( rawTensor , true ) ) ;
1879
+ return ( ITorchTensor < T > ) new ShortTensor ( new ShortTensor . HType ( rawTensor , true ) , shouldClean ) ;
1820
1880
}
1821
1881
case bool _ when typeof ( T ) == typeof ( int ) :
1822
1882
{
1823
- return ( ITorchTensor < T > ) new IntTensor ( new IntTensor . HType ( rawTensor , true ) ) ;
1883
+ return ( ITorchTensor < T > ) new IntTensor ( new IntTensor . HType ( rawTensor , true ) , shouldClean ) ;
1824
1884
}
1825
1885
case bool _ when typeof ( T ) == typeof ( long ) :
1826
1886
{
1827
- return ( ITorchTensor < T > ) new LongTensor ( new LongTensor . HType ( rawTensor , true ) ) ;
1887
+ return ( ITorchTensor < T > ) new LongTensor ( new LongTensor . HType ( rawTensor , true ) , shouldClean ) ;
1828
1888
}
1829
1889
case bool _ when typeof ( T ) == typeof ( double ) :
1830
1890
{
1831
- return ( ITorchTensor < T > ) new DoubleTensor ( new DoubleTensor . HType ( rawTensor , true ) ) ;
1891
+ return ( ITorchTensor < T > ) new DoubleTensor ( new DoubleTensor . HType ( rawTensor , true ) , shouldClean ) ;
1832
1892
}
1833
1893
case bool _ when typeof ( T ) == typeof ( float ) :
1834
1894
{
1835
- return ( ITorchTensor < T > ) new FloatTensor ( new FloatTensor . HType ( rawTensor , true ) ) ;
1895
+ return ( ITorchTensor < T > ) new FloatTensor ( new FloatTensor . HType ( rawTensor , true ) , shouldClean ) ;
1836
1896
}
1837
1897
default : throw new NotImplementedException ( $ "Creating tensor of type { typeof ( T ) } is not supported.") ;
1838
1898
}
0 commit comments