1
-
2
-
3
-
4
-
5
- using System ;
1
+ using System ;
6
2
using System . Linq ;
7
3
using System . Runtime . InteropServices ;
8
4
using System . Text ;
9
5
10
6
namespace TorchSharp . Tensor {
11
7
12
-
13
8
/// <summary>
14
9
/// Tensor of type Byte.
15
10
/// This tensor maps to a Torch variable (see torch/csrc/autograd/variable.h).
@@ -181,6 +176,22 @@ public string Device
181
176
}
182
177
}
183
178
179
+ [ DllImport ( "LibTorchSharp" ) ]
180
+ extern static IntPtr THS_cpu ( IntPtr handle ) ;
181
+
182
+ public ITorchTensor < byte > Cpu ( )
183
+ {
184
+ return new ByteTensor ( THS_cpu ( handle ) ) ;
185
+ }
186
+
187
+ [ DllImport ( "LibTorchSharp" ) ]
188
+ extern static IntPtr THS_cuda ( IntPtr handle ) ;
189
+
190
+ public ITorchTensor < byte > Cuda ( )
191
+ {
192
+ return new ByteTensor ( THS_cuda ( handle ) ) ;
193
+ }
194
+
184
195
/// <summary>
185
196
/// Retrieves the size of the specified dimension in the tensor.
186
197
/// </summary>
@@ -344,7 +355,6 @@ public override string ToString()
344
355
return sb . ToString ( ) ;
345
356
}
346
357
}
347
-
348
358
/// <summary>
349
359
/// Tensor of type Short.
350
360
/// This tensor maps to a Torch variable (see torch/csrc/autograd/variable.h).
@@ -516,6 +526,22 @@ public string Device
516
526
}
517
527
}
518
528
529
+ [ DllImport ( "LibTorchSharp" ) ]
530
+ extern static IntPtr THS_cpu ( IntPtr handle ) ;
531
+
532
+ public ITorchTensor < short > Cpu ( )
533
+ {
534
+ return new ShortTensor ( THS_cpu ( handle ) ) ;
535
+ }
536
+
537
+ [ DllImport ( "LibTorchSharp" ) ]
538
+ extern static IntPtr THS_cuda ( IntPtr handle ) ;
539
+
540
+ public ITorchTensor < short > Cuda ( )
541
+ {
542
+ return new ShortTensor ( THS_cuda ( handle ) ) ;
543
+ }
544
+
519
545
/// <summary>
520
546
/// Retrieves the size of the specified dimension in the tensor.
521
547
/// </summary>
@@ -679,7 +705,6 @@ public override string ToString()
679
705
return sb . ToString ( ) ;
680
706
}
681
707
}
682
-
683
708
/// <summary>
684
709
/// Tensor of type Int.
685
710
/// This tensor maps to a Torch variable (see torch/csrc/autograd/variable.h).
@@ -851,6 +876,22 @@ public string Device
851
876
}
852
877
}
853
878
879
+ [ DllImport ( "LibTorchSharp" ) ]
880
+ extern static IntPtr THS_cpu ( IntPtr handle ) ;
881
+
882
+ public ITorchTensor < int > Cpu ( )
883
+ {
884
+ return new IntTensor ( THS_cpu ( handle ) ) ;
885
+ }
886
+
887
+ [ DllImport ( "LibTorchSharp" ) ]
888
+ extern static IntPtr THS_cuda ( IntPtr handle ) ;
889
+
890
+ public ITorchTensor < int > Cuda ( )
891
+ {
892
+ return new IntTensor ( THS_cuda ( handle ) ) ;
893
+ }
894
+
854
895
/// <summary>
855
896
/// Retrieves the size of the specified dimension in the tensor.
856
897
/// </summary>
@@ -1014,7 +1055,6 @@ public override string ToString()
1014
1055
return sb . ToString ( ) ;
1015
1056
}
1016
1057
}
1017
-
1018
1058
/// <summary>
1019
1059
/// Tensor of type Long.
1020
1060
/// This tensor maps to a Torch variable (see torch/csrc/autograd/variable.h).
@@ -1186,6 +1226,22 @@ public string Device
1186
1226
}
1187
1227
}
1188
1228
1229
+ [ DllImport ( "LibTorchSharp" ) ]
1230
+ extern static IntPtr THS_cpu ( IntPtr handle ) ;
1231
+
1232
+ public ITorchTensor < long > Cpu ( )
1233
+ {
1234
+ return new LongTensor ( THS_cpu ( handle ) ) ;
1235
+ }
1236
+
1237
+ [ DllImport ( "LibTorchSharp" ) ]
1238
+ extern static IntPtr THS_cuda ( IntPtr handle ) ;
1239
+
1240
+ public ITorchTensor < long > Cuda ( )
1241
+ {
1242
+ return new LongTensor ( THS_cuda ( handle ) ) ;
1243
+ }
1244
+
1189
1245
/// <summary>
1190
1246
/// Retrieves the size of the specified dimension in the tensor.
1191
1247
/// </summary>
@@ -1349,7 +1405,6 @@ public override string ToString()
1349
1405
return sb . ToString ( ) ;
1350
1406
}
1351
1407
}
1352
-
1353
1408
/// <summary>
1354
1409
/// Tensor of type Double.
1355
1410
/// This tensor maps to a Torch variable (see torch/csrc/autograd/variable.h).
@@ -1521,6 +1576,22 @@ public string Device
1521
1576
}
1522
1577
}
1523
1578
1579
+ [ DllImport ( "LibTorchSharp" ) ]
1580
+ extern static IntPtr THS_cpu ( IntPtr handle ) ;
1581
+
1582
+ public ITorchTensor < double > Cpu ( )
1583
+ {
1584
+ return new DoubleTensor ( THS_cpu ( handle ) ) ;
1585
+ }
1586
+
1587
+ [ DllImport ( "LibTorchSharp" ) ]
1588
+ extern static IntPtr THS_cuda ( IntPtr handle ) ;
1589
+
1590
+ public ITorchTensor < double > Cuda ( )
1591
+ {
1592
+ return new DoubleTensor ( THS_cuda ( handle ) ) ;
1593
+ }
1594
+
1524
1595
/// <summary>
1525
1596
/// Retrieves the size of the specified dimension in the tensor.
1526
1597
/// </summary>
@@ -1684,7 +1755,6 @@ public override string ToString()
1684
1755
return sb . ToString ( ) ;
1685
1756
}
1686
1757
}
1687
-
1688
1758
/// <summary>
1689
1759
/// Tensor of type Float.
1690
1760
/// This tensor maps to a Torch variable (see torch/csrc/autograd/variable.h).
@@ -1856,6 +1926,22 @@ public string Device
1856
1926
}
1857
1927
}
1858
1928
1929
+ [ DllImport ( "LibTorchSharp" ) ]
1930
+ extern static IntPtr THS_cpu ( IntPtr handle ) ;
1931
+
1932
+ public ITorchTensor < float > Cpu ( )
1933
+ {
1934
+ return new FloatTensor ( THS_cpu ( handle ) ) ;
1935
+ }
1936
+
1937
+ [ DllImport ( "LibTorchSharp" ) ]
1938
+ extern static IntPtr THS_cuda ( IntPtr handle ) ;
1939
+
1940
+ public ITorchTensor < float > Cuda ( )
1941
+ {
1942
+ return new FloatTensor ( THS_cuda ( handle ) ) ;
1943
+ }
1944
+
1859
1945
/// <summary>
1860
1946
/// Retrieves the size of the specified dimension in the tensor.
1861
1947
/// </summary>
@@ -2019,7 +2105,6 @@ public override string ToString()
2019
2105
return sb . ToString ( ) ;
2020
2106
}
2021
2107
}
2022
-
2023
2108
2024
2109
public enum ATenScalarMapping : sbyte
2025
2110
{
@@ -2037,37 +2122,30 @@ internal static ITorchTensor<T> ToTorchTensor<T>(this IntPtr rawTensor)
2037
2122
{
2038
2123
switch ( true )
2039
2124
{
2040
-
2041
2125
case bool _ when typeof ( T ) == typeof ( byte ) :
2042
2126
{
2043
2127
return new ByteTensor ( rawTensor ) as ITorchTensor < T > ;
2044
2128
}
2045
-
2046
2129
case bool _ when typeof ( T ) == typeof ( short ) :
2047
2130
{
2048
2131
return new ShortTensor ( rawTensor ) as ITorchTensor < T > ;
2049
2132
}
2050
-
2051
2133
case bool _ when typeof ( T ) == typeof ( int ) :
2052
2134
{
2053
2135
return new IntTensor ( rawTensor ) as ITorchTensor < T > ;
2054
2136
}
2055
-
2056
2137
case bool _ when typeof ( T ) == typeof ( long ) :
2057
2138
{
2058
2139
return new LongTensor ( rawTensor ) as ITorchTensor < T > ;
2059
2140
}
2060
-
2061
2141
case bool _ when typeof ( T ) == typeof ( double ) :
2062
2142
{
2063
2143
return new DoubleTensor ( rawTensor ) as ITorchTensor < T > ;
2064
2144
}
2065
-
2066
2145
case bool _ when typeof ( T ) == typeof ( float ) :
2067
2146
{
2068
2147
return new FloatTensor ( rawTensor ) as ITorchTensor < T > ;
2069
2148
}
2070
-
2071
2149
default : throw new NotImplementedException ( $ "Creating tensor of type { typeof ( T ) } is not supported.") ;
2072
2150
}
2073
2151
}
@@ -2076,37 +2154,30 @@ public static ITorchTensor<T> ToTorchTensor<T>(this T[] rawArray, long[] dimensi
2076
2154
{
2077
2155
switch ( true )
2078
2156
{
2079
-
2080
2157
case bool _ when typeof ( T ) == typeof ( byte ) :
2081
2158
{
2082
2159
return ByteTensor . From ( rawArray as byte [ ] , dimensions ) as ITorchTensor < T > ;
2083
2160
}
2084
-
2085
2161
case bool _ when typeof ( T ) == typeof ( short ) :
2086
2162
{
2087
2163
return ShortTensor . From ( rawArray as short [ ] , dimensions ) as ITorchTensor < T > ;
2088
2164
}
2089
-
2090
2165
case bool _ when typeof ( T ) == typeof ( int ) :
2091
2166
{
2092
2167
return IntTensor . From ( rawArray as int [ ] , dimensions ) as ITorchTensor < T > ;
2093
2168
}
2094
-
2095
2169
case bool _ when typeof ( T ) == typeof ( long ) :
2096
2170
{
2097
2171
return LongTensor . From ( rawArray as long [ ] , dimensions ) as ITorchTensor < T > ;
2098
2172
}
2099
-
2100
2173
case bool _ when typeof ( T ) == typeof ( double ) :
2101
2174
{
2102
2175
return DoubleTensor . From ( rawArray as double [ ] , dimensions ) as ITorchTensor < T > ;
2103
2176
}
2104
-
2105
2177
case bool _ when typeof ( T ) == typeof ( float ) :
2106
2178
{
2107
2179
return FloatTensor . From ( rawArray as float [ ] , dimensions ) as ITorchTensor < T > ;
2108
2180
}
2109
-
2110
2181
default : throw new NotImplementedException ( $ "Creating tensor of type { typeof ( T ) } is not supported.") ;
2111
2182
}
2112
2183
}
@@ -2115,37 +2186,30 @@ public static ITorchTensor<T> ToTorchTensor<T>(this T scalar)
2115
2186
{
2116
2187
switch ( true )
2117
2188
{
2118
-
2119
2189
case bool _ when typeof ( T ) == typeof ( byte ) :
2120
2190
{
2121
2191
return ByteTensor . From ( ( byte ) ( object ) scalar ) as ITorchTensor < T > ;
2122
2192
}
2123
-
2124
2193
case bool _ when typeof ( T ) == typeof ( short ) :
2125
2194
{
2126
2195
return ShortTensor . From ( ( short ) ( object ) scalar ) as ITorchTensor < T > ;
2127
2196
}
2128
-
2129
2197
case bool _ when typeof ( T ) == typeof ( int ) :
2130
2198
{
2131
2199
return IntTensor . From ( ( int ) ( object ) scalar ) as ITorchTensor < T > ;
2132
2200
}
2133
-
2134
2201
case bool _ when typeof ( T ) == typeof ( long ) :
2135
2202
{
2136
2203
return LongTensor . From ( ( long ) ( object ) scalar ) as ITorchTensor < T > ;
2137
2204
}
2138
-
2139
2205
case bool _ when typeof ( T ) == typeof ( double ) :
2140
2206
{
2141
2207
return DoubleTensor . From ( ( double ) ( object ) scalar ) as ITorchTensor < T > ;
2142
2208
}
2143
-
2144
2209
case bool _ when typeof ( T ) == typeof ( float ) :
2145
2210
{
2146
2211
return FloatTensor . From ( ( float ) ( object ) scalar ) as ITorchTensor < T > ;
2147
2212
}
2148
-
2149
2213
default : throw new NotImplementedException ( $ "Creating tensor of type { typeof ( T ) } is not supported.") ;
2150
2214
}
2151
2215
}
0 commit comments