|
1 | | -@testset "Converting Stochastic FP to BFloat16sr" begin |
| 1 | +@testset "Deterministic conversion of Stochastic FP to BFloat16sr" begin |
2 | 2 | for k in 1:100000 |
3 | 3 | trueVal = randn(Float64) |
4 | | - # Convert to each deterministic type to ensure the result is |
| 4 | + # Convert to each deterministic type to ensure the result is |
5 | 5 | # representable in all precisions |
6 | 6 | trueVal=Float16(Float32(BFloat16(trueVal))) |
7 | 7 | bfloatVal = BFloat16sr(trueVal) |
|
14 | 14 | end |
15 | 15 | end |
16 | 16 |
|
17 | | -@testset "Converting Stochastic FP to Float16sr" begin |
| 17 | +@testset "Deterministic conversion of Stochastic FP to Float16sr" begin |
18 | 18 | for k in 1:100000 |
19 | 19 | trueVal = randn(Float64) |
20 | | - # Convert to each deterministic type to ensure the result is |
| 20 | + # Convert to each deterministic type to ensure the result is |
21 | 21 | # representable in all precisions |
22 | 22 | trueVal=Float16(Float32(BFloat16(trueVal))) |
23 | 23 | bfloatVal = BFloat16sr(trueVal) |
|
30 | 30 | end |
31 | 31 | end |
32 | 32 |
|
33 | | -@testset "Converting Stochastic FP to Float32sr" begin |
| 33 | +@testset "Deterministic conversion of Stochastic FP to Float32sr" begin |
34 | 34 | for k in 1:100000 |
35 | 35 | trueVal = randn(Float64) |
36 | | - # Convert to each deterministic type to ensure the result is |
| 36 | + # Convert to each deterministic type to ensure the result is |
37 | 37 | # representable in all precisions |
38 | 38 | trueVal=Float16(Float32(BFloat16(trueVal))) |
39 | 39 | bfloatVal = BFloat16sr(trueVal) |
|
45 | 45 | @test float32Val == Float32sr(float16Val) |
46 | 46 | end |
47 | 47 | end |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +for SR in [Float16sr, BFloat16sr, Float32sr] |
| 52 | + |
| 53 | + # corresponding FP type |
| 54 | + FP = float(SR) |
| 55 | + |
| 56 | + @eval begin |
| 57 | + @testset "Stochastic conversion of Float64 to $(nameof($SR))" begin |
| 58 | + for k in 1:100 |
| 59 | + trueVal = randn(Float64) |
| 60 | + |
| 61 | + # make sure that this is not representable by FP |
| 62 | + if trueVal == $FP(trueVal) |
| 63 | + trueVal = nextfloat(trueVal) |
| 64 | + end |
| 65 | + |
| 66 | + roundedVal = $SR(trueVal) |
| 67 | + |
| 68 | + # check that rounding is not deterministic |
| 69 | + is_stochastic = false |
| 70 | + for l in 1:10000 |
| 71 | + if roundedVal != $SR(trueVal) |
| 72 | + is_stochastic = true |
| 73 | + break |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + @test is_stochastic == true |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | +end |
0 commit comments