Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

colonful reshape may spuriously throw DivideError #54245

Closed
nsajko opened this issue Apr 25, 2024 · 0 comments · Fixed by #54261
Closed

colonful reshape may spuriously throw DivideError #54245

nsajko opened this issue Apr 25, 2024 · 0 comments · Fixed by #54261
Labels
arrays [a, r, r, a, y, s] bug Indicates an unexpected problem or unintended behavior

Comments

@nsajko
Copy link
Contributor

nsajko commented Apr 25, 2024

julia> reshape([], (1, :)) == reshape([], (1, 0))
true

julia> reshape([], (1, :)) == reshape([], (1, 0)) == reshape([], (0, :))  # shouldn't throw
ERROR: DivideError: integer division error
Stacktrace:
 [1] div
   @ ./int.jl:295 [inlined]
 [2] divrem
   @ ./div.jl:203 [inlined]
 [3] divrem
   @ ./div.jl:181 [inlined]
 [4] _reshape_uncolon
   @ ./reshapedarray.jl:138 [inlined]
 [5] reshape(parent::Vector{Any}, dims::Tuple{Int64, Colon})
   @ Base ./reshapedarray.jl:129
 [6] top-level scope
   @ REPL[2]:1

julia> reshape([1], (0, :))  # should throw, but not `DivideError`
ERROR: DivideError: integer division error
Stacktrace:
 [1] div
   @ ./int.jl:295 [inlined]
 [2] divrem
   @ ./div.jl:203 [inlined]
 [3] divrem
   @ ./div.jl:181 [inlined]
 [4] _reshape_uncolon
   @ ./reshapedarray.jl:138 [inlined]
 [5] reshape(parent::Vector{Int64}, dims::Tuple{Int64, Colon})
   @ Base ./reshapedarray.jl:129
 [6] top-level scope
   @ REPL[2]:1

julia> b = prevpow(2, typemax(Int))
4611686018427387904

julia> iszero(b*b)  # `b` is a square root of zero, thus a zero divisor
true

julia> reshape([1], (b, :, b))  # should throw, but not `DivideError`
ERROR: DivideError: integer division error
Stacktrace:
 [1] div
   @ ./int.jl:295 [inlined]
 [2] divrem
   @ ./div.jl:203 [inlined]
 [3] divrem
   @ ./div.jl:181 [inlined]
 [4] _reshape_uncolon
   @ ./reshapedarray.jl:138 [inlined]
 [5] reshape(parent::Vector{Int64}, dims::Tuple{Int64, Colon, Int64})
   @ Base ./reshapedarray.jl:129
 [6] top-level scope
   @ REPL[6]:1

julia> reshape([], (b, :, b))
ERROR: DivideError: integer division error
Stacktrace:
 [1] div
   @ ./int.jl:295 [inlined]
 [2] divrem
   @ ./div.jl:203 [inlined]
 [3] divrem
   @ ./div.jl:181 [inlined]
 [4] _reshape_uncolon
   @ ./reshapedarray.jl:138 [inlined]
 [5] reshape(parent::Vector{Any}, dims::Tuple{Int64, Colon, Int64})
   @ Base ./reshapedarray.jl:129
 [6] top-level scope
   @ REPL[4]:1

For the last example, the expected result depends on the resolution of #54244, but in any case it shouldn't throw DivideError.

I've got a fix, but waiting for a decision on #54244.

julia> versioninfo()
Julia Version 1.12.0-DEV.387
Commit b5bfd83a3d0 (2024-04-22 13:12 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × AMD Ryzen 3 5300U with Radeon Graphics
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver2)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Environment:
  JULIA_NUM_PRECOMPILE_TASKS = 3
  JULIA_PKG_PRECOMPILE_AUTO = 0

This is the affected code:

julia/base/reshapedarray.jl

Lines 130 to 141 in 8f6418e

@inline function _reshape_uncolon(A, dims)
@noinline throw1(dims) = throw(DimensionMismatch(string("new dimensions $(dims) ",
"may have at most one omitted dimension specified by `Colon()`")))
@noinline throw2(A, dims) = throw(DimensionMismatch(string("array size $(length(A)) ",
"must be divisible by the product of the new dimensions $dims")))
pre = _before_colon(dims...)
post = _after_colon(dims...)
_any_colon(post...) && throw1(dims)
sz, remainder = divrem(length(A), prod(pre)*prod(post))
remainder == 0 || throw2(A, dims)
(pre..., Int(sz), post...)
end

@nsajko nsajko added bug Indicates an unexpected problem or unintended behavior arrays [a, r, r, a, y, s] labels Apr 25, 2024
nsajko added a commit to nsajko/julia that referenced this issue Apr 26, 2024
In several different situations `reshape` had thrown `DivideError`
instead of returning a correct result or throwing.

Fixes JuliaLang#54245
nsajko added a commit to nsajko/julia that referenced this issue Apr 29, 2024
In several different situations `reshape` had thrown `DivideError`
instead of returning a correct result or throwing.

Fixes JuliaLang#54245
oscardssmith pushed a commit that referenced this issue May 13, 2024
lazarusA pushed a commit to lazarusA/julia that referenced this issue Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant