-
Notifications
You must be signed in to change notification settings - Fork 92
Add Turing integration tests (and more Bijectors tests) #2863
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f5037c
Add Turing integration tests (and more Bijectors tests)
penelopeysm 60989ec
twiddle with 1.10 Bijectors test
penelopeysm 939e342
Bump DynamicPPL version
penelopeysm 2c72d16
Merge branch 'main' into main
penelopeysm 5a43826
check for need for runtime activity with Bijectors
penelopeysm 3e7727c
update Turing tests as Const no longer needed
penelopeysm e2d9f97
Format
penelopeysm f4ae5ad
fix 1.10 runtime activity
penelopeysm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,7 @@ jobs: | |
| - Molly | ||
| - MPI | ||
| - Comrade | ||
| - Turing | ||
| exclude: | ||
| - version: '1.10' | ||
| os: linux-x86-n2-32 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [deps] | ||
| ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" | ||
| DynamicPPL = "366bfd00-2699-11ea-058f-f148b4cae6d8" | ||
| Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9" | ||
| EnzymeCore = "f151be2c-9106-41f4-ab19-57ee4f262869" | ||
| ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" | ||
| LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c" | ||
| StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3" | ||
| Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" | ||
|
|
||
| [sources] | ||
| Enzyme = {path = "../../.."} | ||
| EnzymeCore = {path = "../../../lib/EnzymeCore"} | ||
|
|
||
| [compat] | ||
| Turing = "=0.42.1" | ||
| DynamicPPL = "=0.39.9" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| module TuringIntegrationTests | ||
|
|
||
| using ADTypes: AutoEnzyme | ||
| using DynamicPPL | ||
| using Enzyme: Enzyme | ||
| import ForwardDiff | ||
| using StableRNGs: StableRNG | ||
| using Test | ||
| using Turing | ||
|
|
||
| adtypes = ( | ||
| AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Forward)), | ||
| AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Reverse)), | ||
| ) | ||
|
|
||
| # Some supplements to DynamicPPL.TestUtils.ALL_MODELS. | ||
| @model function assume_normal() | ||
| a ~ Normal() | ||
| end | ||
| dppl_lda = begin | ||
| v = 100 # words | ||
| k = 5 # topics | ||
| m = 10 # number of docs | ||
| alpha = ones(k) | ||
| beta = ones(v) | ||
| phi = rand(Dirichlet(beta), k) | ||
| theta = rand(Dirichlet(alpha), m) | ||
| doc_lengths = rand(Poisson(1_000), m) | ||
| n = sum(doc_lengths) | ||
| w = Vector{Int}(undef, n) | ||
| doc = Vector{Int}(undef, n) | ||
| for i in 1:m | ||
| local idx = sum(doc_lengths[1:(i - 1)]) # starting index for inner loop | ||
| for j in 1:doc_lengths[i] | ||
| z = rand(Categorical(theta[:, i])) | ||
| w[idx + j] = rand(Categorical(phi[:, z])) | ||
| doc[idx + j] = i | ||
| end | ||
| end | ||
| @model function dppl_lda(k, m, w, doc, alpha, beta) | ||
| theta ~ product_distribution(fill(Dirichlet(alpha), m)) | ||
| phi ~ product_distribution(fill(Dirichlet(beta), k)) | ||
| log_phi_dot_theta = log.(phi * theta) | ||
| @addlogprob! sum(log_phi_dot_theta[CartesianIndex.(w, doc)]) | ||
| end | ||
| dppl_lda | ||
| end | ||
| MODELS = [ | ||
| DynamicPPL.TestUtils.ALL_MODELS..., | ||
| assume_normal(), | ||
| dppl_lda(k, m, w, doc, alpha, beta), | ||
| ] | ||
|
|
||
| @testset "AD on logdensity" begin | ||
| # This code is essentially what Turing's HMC/NUTS samplers use internally | ||
| @testset "$(model.f)" for model in MODELS | ||
| @testset "AD type: $(adtype)" for adtype in adtypes | ||
| @test DynamicPPL.TestUtils.AD.run_ad(model, adtype; rng = StableRNG(468), test = true, benchmark = false) isa Any | ||
| end | ||
| end | ||
| end | ||
|
|
||
| @testset "AD / Gibbs sampling" begin | ||
| # The code to differentiate for the Gibbs sampler is slightly different from the | ||
| # HMC/NUTS samplers (even though each individual variable is sampled with HMC) so we | ||
| # have to test it separately. | ||
| @testset "AD type: $(adtype)" for adtype in adtypes | ||
| spl = Gibbs( | ||
| @varname(s) => HMC(0.1, 10; adtype = adtype), | ||
| @varname(m) => HMC(0.1, 10; adtype = adtype), | ||
| ) | ||
| @testset "model=$(model.f)" for model in DynamicPPL.TestUtils.DEMO_MODELS | ||
| @info "Sampling model=$(model.f) with AD type=$(adtype)" | ||
| @test sample(StableRNG(468), model, spl, 2; progress = false) isa Any | ||
| end | ||
| end | ||
| end | ||
|
|
||
| end # module | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
once TuringLang/DynamicPPL.jl#1172 is in it shouldn't need Const.