Skip to content

Commit

Permalink
Merge #924
Browse files Browse the repository at this point in the history
924: Throw error when trying to accum two NamedTuples with different keys r=DhairyaLGandhi a=mzgubic

Would have helped here:
#922 (comment)



Co-authored-by: Miha Zgubic <[email protected]>
  • Loading branch information
bors[bot] and Miha Zgubic authored Mar 23, 2021
2 parents 890b6f5 + ada3d02 commit 2717d48
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Zygote"
uuid = "e88e6eb3-aa80-5325-afca-941959d7151f"
version = "0.6.4"
version = "0.6.5"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
5 changes: 4 additions & 1 deletion src/lib/lib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ accum(x::Tuple, y::Tuple) = accum.(x, y)
accum(x::AbstractArray, y::AbstractArray) = accum.(x, y)

@generated function accum(x::NamedTuple, y::NamedTuple)
grad(x) = x in fieldnames(y) ? :(y.$x) : :nothing
# assumes that y has no keys apart from those also in x
fieldnames(y) fieldnames(x) || throw(ArgumentError("$y keys must be a subset of $x keys"))

grad(field) = field in fieldnames(y) ? :(y.$field) : :nothing
Expr(:tuple, [:($f=accum(x.$f, $(grad(f)))) for f in fieldnames(x)]...)
end

Expand Down
8 changes: 8 additions & 0 deletions test/lib/lib.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@testset "lib.jl" begin
@testset "accum" begin
t1 = (a=1, b=2, c=3)
t2 = (a=1, b=2)
@test Zygote.accum(t1, t2) == (a = 2, b = 4, c = 3)
@test_throws ArgumentError Zygote.accum(t2, t1)
end
end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ end
include("utils.jl")
end

@testset "lib/number" begin
@testset "lib" begin
include("lib/number.jl")
include("lib/lib.jl")
end

@testset "Features" begin
Expand Down

0 comments on commit 2717d48

Please sign in to comment.