-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Open
Description
Description
It is currently not possible to create a tensor with a single value (FlattenedLength == 1), because it results in an empty backing array. Explicitly specifying the strides causes an exception (The provided lengths and strides would allow you to access elements outside the provided memory.), but it should not, as no memory was provided and it should be allocated correctly by the method itself.
Reproduction Steps
To reproduce, compile and run the code for each scenario. Using CreateFromShape instead of CreateFromShapeUninitialized yields the same results.
A (without explicit strides)
var src = Tensor.Create([1.0]);
Console.WriteLine($"src lengths: {String.Join(", ", src.Lengths.ToArray())}; strides: {String.Join(", ", src.Strides.ToArray())}; flattened length: {src.FlattenedLength}");
var dst = Tensor.CreateFromShapeUninitialized<double>(src.Lengths);
Console.WriteLine($"dst lengths: {String.Join(", ", dst.Lengths.ToArray())}; strides: {String.Join(", ", dst.Strides.ToArray())}; flattened length: {src.FlattenedLength}");
src.CopyTo(dst);B (with explicit strides)
var src = Tensor.Create([1.0]);
Console.WriteLine($"src lengths: {String.Join(", ", src.Lengths.ToArray())}; strides: {String.Join(", ", src.Strides.ToArray())}; flattened length: {src.FlattenedLength}");
var dst = Tensor.CreateFromShapeUninitialized<double>(src.Lengths, src.Strides); // <- notice the difference here
Console.WriteLine($"dst lengths: {String.Join(", ", dst.Lengths.ToArray())}; strides: {String.Join(", ", dst.Strides.ToArray())}; flattened length: {src.FlattenedLength}");
src.CopyTo(dst);Expected behavior
In both scenarios, the values in src are successfully copied to dst.
Actual behavior
In both scenarios, an ArgumentException is thrown, but for different reasons.
A
Output
src lengths: 1; strides: 1; flattened length: 1
dst lengths: 1; strides: 0; flattened length: 1
Exception
System.ArgumentException: 'Destination is too short. (Parameter 'destination')'
B
Output
src lengths: 1; strides: 1; flattened length: 1
Exception
System.ArgumentException: 'The provided lengths and strides would allow you to access elements outside the provided memory.'
Regression?
No response
Known Workarounds
No response
Configuration
- .NET: 10.0.3
- .NET SDK: 10.0.200-preview.0.26103.119
- OS: Windows 11 Home 25H2 26200.7922
- Architecture: x64
Other information
No response
Reactions are currently unavailable