ITADN

normalize on CuArray fails due to scalar indexing

#3097Closedsethaxen 创建于 2026-04-14
bug
S
sethaxencommented
**Describe the bug** `LinearAlgebra.normalize(::CuArray)` uses scalar indexing and errors. **To reproduce** The Minimal Working Example (MWE) for this bug: ```julia julia> using CUDA, LinearAlgebra julia> x = CuArray(randn(64)); julia> normalize(x) ERROR: Scalar indexing is disallowed. Invocation of getindex resulted in scalar indexing of a GPU array. This is typically caused by calling an iterating implementation of a method. Such implementations *do not* execute on the GPU, but very slowly on the CPU, and therefore should be avoided. If you want to allow scalar iteration, use `allowscalar` or `@allowscalar` to enable scalar iteration globally or for the operations in question. Stacktrace: [1] error(s::String) @ Base ./error.jl:44 [2] errorscalar(op::String) @ GPUArraysCore ~/.julia/packages/GPUArraysCore/aNaXo/src/GPUArraysCore.jl:151 [3] _assertscalar(op::String, behavior::GPUArraysCore.ScalarIndexing) @ GPUArraysCore ~/.julia/packages/GPUArraysCore/aNaXo/src/GPUArraysCore.jl:124 [4] assertscalar(op::String) @ GPUArraysCore ~/.julia/packages/GPUArraysCore/aNaXo/src/GPUArraysCore.jl:112 [5] getindex @ ~/.julia/packages/GPUArrays/3a5jB/src/host/indexing.jl:50 [inlined] [6] first @ ./abstractarray.jl:452 [inlined] [7] normalize(a::CuArray{Float64, 1, CUDACore.DeviceMemory}, p::Int64) @ LinearAlgebra ~/.julia/juliaup/julia-1.12.6+0.x64.linux.gnu/share/julia/stdlib/v1.12/LinearAlgebra/src/generic.jl:2077 [8] normalize(a::CuArray{Float64, 1, CUDACore.DeviceMemory}) @ LinearAlgebra ~/.julia/juliaup/julia-1.12.6+0.x64.linux.gnu/share/julia/stdlib/v1.12/LinearAlgebra/src/generic.jl:2075 [9] top-level scope @ REPL[4]:1 julia> CUDA.@allowscalar normalize(x) 64-element CuArray{Float64, 1, CUDACore.DeviceMemory}: -0.01320564935559139 -0.004550218275742979 -0.14597309489603136 0.062294619076320265 0.025671702439414863 -0.15911349975709466 0.010569044875222535 0.13713227216027524 -0.20730555879537835 0.14939344769412588 0.1966272525873343 0.14212211032830602 ⋮ ``` <details><summary>Manifest.toml</summary> <p> ```julia (jl_OEzSm0) pkg> st --manifest Status `/tmp/jl_OEzSm0/Manifest.toml` [621f4979] AbstractFFTs v1.5.0 [79e6a3ab] Adapt v4.5.2 [a9b6321e] Atomix v1.1.3 [ab4f0b2a] BFloat16s v0.6.1 [fa961155] CEnum v0.5.0 [052768ef] CUDA v6.0.0 [bd0ed864] CUDACore v6.0.0 [9ec180c6] CUDATools v6.0.0 [1af6417a] CUDA_Runtime_Discovery v1.0.0 [9e67e8f6] CUPTI v6.0.0 [a8cc5b0e] Crayons v4.1.1 [9a962f9c] DataAPI v1.16.0 [e2d170a0] DataValueInterfaces v1.0.0 [e2ba6199] ExprTools v0.1.10 [0c68f7d7] GPUArrays v11.4.1 [46192b85] GPUArraysCore v0.2.0 [61eb1bfa] GPUCompiler v1.9.1 [096a3bc2] GPUToolbox v1.1.1 [076d061b] HashArrayMappedTries v0.2.0 [82899510] IteratorInterfaceExtensions v1.0.0 [692b3bcd] JLLWrappers v1.7.1 [63c18a36] KernelAbstractions v0.9.41 [929cbde3] LLVM v9.4.6 [8b046642] LLVMLoopInfo v1.0.0 [b964fa9f] LaTeXStrings v1.4.0 [1914dd2f] MacroTools v0.5.16 [611af6d1] NVML v6.0.0 [5da4648a] NVTX v1.0.3 [bac558e1] OrderedCollections v1.8.1 [aea7be01] PrecompileTools v1.3.3 [21216c6a] Preferences v1.5.2 [08abe8d2] PrettyTables v3.3.2 [74087812] Random123 v1.7.1 [e6cf234a] RandomNumbers v1.6.0 [189a3867] Reexport v1.2.2 [ae029012] Requires v1.3.1 [7e506255] ScopedValues v1.6.0 [6c6a2e73] Scratch v1.3.0 [90137ffa] StaticArrays v1.9.18 [1e83bf80] StaticArraysCore v1.4.4 [10745b16] Statistics v1.11.1 [892a3eda] StringManipulation v0.4.4 [3783bdb8] TableTraits v1.0.1 [bd369af6] Tables v1.12.1 [e689c965] Tracy v0.1.6 [013be700] UnsafeAtomics v0.3.1 [182d3088] cuBLAS v6.0.0 [533571aa] cuFFT v6.0.0 [20fd9a0b] cuRAND v6.0.0 [887afef0] cuSOLVER v6.0.0 [b26da814] cuSPARSE v6.0.0 [d1e2174e] CUDA_Compiler_jll v0.4.2+0 [4ee394cb] CUDA_Driver_jll v13.2.0+0 [76a88914] CUDA_Runtime_jll v0.21.0+0 [9c1d0b0a] JuliaNVTXCallbacks_jll v0.2.1+0 [dad2f222] LLVMExtra_jll v0.0.38+0 [ad6e5548] LibTracyClient_jll v0.13.1+0 [e98f9f5b] NVTX_jll v3.2.2+0 [1e29f10c] demumble_jll v1.3.0+0 [0dad84c5] ArgTools v1.1.2 [56f22d72] Artifacts v1.11.0 [2a0f44e3] Base64 v1.11.0 [ade2ca70] Dates v1.11.0 [f43a241f] Downloads v1.7.0 [7b1f6079] FileWatching v1.11.0 [b77e0a4c] InteractiveUtils v1.11.0 [ac6e5ff7] JuliaSyntaxHighlighting v1.12.0 [4af54fe1] LazyArtifacts v1.11.0 [b27032c2] LibCURL v0.6.4 [76f85450] LibGit2 v1.11.0 [8f399da3] Libdl v1.11.0 [37e2e46d] LinearAlgebra v1.12.0 [56ddb016] Logging v1.11.0 [d6f4376e] Markdown v1.11.0 [ca575930] NetworkOptions v1.3.0 [44cfe95a] Pkg v1.12.1 [de0858da] Printf v1.11.0 [3fa0cd96] REPL v1.11.0 [9a3f8284] Random v1.11.0 [ea8e919c] SHA v0.7.0 [9e88b42a] Serialization v1.11.0 [6462fe0b] Sockets v1.11.0 [2f01184e] SparseArrays v1.12.0 [f489334b] StyledStrings v1.11.0 [fa267f1f] TOML v1.0.3 [a4e569a6] Tar v1.10.0 [cf7118a7] UUIDs v1.11.0 [4ec0a83e] Unicode v1.11.0 [e66e0078] CompilerSupportLibraries_jll v1.3.0+1 [deac9b47] LibCURL_jll v8.15.0+0 [e37daf67] LibGit2_jll v1.9.0+0 [29816b5a] LibSSH2_jll v1.11.3+1 [14a3606d] MozillaCACerts_jll v2025.11.4 [4536629a] OpenBLAS_jll v0.3.29+0 [458c3c95] OpenSSL_jll v3.5.4+0 [bea87d4a] SuiteSparse_jll v7.8.3+2 [83775a58] Zlib_jll v1.3.1+2 [8e850b90] libblastrampoline_jll v5.15.0+0 [8e850ede] nghttp2_jll v1.64.0+1 [3f19e933] p7zip_jll v17.7.0+0 ``` </p> </details> **Expected behavior** I expected it to succeed without scalar indexing. **Version info** Details on Julia: ``` # please post the output of: julia> versioninfo() Julia Version 1.12.6 Commit 15346901f00 (2026-04-09 19:20 UTC) Build Info: Official https://julialang.org release Platform Info: OS: Linux (x86_64-linux-gnu) CPU: 24 × AMD Ryzen 9 5900X 12-Core Processor WORD_SIZE: 64 LLVM: libLLVM-18.1.7 (ORCJIT, znver3) GC: Built with stock GC Threads: 24 default, 1 interactive, 24 GC (on 24 virtual cores) Environment: JULIA_NUM_THREADS = auto JULIA_EDITOR = cursor ``` Details on CUDA: ``` # please post the output of: julia> CUDA.versioninfo() CUDA toolchain: - runtime 13.2, artifact installation - driver 580.126.18 for 13.2 - compiler 13.2 CUDA libraries: - cuBLAS: 13.3.0 - cuSPARSE: 12.7.9 - cuSOLVER: 12.1.0 - cuFFT: 12.2.0 - cuRAND: 10.4.2 - CUPTI: 2026.1.0 (API 13.2.0) - NVML: 13.0.0+580.126.18 Julia packages: - CUDACore: 6.0.0 - GPUArrays: 11.4.1 - GPUCompiler: 1.9.1 - KernelAbstractions: 0.9.41 - CUDA_Driver_jll: 13.2.0+0 - CUDA_Compiler_jll: 0.4.2+0 - CUDA_Runtime_jll: 0.21.0+0 Toolchain: - Julia: 1.12.6 - LLVM: 18.1.7 1 device: 0: NVIDIA GeForce RTX 3060 (sm_86, 5.827 GiB / 12.000 GiB available) ``` **Additional context** This seems to happen because `normalize` uses `first(x)` to determine the eltype of the output (https://github.com/JuliaLang/LinearAlgebra.jl/blob/a944a86905ecb980942ff3acf935784c0f821f0f/src/generic.jl#L2111) `normalize!` works fine.
关闭于 2026-04-14 0 条评论