-
Notifications
You must be signed in to change notification settings - Fork 19
Progress bars when sampling multiple chains #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 27 commits
b3434ac
15250c7
367718b
60134f8
e0ae513
6b514e4
bbda3c8
a9e5306
a03692d
838db60
6b59b21
b340ebc
1195503
594483f
7def4b4
022678e
5b2577f
cefafb0
c6f9e78
d9c2e86
f8a8b64
64b0bfb
27569b3
4cd647a
9f8970d
3e43f6a
7276fc2
284741f
5c5b912
eebb10b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,109 @@ | ||
# avoid creating a progress bar with @withprogress if progress logging is disabled | ||
# and add a custom progress logger if the current logger does not seem to be able to handle | ||
# progress logs | ||
macro ifwithprogresslogger(progress, exprs...) | ||
""" | ||
AbstractProgressKwarg | ||
|
||
Abstract type representing the values that the `progress` keyword argument can | ||
internally take for single-chain sampling. | ||
""" | ||
abstract type AbstractProgressKwarg end | ||
|
||
""" | ||
CreateNewProgressBar | ||
|
||
Create a new logger for progress logging. | ||
""" | ||
struct CreateNewProgressBar{S<:AbstractString} <: AbstractProgressKwarg | ||
name::S | ||
uuid::UUIDs.UUID | ||
function CreateNewProgressBar(name::AbstractString) | ||
return new{typeof(name)}(name, UUIDs.uuid4()) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the below functions have a trailing (Doesn't really matter, we just as well leave the names as is) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, they are mutating (although the macro hides the worst of it). Will change. |
||
function init_progress!(p::CreateNewProgressBar) | ||
ProgressLogging.@logprogress p.name nothing _id = p.uuid | ||
end | ||
function update_progress!(p::CreateNewProgressBar, progress_frac) | ||
ProgressLogging.@logprogress p.name progress_frac _id = p.uuid | ||
end | ||
function finish_progress!(p::CreateNewProgressBar) | ||
ProgressLogging.@logprogress p.name "done" _id = p.uuid | ||
end | ||
|
||
""" | ||
NoLogging | ||
|
||
Do not log progress at all. | ||
""" | ||
struct NoLogging <: AbstractProgressKwarg end | ||
init_progress!(::NoLogging) = nothing | ||
update_progress!(::NoLogging, ::Any) = nothing | ||
finish_progress!(::NoLogging) = nothing | ||
|
||
""" | ||
ExistingProgressBar | ||
|
||
Use an existing progress bar to log progress. This is used for tracking | ||
progress in a progress bar that has been previously generated elsewhere, | ||
specifically, when `sample(..., MCMCThreads(), ...; progress=:perchain)` is | ||
called. In this case we can use `@logprogress name progress_frac _id = uuid` to | ||
log progress. | ||
""" | ||
struct ExistingProgressBar{S<:AbstractString} <: AbstractProgressKwarg | ||
name::S | ||
uuid::UUIDs.UUID | ||
end | ||
function init_progress!(p::ExistingProgressBar) | ||
# Hacky code to reset the start timer if called from a multi-chain sampling | ||
# process. We need this because the progress bar is constructed in the | ||
# multi-chain method, i.e. if we don't do this the progress bar shows the | ||
# time elapsed since _all_ sampling began, not since the current chain | ||
# started. | ||
try | ||
bartrees = Logging.current_logger().loggers[1].logger.bartrees | ||
bar = TerminalLoggers.findbar(bartrees, p.uuid).data | ||
bar.tfirst = time() | ||
catch | ||
end | ||
ProgressLogging.@logprogress p.name nothing _id = p.uuid | ||
end | ||
function update_progress!(p::ExistingProgressBar, progress_frac) | ||
ProgressLogging.@logprogress p.name progress_frac _id = p.uuid | ||
end | ||
function finish_progress!(p::ExistingProgressBar) | ||
ProgressLogging.@logprogress p.name "done" _id = p.uuid | ||
end | ||
|
||
""" | ||
ChannelProgress | ||
|
||
Use a `Channel` to log progress. This is used for 'reporting' progress back | ||
to the main thread or worker when using `progress=:overall` with MCMCThreads or | ||
MCMCDistributed. | ||
|
||
n_updates is the number of updates that each child thread is expected to report | ||
back to the main thread. | ||
""" | ||
struct ChannelProgress{T<:Union{Channel{Bool},Distributed.RemoteChannel{Channel{Bool}}}} <: | ||
AbstractProgressKwarg | ||
channel::T | ||
n_updates::Int | ||
end | ||
init_progress!(::ChannelProgress) = nothing | ||
update_progress!(p::ChannelProgress, ::Any) = put!(p.channel, true) | ||
# Note: We don't want to `put!(p.channel, false)`, because that would stop the | ||
# channel from being used for further updates e.g. from other chains. | ||
finish_progress!(::ChannelProgress) = nothing | ||
|
||
# Add a custom progress logger if the current logger does not seem to be able to handle | ||
# progress logs. | ||
macro maybewithricherlogger(expr) | ||
return esc( | ||
quote | ||
if $progress | ||
if $hasprogresslevel($Logging.current_logger()) | ||
$ProgressLogging.@withprogress $(exprs...) | ||
else | ||
$with_progresslogger($Base.@__MODULE__, $Logging.current_logger()) do | ||
$ProgressLogging.@withprogress $(exprs...) | ||
end | ||
if !($hasprogresslevel($Logging.current_logger())) | ||
$with_progresslogger($Base.@__MODULE__, $Logging.current_logger()) do | ||
$(expr) | ||
end | ||
else | ||
$(exprs[end]) | ||
$(expr) | ||
end | ||
end, | ||
) | ||
|
@@ -39,8 +129,7 @@ end | |
function progresslogger() | ||
# detect if code is running under IJulia since TerminalLogger does not work with IJulia | ||
# https://github.com/JuliaLang/IJulia.jl#detecting-that-code-is-running-under-ijulia | ||
if (Sys.iswindows() && VERSION < v"1.5.3") || | ||
(isdefined(Main, :IJulia) && Main.IJulia.inited) | ||
if (isdefined(Main, :IJulia) && Main.IJulia.inited) | ||
return ConsoleProgressMonitor.ProgressLogger() | ||
else | ||
return TerminalLoggers.TerminalLogger() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be "same as
:none
"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and also "use
:perchain
for 10 or fewer chains, and:overall
" 😅There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, changed.