-
Notifications
You must be signed in to change notification settings - Fork 13
Deterministic seeded random streams across runs #194
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 4 commits
d5c03f2
c12ff3b
8b3ea0a
212724c
7336afe
ce3d265
24a7eff
102fe6d
393b3e3
ae3a300
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 |
|---|---|---|
|
|
@@ -2,6 +2,19 @@ function worker_init(f::File, options::Dict) | |
| project = WorkerSetup.LOADER_ENV[] | ||
| return lock(WorkerSetup.WORKER_SETUP_LOCK) do | ||
| return quote | ||
| # issue #192 | ||
| # Malt itself uses a new task for each `remote_eval` and because of this, random number streams | ||
| # are not consistens across runs even if seeded, as each task introduces a new state for its | ||
| # task-local RNG. As a workaround, we use feed all `remote_eval` requests through these channels, such | ||
| # that the task executing code is always the same. | ||
| var"__stable_execution_task_channel_out" = Channel() | ||
| var"__stable_execution_task_channel_in" = Channel() do chan | ||
jkrumbiegel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for expr in chan | ||
| result = Core.eval(Main, expr) | ||
|
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. I think QuartoNotebookWorker has its own error handling, but you may want to catch errors from this call to prevent future
Collaborator
Author
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. Hm yes I might have to think about what to do in this case, but for normal notebook usage, barring bugs in QuartoNotebookRunner, the error handling inside the passed expressions should be sufficient (as indicated by the test suite passing). Of course it would be nicer if in case of bugs, the behavior was still somewhat robust |
||
| put!(var"__stable_execution_task_channel_out", result) | ||
| end | ||
| end | ||
|
|
||
| push!(LOAD_PATH, $(project)) | ||
|
|
||
| let QNW = task_local_storage(:QUARTO_NOTEBOOK_WORKER_OPTIONS, $(options)) do | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --- | ||
| title: Random seed | ||
| --- | ||
|
|
||
| ```{julia} | ||
| using Random | ||
| Random.seed!(123) | ||
| rand() | ||
| ``` | ||
|
|
||
| ```{julia} | ||
| rand() | ||
| ``` | ||
|
|
||
| ```{julia} | ||
| rand() | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| include("../utilities/prelude.jl") | ||
|
|
||
| @testset "seeded random numbers are consistent across runs" begin | ||
| mktempdir() do dir | ||
| content = read(joinpath(@__DIR__, "../examples/random_seed.qmd"), String) | ||
| cd(dir) do | ||
| server = QuartoNotebookRunner.Server() | ||
| write("notebook.qmd", content) | ||
|
|
||
| jsons = map(1:2) do _ | ||
| QuartoNotebookRunner.run!(server, "notebook.qmd"; showprogress = false) | ||
| end | ||
|
|
||
| _output(cell) = only(cell.outputs).data["text/plain"] | ||
|
|
||
| @test tryparse(Float64, _output(jsons[1].cells[2])) !== nothing | ||
| @test tryparse(Float64, _output(jsons[1].cells[4])) !== nothing | ||
| @test tryparse(Float64, _output(jsons[1].cells[6])) !== nothing | ||
|
|
||
| @test length(unique([_output(jsons[1].cells[i]) for i in [2, 4, 6]])) == 3 | ||
|
|
||
| @test _output(jsons[1].cells[2]) == _output(jsons[2].cells[2]) | ||
| @test _output(jsons[1].cells[4]) == _output(jsons[2].cells[4]) | ||
| @test _output(jsons[1].cells[6]) == _output(jsons[2].cells[6]) | ||
|
|
||
| close!(server) | ||
| end | ||
| end | ||
| end |
Uh oh!
There was an error while loading. Please reload this page.