Skip to content

Fix Tensor stride computation for single-element (length <= 1) tensors#125403

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-tensor-create-from-shape
Draft

Fix Tensor stride computation for single-element (length <= 1) tensors#125403
Copilot wants to merge 2 commits intomainfrom
copilot/fix-tensor-create-from-shape

Conversation

Copy link
Contributor

Copilot AI commented Mar 10, 2026

Description

Two shortcut TensorShape.Create overloads hardcoded strides: [1] instead of using stride = (linearLength > 1) ? 1 : 0, bypassing the length == 1 special case that the normal stride computation path handles correctly. This made it impossible to work with single-element tensors:

var src = Tensor.Create([1.0]);          // stride incorrectly set to 1
var dst = Tensor.CreateFromShapeUninitialized<double>(src.Lengths);  // stride correctly 0
src.CopyTo(dst);  // throws: "Destination is too short"

// Passing explicit strides also fails
var dst2 = Tensor.CreateFromShapeUninitialized<double>(src.Lengths, src.Strides);
// throws: "The provided lengths and strides would allow you to access elements outside the provided memory."

Changes

  • TensorShape.Create<T>(T[]? array) — Compute stride as (linearLength > 1) ? 1 : 0 and set IsBroadcast flag when stride is 0. Affects Tensor.Create(T[]), TensorSpan<T>(T[]), ReadOnlyTensorSpan<T>(T[]).
  • TensorShape.Create<T>(ref readonly T, nint, bool) — Same fix. Affects TensorSpan<T>(Span<T>) and ReadOnlyTensorSpan<T>(ReadOnlySpan<T>).
  • Existing test corrections — Two tests in TensorSpanTests.cs and ReadOnlyTensorSpanTests.cs asserted stride == 1 for empty arrays; corrected to expect 0 (consistent with auto-computed strides).
  • Regression testTensorCreateSingleElementTests covers Create, CreateFromShape, CreateFromShapeUninitialized (with and without explicit strides), CopyTo, and span-based construction for single-element tensors.
Original prompt

This section details on the original issue you should resolve

<issue_title>Tensor.CreateFromShape and Tensor.CreateFromShapeUninitialized do not work correctly when FlattenedLength == 1</issue_title>
<issue_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</issue_description>

<agent_instructions>Ensure that Tensor.Create(T[]) correctly initializes the stride to 0 if length is 1. Add a corresponding regression test and check if any other Create APIs have a similar issue where they short circuit the normal strides computation logic and so fail to account for the length == 1 special case.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@tannergooding Looks like this is rather a bug in `Tensor.Create(T[])` where it fails to do `stride = (length > 1) ? length : 0` and only does `stride = length`

Fix is simple, will have it up momentarily.</comment_new>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/runtime-infrastructure
See info in area-owners.md if you want to be subscribed.

Two shortcut TensorShape.Create overloads hardcoded strides: [1] instead
of correctly using stride 0 when linearLength <= 1, causing:
- Tensor.Create(T[]) with single-element arrays had stride 1 instead of 0
- CreateFromShapeUninitialized with explicit strides [1] for length 1 threw
- CopyTo between tensors with mismatched strides failed

Fixed TensorShape.Create<T>(T[]?) and TensorShape.Create<T>(ref readonly T, nint, bool)
to use stride 0 and set IsBroadcast flag when linearLength <= 1.
Updated existing tests that expected incorrect stride values.
Added regression test covering the reported scenarios.

Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Tensor.CreateFromShape for FlattenedLength of one Fix Tensor stride computation for single-element (length <= 1) tensors Mar 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Tensor.CreateFromShape and Tensor.CreateFromShapeUninitialized do not work correctly when FlattenedLength == 1

2 participants