Skip to content

Unexpected result from accumulate  #67

@jariji

Description

@jariji

Here is an associative operation bic_combine:

julia> a = Bic(Int32(0), Int32(1))
Bic(0, 1)

julia> b = Bic(Int32(1), Int32(0))
Bic(1, 0)

julia> c = Bic(Int32(1), Int32(1))
Bic(1, 1)

julia> bic_combine(bic_combine(a, b), c)  
Bic(1, 1)

julia> bic_combine(a, bic_combine(b, c))  
Bic(1, 1)

julia> using Supposition
gen = Supposition.@composed function _gen(a=Data.Integers(0,100), b=Data.Integers(0,100)) 
           Bic(Int32(a),Int32(b))
end
@check function assoc(a=gen,b=gen,c=gen)
           bic_combine(bic_combine(a,b),c) == bic_combine(a, bic_combine(b,c))
       end
Test passed

So I expected accumulate to work the same on GPU vs CPU, but it doesn't. I'm not really sure what's going on.

using AMDGPU, AcceleratedKernels, Test, AutoHashEquals

@auto_hash_equals struct Bic
    a::Int32
    b::Int32
end

@inline bic_combine(x::Bic, y::Bic) = 
    Bic(x.a + y.a - min(x.b, y.a), x.b + y.b - min(x.b, y.a))

Base.zero(::Type{Bic}) = Bic(Int32(0), Int32(0))
AcceleratedKernels.neutral_element(::typeof(bic_combine), ::Type{Bic}) = Bic(Int32(0), Int32(0))

data = [Bic(Int32(0), Int32(1)), Bic(Int32(1), Int32(0))]

@test accumulate(bic_combine, data) == Array(accumulate(bic_combine, ROCArray(data)))
#=   Expression: accumulate(bic_combine, data) == Array(accumulate(bic_combine, ROCArray(data)))
   Evaluated: Bic[Bic(0, 1), Bic(0, 0)] == Bic[Bic(0, 1), Bic(1, 1)] =#
  [21141c5a] AMDGPU v2.1.2
  [6a4ca0a5] AcceleratedKernels v0.4.3
  [79e6a3ab] Adapt v4.4.0
  [a9b6321e] Atomix v1.1.2
  [15f4f7f2] AutoHashEquals v2.2.0
  [6e4b80f9] BenchmarkTools v1.6.3
  [38052440] DataManipulation v0.1.21
  [0c68f7d7] GPUArrays v11.2.6
  [63c18a36] KernelAbstractions v0.9.39

julia> versioninfo()
Julia Version 1.11.6
Commit 9615af0f269 (2025-07-09 12:58 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 24 × AMD Ryzen 9 3900XT 12-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver2)
Threads: 12 default, 0 interactive, 6 GC (on 24 virtual cores)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions