Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import halide as hl
- import numpy as np
- import imageio
- #simplified version for easier readability of the lowered stmt
- def hldot(x,y):
- tx = x * 12.9898
- ty = y * 78.233
- dot = ty + tx
- return dot
- #full version slightly harder to read, slightly more bugs
- def hlrand(x,y):
- tx = x * 12.9898
- ty = y * 78.233
- dot = ty + tx
- sval = hl.sin(dot)
- sval_mul = sval * 43758.5453123
- fract = sval_mul - hl.floor(sval_mul)
- Res = fract
- return Res
- # noise[(noise.s0.x + t7)] = (((float32(noise.s0.x)*12.989800f) + t6) + 12.989800f)
- # When using floats you can't count on (x+1)*n and (x*n)+n giving exact identical results
- # even when using all IEEE 754 calculations order matters.
- def dot_plain(x,y, offset):
- noise = hl.Func("noise")
- noise[x,y] = hldot( x + offset, y)
- return noise
- # Issue 3549 suggested using strict_float, allright, lets give it a whirl!
- # noise[(noise.s0.x + t13)] = strict_float(strict_float((t12 + strict_float((strict_float((strict_float(float32(noise.s0.x)) + t9))*t10)))))
- # removing the excessive strict_floats everywhere...
- # noise[(noise.s0.x + t13)] = (((t12 + (((((float32(noise.s0.x)) + t9))*t10)))))
- # you end up with (x+1) * n , this is what we want, great!
- def dot_strict(x,y, offset):
- noise = hl.Func("noise")
- noise[x,y] = hl.strict_float(hldot( x + offset, y))
- return noise
- #now lets scale this up to a more complicated function
- #noise[(noise.s0.x + t12)] = (let t9 = sin_f32((((float32(noise.s0.x)*12.989800f) + t11) + 12.989800f)) in ((t9*43758.546875f) - floor_f32((t9*43758.546875f))))
- #as expected, same thing as with dot_plain (x+1)*n != (x*n)+n.
- #also slightly confused (t9*43758.546875f) didn't get simplified
- def noise_plain(x,y, offset):
- noise = hl.Func("noise")
- noise[x,y] = hlrand( x + offset, y)
- return noise
- #ok lets apply strict_float again, worked wonders for us before!
- #noise[(noise.s0.x + t14)] = (let t10 = strict_float((strict_float(sin_f32(((t13 + (float32(noise.s0.x)*12.989800f)) + 12.989800f)))*t11)) in strict_float(strict_float((t10 - strict_float(floor_f32(t10))))))
- #hey, we're still doing (x*n)+n instead of doing (x+1)*n
- #however we did manage to simplify the floor function
- def noise_strict(x,y, offset):
- noise = hl.Func("noise")
- noise[x,y] = hl.strict_float(hlrand( x + offset, y))
- return noise
- #Ok, lets try giving it a helping hand.
- #noise[(noise.s0.x + t16)] = (let t11 = strict_float((strict_float(sin_f32((t15 + (strict_float(strict_float((strict_float(float32(noise.s0.x)) + t12)))*12.989800f))))*t13)) in strict_float(strict_float((t11 - strict_float(floor_f32(t11))))))
- #This will actually give good results, however, you have to dig
- #though the generated code to figure out where the error is
- #which isn't always an option for more complex calculations
- #also somewhat of a time vampire.
- def noise_strict_handhelped(x,y, offset):
- noise = hl.Func("noise")
- noise[x,y] = hl.strict_float(hlrand( hl.strict_float(x + offset), y))
- return noise
- def main():
- x,y = hl.Var("x"), hl.Var("y")
- #swap out noise_strict for any of the other functions to try them
- cpunoise_offset = noise_strict(x,y,1.0 )
- cpuimage_offset = cpunoise_offset.realize(512,512)
- cpunoise_offset.compile_to_lowered_stmt("cpunoise_offs.txt", [])
- #don't forget this one
- cpunoise = noise_strict(x,y,0.0 )
- cpuimage = cpunoise.realize(512,512)
- for x in range(511):
- for y in range(512):
- expected = cpuimage[x+1, y]
- actual = cpuimage_offset[x, y]
- assert expected == actual, "Expected %s Actual %s" % (expected, actual)
- return
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment