You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 1ee1930
Browse filesBrowse the repository at this point in the historyBrowse files
[release/11.0] Backport Tensor correctness and shape consistency fixes (#135281)
Fixes Issue <!-- Issue Number -->
[#133459](#133459),
[#134691](#134691),
[#128555](#128555),
and
[#121463](#121463).
main PR <!-- Link to PR if any that fixed this in the main branch. -->
[#134914](#134914)
and
[#135060](#135060).
# Description
<!-- Give a brief summary of the issue and how the pull request is
fixing it. -->
Backport both merged Tensor fixes to `release/11.0` in their original
order:
- `137a6701515dd7427e78bc46bcdd520c8d6e3a76`: fix array element-type
validation, slice pinning, shape/storage validation, broadcasting,
overlap handling, resize zero-fill, enumeration, and numerical
operations. Restore efficient dense and contiguous-slice dispatch,
including the tangent range-reduction accuracy correction needed by that
dispatch.
- `be7b579e4230520b5c47cd956513147e13f737a6`: make shape alignment
consistent across operations, treat default rank-zero empties as
effective shape `[0]` without changing their stored metadata, fix all
ten internal `Any` span kernels, and preserve native-width counts,
overlap-safe copy ordering, and valid empty-view origins.
Both cherry-picks applied without conflicts or backport-specific source
changes. The combined implementation, regression tests, README, and API
remarks match the merged changes; release-specific compatibility
suppressions are left unchanged. No public API is added.
# Customer Impact
<!-- What is the impact to customers of not taking this fix? -->
Without these fixes, incompatible `System.Array` storage can be
interpreted as the wrong element type and read beyond its bounds.
Pinning a tensor slice can pass the parent's data, rather than the
slice's data, to native consumers such as inference engines. Other
affected operations can produce incorrect results, mishandle empty or
broadcast shapes, omit the required resize zero-fill, or corrupt
overlapping output.
The existing cropped/gapped `FlattenTo` performance regression and
per-element overhead in dense and contiguous-slice operations also
remain. Native-backed spans with more than `int.MaxValue` logical
elements retain paths that prematurely narrow counts or offsets. Taking
both fixes together keeps the initial correctness/dispatch changes
paired with the subsequent shape-consistency and native-width
corrections.
# Regression
<!-- Is this fixing a problem that was introduced in the most recent
release, ie., fixing a regression? -->
Yes, in part, but these are not all newly introduced .NET 11
regressions. The array type-safety issue and cropped `FlattenTo`
regression were introduced by the .NET 10 Tensor rewrite in
[#114927](#114927)
and remain in `release/11.0`. Slice pinning was reproduced with the
10.0.9 package. The remaining changes address existing correctness and
consistency defects rather than one recent servicing regression.
# Testing
<!-- What kind of testing has been done with the fix. -->
Local Windows x64 validation on the actual `release/11.0` backport:
- `.\build.cmd clr+libs -rc release -lc release` on the unmodified
release baseline: passed, zero warnings/errors.
- `.\dotnet.cmd build
src\libraries\System.Numerics.Tensors\System.Numerics.Tensors.slnx -c
Release /p:RuntimeConfiguration=Release` after both cherry-picks:
passed, zero warnings/errors. This built the configured `net11.0`,
`net10.0`, `netstandard2.0`, and `net462` implementations and both test
targets.
- `.\dotnet.cmd build
src\libraries\System.Numerics.Tensors\tests\System.Numerics.Tensors.Tests.csproj
-t:Test -c Release /p:RuntimeConfiguration=Release`: passed in the
default configuration.
- The same full test command with `/p:testnobuild=true --no-restore` was
rerun under each environment override below: all passed. Fresh XML
results were checked for the full counts, with no failures, errors, or
skips.
| Configuration | Environment override | net11.0 passed | net481 passed
|
| --- | --- | ---: | ---: |
| Default intrinsics | None | 6,266 | 200 |
| 256-bit vectors | `DOTNET_PreferredVectorBitWidth=256` | 6,266 | 200 |
| 128-bit vectors | `DOTNET_PreferredVectorBitWidth=128` | 6,266 | 200 |
| AVX2/FMA disabled | `DOTNET_EnableAVX2=0` | 6,266 | 200 |
| All intrinsics disabled | `DOTNET_EnableHWIntrinsic=0` | 6,266 | 200 |
Coverage includes incompatible arrays, pinning, dense/strided/broadcast
layouts, rejection before writes, empty shapes/views, high ranks,
native-width logical counts, forced copy/reduction chunk boundaries,
NaNs/ties/signed zero, and protected-memory boundaries.
Local BenchmarkDotNet ShortRun comparisons used the same built Release
runtime with distinct baseline/backport Tensor assemblies on a Ryzen 9
7950X. Assembly loading and the active vector/FMA configuration were
verified. The 10 default-intrinsic jobs and four non-FMA tangent jobs
all produced measured results:
| Scenario | Release baseline | Combined backport |
| --- | ---: | ---: |
| Dense float Add, 64x16 | 16.37 us | 0.114 us |
| Gapped-row float Add, 64x16, strides `[32, 1]` | 16.25 us | 2.05 us |
| Gapped-row float FlattenTo, same layout | 7.92 us | 0.875 us |
| Float Tan, 4,096 values in `[-1, 1]`, FMA | 1.80 us | 1.72 us |
| Float Tan, 4,096 values in `[-50, 50]`, FMA | 1.63 us | 1.53 us |
| Float Tan, same small range, no FMA | 4.98 us | 5.08 us |
| Float Tan, same wide range, no FMA | 5.60 us | 9.19 us |
No managed allocation was measured in these samples. Small timing
differences are not claimed as precise gains or proof of performance
neutrality; the non-FMA wide-range slowdown is an accuracy tradeoff,
discussed below. These checks do not replace the more extensive
measurements in the main PRs.
Linux/macOS, Arm64, Mono, NativeAOT, and browser/WASM were not locally
validated.
# Risk
<!-- Please assess the risk of taking this fix. Provide details backing
up your assessment. -->
Low-to-moderate risk. The changes are consistent backports of two fixes
already merged on main, applied without conflicts or release-specific
source adaptations. The extensive regression tests passed in all five
local configurations, and the scope is confined to
`System.Numerics.Tensors`. Remaining risk is in the intentional behavior
changes and edge cases described below, rather than the patch size.
Intentional behavior changes include accepting/rejecting shape
combinations consistently, interpreting default empties as effective
shape `[0]`, rejecting incompatible array element types and unsupported
overlapping layouts before writes, and zero-filling newly exposed
resized elements. Consumers relying on the previous inconsistent
behavior can be affected. There is no compatibility switch.
On hardware without FMA, cancellation-sensitive tangent vectors now use
scalar evaluation to avoid inaccurate results. The wide-range local
sample increased from 5.60 to 9.19 us, approximately 64%; this is an
acknowledged throughput cost for correctness, not a performance-neutral
change. FMA-capable hardware retains fused vector range reduction.
Breaking-change documentation is already tracked by
[dotnet/docs#56290](dotnet/docs#56290) and
[dotnet/docs#56305](dotnet/docs#56305). Both
currently name .NET 12 Preview 1; their shipment-version metadata needs
to be aligned with the approved .NET 11 delivery if this backport is
accepted.
# Package authoring no longer needed in .NET 9
IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet
package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older
versions.
> [!NOTE]
> This pull request description was generated with assistance from
GitHub Copilot.
---------
Co-authored-by: Copilot App <[email protected]>
Copy file name to clipboardExpand all lines: src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Common/TensorPrimitives.IIndexOfMinMaxOperator.cs
/// <exception cref="ArgumentException"><paramref name="destination" /> is shorter than the source tensor.</exception>
53
-
/// <remarks>This method copies all of the source tensor to <paramref name="destination" /> even if they overlap.</remarks>
52
+
/// <exception cref="ArgumentException"><paramref name="destination" /> is shorter than the source tensor, or the source and destination overlap in an unsupported layout.</exception>
53
+
/// <remarks>Overlapping dense tensors with equal element counts and identical views are supported. Other overlapping layouts are rejected before copying.</remarks>
54
54
voidCopyTo(scopedinTensorSpan<T>destination);
55
55
56
56
/// <summary>Flattens the contents of the tensor into a destination span.</summary>
/// <exception cref="ArgumentException"><paramref name="destination" /> is shorter than the source tensor.</exception>
59
-
/// <remarks>This method copies all of the source tensor to <paramref name="destination" /> even if they overlap.</remarks>
58
+
/// <exception cref="ArgumentException"><paramref name="destination" /> is shorter than the source tensor, or the source and destination overlap in an unsupported layout.</exception>
59
+
/// <remarks>Overlapping dense tensors are supported. Other overlapping layouts are rejected before copying.</remarks>
60
60
voidFlattenTo(scoped Span<T>destination);
61
61
62
62
/// <summary>Returns a span that can be used to access the flattened elements for a given dimension.</summary>
@@ -110,17 +110,19 @@ public interface IReadOnlyTensor<TSelf, T> : IReadOnlyTensor
110
110
/// <summary>Attempts to copy the contents of this tensor into a destination tensor span and returns a value to indicate whether or not the operation succeeded.</summary>
111
111
/// <param name="destination">The target of the copy operation.</param>
112
112
/// <returns><see langword="true"/> if the copy operation succeeded; otherwise, <c>false</c>.</returns>
113
+
/// <exception cref="ArgumentException">The source and <paramref name="destination" /> overlap in an unsupported layout.</exception>
113
114
/// <remarks>
114
-
/// <para>If the source and <paramref name="destination" /> overlap, the entirety of the source is handled as if it was copied to a temporary location before it is copied to <paramref name="destination" />.</para>
115
+
/// <para>Overlapping dense tensors with equal element counts and identical views are supported. Other overlapping layouts throw <see cref="ArgumentException" /> before copying.</para>
115
116
/// <para>If the <paramref name="destination" /> length is shorter than the source, no items are copied and the method returns <c>false</c>.</para>
116
117
/// </remarks>
117
118
boolTryCopyTo(scopedinTensorSpan<T>destination);
118
119
119
120
/// <summary>Attempts to flatten the contents of this tensor into a destination span and returns a value to indicate whether or not the operation succeeded.</summary>
120
121
/// <param name="destination">The target of the copy operation.</param>
121
122
/// <returns><see langword="true"/> if the copy operation succeeded; otherwise, <c>false</c>.</returns>
123
+
/// <exception cref="ArgumentException">The source and <paramref name="destination" /> overlap in an unsupported layout.</exception>
122
124
/// <remarks>
123
-
/// <para>If the source and <paramref name="destination" /> overlap, the entirety of the source is handled as if it was flattened to a temporary location before it is copied to <paramref name="destination" />.</para>
125
+
/// <para>Overlapping dense tensors are supported. Other overlapping layouts throw <see cref="ArgumentException" /> before copying.</para>
124
126
/// <para>If the <paramref name="destination" /> length is shorter than the source, no items are copied and the method returns <c>false</c>.</para>
Copy file name to clipboardExpand all lines: src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorDimensionSpan_1.cs
+11-3Lines changed: 11 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -18,16 +18,24 @@ public readonly ref struct ReadOnlyTensorDimensionSpan<T>
Copy file name to clipboardExpand all lines: src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan_1.cs
+31-15Lines changed: 31 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -148,8 +148,11 @@ public ReadOnlyTensorSpan(ReadOnlySpan<T> span, scoped ReadOnlySpan<nint> length
148
148
/// <para>Returns default when <paramref name="array"/> is null.</para>
149
149
/// <para>The created tensor span has a single dimension that is the same length as <paramref name="array" />.</para>
150
150
/// </remarks>
151
+
/// <exception cref="ArrayTypeMismatchException">The type of <paramref name="array"/> is not compatible with an array of <typeparamref name="T"/>.</exception>
@@ -175,8 +178,11 @@ public ReadOnlyTensorSpan(Array? array)
175
178
/// * <paramref name="strides" /> is not empty and contains an element that is negative.
176
179
/// * <paramref name="strides" /> is not empty and contains an element that is zero in a non leading position.
177
180
/// </exception>
181
+
/// <exception cref="ArrayTypeMismatchException">The type of <paramref name="array"/> is not compatible with an array of <typeparamref name="T"/>.</exception>
0 commit comments