/
util.jl
440 lines (340 loc) · 12 KB
/
util.jl
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import Base: ==
==(x,y,z,ws...) = x==y && ==(y,z,ws...)
"""
Return the type's fields as a tuple
"""
@generated fieldvalues(x) = Expr(:tuple, (:(x.$f) for f=fieldnames(x))...)
@generated fields(x) = Expr(:tuple, (:($f=x.$f) for f=fieldnames(x))...)
firstfield(x) = first(fieldvalues(x))
"""
Rewrites `@! x = f(args...)` to `x = f!(x,args...)`
Special cases for `*` and `\\` forward to `mul!` and `ldiv!`, respectively.
"""
macro !(ex)
if @capture(ex, x_ = f_(args__; kwargs_...))
esc(:($(Symbol(string(f,"!")))($x,$(args...); $kwargs...)))
elseif @capture(ex, x_ = f_(args__))
if f == :*
f = :mul
elseif f==:\
f = :ldiv
end
esc(:($x = $(Symbol(string(f,"!")))($x,$(args...))::typeof($x))) # ::typeof part helps inference sometimes
else
error("Usage: @! x = f(...)")
end
end
nan2zero(x::T) where {T} = !isfinite(x) ? zero(T) : x
nan2zero(x::Diagonal{T}) where {T} = Diagonal{T}(nan2zero.(x.diag))
nan2inf(x::T) where {T} = !isfinite(x) ? T(Inf) : x
""" Return a tuple with the expression repeated n times """
macro repeated(ex,n)
:(tuple($(repeated(esc(ex),n)...)))
end
"""
Pack some variables in a dictionary
```julia
> x = 3
> y = 4
> @dict x y z=>5
Dict(:x=>3,:y=>4,:z=>5)
```
"""
macro dict(exs...)
kv(ex::Symbol) = :($(QuoteNode(ex))=>$(esc(ex)))
kv(ex) = isexpr(ex,:call) && ex.args[1]==:(=>) ? :($(QuoteNode(ex.args[2]))=>$(esc(ex.args[3]))) : error()
:(Dict($((kv(ex) for ex=exs)...)))
end
"""
Pack some variables into a NamedTuple. E.g.:
```julia
> x = 3
> y = 4
> @namedtuple(x, y, z=5)
(x=3,y=4,z=5)
```
"""
macro namedtuple(exs...)
if length(exs)==1 && isexpr(exs[1],:tuple)
exs = exs[1].args
end
kv(ex::Symbol) = :($(esc(ex))=$(esc(ex)))
kv(ex) = isexpr(ex,:(=)) ? :($(esc(ex.args[1]))=$(esc(ex.args[2]))) : error()
Expr(:tuple, (kv(ex) for ex=exs)...)
end
# these allow pinv and sqrt of SMatrices of Diagonals to work correctly, which
# we use for the T-E block of the covariance. hopefully some of this can be cut
# down on in the futue with some PRs into StaticArrays.
permutedims(A::SMatrix{2,2}) = @SMatrix[A[1] A[3]; A[2] A[4]]
function sqrt(A::SMatrix{2,2,<:Diagonal})
a,b,c,d = A
s = @. sqrt(a*d-b*c)
t = pinv(@. sqrt(a+d+2s))
@SMatrix[t*(a+s) t*b; t*c t*(d+s)]
end
function pinv(A::SMatrix{2,2,<:Diagonal})
a,b,c,d = A
idet = pinv(@. a*d-b*c)
@SMatrix[d*idet -(b*idet); -(c*idet) a*idet]
end
# some usefule tuple manipulation functions:
# see: https://discourse.julialang.org/t/efficient-tuple-concatenation/5398/10
# and https://github.com/JuliaLang/julia/issues/27988
@inline tuplejoin(x) = x
@inline tuplejoin(x, y) = (x..., y...)
@inline tuplejoin(x, y, z...) = (x..., tuplejoin(y, z...)...)
# see https://discourse.julialang.org/t/any-way-to-make-this-one-liner-type-stable/10636/2
using Base: tuple_type_cons, tuple_type_head, tuple_type_tail, first, tail
map_tupleargs(f,::Type{T}) where {T<:Tuple} =
(f(tuple_type_head(T)), map_tupleargs(f,tuple_type_tail(T))...)
map_tupleargs(f,::Type{T},::Type{S}) where {T<:Tuple,S<:Tuple} =
(f(tuple_type_head(T),tuple_type_head(S)), map_tupleargs(f,tuple_type_tail(T),tuple_type_tail(S))...)
map_tupleargs(f,::Type{T},s::Tuple) where {T<:Tuple} =
(f(tuple_type_head(T),first(s)), map_tupleargs(f,tuple_type_tail(T),tail(s))...)
map_tupleargs(f,::Type{<:Tuple{}}...) = ()
map_tupleargs(f,::Type{<:Tuple{}},::Tuple) = ()
# returns the base parametric type with all type parameters stripped out
basetype(::Type{T}) where {T} = T.name.wrapper
@generated function basetype(t::UnionAll)
unwrap_expr(s::UnionAll, t=:t) = unwrap_expr(s.body, :($t.body))
unwrap_expr(::DataType, t) = t
:($(unwrap_expr(t.parameters[1])).name.wrapper)
end
function ensuresame(args...)
@assert all(args .== Ref(args[1]))
args[1]
end
tuple_type_len(::Type{<:NTuple{N,Any}}) where {N} = N
ensure1d(x::Union{Tuple,AbstractArray}) = x
ensure1d(x) = (x,)
# see https://discourse.julialang.org/t/dispatching-on-the-result-of-unwrap-unionall-seems-weird/25677
# for why we need this
# to use, just decorate the custom show_datatype with it, and make sure the args
# are named `io` and `t`.
macro show_datatype(ex)
def = splitdef(ex)
def[:body] = quote
isconcretetype(t) ? $(def[:body]) : invoke(Base.show_datatype, Tuple{IO,DataType}, io, t)
end
esc(combinedef(def))
end
"""
# symmetric in any of its final arguments except for bar:
@sym_memo foo(bar, @sym(args...)) = <body>
# symmetric in (i,j), but not baz
@sym_memo foo(baz, @sym(i, j)) = <body>
The `@sym_memo` macro should be applied to a definition of a function
which is symmetric in some of its arguments. The arguments in which its
symmetric are specified by being wrapping them in @sym, and they must come at
the very end. The resulting function will be memoized and permutations of the
arguments which are equal due to symmetry will only be computed once.
"""
macro sym_memo(funcdef)
sfuncdef = splitdef(funcdef)
asymargs = sfuncdef[:args][1:end-1]
symargs = collect(@match sfuncdef[:args][end] begin
Expr(:macrocall, [head, _, ex...]), if head==Symbol("@sym") end => ex
_ => error("final argument(s) should be marked @sym")
end)
sfuncdef[:args] = [asymargs..., symargs...]
sfuncdef[:body] = quote
symargs = [$(symargs...)]
sorted_symargs = sort(symargs)
if symargs==sorted_symargs
$((sfuncdef[:body]))
else
$(sfuncdef[:name])($(asymargs...), sorted_symargs...)
end
end
esc(:(@memoize $(combinedef(sfuncdef))))
end
@doc doc"""
```
@subst sum(x*$(y+1) for x=1:2)
```
becomes
```
let tmp=(y+1)
sum(x*tmp for x=1:2)
end
```
to aid in writing clear/succinct code that doesn't recompute things
unnecessarily.
"""
macro subst(ex)
subs = []
ex = postwalk(ex) do x
if isexpr(x, Symbol(raw"$"))
var = gensym()
push!(subs, :($(esc(var))=$(esc(x.args[1]))))
var
else
x
end
end
quote
let $(subs...)
$(esc(ex))
end
end
end
"""
@invokelatest expr...
Rewrites all non-broadcasted function calls anywhere within an expression to use
`Base.invokelatest`. This means functions can be called that have a newer world
age, at the price of making things non-inferrable.
"""
macro invokelatest(ex)
function walk(x)
if isdef(x)
x.args[2:end] .= map(walk, x.args[2:end])
x
elseif @capture(x, f_(args__; kwargs__)) && !startswith(string(f),'.')
:(Base.invokelatest($f, $(map(walk,args)...); $(map(walk,kwargs)...)))
elseif @capture(x, f_(args__)) && !startswith(string(f),'.')
:(Base.invokelatest($f, $(map(walk,args)...)))
elseif isexpr(x)
x.args .= map(walk, x.args)
x
else
x
end
end
esc(walk(ex))
end
"""
@ondemand(Package.function)(args...; kwargs...)
@ondemand(Package.Submodule.function)(args...; kwargs...)
Just like calling `Package.function` or `Package.Submodule.function`, but
`Package` will be loaded on-demand if it is not already loaded. The call is no
longer inferrable.
"""
macro ondemand(ex)
get_root_package(x) = @capture(x, a_.b_) ? get_root_package(a) : x
quote
@eval import $(get_root_package(ex))
(args...; kwargs...) -> Base.invokelatest($(esc(ex)), args...; kwargs...)
end
end
function tmap(f, args...)
@static if nthreads()==1 || VERSION<v"1.3"
map(f, args...)
else
map(fetch,map((args...)->Threads.@spawn(f(args...)),args...))
end
end
get_kwarg_names(func::Function) = Vector{Symbol}(kwarg_decl(first(methods(func)), typeof(methods(func).mt.kwsorter)))
kwarg_decl(m::Method,kw::DataType) = VERSION<=v"1.3.999" ? Base.kwarg_decl(m,kw) : Base.kwarg_decl(m)
# maps a function recursively across all arguments of a Broadcasted expression,
# using the function `broadcasted` to reconstruct the `Broadcasted` object at
# each point.
map_bc_args(f, bc::Broadcasted) = broadcasted(bc.f, map(arg->map_bc_args(f, arg), bc.args)...)
map_bc_args(f, arg) = f(arg)
# adapting a closure adapts the captured variables
# this could probably be a PR into Adapt.jl
@generated function adapt_structure(to, f::F) where {F<:Function}
if fieldcount(F) == 0
:f
else
quote
captured_vars = $(Expr(:tuple, (:(adapt(to, f.$x)) for x=fieldnames(F))...))
$(Expr(:new, :($(F.name.wrapper){map(typeof,captured_vars)...}), (:(captured_vars[$i]) for i=1:fieldcount(F))...))
end
end
end
adapt_structure(to, d::Dict) = Dict(k => adapt(to, v) for (k,v) in d)
@doc doc"""
cpu(xs)
Recursively move an object to CPU memory (i.e. the opposite of `cu`)
"""
cpu(xs) = adapt_structure(Array, xs)
function corrify(H)
σ = sqrt.(abs.(diag(H)))
for i=1:checksquare(H)
H[i,:] ./= σ
H[:,i] ./= σ
end
H
end
struct FailedPyimport
err
end
getproperty(p::FailedPyimport, ::Symbol) = throw(getfield(p,:err))
@doc doc"""
safe_pyimport(s)
Like `pyimport`, but if `s` fails to import, instead of an error right away, the
error will be thrown the first time the user tries to access the contents of the
module.
"""
function safe_pyimport(s)
try
pyimport(s)
catch err
FailedPyimport(err)
end
end
@doc doc"""
@ismain()
Return true if the current file is being run as a script.
"""
macro ismain()
(__source__ != nothing) && (String(__source__.file) == abspath(PROGRAM_FILE))
end
@doc doc"""
seed_for_storage!(storage[, seed])
seed_for_storage!((storage1, storage2, ...)[, seed])
Set the global random seed for the RNG which controls `storage`-type.
"""
seed_for_storage!(::Type{<:Array}, seed=nothing) =
Random.seed!((seed == nothing ? () : (seed,))...)
seed_for_storage!(storage::Any, seed=nothing) =
error("Don't know how to set seed for storage=$storage")
seed_for_storage!(storages::Tuple, seed=nothing) =
seed_for_storage!.(storages, seed)
### parallel utility function
@init @require MPIClusterManagers="e7922434-ae4b-11e9-05c5-9780451d2c66" begin
using .MPIClusterManagers: MPI, start_main_loop, TCP_TRANSPORT_ALL
@doc doc"""
init_MPI_workers()
Initialize MPI processes as Julia workers that can be used in a
parallel chain run.
If CuArrays is loaded and functional in the Main module,
additionally bind each MPI process to one GPU.
This call only returns on the master process.
"""
function init_MPI_workers(;stdout_to_master=false, stderr_to_master=false)
if !MPI.Initialized()
MPI.Init()
end
size = MPI.Comm_size(MPI.COMM_WORLD)
rank = MPI.Comm_rank(MPI.COMM_WORLD)
if size>1
if isdefined(Main, :CuArrays) && CuArrays.functional()
# cant use @eval because of https://github.com/JuliaPackaging/Requires.jl/issues/86
Main.eval(:(CuArrays.CUDAnative.device!(mod($rank,$size-1))))
device_string = @eval Main CuArrays.CUDAdrv.device()
else
device_string = "CPU"
end
@info "MPI process $rank $(rank==0 ? "(master)" : "(worker)") is using $device_string"
start_main_loop(TCP_TRANSPORT_ALL, stdout_to_master=stdout_to_master, stderr_to_master=stderr_to_master)
end
end
end
firsthalf(x) = x[1:end÷2]
lasthalf(x) = x[end÷2:end]
function sum_kbn(A::Array{T,N}; dims=:) where {T,N}
if (dims == (:)) || (N == length(dims))
KahanSummation.sum_kbn(A)
else
dropdims(mapslices(sum_kbn, A, dims=dims), dims=dims) :: Array{T,N-length(dims)}
end
end
# courtesy of Takafumi Arakaki
versionof(pkg::Module) = versionof(Base.PkgId(pkg))
if isdefined(Pkg, :dependencies)
# can drop this branch once I drop support for 1.3
versionof(pkg::Base.PkgId) = Pkg.dependencies()[pkg.uuid].version
else
versionof(pkg::Base.PkgId) = Pkg.installed()[pkg.name]
end