Skip to content

Commit

Permalink
Fix #647: issue with Knet.save
Browse files Browse the repository at this point in the history
  • Loading branch information
denizyuret committed Dec 14, 2020
1 parent ae149dd commit 5aff4be
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 107 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Knet"
uuid = "1902f260-5fb4-5aff-8c31-6271790ab950"
authors = ["Deniz Yuret <[email protected]>"]
version = "1.4.4"
version = "1.4.5"

[deps]
AutoGrad = "6710c13c-97f1-543f-91c5-74e8f7d95b35"
Expand Down
2 changes: 1 addition & 1 deletion src/knetarrays/KnetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ include("unary.jl")
include("reduction.jl")

include("serialization.jl") # serialize and deserialize of KnetArrays
include("jld2.jl"); export save, load, @save, @load # deprecated, use FileIO and JLD2
include("jld2.jl"); export save, load, @save, @load, load143 # deprecated, use FileIO and JLD2

end
113 changes: 8 additions & 105 deletions src/knetarrays/jld2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,108 +6,11 @@ JLD2.writeas(::Type{KnetArray{T,N}}) where {T,N} = JLD2KnetArray{T,N}
JLD2.wconvert(::Type{JLD2KnetArray{T,N}}, x::KnetArray{T,N}) where {T,N} = JLD2KnetArray(Array(x))
JLD2.rconvert(::Type{KnetArray{T,N}}, x::JLD2KnetArray{T,N}) where {T,N} = KnetArray(x.array)


# These are deprecated functions and macros for backward compatibility and loading old files

function save(file, args...; options...)
@warn "Knet.save is deprecated, please use FileIO.save/load instead" maxlog=1
FileIO.save(file, jld2serialize.(args)...; options...)
end

function load(file, args...; options...)
@warn "Knet.load is deprecated, please use FileIO.save/load instead" maxlog=1
jld2serialize(FileIO.load(file, args...; options...))
end


"""
Knet.@save "filename" variable1 variable2...
Save the values of the specified variables to filename in JLD2 format.
When called with no variable arguments, write all variables in the global scope of the current
module to filename. See [JLD2](https://github.com/JuliaIO/JLD2.jl).
This macro is deprecated, please use `JLD2.@save` instead.
"""
macro save(filename, vars...)
if isempty(vars)
# Save all variables in the current module
quote
@warn "Knet.@save is deprecated, please use JLD2.@save/@load instead" maxlog=1
let
m = $(__module__)
f = JLD2.jldopen($(esc(filename)), "w")
wsession = JLD2.JLDWriteSession()
try
for vname in names(m; all=true)
s = string(vname)
if !occursin(r"^_+[0-9]*$", s) # skip IJulia history vars
v = getfield(m, vname)
if !isa(v, Module)
try
write(f, s, jld2serialize(v), wsession)
catch e
if isa(e, PointerException)
@warn("skipping $vname because it contains a pointer")
else
rethrow(e)
end
end
end
end
end
finally
close(f)
end
end
end
else
writeexprs = Vector{Expr}(undef, length(vars))
for i = 1:length(vars)
writeexprs[i] = :(write(f, $(string(vars[i])), jld2serialize($(esc(vars[i]))), wsession))
end

quote
@warn "Knet.@save is deprecated, please use JLD2.@save/@load instead" maxlog=1
JLD2.jldopen($(esc(filename)), "w") do f
wsession = JLD2.JLDWriteSession()
$(Expr(:block, writeexprs...))
end
end
end
end

"""
Knet.@load "filename" variable1 variable2...
Load the values of the specified variables from filename in JLD2 format.
When called with no variable arguments, load all variables in filename. See
[JLD2](https://github.com/JuliaIO/JLD2.jl).
This macro is deprecated, please use `JLD2.@load` instead.
"""
macro load(filename, vars...)
if isempty(vars)
if isa(filename, Expr)
throw(ArgumentError("filename argument must be a string literal unless variable names are specified"))
end
# Load all variables in the top level of the file
readexprs = Expr[]
vars = Symbol[]
f = JLD2.jldopen(filename)
try
for n in keys(f)
if !JLD2.isgroup(f, JLD2.lookup_offset(f.root_group, n))
push!(vars, Symbol(n))
end
end
finally
close(f)
end
end
return quote
@warn "Knet.@load is deprecated, please use JLD2.@save/@load instead" maxlog=1
($([esc(x) for x in vars]...),) = JLD2.jldopen($(esc(filename))) do f
($([:(jld2serialize(read(f, $(string(x))))) for x in vars]...),)
end
$(Symbol[v for v in vars]) # convert to Array
end
end
# For people who use Knet.load, Knet.@save etc.
save(x...; o...) = FileIO.save(x...; o...)
load(x...; o...) = FileIO.load(x...; o...)
macro save(x...); a=:(JLD2.@save); append!(a.args,x); a; end
macro load(x...); a=:(JLD2.@load); append!(a.args,x); a; end

# To load a file saved with Knet-1.4.3 or earlier:
load143(x...; o...)=jld2serialize(FileIO.load(x...; o...))

2 comments on commit 5aff4be

@denizyuret
Copy link
Owner Author

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/26432

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 v1.4.5 -m "<description of version>" 5aff4bebab127dfa79cc8c60fdda657b12750ead
git push origin v1.4.5

Please sign in to comment.