This repository has been archived by the owner on Dec 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding two unit tests for the nnc_compile option
- Loading branch information
1 parent
8eda5f0
commit acd2287
Showing
2 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,47 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import beanmachine.ppl as bm | ||
import torch.distributions as dist | ||
import warnings | ||
import torch | ||
import pytest | ||
|
||
|
||
class SampleModel: | ||
@bm.random_variable | ||
def foo(self): | ||
return dist.Normal(0.0, 1.0) | ||
|
||
@bm.random_variable | ||
def bar(self): | ||
return dist.Normal(self.foo(), 1.0) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"algorithm", | ||
[ | ||
bm.GlobalNoUTurnSampler(nnc_compile=True), | ||
bm.GlobalHamiltonianMonteCarlo(trajectory_length=1.0, nnc_compile=True), | ||
], | ||
) | ||
def test_nnc_compile(algorithm): | ||
model = SampleModel() | ||
queries = [model.foo()] | ||
observations = {model.bar(): torch.tensor(0.5)} | ||
num_samples = 30 | ||
num_chains = 2 | ||
with warnings.catch_warnings(): | ||
warnings.simplefilter("ignore") | ||
# verify that NNC can run through | ||
samples = algorithm.infer( | ||
queries, | ||
observations, | ||
num_samples, | ||
num_adaptive_samples=num_samples, | ||
num_chains=num_chains, | ||
) | ||
# sanity check: make sure that the samples are valid | ||
assert not torch.isnan(samples[model.foo()]).any() |
This file contains 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