Skip to content

Commit b222653

Browse files
committed
Add test for deterministic -> stochastic
1 parent ffcbc88 commit b222653

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

test/conversions.jl

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
@testset "Converting Stochastic FP to BFloat16sr" begin
1+
@testset "Deterministic conversion of Stochastic FP to BFloat16sr" begin
22
for k in 1:100000
33
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
55
# representable in all precisions
66
trueVal=Float16(Float32(BFloat16(trueVal)))
77
bfloatVal = BFloat16sr(trueVal)
@@ -14,10 +14,10 @@
1414
end
1515
end
1616

17-
@testset "Converting Stochastic FP to Float16sr" begin
17+
@testset "Deterministic conversion of Stochastic FP to Float16sr" begin
1818
for k in 1:100000
1919
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
2121
# representable in all precisions
2222
trueVal=Float16(Float32(BFloat16(trueVal)))
2323
bfloatVal = BFloat16sr(trueVal)
@@ -30,10 +30,10 @@ end
3030
end
3131
end
3232

33-
@testset "Converting Stochastic FP to Float32sr" begin
33+
@testset "Deterministic conversion of Stochastic FP to Float32sr" begin
3434
for k in 1:100000
3535
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
3737
# representable in all precisions
3838
trueVal=Float16(Float32(BFloat16(trueVal)))
3939
bfloatVal = BFloat16sr(trueVal)
@@ -45,3 +45,38 @@ end
4545
@test float32Val == Float32sr(float16Val)
4646
end
4747
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+
@show l
74+
break
75+
end
76+
end
77+
78+
@test is_stochastic == true
79+
end
80+
end
81+
end
82+
end

0 commit comments

Comments
 (0)