1414#nullable enable
1515namespace TorchSharp
1616{
17+ public static partial class TensorLeakDetector
18+ {
19+ /// <summary>
20+ /// Allows implicit conversion to torch.Tensor.<br/>
21+ /// FIXME: Declared true for default to be compatible to 0.105.1 or earlier.
22+ /// </summary>
23+ public static bool allowImplicitConversionOperator { get ; set ; } = true ;
24+ /// <summary>
25+ /// Throws an exception if implicit conversion is not allowed.
26+ /// </summary>
27+ /// <exception cref="InvalidCastException"></exception>
28+ public static void ThrowIfImplicitConversionNotAllowed ( )
29+ {
30+ if ( ! allowImplicitConversionOperator ) {
31+ throw new InvalidCastException ( "Unexpected implicit conversion to torch.Tensor." ) ;
32+ }
33+ }
34+ }
1735 public static partial class torch
1836 {
1937 /// <summary>
@@ -6321,6 +6339,7 @@ public Tensor where(Tensor condition, Tensor y)
63216339 /// <param name="value">The numeric value.</param>
63226340 public static implicit operator Tensor ( byte value )
63236341 {
6342+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
63246343 return torch . tensor ( value ) ;
63256344 }
63266345
@@ -6330,6 +6349,7 @@ public static implicit operator Tensor(byte value)
63306349 /// <param name="value">The numeric value.</param>
63316350 public static implicit operator Tensor ( sbyte value )
63326351 {
6352+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
63336353 return torch . tensor ( value ) ;
63346354 }
63356355
@@ -6339,6 +6359,7 @@ public static implicit operator Tensor(sbyte value)
63396359 /// <param name="value">The numeric value.</param>
63406360 public static implicit operator Tensor ( short value )
63416361 {
6362+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
63426363 return torch . tensor ( value ) ;
63436364 }
63446365
@@ -6348,6 +6369,7 @@ public static implicit operator Tensor(short value)
63486369 /// <param name="value">The numeric value.</param>
63496370 public static implicit operator Tensor ( int value )
63506371 {
6372+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
63516373 return torch . tensor ( value ) ;
63526374 }
63536375
@@ -6357,6 +6379,7 @@ public static implicit operator Tensor(int value)
63576379 /// <param name="value">The numeric value.</param>
63586380 public static implicit operator Tensor ( long value )
63596381 {
6382+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
63606383 return torch . tensor ( value ) ;
63616384 }
63626385
@@ -6366,6 +6389,7 @@ public static implicit operator Tensor(long value)
63666389 /// <param name="value">The numeric value.</param>
63676390 public static implicit operator Tensor ( float value )
63686391 {
6392+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
63696393 return torch . tensor ( value ) ;
63706394 }
63716395
@@ -6375,6 +6399,7 @@ public static implicit operator Tensor(float value)
63756399 /// <param name="value">The numeric value.</param>
63766400 public static implicit operator Tensor ( double value )
63776401 {
6402+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
63786403 return torch . tensor ( value ) ;
63796404 }
63806405
@@ -6384,6 +6409,7 @@ public static implicit operator Tensor(double value)
63846409 /// <param name="value">The numeric value.</param>
63856410 public static implicit operator Tensor ( bool value )
63866411 {
6412+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
63876413 return torch . tensor ( value ) ;
63886414 }
63896415
@@ -6393,6 +6419,7 @@ public static implicit operator Tensor(bool value)
63936419 /// <param name="value">The numeric value.</param>
63946420 public static implicit operator Tensor ( ( float , float ) value )
63956421 {
6422+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
63966423 return torch . tensor ( value ) ;
63976424 }
63986425
@@ -6402,6 +6429,7 @@ public static implicit operator Tensor((float, float) value)
64026429 /// <param name="value">The numeric value.</param>
64036430 public static implicit operator Tensor ( System . Numerics . Complex value )
64046431 {
6432+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
64056433 return torch . tensor ( value ) ;
64066434 }
64076435
@@ -6411,6 +6439,7 @@ public static implicit operator Tensor(System.Numerics.Complex value)
64116439 /// <param name="value">The numeric value array.</param>
64126440 public static implicit operator Tensor ( byte [ ] value )
64136441 {
6442+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
64146443 return torch . tensor ( value ) ;
64156444 }
64166445
@@ -6420,6 +6449,7 @@ public static implicit operator Tensor(byte[] value)
64206449 /// <param name="value">The numeric value array.</param>
64216450 public static implicit operator Tensor ( sbyte [ ] value )
64226451 {
6452+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
64236453 return torch . tensor ( value ) ;
64246454 }
64256455
@@ -6429,6 +6459,7 @@ public static implicit operator Tensor(sbyte[] value)
64296459 /// <param name="value">The numeric value array.</param>
64306460 public static implicit operator Tensor ( short [ ] value )
64316461 {
6462+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
64326463 return torch . tensor ( value ) ;
64336464 }
64346465
@@ -6438,6 +6469,7 @@ public static implicit operator Tensor(short[] value)
64386469 /// <param name="value">The numeric value array.</param>
64396470 public static implicit operator Tensor ( int [ ] value )
64406471 {
6472+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
64416473 return torch . tensor ( value ) ;
64426474 }
64436475
@@ -6447,6 +6479,7 @@ public static implicit operator Tensor(int[] value)
64476479 /// <param name="value">The numeric value array.</param>
64486480 public static implicit operator Tensor ( long [ ] value )
64496481 {
6482+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
64506483 return torch . tensor ( value ) ;
64516484 }
64526485
@@ -6456,6 +6489,7 @@ public static implicit operator Tensor(long[] value)
64566489 /// <param name="value">The numeric value array.</param>
64576490 public static implicit operator Tensor ( float [ ] value )
64586491 {
6492+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
64596493 return torch . tensor ( value ) ;
64606494 }
64616495
@@ -6465,6 +6499,7 @@ public static implicit operator Tensor(float[] value)
64656499 /// <param name="value">The numeric value array.</param>
64666500 public static implicit operator Tensor ( double [ ] value )
64676501 {
6502+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
64686503 return torch . tensor ( value ) ;
64696504 }
64706505
@@ -6474,6 +6509,7 @@ public static implicit operator Tensor(double[] value)
64746509 /// <param name="value">The numeric value array.</param>
64756510 public static implicit operator Tensor ( bool [ ] value )
64766511 {
6512+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
64776513 return torch . tensor ( value ) ;
64786514 }
64796515
@@ -6483,6 +6519,7 @@ public static implicit operator Tensor(bool[] value)
64836519 /// <param name="value">The numeric value array.</param>
64846520 public static implicit operator Tensor ( ( float , float ) [ ] value )
64856521 {
6522+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
64866523 return torch . tensor ( value ) ;
64876524 }
64886525
@@ -6492,6 +6529,7 @@ public static implicit operator Tensor((float, float)[] value)
64926529 /// <param name="value">The numeric value array.</param>
64936530 public static implicit operator Tensor ( System . Numerics . Complex [ ] value )
64946531 {
6532+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
64956533 return torch . tensor ( value ) ;
64966534 }
64976535
@@ -6502,6 +6540,7 @@ public static implicit operator Tensor(System.Numerics.Complex[] value)
65026540 public static implicit operator Tensor ( Scalar scalar )
65036541 {
65046542 _throw ( ) ;
6543+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
65056544 return new Tensor ( IntPtr . Zero ) ;
65066545 }
65076546
@@ -7246,6 +7285,7 @@ public static implicit operator TensorIndex(long value)
72467285 public static implicit operator Tensor ( TensorIndex value )
72477286 {
72487287 _throw ( ) ;
7288+ TensorLeakDetector . ThrowIfImplicitConversionNotAllowed ( ) ;
72497289 return new Tensor ( IntPtr . Zero ) ;
72507290 }
72517291
0 commit comments