1
- using System ;
1
+
2
+
3
+
4
+
5
+ using System ;
2
6
using System . Linq ;
3
7
using System . Runtime . InteropServices ;
4
8
using System . Text ;
5
9
6
10
namespace TorchSharp . Tensor {
7
11
12
+
8
13
/// <summary>
9
14
/// Tensor of type Byte.
10
15
/// This tensor maps to a Torch variable (see torch/csrc/autograd/variable.h).
@@ -179,7 +184,7 @@ static public ITorchTensor<byte> Ones(long[] size, string device = "cpu", bool r
179
184
{
180
185
fixed ( long * psizes = size )
181
186
{
182
- return new ByteTensor ( THS_ones ( ( IntPtr ) psizes , size . Length , ( short ) ATenScalarMapping . Byte , device , requiresGrad ) ) ;
187
+ return new ByteTensor ( THS_ones ( ( IntPtr ) psizes , size . Length , ( sbyte ) ATenScalarMapping . Byte , device , requiresGrad ) ) ;
183
188
}
184
189
}
185
190
}
@@ -196,7 +201,7 @@ static public ITorchTensor<byte> RandomN(long[] size, string device = "cpu", boo
196
201
{
197
202
fixed ( long * psizes = size )
198
203
{
199
- return new ByteTensor ( THS_randn ( ( IntPtr ) psizes , size . Length , ( short ) ATenScalarMapping . Byte , device , requiresGrad ) ) ;
204
+ return new ByteTensor ( THS_randn ( ( IntPtr ) psizes , size . Length , ( sbyte ) ATenScalarMapping . Byte , device , requiresGrad ) ) ;
200
205
}
201
206
}
202
207
}
@@ -292,6 +297,7 @@ public override string ToString()
292
297
return sb . ToString ( ) ;
293
298
}
294
299
}
300
+
295
301
/// <summary>
296
302
/// Tensor of type Short.
297
303
/// This tensor maps to a Torch variable (see torch/csrc/autograd/variable.h).
@@ -466,7 +472,7 @@ static public ITorchTensor<short> Ones(long[] size, string device = "cpu", bool
466
472
{
467
473
fixed ( long * psizes = size )
468
474
{
469
- return new ShortTensor ( THS_ones ( ( IntPtr ) psizes , size . Length , ( short ) ATenScalarMapping . Short , device , requiresGrad ) ) ;
475
+ return new ShortTensor ( THS_ones ( ( IntPtr ) psizes , size . Length , ( sbyte ) ATenScalarMapping . Short , device , requiresGrad ) ) ;
470
476
}
471
477
}
472
478
}
@@ -483,7 +489,7 @@ static public ITorchTensor<short> RandomN(long[] size, string device = "cpu", bo
483
489
{
484
490
fixed ( long * psizes = size )
485
491
{
486
- return new ShortTensor ( THS_randn ( ( IntPtr ) psizes , size . Length , ( short ) ATenScalarMapping . Short , device , requiresGrad ) ) ;
492
+ return new ShortTensor ( THS_randn ( ( IntPtr ) psizes , size . Length , ( sbyte ) ATenScalarMapping . Short , device , requiresGrad ) ) ;
487
493
}
488
494
}
489
495
}
@@ -579,6 +585,7 @@ public override string ToString()
579
585
return sb . ToString ( ) ;
580
586
}
581
587
}
588
+
582
589
/// <summary>
583
590
/// Tensor of type Int.
584
591
/// This tensor maps to a Torch variable (see torch/csrc/autograd/variable.h).
@@ -753,7 +760,7 @@ static public ITorchTensor<int> Ones(long[] size, string device = "cpu", bool re
753
760
{
754
761
fixed ( long * psizes = size )
755
762
{
756
- return new IntTensor ( THS_ones ( ( IntPtr ) psizes , size . Length , ( short ) ATenScalarMapping . Int , device , requiresGrad ) ) ;
763
+ return new IntTensor ( THS_ones ( ( IntPtr ) psizes , size . Length , ( sbyte ) ATenScalarMapping . Int , device , requiresGrad ) ) ;
757
764
}
758
765
}
759
766
}
@@ -770,7 +777,7 @@ static public ITorchTensor<int> RandomN(long[] size, string device = "cpu", bool
770
777
{
771
778
fixed ( long * psizes = size )
772
779
{
773
- return new IntTensor ( THS_randn ( ( IntPtr ) psizes , size . Length , ( short ) ATenScalarMapping . Int , device , requiresGrad ) ) ;
780
+ return new IntTensor ( THS_randn ( ( IntPtr ) psizes , size . Length , ( sbyte ) ATenScalarMapping . Int , device , requiresGrad ) ) ;
774
781
}
775
782
}
776
783
}
@@ -866,6 +873,7 @@ public override string ToString()
866
873
return sb . ToString ( ) ;
867
874
}
868
875
}
876
+
869
877
/// <summary>
870
878
/// Tensor of type Long.
871
879
/// This tensor maps to a Torch variable (see torch/csrc/autograd/variable.h).
@@ -1040,7 +1048,7 @@ static public ITorchTensor<long> Ones(long[] size, string device = "cpu", bool r
1040
1048
{
1041
1049
fixed ( long * psizes = size )
1042
1050
{
1043
- return new LongTensor ( THS_ones ( ( IntPtr ) psizes , size . Length , ( short ) ATenScalarMapping . Long , device , requiresGrad ) ) ;
1051
+ return new LongTensor ( THS_ones ( ( IntPtr ) psizes , size . Length , ( sbyte ) ATenScalarMapping . Long , device , requiresGrad ) ) ;
1044
1052
}
1045
1053
}
1046
1054
}
@@ -1057,7 +1065,7 @@ static public ITorchTensor<long> RandomN(long[] size, string device = "cpu", boo
1057
1065
{
1058
1066
fixed ( long * psizes = size )
1059
1067
{
1060
- return new LongTensor ( THS_randn ( ( IntPtr ) psizes , size . Length , ( short ) ATenScalarMapping . Long , device , requiresGrad ) ) ;
1068
+ return new LongTensor ( THS_randn ( ( IntPtr ) psizes , size . Length , ( sbyte ) ATenScalarMapping . Long , device , requiresGrad ) ) ;
1061
1069
}
1062
1070
}
1063
1071
}
@@ -1153,6 +1161,7 @@ public override string ToString()
1153
1161
return sb . ToString ( ) ;
1154
1162
}
1155
1163
}
1164
+
1156
1165
/// <summary>
1157
1166
/// Tensor of type Double.
1158
1167
/// This tensor maps to a Torch variable (see torch/csrc/autograd/variable.h).
@@ -1327,7 +1336,7 @@ static public ITorchTensor<double> Ones(long[] size, string device = "cpu", bool
1327
1336
{
1328
1337
fixed ( long * psizes = size )
1329
1338
{
1330
- return new DoubleTensor ( THS_ones ( ( IntPtr ) psizes , size . Length , ( short ) ATenScalarMapping . Double , device , requiresGrad ) ) ;
1339
+ return new DoubleTensor ( THS_ones ( ( IntPtr ) psizes , size . Length , ( sbyte ) ATenScalarMapping . Double , device , requiresGrad ) ) ;
1331
1340
}
1332
1341
}
1333
1342
}
@@ -1344,7 +1353,7 @@ static public ITorchTensor<double> RandomN(long[] size, string device = "cpu", b
1344
1353
{
1345
1354
fixed ( long * psizes = size )
1346
1355
{
1347
- return new DoubleTensor ( THS_randn ( ( IntPtr ) psizes , size . Length , ( short ) ATenScalarMapping . Double , device , requiresGrad ) ) ;
1356
+ return new DoubleTensor ( THS_randn ( ( IntPtr ) psizes , size . Length , ( sbyte ) ATenScalarMapping . Double , device , requiresGrad ) ) ;
1348
1357
}
1349
1358
}
1350
1359
}
@@ -1440,6 +1449,7 @@ public override string ToString()
1440
1449
return sb . ToString ( ) ;
1441
1450
}
1442
1451
}
1452
+
1443
1453
/// <summary>
1444
1454
/// Tensor of type Float.
1445
1455
/// This tensor maps to a Torch variable (see torch/csrc/autograd/variable.h).
@@ -1614,7 +1624,7 @@ static public ITorchTensor<float> Ones(long[] size, string device = "cpu", bool
1614
1624
{
1615
1625
fixed ( long * psizes = size )
1616
1626
{
1617
- return new FloatTensor ( THS_ones ( ( IntPtr ) psizes , size . Length , ( short ) ATenScalarMapping . Float , device , requiresGrad ) ) ;
1627
+ return new FloatTensor ( THS_ones ( ( IntPtr ) psizes , size . Length , ( sbyte ) ATenScalarMapping . Float , device , requiresGrad ) ) ;
1618
1628
}
1619
1629
}
1620
1630
}
@@ -1631,7 +1641,7 @@ static public ITorchTensor<float> RandomN(long[] size, string device = "cpu", bo
1631
1641
{
1632
1642
fixed ( long * psizes = size )
1633
1643
{
1634
- return new FloatTensor ( THS_randn ( ( IntPtr ) psizes , size . Length , ( short ) ATenScalarMapping . Float , device , requiresGrad ) ) ;
1644
+ return new FloatTensor ( THS_randn ( ( IntPtr ) psizes , size . Length , ( sbyte ) ATenScalarMapping . Float , device , requiresGrad ) ) ;
1635
1645
}
1636
1646
}
1637
1647
}
@@ -1727,8 +1737,9 @@ public override string ToString()
1727
1737
return sb . ToString ( ) ;
1728
1738
}
1729
1739
}
1740
+
1730
1741
1731
- internal enum ATenScalarMapping : short
1742
+ public enum ATenScalarMapping : short
1732
1743
{
1733
1744
Byte = 0 ,
1734
1745
Short = 2 ,
@@ -1744,30 +1755,37 @@ internal static ITorchTensor<T> ToTorchTensor<T>(this IntPtr rawTensor)
1744
1755
{
1745
1756
switch ( true )
1746
1757
{
1758
+
1747
1759
case bool _ when typeof ( T ) == typeof ( byte ) :
1748
1760
{
1749
1761
return new ByteTensor ( rawTensor ) as ITorchTensor < T > ;
1750
1762
}
1763
+
1751
1764
case bool _ when typeof ( T ) == typeof ( short ) :
1752
1765
{
1753
1766
return new ShortTensor ( rawTensor ) as ITorchTensor < T > ;
1754
1767
}
1768
+
1755
1769
case bool _ when typeof ( T ) == typeof ( int ) :
1756
1770
{
1757
1771
return new IntTensor ( rawTensor ) as ITorchTensor < T > ;
1758
1772
}
1773
+
1759
1774
case bool _ when typeof ( T ) == typeof ( long ) :
1760
1775
{
1761
1776
return new LongTensor ( rawTensor ) as ITorchTensor < T > ;
1762
1777
}
1778
+
1763
1779
case bool _ when typeof ( T ) == typeof ( double ) :
1764
1780
{
1765
1781
return new DoubleTensor ( rawTensor ) as ITorchTensor < T > ;
1766
1782
}
1783
+
1767
1784
case bool _ when typeof ( T ) == typeof ( float ) :
1768
1785
{
1769
1786
return new FloatTensor ( rawTensor ) as ITorchTensor < T > ;
1770
1787
}
1788
+
1771
1789
default : throw new NotImplementedException ( $ "Creating tensor of type { typeof ( T ) } is not supported.") ;
1772
1790
}
1773
1791
}
0 commit comments