-
Notifications
You must be signed in to change notification settings - Fork 10
/
util.jl
500 lines (392 loc) · 14.1 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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
versionof(pkg::Module) = Pkg.dependencies()[Base.PkgId(pkg).uuid].version
"""
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))...)
"""
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) ? x : zero(T)
@adjoint nan2zero(x::T) where {T} = nan2zero(x), Δ -> (isfinite(x) ? Δ : zero(T),)
""" 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
macro namedtuple(exs...)
Base.depwarn("@namedtuple(x,y) is deprecated and will be removed soon, just use the built-in Julia (;x,y) now.", nothing)
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,)
# https://discourse.julialang.org/t/dispatching-on-the-result-of-unwrap-unionall-seems-weird/25677
# has some background related to this function. we can simplify this in 1.6
typealias(t::UnionAll) = sprint(io -> Base.show(io, t))
function typealias(t::DataType)
if isconcretetype(t)
ta = typealias_def(t)
if !isnothing(ta)
return ta
end
end
sprint(io -> invoke(Base.show_datatype, Tuple{IO,DataType}, io, t))
end
typealias_def(t) = nothing
@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
"""
@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))
InvokeLatestFunction($(esc(ex)))
end
end
struct InvokeLatestFunction
func
end
(func::InvokeLatestFunction)(args...; kwargs...) = Base.@invokelatest(func.func(args...; kwargs...))
Base.broadcast(func::InvokeLatestFunction, args...; kwargs...) = Base.@invokelatest(broadcast(func.func, args...; kwargs...))
get_kwarg_names(func::Function) = Vector{Symbol}(Base.kwarg_decl(first(methods(func))))
# https://discourse.julialang.org/t/is-there-a-way-to-modify-captured-variables-in-a-closure/31213/16
@static if versionof(Adapt) < v"3.1.0"
@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
end
adapt_structure(to, d::Dict) = Dict(k => adapt(to, v) for (k,v) in d)
@doc doc"""
cpu(x)
Recursively move an object to CPU memory. See also [`gpu`](@ref).
"""
cpu(x) = adapt_structure(Array, x)
@doc doc"""
@cpu! x y
Equivalent to `x = cpu(x)`, `y = cpu(y)`, etc... for any number of
listed variables. See [`cpu`](@ref).
"""
macro cpu!(vars...)
:(begin; $((:($(esc(var)) = cpu($(esc(var)))) for var in vars)...); nothing; end)
end
@doc doc"""
gpu(x)
Recursively move an object to GPU memory. Note that, unlike `cu(x)`,
this does not change the `eltype` of any underlying arrays. See also
[`cpu`](@ref).
"""
function gpu end # defined in gpu.jl only when CUDA.jl is loaded
function corrify(H)
H = copy(H)
σ = sqrt.(abs.(diag(H)))
for i=1:checksquare(H)
H[i,:] ./= σ
H[:,i] ./= σ
end
H
end
struct LazyPyImport
pkg
end
function getproperty(p::LazyPyImport, s::Symbol)
pkg = @ondemand(PyCall.pyimport)(getfield(p,:pkg))
Base.invokelatest() do
prop = getproperty(pkg, s)
if PyCall.pybuiltin(:callable)(prop)
(args...; kwargs...) -> Base.invokelatest(prop, args...; kwargs...)
else
prop
end
end
end
@doc doc"""
lazy_pyimport(s)
Like `pyimport(s)`, but doesn't actually load anything (not even
PyCall) until a property of the returned module is accessed, allowing
this to go in `__init__` and still delay loading PyCall, as well as
preventing a Julia module load error if a Python module failed to load.
"""
lazy_pyimport(s) = LazyPyImport(s)
@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
firsthalf(x) = x[1:end÷2]
lasthalf(x) = x[end÷2:end]
USE_SUM_KBN = false
use_sum_kbn!(flag) = (global USE_SUM_KBN = flag)
# type-stable combination of summing and dropping dims, which uses
# either sum or sum_kbn (to reduce roundoff error), depending on
# CMBLensing.USE_SUM_KBN constant
function sum_dropdims(A::AbstractArray{T,N}; dims=:) where {T,N}
if (dims == (:)) || (N == length(dims))
if USE_SUM_KBN
sum_kbn(cpu(A))
else
sum(A)
end :: T
else
if USE_SUM_KBN
dropdims(mapslices(sum_kbn, cpu(A), dims=dims), dims=dims) :: Array{T,N-length(dims)}
else
dropdims(sum(A, dims=dims), dims=dims)
end
end
end
@adjoint sum_dropdims(A) = sum_dropdims(A), Δ -> (fill!(similar(A),Δ),)
# for mixed eltype, which Loess stupidly does not support
Loess.loess(x::AbstractVector, y::AbstractVector; kwargs...) =
loess(collect.(zip(promote.(x,y)...))...; kwargs...)
expnorm(x) = exp.(x .- maximum(x))
# MacroTool's is broken https://github.com/FluxML/MacroTools.jl/issues/154
_isdef(ex) = @capture(ex, function f_(arg__) body_ end)
"""
@⌛ [label] code ...
@⌛ [label] function_definition() = ....
Label a section of code to be timed. If a label string is not
provided, the first form uses the code itselfs as a label, the second
uses the function name, and its the body of the function which is
timed.
To run the timer and print output, returning the result of the
calculation, use
@show⌛ run_code()
Timing uses `TimerOutputs.get_defaulttimer()`.
"""
macro ⌛(args...)
if length(args)==1
label, ex = nothing, args[1]
else
label, ex = esc(args[1]), args[2]
end
source_str = last(splitpath(string(__source__.file)))*":"*string(__source__.line)
if _isdef(ex)
sdef = splitdef(ex)
if isnothing(label)
label = "$(string(sdef[:name]))(…) ($source_str)"
end
sdef[:body] = quote
CMBLensing.@timeit $label $(sdef[:body])
end
esc(combinedef(sdef))
else
if isnothing(label)
label = "$(Base._truncate_at_width_or_chars(string(prewalk(rmlines,ex)),26)) ($source_str)"
end
:(@timeit $label $(esc(ex)))
end
end
"""
See [`@⌛`](@ref)
"""
macro show⌛(ex)
quote
reset_timer!(get_defaulttimer())
result = $(esc(ex))
show(get_defaulttimer())
result
end
end
# used in a couple of places to create a Base.promote_rule-like system
# where you can specify a set of rules for promotion via dispatch but
# don't need to write a method for both orders
select_known_rule(rule, x, y) = select_known_rule(rule, x, y, rule(x,y), rule(y,x))
select_known_rule(rule, x, y, R₁::Any, R₂::Unknown) = R₁
select_known_rule(rule, x, y, R₁::Unknown, R₂::Any) = R₂
select_known_rule(rule, x, y, R₁::Any, R₂::Any) = (R₁ == R₂) ? R₁ : error("Conflicting rules.")
select_known_rule(rule, x, y, R₁::Unknown, R₂::Unknown) = unknown_rule_error(rule, x, y)
"""
@auto_adjoint foo(args...; kwargs...) = body
is equivalent to
_foo(args...; kwargs...) = body
foo(args...; kwargs...) = _foo(args...; kwargs...)
@adjoint foo(args...; kwargs...) = Zygote.pullback(_foo, args...; kwargs...)
That is, it defines the function as well as a Zygote adjoint which
takes a gradient explicitly through the body of the function, rather
than relying on rules which may be defined for `foo`. Mainly useful in
the case that `foo` is a common function with existing rules, but
which you do *not* want to be used.
"""
macro auto_adjoint(funcdef)
sdef = splitdef(funcdef)
name = sdef[:name]
sdef[:name] = symname = gensym(string(name))
defs = []
push!(defs, combinedef(sdef))
sdef[:name] = name
sdef[:body] = :($symname($(sdef[:args]...); $(sdef[:kwargs]...)))
push!(defs, :(Core.@__doc__ $(combinedef(sdef))))
sdef[:body] = :($Zygote.pullback($symname, $(sdef[:args]...); $(sdef[:kwargs]...)))
push!(defs, :($Zygote.@adjoint $(combinedef(sdef))))
esc(Expr(:block, defs...))
end
string_trunc(x) = Base._truncate_at_width_or_chars(string(x), displaysize(stdout)[2]-14)
import NamedTupleTools
NamedTupleTools.select(d::Dict, keys) = (;(k=>d[k] for k in keys)...)
@init @require ComponentArrays="b0b7db55-cfe3-40fc-9ded-d10e2dbeff66" begin
using .ComponentArrays
# a Zygote-compatible conversion of ComponentVector to a NamedTuple
Base.convert(::Type{NamedTuple}, x::ComponentVector) = NamedTuple{keys(x)}([x[k] for k in keys(x)])
@adjoint function Base.convert(::Type{NamedTuple}, x::ComponentVector)
nt = convert(NamedTuple, x)
function back(Δ)
(nothing, ComponentArray(;(k => isnothing(Δₖ) ? zero(ntₖ) : Δₖ for (k,ntₖ,Δₖ) in zip(keys(nt), nt, Δ))...))
end
nt, back
end
end
# https://github.com/JuliaLang/julia/issues/41030
@init ccall(:jl_generating_output,Cint,())!=1 && @eval Base function start_worker_task!(worker_tasks, exec_func, chnl, batch_size=nothing)
t = @async begin
retval = nothing
try
if isa(batch_size, Number)
while isopen(chnl)
# The mapping function expects an array of input args, as it processes
# elements in a batch.
batch_collection=Any[]
n = 0
for exec_data in chnl
push!(batch_collection, exec_data)
n += 1
(n == batch_size) && break
end
if n > 0
exec_func(batch_collection)
end
end
else
for exec_data in chnl
exec_func(exec_data...)
end
end
catch e
close(chnl)
Base.display_error(stderr, Base.catch_stack())
retval = e
end
retval
end
push!(worker_tasks, t)
end
real_type(T) = promote_type(real(T), Float32)
@init @require Unitful="1986cc42-f94f-5a68-af5c-568840ba703d" real_type(::Type{<:Unitful.Quantity{T}}) where {T} = real_type(T)