Guest User

Untitled

a guest
Apr 12th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. import halide as hl
  2. import numpy as np
  3. import imageio
  4.  
  5. #simplified version for easier readability of the lowered stmt
  6. def hldot(x,y):
  7. tx = x * 12.9898
  8. ty = y * 78.233
  9. dot = ty + tx
  10. return dot
  11.  
  12. #full version slightly harder to read, slightly more bugs
  13. def hlrand(x,y):
  14. tx = x * 12.9898
  15. ty = y * 78.233
  16. dot = ty + tx
  17. sval = hl.sin(dot)
  18. sval_mul = sval * 43758.5453123
  19. fract = sval_mul - hl.floor(sval_mul)
  20. Res = fract
  21. return Res
  22.  
  23. # noise[(noise.s0.x + t7)] = (((float32(noise.s0.x)*12.989800f) + t6) + 12.989800f)
  24. # When using floats you can't count on (x+1)*n and (x*n)+n giving exact identical results
  25. # even when using all IEEE 754 calculations order matters.
  26. def dot_plain(x,y, offset):
  27. noise = hl.Func("noise")
  28. noise[x,y] = hldot( x + offset, y)
  29. return noise
  30.  
  31. # Issue 3549 suggested using strict_float, allright, lets give it a whirl!
  32.  
  33. # noise[(noise.s0.x + t13)] = strict_float(strict_float((t12 + strict_float((strict_float((strict_float(float32(noise.s0.x)) + t9))*t10)))))
  34. # removing the excessive strict_floats everywhere...
  35. # noise[(noise.s0.x + t13)] = (((t12 + (((((float32(noise.s0.x)) + t9))*t10)))))
  36. # you end up with (x+1) * n , this is what we want, great!
  37. def dot_strict(x,y, offset):
  38. noise = hl.Func("noise")
  39. noise[x,y] = hl.strict_float(hldot( x + offset, y))
  40. return noise
  41.  
  42. #now lets scale this up to a more complicated function
  43. #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))))
  44. #as expected, same thing as with dot_plain (x+1)*n != (x*n)+n.
  45. #also slightly confused (t9*43758.546875f) didn't get simplified
  46. def noise_plain(x,y, offset):
  47. noise = hl.Func("noise")
  48. noise[x,y] = hlrand( x + offset, y)
  49. return noise
  50.  
  51. #ok lets apply strict_float again, worked wonders for us before!
  52. #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))))))
  53. #hey, we're still doing (x*n)+n instead of doing (x+1)*n
  54. #however we did manage to simplify the floor function
  55. def noise_strict(x,y, offset):
  56. noise = hl.Func("noise")
  57. noise[x,y] = hl.strict_float(hlrand( x + offset, y))
  58. return noise
  59.  
  60. #Ok, lets try giving it a helping hand.
  61.  
  62. #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))))))
  63. #This will actually give good results, however, you have to dig
  64. #though the generated code to figure out where the error is
  65. #which isn't always an option for more complex calculations
  66. #also somewhat of a time vampire.
  67. def noise_strict_handhelped(x,y, offset):
  68. noise = hl.Func("noise")
  69. noise[x,y] = hl.strict_float(hlrand( hl.strict_float(x + offset), y))
  70. return noise
  71.  
  72. def main():
  73. x,y = hl.Var("x"), hl.Var("y")
  74.  
  75. #swap out noise_strict for any of the other functions to try them
  76. cpunoise_offset = noise_strict(x,y,1.0 )
  77. cpuimage_offset = cpunoise_offset.realize(512,512)
  78. cpunoise_offset.compile_to_lowered_stmt("cpunoise_offs.txt", [])
  79.  
  80. #don't forget this one
  81. cpunoise = noise_strict(x,y,0.0 )
  82. cpuimage = cpunoise.realize(512,512)
  83.  
  84. for x in range(511):
  85. for y in range(512):
  86. expected = cpuimage[x+1, y]
  87. actual = cpuimage_offset[x, y]
  88. assert expected == actual, "Expected %s Actual %s" % (expected, actual)
  89. return
  90.  
  91. if __name__ == '__main__':
  92. main()
Advertisement
Add Comment
Please, Sign In to add comment