Skip to content

Commit 026292d

Browse files
authored
Merge branch 'master' into uniform-float
2 parents ee1653c + 7fcc5eb commit 026292d

File tree

16 files changed

+287
-182
lines changed

16 files changed

+287
-182
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ jobs:
7979
run: |
8080
cargo test --target ${{ matrix.target }} --features=nightly
8181
cargo test --target ${{ matrix.target }} --all-features
82-
cargo test --target ${{ matrix.target }} --benches --features=nightly,small_rng
82+
cargo test --target ${{ matrix.target }} --benches --features=small_rng,nightly
8383
cargo test --target ${{ matrix.target }} --manifest-path rand_distr/Cargo.toml --benches
8484
cargo test --target ${{ matrix.target }} --lib --tests --no-default-features
8585
- name: Test rand
8686
run: |
8787
cargo test --target ${{ matrix.target }} --lib --tests --no-default-features
88-
cargo build --target ${{ matrix.target }} --no-default-features --features alloc,getrandom,small_rng
88+
cargo build --target ${{ matrix.target }} --no-default-features --features alloc,getrandom,small_rng,unbiased
8989
cargo test --target ${{ matrix.target }} --lib --tests --no-default-features --features=alloc,getrandom,small_rng
9090
cargo test --target ${{ matrix.target }} --examples
9191
- name: Test rand (all stable features)

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
1212
### Distributions
1313
- `{Uniform, UniformSampler}::{new, new_inclusive}` return a `Result` (instead of potentially panicking) (#1229)
1414
- `Uniform` implements `TryFrom` instead of `From` for ranges (#1229)
15+
- `Uniform` now uses Canon's method (single sampling) / Lemire's method (distribution sampling) for faster sampling (breaks value stability; #1287)
1516

1617
### Other
1718
- Simpler and faster implementation of Floyd's F2 (#1277). This

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ std_rng = ["rand_chacha"]
5151
# Option: enable SmallRng
5252
small_rng = []
5353

54+
# Option: use unbiased sampling for algorithms supporting this option: Uniform distribution.
55+
# By default, bias affecting no more than one in 2^48 samples is accepted.
56+
# Note: enabling this option is expected to affect reproducibility of results.
57+
unbiased = []
58+
5459
[workspace]
5560
members = [
5661
"rand_core",
@@ -76,6 +81,10 @@ bincode = "1.2.1"
7681
rayon = "1.5.3"
7782
criterion = { version = "0.4" }
7883

84+
[[bench]]
85+
name = "uniform"
86+
harness = false
87+
7988
[[bench]]
8089
name = "seq_choose"
8190
path = "benches/seq_choose.rs"

benches/uniform.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2021 Developers of the Rand project.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
//! Implement benchmarks for uniform distributions over integer types
10+
11+
use core::time::Duration;
12+
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
13+
use rand::distributions::uniform::{SampleRange, Uniform};
14+
use rand::prelude::*;
15+
use rand_chacha::ChaCha8Rng;
16+
use rand_pcg::{Pcg32, Pcg64};
17+
18+
const WARM_UP_TIME: Duration = Duration::from_millis(1000);
19+
const MEASUREMENT_TIME: Duration = Duration::from_secs(3);
20+
const SAMPLE_SIZE: usize = 100_000;
21+
const N_RESAMPLES: usize = 10_000;
22+
23+
macro_rules! sample {
24+
($R:ty, $T:ty, $U:ty, $g:expr) => {
25+
$g.bench_function(BenchmarkId::new(stringify!($R), "single"), |b| {
26+
let mut rng = <$R>::from_entropy();
27+
let x = rng.gen::<$U>();
28+
let bits = (<$T>::BITS / 2);
29+
let mask = (1 as $U).wrapping_neg() >> bits;
30+
let range = (x >> bits) * (x & mask);
31+
let low = <$T>::MIN;
32+
let high = low.wrapping_add(range as $T);
33+
34+
b.iter(|| (low..=high).sample_single(&mut rng));
35+
});
36+
37+
$g.bench_function(BenchmarkId::new(stringify!($R), "distr"), |b| {
38+
let mut rng = <$R>::from_entropy();
39+
let x = rng.gen::<$U>();
40+
let bits = (<$T>::BITS / 2);
41+
let mask = (1 as $U).wrapping_neg() >> bits;
42+
let range = (x >> bits) * (x & mask);
43+
let low = <$T>::MIN;
44+
let high = low.wrapping_add(range as $T);
45+
let dist = Uniform::<$T>::new_inclusive(<$T>::MIN, high).unwrap();
46+
47+
b.iter(|| dist.sample(&mut rng));
48+
});
49+
};
50+
51+
($c:expr, $T:ty, $U:ty) => {{
52+
let mut g = $c.benchmark_group(concat!("sample", stringify!($T)));
53+
g.sample_size(SAMPLE_SIZE);
54+
g.warm_up_time(WARM_UP_TIME);
55+
g.measurement_time(MEASUREMENT_TIME);
56+
g.nresamples(N_RESAMPLES);
57+
sample!(SmallRng, $T, $U, g);
58+
sample!(ChaCha8Rng, $T, $U, g);
59+
sample!(Pcg32, $T, $U, g);
60+
sample!(Pcg64, $T, $U, g);
61+
g.finish();
62+
}};
63+
}
64+
65+
fn sample(c: &mut Criterion) {
66+
sample!(c, i8, u8);
67+
sample!(c, i16, u16);
68+
sample!(c, i32, u32);
69+
sample!(c, i64, u64);
70+
sample!(c, i128, u128);
71+
}
72+
73+
criterion_group! {
74+
name = benches;
75+
config = Criterion::default();
76+
targets = sample
77+
}
78+
criterion_main!(benches);

rand_core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ impl<R: RngCore + ?Sized> RngCore for Box<R> {
451451
///
452452
/// # Examples
453453
///
454-
/// ```rust
454+
/// ```no_run
455455
/// # use std::{io, io::Read};
456456
/// # use std::fs::File;
457457
/// # use rand_core::{OsRng, RngCore};

rand_distr/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
This breaks serialization compatibility with older versions.
1010
- Upgrade Rand
1111
- Fix Knuth's method so `Poisson` doesn't return -1.0 for small lambda
12+
- Fix `Poisson` distribution instantiation so it return an error if lambda is infinite
13+
- `Dirichlet` now uses `const` generics, which means that its size is required at compile time (#1292)
14+
- The `Dirichlet::new_with_size` constructor was removed (#1292)
1215

1316
## [0.4.3] - 2021-12-30
1417
- Fix `no_std` build (#1208)

rand_distr/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ serde1 = ["serde", "rand/serde1"]
2727
rand = { path = "..", version = "0.9.0", default-features = false }
2828
num-traits = { version = "0.2", default-features = false, features = ["libm"] }
2929
serde = { version = "1.0.103", features = ["derive"], optional = true }
30+
serde_with = { version = "1.14.0", optional = true }
3031

3132
[dev-dependencies]
3233
rand_pcg = { version = "0.4.0", path = "../rand_pcg" }

rand_distr/src/dirichlet.rs

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use num_traits::Float;
1313
use crate::{Distribution, Exp1, Gamma, Open01, StandardNormal};
1414
use rand::Rng;
1515
use core::fmt;
16-
use alloc::{boxed::Box, vec, vec::Vec};
16+
#[cfg(feature = "serde_with")]
17+
use serde_with::serde_as;
1718

1819
/// The Dirichlet distribution `Dirichlet(alpha)`.
1920
///
@@ -27,22 +28,23 @@ use alloc::{boxed::Box, vec, vec::Vec};
2728
/// use rand::prelude::*;
2829
/// use rand_distr::Dirichlet;
2930
///
30-
/// let dirichlet = Dirichlet::new(&[1.0, 2.0, 3.0]).unwrap();
31+
/// let dirichlet = Dirichlet::new([1.0, 2.0, 3.0]).unwrap();
3132
/// let samples = dirichlet.sample(&mut rand::thread_rng());
3233
/// println!("{:?} is from a Dirichlet([1.0, 2.0, 3.0]) distribution", samples);
3334
/// ```
3435
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
36+
#[cfg_attr(feature = "serde_with", serde_as)]
3537
#[derive(Clone, Debug, PartialEq)]
36-
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
37-
pub struct Dirichlet<F>
38+
pub struct Dirichlet<F, const N: usize>
3839
where
3940
F: Float,
4041
StandardNormal: Distribution<F>,
4142
Exp1: Distribution<F>,
4243
Open01: Distribution<F>,
4344
{
4445
/// Concentration parameters (alpha)
45-
alpha: Box<[F]>,
46+
#[cfg_attr(feature = "serde_with", serde_as(as = "[_; N]"))]
47+
alpha: [F; N],
4648
}
4749

4850
/// Error type returned from `Dirchlet::new`.
@@ -72,7 +74,7 @@ impl fmt::Display for Error {
7274
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
7375
impl std::error::Error for Error {}
7476

75-
impl<F> Dirichlet<F>
77+
impl<F, const N: usize> Dirichlet<F, N>
7678
where
7779
F: Float,
7880
StandardNormal: Distribution<F>,
@@ -83,8 +85,8 @@ where
8385
///
8486
/// Requires `alpha.len() >= 2`.
8587
#[inline]
86-
pub fn new(alpha: &[F]) -> Result<Dirichlet<F>, Error> {
87-
if alpha.len() < 2 {
88+
pub fn new(alpha: [F; N]) -> Result<Dirichlet<F, N>, Error> {
89+
if N < 2 {
8890
return Err(Error::AlphaTooShort);
8991
}
9092
for &ai in alpha.iter() {
@@ -93,36 +95,19 @@ where
9395
}
9496
}
9597

96-
Ok(Dirichlet { alpha: alpha.to_vec().into_boxed_slice() })
97-
}
98-
99-
/// Construct a new `Dirichlet` with the given shape parameter `alpha` and `size`.
100-
///
101-
/// Requires `size >= 2`.
102-
#[inline]
103-
pub fn new_with_size(alpha: F, size: usize) -> Result<Dirichlet<F>, Error> {
104-
if !(alpha > F::zero()) {
105-
return Err(Error::AlphaTooSmall);
106-
}
107-
if size < 2 {
108-
return Err(Error::SizeTooSmall);
109-
}
110-
Ok(Dirichlet {
111-
alpha: vec![alpha; size].into_boxed_slice(),
112-
})
98+
Ok(Dirichlet { alpha })
11399
}
114100
}
115101

116-
impl<F> Distribution<Vec<F>> for Dirichlet<F>
102+
impl<F, const N: usize> Distribution<[F; N]> for Dirichlet<F, N>
117103
where
118104
F: Float,
119105
StandardNormal: Distribution<F>,
120106
Exp1: Distribution<F>,
121107
Open01: Distribution<F>,
122108
{
123-
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Vec<F> {
124-
let n = self.alpha.len();
125-
let mut samples = vec![F::zero(); n];
109+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> [F; N] {
110+
let mut samples = [F::zero(); N];
126111
let mut sum = F::zero();
127112

128113
for (s, &a) in samples.iter_mut().zip(self.alpha.iter()) {
@@ -140,27 +125,12 @@ where
140125

141126
#[cfg(test)]
142127
mod test {
128+
use alloc::vec::Vec;
143129
use super::*;
144130

145131
#[test]
146132
fn test_dirichlet() {
147-
let d = Dirichlet::new(&[1.0, 2.0, 3.0]).unwrap();
148-
let mut rng = crate::test::rng(221);
149-
let samples = d.sample(&mut rng);
150-
let _: Vec<f64> = samples
151-
.into_iter()
152-
.map(|x| {
153-
assert!(x > 0.0);
154-
x
155-
})
156-
.collect();
157-
}
158-
159-
#[test]
160-
fn test_dirichlet_with_param() {
161-
let alpha = 0.5f64;
162-
let size = 2;
163-
let d = Dirichlet::new_with_size(alpha, size).unwrap();
133+
let d = Dirichlet::new([1.0, 2.0, 3.0]).unwrap();
164134
let mut rng = crate::test::rng(221);
165135
let samples = d.sample(&mut rng);
166136
let _: Vec<f64> = samples
@@ -175,17 +145,17 @@ mod test {
175145
#[test]
176146
#[should_panic]
177147
fn test_dirichlet_invalid_length() {
178-
Dirichlet::new_with_size(0.5f64, 1).unwrap();
148+
Dirichlet::new([0.5]).unwrap();
179149
}
180150

181151
#[test]
182152
#[should_panic]
183153
fn test_dirichlet_invalid_alpha() {
184-
Dirichlet::new_with_size(0.0f64, 2).unwrap();
154+
Dirichlet::new([0.1, 0.0, 0.3]).unwrap();
185155
}
186156

187157
#[test]
188158
fn dirichlet_distributions_can_be_compared() {
189-
assert_eq!(Dirichlet::new(&[1.0, 2.0]), Dirichlet::new(&[1.0, 2.0]));
159+
assert_eq!(Dirichlet::new([1.0, 2.0]), Dirichlet::new([1.0, 2.0]));
190160
}
191161
}

rand_distr/src/poisson.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,17 @@ where F: Float + FloatConst, Standard: Distribution<F>
4444
/// Error type returned from `Poisson::new`.
4545
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4646
pub enum Error {
47-
/// `lambda <= 0` or `nan`.
47+
/// `lambda <= 0`
4848
ShapeTooSmall,
49+
/// `lambda = ∞` or `lambda = nan`
50+
NonFinite,
4951
}
5052

5153
impl fmt::Display for Error {
5254
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5355
f.write_str(match self {
5456
Error::ShapeTooSmall => "lambda is not positive in Poisson distribution",
57+
Error::NonFinite => "lambda is infinite or nan in Poisson distribution",
5558
})
5659
}
5760
}
@@ -66,6 +69,9 @@ where F: Float + FloatConst, Standard: Distribution<F>
6669
/// Construct a new `Poisson` with the given shape parameter
6770
/// `lambda`.
6871
pub fn new(lambda: F) -> Result<Poisson<F>, Error> {
72+
if !lambda.is_finite() {
73+
return Err(Error::NonFinite);
74+
}
6975
if !(lambda > F::zero()) {
7076
return Err(Error::ShapeTooSmall);
7177
}
@@ -163,7 +169,7 @@ mod test {
163169
fn test_poisson_avg() {
164170
test_poisson_avg_gen::<f64>(10.0, 0.1);
165171
test_poisson_avg_gen::<f64>(15.0, 0.1);
166-
172+
167173
test_poisson_avg_gen::<f32>(10.0, 0.1);
168174
test_poisson_avg_gen::<f32>(15.0, 0.1);
169175

@@ -178,6 +184,12 @@ mod test {
178184
Poisson::new(0.0).unwrap();
179185
}
180186

187+
#[test]
188+
#[should_panic]
189+
fn test_poisson_invalid_lambda_infinity() {
190+
Poisson::new(f64::INFINITY).unwrap();
191+
}
192+
181193
#[test]
182194
#[should_panic]
183195
fn test_poisson_invalid_lambda_neg() {
@@ -188,4 +200,4 @@ mod test {
188200
fn poisson_distributions_can_be_compared() {
189201
assert_eq!(Poisson::new(1.0), Poisson::new(1.0));
190202
}
191-
}
203+
}

rand_distr/tests/value_stability.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,10 @@ fn weibull_stability() {
348348
fn dirichlet_stability() {
349349
let mut rng = get_rng(223);
350350
assert_eq!(
351-
rng.sample(Dirichlet::new(&[1.0, 2.0, 3.0]).unwrap()),
352-
vec![0.12941567177708177, 0.4702121891675036, 0.4003721390554146]
351+
rng.sample(Dirichlet::new([1.0, 2.0, 3.0]).unwrap()),
352+
[0.12941567177708177, 0.4702121891675036, 0.4003721390554146]
353353
);
354-
assert_eq!(rng.sample(Dirichlet::new_with_size(8.0, 5).unwrap()), vec![
354+
assert_eq!(rng.sample(Dirichlet::new([8.0; 5]).unwrap()), [
355355
0.17684200044809556,
356356
0.29915953935953055,
357357
0.1832858056608014,

0 commit comments

Comments
 (0)