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

Support struct types in create_result #6

Merged
merged 16 commits into from
May 28, 2024
30 changes: 30 additions & 0 deletions src/Reactant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -720,10 +720,40 @@ function generate_jlfunc(concrete_result, client, mod, Nargs, linear_args, linea
end)
return
end
if T <: Array
elems = Symbol[]
for (i, v) in enumerate(tocopy)
sym = Symbol(string(resname)*"_"*string(i))
create_result(v, sym, (path...,i))
push!(elems, sym)
end
push!(concrete_result_maker, quote
$resname = $(eltype(T))[$(elems...)]
end)
return
end
if T <: Int || T <: AbstractFloat || T <: AbstractString || T <: Nothing
mofeing marked this conversation as resolved.
Show resolved Hide resolved
push!(concrete_result_maker, :($resname = $tocopy))
return
end
if T <: Symbol
push!(concrete_result_maker, :($resname = $(QuoteNode(tocopy))))
return
end
if isstructtype(T)
elems = Symbol[]
nf = fieldcount(T)
for i in 1:nf
sym = Symbol(resname, :_, i)
create_result(getfield(tocopy, i), sym, (path..., i))
push!(elems, sym)
end
push!(concrete_result_maker, quote
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this depends on whether the structure is mutable (Enzyme.make_zero should have some relevant code to look at).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test on a mutable struct type and seems to work... although it might be too naive?

flds = Any[$(elems...)]
$resname = ccall(:jl_new_structv, Any, (Any, Ptr{Cvoid}, UInt32), $T, flds, $nf)
end)
return
end

error("canot copy $T")
end
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ include("layout.jl")
include("basic.jl")
include("bcast.jl")
include("nn.jl")
include("struct.jl")
47 changes: 47 additions & 0 deletions test/struct.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Reactant
using Test

# from bsc-quantic/Tenet.jl
struct MockTensor{T,N,A<:AbstractArray{T,N}}
data::A
inds::Vector{Symbol}
end

MockTensor(data::A, inds) where {T,N,A<:AbstractArray{T,N}} = MockTensor{T,N,A}(data, inds)
Base.parent(t::MockTensor) = t.data

Base.cos(x::MockTensor) = MockTensor(cos(parent(x)), x.inds)

@testset "Struct" begin
x = MockTensor(rand(4, 4), [:i, :j])
x2 = MockTensor(Reactant.ConcreteRArray(parent(x)), x.inds)

f = Reactant.compile(cos, (x2,))
y = f(x2)

@test y isa MockTensor{Float64,2,Reactant.ConcreteRArray{Float64,(4, 4),2}}
@test isapprox(parent(y), cos.(parent(x)))
# TODO test that y.inds doesn't crash
end

mutable struct MutableMockTensor{T,N,A<:AbstractArray{T,N}}
data::A
inds::Vector{Symbol}
end

MutableMockTensor(data::A, inds) where {T,N,A<:AbstractArray{T,N}} = MutableMockTensor{T,N,A}(data, inds)
Base.parent(t::MutableMockTensor) = t.data

Base.cos(x::MutableMockTensor) = MutableMockTensor(cos(parent(x)), x.inds)

@testset "Mutable Struct" begin
x = MutableMockTensor(rand(4, 4), [:i, :j])
x2 = MutableMockTensor(Reactant.ConcreteRArray(parent(x)), x.inds)

f = Reactant.compile(cos, (x2,))
y = f(x2)

@test y isa MutableMockTensor{Float64,2,Reactant.ConcreteRArray{Float64,(4, 4),2}}
@test isapprox(parent(y), cos.(parent(x)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these test against non array values just in case

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm I don't follow. do you mean to add a test against non-array fields?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test against the inds field, which is an Array but not a RArray. Is this enough?

# TODO test that y.inds doesn't crash
end
Loading