Repository navigation
Move errors out of line, unroll stride normalization, extend unsafe_stridedview - #68
Merged
Merged
Conversation
…tridedview - Throw errors from `@noinline` helpers, with `LazyString` messages where they interpolate, so that building them does not end up in the callers. - `permutedims` indexes the size and strides with `@inbounds`, since the permutation has been checked; same for `_strides(::PermutedDimsArray)`. - `unsafe_stridedview(a::StridedView, size, strides, offset)` replaces the layout of a view while keeping its element type, parent and operation. The strides are used as given, so this also supports zero strides. - `_normalizestrides` is a fully unrolled scan, with the same results as before (checked against the previous implementation in the tests). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…utation check - Slicing a `StridedView` (`getindex` with ranges and colons, `view` and `sview`) now checks bounds; previously an out-of-range slice silently produced a view outside the parent. The check can be skipped with `@inbounds`. - `sreshape` checks the lengths up front, with a message that gives both sizes and lengths, instead of an empty `DimensionMismatch` from inside `_computereshapestrides`, whose checks were therefore redundant. - `_isperm` records the entries in a 64-bit mask for N <= 64, which is O(N) without branches or allocations, instead of `Base.isperm`, which is O(N^2) for tuples and allocates for vectors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
lkdvos
approved these changes
Nov 25, 2026
lkdvos
left a comment
Member
There was a problem hiding this comment.
Left some minor style comments, otherwise definitely in favor of having these changes here, rather than in Strided. Definitely some cool implementation improvements as well!
| _computereshapestrides(newsize::Dims, oldsize::Tuple{}, strides::Tuple{}) = newsize | ||
| function _computereshapestrides(newsize::Dims, oldsize::Dims{N}, strides::Dims{N}) where {N} | ||
| d, r = divrem(oldsize[1], newsize[1]) | ||
| return if r == 0 |
Member
There was a problem hiding this comment.
Again just a style comment: the branches all have return statements, so this one is probably not required?
Member
Author
There was a problem hiding this comment.
Forgot this one, will change in a next PR.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This moves some of the improvements from QuantumKitHub/Strided.jl#104 to StridedViews, so that Strided can build on them, and fixes a few things found along the way.
Changes
@noinlinehelpers (_throw_invalidperm,_throw_reshape_dimensionmismatch,_throw_reshapeexception, and two forReinterpretArray). Messages that interpolate areLazyStrings, so building the exception and its message does not end up in the code of the callers.sreshapelength errors.sreshapechecks the lengths up front, e.g.DimensionMismatch: Cannot reshape a StridedView of size (2, 3, 4) (length 24) to size (5, 5) (length 25), instead of throwing an emptyDimensionMismatch()from inside_computereshapestrides. The checks there were redundant after this and are removed; the recursion preservesprod(newsize) == prod(oldsize).StridedViewwith ranges and colons (getindex,view,sview) did not check bounds, so e.g.B[1:5, :]on a 3×4 view returned a 5×4 view reaching outside the parent; Strided's kernels then read or write that memory with@inbounds. Slicing now throws aBoundsError. The check can be skipped with@inbounds, whichviewandsviewpropagate._ispermrecords the entries as bits in a 64-bit mask: N entries in1:Nform a permutation if and only if they set all N bits. This is O(N), without branches or allocations, whereasBase.ispermcompares all pairs for tuples (O(N²)) and allocates for vectors. For tuples with N ≤ 4,Base.ispermis just as fast and is still used. Tests compare it withBase.ispermexhaustively for N ≤ 5 (with out-of-range entries) and on random cases up to N = 65.@inboundswhere the indices are already checked.permutedimsreadsa.size[p[n]]anda.strides[p[n]]with@inbounds, after_ispermhas checkedp._strides(::PermutedDimsArray)does the same with its type parameterperm.unsafe_stridedview(a::StridedView, size = size(a), strides = strides(a), offset = offset(a)). This new method replaces the layout of a view while keeping its element type, parent and operation. The number of dimensions can change, and the strides are used as given, so zero strides (broadcasting, reductions) survive. This is what Strided's_relayoutdoes in Pass arrays to the kernels and reduce the fixed cost for small inputs Strided.jl#104, which can then simply be this method. Internally it replaces the longer form ingetindex,permutedimsandsreshape._normalizestrides. It is now a scan that carries the extent of the preceding dimension, like_normalizesingletonsin Pass arrays to the kernels and reduce the fixed cost for small inputs Strided.jl#104. The results are unchanged: a leading size 1 dimension still gets stride 1, any other size 1 dimension gets stride × size of the preceding dimension, and an array with a zero-size dimension gets contiguous strides. A test compares it with the previous implementation on random inputs.Relation to
_normalizesingletonsin QuantumKitHub/Strided.jl#104Both functions give size 1 dimensions the extent of the preceding dimension as their stride. They differ only for leading size 1 dimensions: here these keep stride 1, while
_normalizesingletonsgives them the stride of the first dimension with size > 1, so that they fuse. Changing the convention here would change the publicstridesof e.g. a 1×n row view from(1, m)to(m, m), and break checks such asstride(A, 1) == 1downstream. The leading-singleton handling therefore stays in Strided, where_fusedimscan skip size 1 dimensions altogether.Timings
Apple M-series, Julia 1.13, minimum times:
permutedimsN = 6, tuplepermutedimsN = 6, vectorpermutedimsN = 12, tupleB[2:3, :, 1:3, 2]@inbounds)_normalizestridesN = 6The version is not bumped; Strided will need the release that includes the new
unsafe_stridedviewmethod.🤖 Generated with Claude Code