Skip to content

EnzymeNoDerivativeError with Ark MWE #3443

Description

@ameligrana

MWE (which doesn't depend on Ark, but reduced from the code in ark-ecs/Ark.jl#692):

using Enzyme

struct Node
    neighbors::Vector{Node}
end

Node() = Node(Vector{Node}(undef, 5))

mutable struct Archetype
    tables::Vector{UInt32}
    has_relations::Bool
    node::Node
end

struct Table
    entities::Vector{Int64}
    id::UInt32
end

struct Query{S<:Tuple}
    archetypes::Vector{Archetype}
    tables::Vector{Table}
    storages::S
end

function _get_columns(q::Query, table::Table)
    @inbounds positions = q.storages[1][table.id]
    return (table.entities, positions)
end

function _iterate(q::Query, state::Tuple{Int,Int})
    arch, tab = state
    while arch <= length(q.archetypes)
        @inbounds archetype = q.archetypes[arch]
        if tab == 0
            if !archetype.has_relations
                table = @inbounds q.tables[Int(archetype.tables[1])]
                if isempty(table.entities)
                    arch += 1
                    continue
                end
                return _get_columns(q, table), (arch + 1, 0)
            end

            if isempty(archetype.tables)
                arch += 1
                continue
            end

            tab = 1
        end

        tables = archetype.tables

        while tab <= length(tables)
            @inbounds table = q.tables[Int(tables[tab])]
            return _get_columns(q, table), (arch, tab + 1)
        end

        arch += 1
        tab = 0
    end

    return nothing
end

function Base.iterate(q::Query, state::Tuple{Int,Int})
    return _iterate(q, state)
end

function Base.iterate(q::Query)
    return Base.iterate(q, (1, 0))
end

function run_world(args::AbstractVector{T}) where {T<:Number}
    alpha, beta = args
    n = 1

    archetypes = Archetype[Archetype(UInt32[1], false, Node())]
    tables = Table[Table(collect(Int64, 1:n), UInt32(1))]

    push!(archetypes, Archetype(UInt32[2], true, Node()))
    push!(tables, Table(Int64[101], UInt32(2)))

    columns = Vector{Vector{Float64}}([Float64[T(i) for i in 1:n] for _ in 1:2])
    q = Query(archetypes, tables, (columns,))

    total = zero(alpha + beta)
    for (entities, positions) in q
        @inbounds for pos in positions
            total += pos
        end
    end

    return total
end

point = [0.1, 0.5]
Enzyme.gradient(Enzyme.set_runtime_activity(Enzyme.Reverse), run_world, point)

the error is minimal_no_ark_error.txt (was too long to paste) using Enzyme v0.13.198. Hopefully this is the last bug to make Ark work with Enzyme :-)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions