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 137a670
Browse filesBrowse the repository at this point in the historyBrowse files
Fix Tensor correctness and accelerate contiguous operations (dotnet#134914)
## Summary
- Fix Tensor shape, storage, broadcasting, overlap, resize, and
enumeration correctness; cover the behavior with regression tests and
document intentional differences from NumPy. Empty shapes use zero
automatically computed strides so unreachable products cannot overflow
before the zero-length dimension.
- Dispatch elementwise Tensor operations to TensorPrimitives for dense
layouts and contiguous trailing slices, including compatible
outer-dimension broadcasts. Preserve indexed traversal for incompatible
layouts and ordering-sensitive reductions.
- Accelerate strided flattening, equality, non-dense random fills, and
non-dense Resize without changing logical iteration order.
- Correct vector tangent range reduction discovered by CI: use
guaranteed fused multiply-add on FMA hardware and an input-dependent
scalar fallback for cancellation-sensitive vectors on other hardware,
retaining vector evaluation for safe inputs.
## Bug reports and fuzzing findings
- `ReadOnlyTensorSpan<T>` now rejects incompatible `System.Array`
element types (dotnet#133459), and `Tensor<T>` slice pinning
starts at the slice offset (dotnet#134691).
- `ResizeTo` fills unwritten logical destination elements with
`default(T)` and rejects ambiguous overlapping destination layouts; the
behavior is documented (dotnet#128555).
- Cropped `TensorSpan<T>.FlattenTo` now copies contiguous rows rather
than individual elements, addressing the reported regression
(dotnet#121463); the exact issue repro is measured below.
- This PR addresses the `Tensor` findings [3, 6, 7, 8, 9, and 13 in the
community SharpFuzz
report](https://github.com/Mrnikbobjeff/runtime/blob/combined-sharpfuzz-net11-findings/src/libraries/Fuzzing/Findings/FINDINGS.md):
strided `ToString`, broadcast pairwise comparisons, empty `*Any`
comparisons, preserving the element when squeezing all singleton
dimensions, `ResizeTo` zero-fill, and constructor/slice exception
contracts. Squeezing retains a rank-one `[1]` shape rather than NumPy's
rank-zero shape; this intentional difference is documented in the
library README. Findings 4, 5, 10, 11, and 12 concern `TensorPrimitives`
and are not addressed here.
- **Partially addresses dotnet#125663:** contiguous non-dense
slices avoid per-element copying in several operations, but the broader
non-dense performance and allocation work remains open.
## Measurements
Local BenchmarkDotNet on Windows x64 (Ryzen 9 7950X, .NET 11).
Representative results below compare the prior implementation to this
branch except where noted:
| Scenario | Prior | Current |
| --- | ---: | ---: |
| Dense Add (64x16) | 12.69 us | 0.121 us |
| Gapped-row Add (64x16) | 11.58 us | 2.29 us |
| Broadcast-row Add (64x16) | 12.58 us | 1.91 us |
| Transposed Add (64x16) | 12.54 us | 11.60 us |
| Gapped-row FlattenTo | 5.66 us | 0.79 us |
| Gapped-row SequenceEqual | 6.41 us | 0.57 us |
| dotnet#121463 cropped FlattenTo repro (3x512x512 bytes) |
2.030 ms | 19.71 us |
The dotnet#121463 row uses the issue's original 3x2000x2048
input cropped to 3x512x512, run locally against a saved pre-optimization
assembly and this PR's Release assembly on the same .NET 11 host
(approximately 103x faster). Additional local comparisons against
equivalent indexed loops: non-dense Resize 1,909 -> 289 ns; uniform fill
5.56 -> 2.41 us; Gaussian fill 13.97 -> 10.88 us. Unary float Sin versus
equivalent scalar iteration: dense 2.88 -> 0.63 us, gapped rows 8.41 ->
3.15 us. No additional managed allocation was measured in these samples.
The transposed case is not addressed by the trailing-slice optimization.
Following review, sliced elementwise dispatch is gated centrally at 32
elements, or 16 for tensor/tensor binary operations. Against the
previously published PR head, the threshold follow-up improved
four-element gapped Add from 237 to 135 ns and Abs from 134 to 68 ns;
sixteen-element Add measured 235 to 243 ns, and retained sliced paths
had small differences in both directions. Normal-path pool returns are
retained without exceptional-path `finally` cleanup. Removing the
reinstated `finally` blocks changed permutation codegen: rank-three
permutation measured about 45 to 59 ns in the focused comparison, with
unchanged allocations. These follow-up changes are not claimed as
universally performance-neutral.
The empty-stride correction was also compared locally with the preceding
PR head across 12 dense-construction and reshape cases (ranks 1, 3, and
6, empty and nonempty). Allocation counts were unchanged; throughput
measurements were noisy across repeated runs, so no performance
improvement is claimed for this correctness fix.
CI exposed tangent range-reduction errors in the newly activated vector
path. Without fused multiply-add, `Tan(-32.986717f)` returned about
-148998.98 instead of the scalar/reference result -177349.88. The
correction is in `TensorPrimitives.Tan`, preserving Tensor's vector
dispatch and existing tangent forwarding tolerances. FMA hardware uses
guaranteed fused reduction rather than `MultiplyAddEstimate`, whose
fused behavior is not guaranteed across runtimes. Other hardware retains
non-fused vector evaluation when `abs(f) >= dn / 256`; otherwise the
affected vector uses the existing scalar fallback. The threshold scales
with the reduction count and has a documented rounding-error bound.
With FMA unavailable, dense 4x1024 float Tan measured 4.31 -> 4.70 us on
random inputs in [-1, 1] and 4.42 -> 8.08 us on [-50, 50], comparing the
original vector implementation with the accuracy correction. Against a
local global-FMA-gate implementation that scalarizes every non-FMA
input, the corresponding results were 11.03 -> 4.70 us and 13.73 -> 8.08
us. Allocation counts remain unchanged. On FMA hardware, float and
double kernels at all three widths retain the original instruction
counts and code sizes; register allocation differs. FMA throughput
measurements varied with code placement, so no precise
performance-neutrality claim is made.
## Validation
- System.Numerics.Tensors.Tests: 6,100 net11.0 and 200 net481 tests
passed in each of five local Windows x64 configurations: default
intrinsics, FMA-enabled 256-bit vectors, FMA-enabled 128-bit vectors,
non-FMA 128-bit vectors, and intrinsics disabled. ARM64/Mono and Linux
CI have not yet validated the tangent correction.
- System.Numerics.Tensors Release analyzer build succeeded with zero
warnings or errors.
- Direct float/double vector-operator probes covered 1,966,080 checks
per FMA configuration across all three widths, small and large random
inputs, broad exponents, and homogeneous cancellation-boundary vectors.
No tolerance failures occurred with or without FMA; non-FMA 256/512-bit
operators were exercised through their software vector implementations
on this host.
- Independent deterministic layout oracle: 28,049 checks passed across
1,500 cases covering sliced, permuted, gapped, broadcast, and empty
layouts.
Resolvesdotnet#128555Resolvesdotnet#133459Resolvesdotnet#134691Resolvesdotnet#121463
> [!NOTE]
> This pull request description was generated by GitHub Copilot.
---------
Co-authored-by: Copilot App <[email protected]>
/// <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
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ internal ReadOnlyTensorDimensionSpan(ReadOnlyTensorSpan<T> tensor, int dimension
Copy file name to clipboardExpand all lines: src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan_1.cs
+30-14Lines changed: 30 additions & 14 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