-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSerialization.jl
More file actions
392 lines (355 loc) · 13 KB
/
Serialization.jl
File metadata and controls
392 lines (355 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
## Version checking
#NOTE fixed really bad function but kept similar as fallback #TODO upgrade to use pkgversion(m::Module)
function _getDFGVersion()
if VERSION >= v"1.9"
return pkgversion(DistributedFactorGraphs)
end
#TODO when we drop jl<1.9 remove the rest here
pkgorigin = get(Base.pkgorigins, Base.PkgId(DistributedFactorGraphs), nothing)
if !isnothing(pkgorigin) && !isnothing(pkgorigin.version)
return pkgorigin.version
end
dep =
get(Pkg.dependencies(), Base.UUID("b5cc3c7e-6572-11e9-2517-99fb8daf2f04"), nothing)
if !isnothing(dep)
return dep.version
else
# This is arguably slower, but needed for Travis.
return Pkg.TOML.parse(
read(joinpath(dirname(pathof(@__MODULE__)), "..", "Project.toml"), String),
)["version"] |> VersionNumber
end
end
function _versionCheck(node::Union{<:VariableDFG, <:FactorDFG})
if VersionNumber(node._version).minor < _getDFGVersion().minor
@warn "This data was serialized using DFG $(node._version) but you have $(_getDFGVersion()) installed, there may be deserialization issues." maxlog =
10
end
end
function stringVariableType(varT::InferenceVariable)
T = typeof(varT)
#FIXME maybe don't use .parameters
Tparams = T.parameters
if length(Tparams) == 0
return string(parentmodule(T), ".", nameof(T))
elseif length(Tparams) == 1 && Tparams[1] isa Integer
return string(parentmodule(T), ".", nameof(T), "{", join(Tparams, ","), "}")
else
throw(
SerializationError(
"Serializing Variable State type only supports 1 integer parameter, got '$(T)'.",
),
)
end
end
function parseVariableType(_typeString::AbstractString)
m = match(r"{(\d+)}", _typeString)
if !isnothing(m) #parameters in type
param = parse(Int, m[1])
typeString = _typeString[1:(m.offset - 1)]
else
param = nothing
typeString = _typeString
end
split_typeSyms = Symbol.(split(typeString, "."))
subtype = nothing
if length(split_typeSyms) == 1
@warn "Module not found in variable '$typeString'." maxlog = 1
subtype = getfield(Main, split_typeSyms[1]) # no module specified, use Main
#FIXME interm fallback for backwards compatibility in IIFTypes and RoMETypes
elseif split_typeSyms[1] in Symbol.(values(Base.loaded_modules))
m = getfield(Main, split_typeSyms[1])
subtype = getfield(m, split_typeSyms[end])
else
@warn "Module not found in Main, using Main for type '$typeString'." maxlog = 1
subtype = getfield(Main, split_typeSyms[end])
end
if isnothing(subtype)
throw(SerializationError("Unable to deserialize type $(_typeString), not found"))
return nothing
end
if isnothing(param)
# no parameters, just return the type
return subtype
else
# return the type with parameters
return subtype{param}
end
end
"""
$(SIGNATURES)
Get a type from the serialization module.
"""
function getTypeFromSerializationModule(_typeString::AbstractString)
@debug "DFG converting type string to Julia type" _typeString
try
# split the type at last `.`
split_st = split(_typeString, r"\.(?!.*\.)")
#if module is specified look for the module in main, otherwise use Main
if length(split_st) == 2
m = getfield(Main, Symbol(split_st[1]))
else
m = Main
end
noparams = split(split_st[end], r"{")
ret = if 1 < length(noparams)
# fix #671, but does not work with specific module yet
bidx = findfirst(r"{", split_st[end])[1]
Core.eval(m, Base.Meta.parse("$(noparams[1])$(split_st[end][bidx:end])"))
# eval(Base.Meta.parse("Main.$(noparams[1])$(split_st[end][bidx:end])"))
else
getfield(m, Symbol(split_st[end]))
end
return ret
catch ex
@error "Unable to deserialize type $(_typeString)"
io = IOBuffer()
showerror(io, ex, catch_backtrace())
err = String(take!(io))
@error(err)
end
return nothing
end
# returns a PackedVariableState
function packVariableState(d::VariableState{T}) where {T <: InferenceVariable}
@debug "Dispatching conversion variable -> packed variable for type $(string(getVariableType(d)))"
castval = if 0 < length(d.val)
precast = getCoordinates.(T, d.val)
@cast castval[i, j] := precast[j][i]
castval
else
zeros(1, 0)
end
_val = castval[:]
length(d.covar) > 1 && @warn(
"Packing of more than one parametric covariance is NOT supported yet, only packing first."
)
return PackedVariableState(
d.id,
_val,
size(castval, 1),
d.bw[:],
size(d.bw, 1),
d.BayesNetOutVertIDs,
d.dimIDs,
d.dims,
d.eliminated,
d.BayesNetVertID,
d.separator,
stringVariableType(getVariableType(d)),
d.initialized,
d.infoPerCoord,
d.ismargin,
d.dontmargin,
d.solveInProgress,
d.solvedCount,
d.solveKey,
isempty(d.covar) ? Float64[] : vec(d.covar[1]),
string(_getDFGVersion()),
)
end
function unpackVariableState(d::PackedVariableState)
@debug "Dispatching conversion packed variable -> variable for type $(string(d.variableType))"
# Figuring out the variableType
# TODO deprecated remove in v0.11 - for backward compatibility for saved variableTypes.
ststring = string(split(d.variableType, "(")[1])
T = parseVariableType(ststring)
isnothing(T) && error(
"The variable doesn't seem to have a variableType. It needs to set up with an InferenceVariable from IIF. This will happen if you use DFG to add serialized variables directly and try use them. Please use IncrementalInference.addVariable().",
)
r3 = d.dimval
c3 = r3 > 0 ? floor(Int, length(d.vecval) / r3) : 0
M3 = reshape(d.vecval, r3, c3)
@cast val_[j][i] := M3[i, j]
vals = Vector{getPointType(T)}(undef, length(val_))
# vals = getPoint.(T, val_)
for (i, v) in enumerate(val_)
vals[i] = getPoint(T, v)
end
r4 = d.dimbw
c4 = r4 > 0 ? floor(Int, length(d.vecbw) / r4) : 0
BW = reshape(d.vecbw, r4, c4)
#
N = getDimension(T)
return VariableState{T, getPointType(T), N}(;
id = d.id,
val = vals,
bw = BW,
#TODO only one covar is currently supported in packed VND
covar = isempty(d.covar) ? SMatrix{N, N, Float64}[] : [d.covar],
BayesNetOutVertIDs = Symbol.(d.BayesNetOutVertIDs),
dimIDs = d.dimIDs,
dims = d.dims,
eliminated = d.eliminated,
BayesNetVertID = Symbol(d.BayesNetVertID),
separator = Symbol.(d.separator),
initialized = d.initialized,
infoPerCoord = d.infoPerCoord,
ismargin = d.ismargin,
dontmargin = d.dontmargin,
solveInProgress = d.solveInProgress,
solvedCount = d.solvedCount,
solveKey = Symbol(d.solveKey),
events = Dict{Symbol, Threads.Condition}(),
)
end
##==============================================================================
## Variable Packing and unpacking
##==============================================================================
function packVariable(
v::VariableCompute;
includePPEs::Bool = true,
includeSolveData::Bool = true,
includeDataEntries::Bool = true,
)
return VariableDFG(;
id = v.id,
label = v.label,
timestamp = v.timestamp,
nstime = string(v.nstime.value),
tags = collect(v.tags), # Symbol.()
ppes = collect(values(v.ppeDict)),
solverData = packVariableState.(collect(values(v.solverDataDict))),
metadata = base64encode(JSON3.write(v.smallData)),
solvable = v.solvable,
variableType = stringVariableType(DFG.getVariableType(v)),
blobEntries = collect(values(v.dataDict)),
_version = string(DFG._getDFGVersion()),
)
end
function packVariable(
v::VariableDFG;
includePPEs::Bool = true,
includeSolveData::Bool = true,
includeDataEntries::Bool = true,
)
return v
end
function unpackVariable(variable::VariableDFG; skipVersionCheck::Bool = false)
!skipVersionCheck && _versionCheck(variable)
# Variable and point type
variableType = parseVariableType(variable.variableType)
isnothing(variableType) && error(
"Cannot deserialize variableType '$(variable.variableType)' in variable '$(variable.label)'",
)
pointType = DFG.getPointType(variableType)
ppeDict =
Dict{Symbol, MeanMaxPPE}(map(p -> p.solveKey, variable.ppes) .=> variable.ppes)
N = getDimension(variableType)
solverDict = Dict{Symbol, VariableState{variableType, pointType, N}}(
map(sd -> sd.solveKey, variable.solverData) .=>
map(sd -> DFG.unpackVariableState(sd), variable.solverData),
)
dataDict = Dict{Symbol, Blobentry}(
map(de -> de.label, variable.blobEntries) .=> variable.blobEntries,
)
metadata = JSON3.read(base64decode(variable.metadata), Dict{Symbol, DFG.SmallDataTypes})
return VariableCompute(
variable.label,
variableType;
id = variable.id,
timestamp = variable.timestamp,
nstime = Nanosecond(variable.nstime),
tags = Set(variable.tags),
ppeDict = ppeDict,
solverDataDict = solverDict,
smallData = metadata,
dataDict = dataDict,
solvable = variable.solvable,
)
end
VariableCompute(v::VariableCompute) = v
VariableCompute(v::VariableDFG) = unpackVariable(v)
VariableDFG(v::VariableDFG) = v
VariableDFG(v::VariableCompute) = packVariable(v)
##==============================================================================
## Factor Packing and unpacking
##==============================================================================
# returns FactorDFG
function packFactor(f::FactorCompute)
obstype = typeof(getObservation(f))
fnctype = string(parentmodule(obstype), ".", nameof(obstype))
return FactorDFG(;
id = f.id,
label = f.label,
tags = f.tags,
_variableOrderSymbols = f._variableOrderSymbols,
timestamp = f.timestamp,
nstime = string(f.nstime.value),
#TODO fully test include module name in factor fnctype, see #1140
# fnctype = String(_getname(getObservation(f))),
fnctype,
solvable = getSolvable(f),
metadata = base64encode(JSON3.write(f.smallData)),
# Pack the node data
_version = string(_getDFGVersion()),
state = f.state,
observJSON = JSON3.write(packObservation(f)),
)
return props
end
packFactor(f::FactorDFG) = f
function unpackObservation(factor::FactorDFG)
try
return unpack(getObservation(factor))
catch e
if e isa MethodError && e.f == unpack
Base.depwarn(
"""$e\nPlease implement pack and unpack methods for the factor type '$(typeof(getObservation(factor)))'.
Falling back to deprecated convert method.""",
:unpackObservation,
)
#FIXME completely refactor to not need getTypeFromSerializationModule and just use StructTypes
#TODO change to unpack: observ = unpack(observpacked)
# currently the observation type is stored in the factor and this complicates unpacking of seperate observations
observpacked = getObservation(factor)
return convert(convertStructType(typeof(observpacked)), observpacked)
else
rethrow()
end
end
end
packObservation(f::FactorCompute) = packObservation(getObservation(f))
function packObservation(observ::AbstractFactorObservation)
try
return pack(observ)
catch e
if e isa MethodError
Base.depwarn(
"$e\nPlease implement pack and unpack methods for the factor type '$(typeof(observ))'.
\nFalling back to deprecated convert method.",
:packObservation,
)
packtype = convertPackedType(observ)
return convert(packtype, observ)
else
rethrow()
end
end
end
function unpackFactor(factor::FactorDFG; skipVersionCheck::Bool = false)
#
@debug "DECODING factor type = '$(factor.fnctype)' for factor '$(factor.label)'"
!skipVersionCheck && _versionCheck(factor)
local observation
try
observation = unpackObservation(factor) #TODO maybe getObservation(factor)
catch
@error "Error while unpacking '$(factor.label)' as '$(factor.fnctype)', please check the unpacking/packing converters for this factor"
rethrow()
end
return FactorCompute(
factor.id,
factor.label,
factor.tags,
Tuple(factor._variableOrderSymbols),
factor.timestamp,
Nanosecond(factor.nstime),
Ref(factor.solvable),
getMetadata(factor),
observation,
factor.state,
Ref{FactorSolverCache}(),
)
end
#