Skip to content

Commit

Permalink
Refactor create_result for Tuple types to include NamedTuples (#11
Browse files Browse the repository at this point in the history
)

* Refactor `create_result` for `Tuple` types to include `NamedTuple`s

* Fix `NamedTuple` codegen in `create_result`

* Add test for `create_result` on `NamedTuple`
  • Loading branch information
mofeing authored May 31, 2024
1 parent 4b2e2b6 commit d319ef4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Reactant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -771,9 +771,9 @@ function generate_jlfunc(
end
if T <: Tuple
elems = Symbol[]
for (i, v) in enumerate(tocopy)
sym = Symbol(string(resname) * "_" * string(i))
create_result(v, sym, (path..., i))
for (k, v) in pairs(tocopy)
sym = Symbol(resname, :_, k)
create_result(v, sym, (path..., k))
push!(elems, sym)
end
push!(
Expand All @@ -784,6 +784,18 @@ function generate_jlfunc(
)
return nothing
end
if T <: NamedTuple
elems = Symbol[]
for (k, v) in pairs(tocopy)
sym = Symbol(resname, :_, k)
create_result(v, sym, (path..., k))
push!(elems, sym)
end
push!(concrete_result_maker, quote
$resname = NamedTuple{$(keys(tocopy))}($elems)
end)
return
end
if T <: Array
elems = Symbol[]
for (i, v) in enumerate(tocopy)
Expand Down
20 changes: 20 additions & 0 deletions test/compile.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Reactant
using Test

@testset "compile" begin
@testset "create_result" begin
@testset "NamedTuple" begin
const MockNamedTuple{T} = @NamedTuple{a::T}
MockNamedTuple(x::T) where {T} = MockNamedTuple{T}((x,))

x = MockNamedTuple(rand(4, 3))
x2 = MockNamedTuple(Reactant.ConcreteRArray(x.a))

Base.sum(x::MockNamedTuple) = MockNamedTuple((sum(x.a),))
f = Reactant.compile(sum, (x2,))

@test f(x2) isa MockNamedTuple{Reactant.ConcreteRArray}
@test isapprox(f(x2).a, sum(x).a)
end
end
end
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("basic.jl")
include("bcast.jl")
include("nn.jl")
include("struct.jl")
include("compile.jl")

2 comments on commit d319ef4

@wsmoses
Copy link
Member

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/108055

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.1 -m "<description of version>" d319ef4885d91e0f1233cf76b330ce334101904e
git push origin v0.1.1

Please sign in to comment.