/rust/registry/src/index.crates.io-1949cf8c6b5b557f/half-2.7.1/src/rand_distr.rs
Line | Count | Source |
1 | | use crate::{bf16, f16}; |
2 | | |
3 | | use rand::{distr::Distribution, Rng}; |
4 | | use rand_distr::uniform::UniformFloat; |
5 | | |
6 | | macro_rules! impl_distribution_via_f32 { |
7 | | ($Ty:ty, $Distr:ty) => { |
8 | | impl Distribution<$Ty> for $Distr { |
9 | 0 | fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $Ty { |
10 | 0 | <$Ty>::from_f32(<Self as Distribution<f32>>::sample(self, rng)) |
11 | 0 | } Unexecuted instantiation: <rand::distr::StandardUniform as rand::distr::distribution::Distribution<half::binary16::f16>>::sample::<_> Unexecuted instantiation: <rand_distr::normal::StandardNormal as rand::distr::distribution::Distribution<half::binary16::f16>>::sample::<_> Unexecuted instantiation: <rand_distr::exponential::Exp1 as rand::distr::distribution::Distribution<half::binary16::f16>>::sample::<_> Unexecuted instantiation: <rand::distr::float::Open01 as rand::distr::distribution::Distribution<half::binary16::f16>>::sample::<_> Unexecuted instantiation: <rand::distr::float::OpenClosed01 as rand::distr::distribution::Distribution<half::binary16::f16>>::sample::<_> Unexecuted instantiation: <rand::distr::StandardUniform as rand::distr::distribution::Distribution<half::bfloat::bf16>>::sample::<_> Unexecuted instantiation: <rand_distr::normal::StandardNormal as rand::distr::distribution::Distribution<half::bfloat::bf16>>::sample::<_> Unexecuted instantiation: <rand_distr::exponential::Exp1 as rand::distr::distribution::Distribution<half::bfloat::bf16>>::sample::<_> Unexecuted instantiation: <rand::distr::float::Open01 as rand::distr::distribution::Distribution<half::bfloat::bf16>>::sample::<_> Unexecuted instantiation: <rand::distr::float::OpenClosed01 as rand::distr::distribution::Distribution<half::bfloat::bf16>>::sample::<_> |
12 | | } |
13 | | }; |
14 | | } |
15 | | |
16 | | impl_distribution_via_f32!(f16, rand_distr::StandardUniform); |
17 | | impl_distribution_via_f32!(f16, rand_distr::StandardNormal); |
18 | | impl_distribution_via_f32!(f16, rand_distr::Exp1); |
19 | | impl_distribution_via_f32!(f16, rand_distr::Open01); |
20 | | impl_distribution_via_f32!(f16, rand_distr::OpenClosed01); |
21 | | |
22 | | impl_distribution_via_f32!(bf16, rand_distr::StandardUniform); |
23 | | impl_distribution_via_f32!(bf16, rand_distr::StandardNormal); |
24 | | impl_distribution_via_f32!(bf16, rand_distr::Exp1); |
25 | | impl_distribution_via_f32!(bf16, rand_distr::Open01); |
26 | | impl_distribution_via_f32!(bf16, rand_distr::OpenClosed01); |
27 | | |
28 | | impl rand::distr::weighted::Weight for f16 { |
29 | | const ZERO: Self = Self::ZERO; |
30 | | |
31 | 0 | fn checked_add_assign(&mut self, v: &Self) -> Result<(), ()> { |
32 | | // Floats have an explicit representation for overflow |
33 | 0 | *self += v; |
34 | 0 | Ok(()) |
35 | 0 | } |
36 | | } |
37 | | |
38 | | impl rand::distr::weighted::Weight for bf16 { |
39 | | const ZERO: Self = Self::ZERO; |
40 | | |
41 | 0 | fn checked_add_assign(&mut self, v: &Self) -> Result<(), ()> { |
42 | | // Floats have an explicit representation for overflow |
43 | 0 | *self += v; |
44 | 0 | Ok(()) |
45 | 0 | } |
46 | | } |
47 | | |
48 | | #[derive(Debug, Clone, Copy)] |
49 | | pub struct Float16Sampler(UniformFloat<f32>); |
50 | | |
51 | | impl rand_distr::uniform::SampleUniform for f16 { |
52 | | type Sampler = Float16Sampler; |
53 | | } |
54 | | |
55 | | impl rand_distr::uniform::UniformSampler for Float16Sampler { |
56 | | type X = f16; |
57 | 0 | fn new<B1, B2>(low: B1, high: B2) -> Result<Self, rand_distr::uniform::Error> |
58 | 0 | where |
59 | 0 | B1: rand_distr::uniform::SampleBorrow<Self::X> + Sized, |
60 | 0 | B2: rand_distr::uniform::SampleBorrow<Self::X> + Sized, |
61 | | { |
62 | 0 | Ok(Self(UniformFloat::new( |
63 | 0 | low.borrow().to_f32(), |
64 | 0 | high.borrow().to_f32(), |
65 | 0 | )?)) |
66 | 0 | } |
67 | 0 | fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Self, rand_distr::uniform::Error> |
68 | 0 | where |
69 | 0 | B1: rand_distr::uniform::SampleBorrow<Self::X> + Sized, |
70 | 0 | B2: rand_distr::uniform::SampleBorrow<Self::X> + Sized, |
71 | | { |
72 | 0 | Ok(Self(UniformFloat::new_inclusive( |
73 | 0 | low.borrow().to_f32(), |
74 | 0 | high.borrow().to_f32(), |
75 | 0 | )?)) |
76 | 0 | } |
77 | 0 | fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X { |
78 | 0 | f16::from_f32(self.0.sample(rng)) |
79 | 0 | } |
80 | | } |
81 | | |
82 | | #[derive(Debug, Clone, Copy)] |
83 | | pub struct BFloat16Sampler(UniformFloat<f32>); |
84 | | |
85 | | impl rand_distr::uniform::SampleUniform for bf16 { |
86 | | type Sampler = BFloat16Sampler; |
87 | | } |
88 | | |
89 | | impl rand_distr::uniform::UniformSampler for BFloat16Sampler { |
90 | | type X = bf16; |
91 | 0 | fn new<B1, B2>(low: B1, high: B2) -> Result<Self, rand_distr::uniform::Error> |
92 | 0 | where |
93 | 0 | B1: rand_distr::uniform::SampleBorrow<Self::X> + Sized, |
94 | 0 | B2: rand_distr::uniform::SampleBorrow<Self::X> + Sized, |
95 | | { |
96 | 0 | Ok(Self(UniformFloat::new( |
97 | 0 | low.borrow().to_f32(), |
98 | 0 | high.borrow().to_f32(), |
99 | 0 | )?)) |
100 | 0 | } |
101 | 0 | fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Self, rand_distr::uniform::Error> |
102 | 0 | where |
103 | 0 | B1: rand_distr::uniform::SampleBorrow<Self::X> + Sized, |
104 | 0 | B2: rand_distr::uniform::SampleBorrow<Self::X> + Sized, |
105 | | { |
106 | 0 | Ok(Self(UniformFloat::new_inclusive( |
107 | 0 | low.borrow().to_f32(), |
108 | 0 | high.borrow().to_f32(), |
109 | 0 | )?)) |
110 | 0 | } |
111 | 0 | fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X { |
112 | 0 | bf16::from_f32(self.0.sample(rng)) |
113 | 0 | } |
114 | | } |
115 | | |
116 | | #[cfg(test)] |
117 | | mod tests { |
118 | | use super::*; |
119 | | |
120 | | #[allow(unused_imports)] |
121 | | use rand::{rng, Rng}; |
122 | | use rand_distr::{StandardNormal, StandardUniform, Uniform}; |
123 | | |
124 | | #[test] |
125 | | fn test_sample_f16() { |
126 | | let mut rng = rng(); |
127 | | let _: f16 = rng.sample(StandardUniform); |
128 | | let _: f16 = rng.sample(StandardNormal); |
129 | | let _: f16 = rng.sample(Uniform::new(f16::from_f32(0.0), f16::from_f32(1.0)).unwrap()); |
130 | | #[cfg(feature = "num-traits")] |
131 | | let _: f16 = |
132 | | rng.sample(rand_distr::Normal::new(f16::from_f32(0.0), f16::from_f32(1.0)).unwrap()); |
133 | | } |
134 | | |
135 | | #[test] |
136 | | fn test_sample_bf16() { |
137 | | let mut rng = rng(); |
138 | | let _: bf16 = rng.sample(StandardUniform); |
139 | | let _: bf16 = rng.sample(StandardNormal); |
140 | | let _: bf16 = rng.sample(Uniform::new(bf16::from_f32(0.0), bf16::from_f32(1.0)).unwrap()); |
141 | | #[cfg(feature = "num-traits")] |
142 | | let _: bf16 = |
143 | | rng.sample(rand_distr::Normal::new(bf16::from_f32(0.0), bf16::from_f32(1.0)).unwrap()); |
144 | | } |
145 | | } |