Skip to content

Instantly share code, notes, and snippets.

@eimfach

eimfach/gpu_api.fut

Created Feb 15, 2020
Embed
What would you like to do?
-- High level API which compiles to GPU code
-- It can also compile to a opencl python module (which we use: gpu.py)
-- Dependency: pyopencl
-- TOTALLY AWESOME STUFF INCOMING !!!
-- See: https://futhark-book.readthedocs.io/en/latest/interoperability.html#calling-futhark-from-python
-- Overview: https://futhark-lang.org
-- vector * vector
let dotprod (u: []f32) (v: []f32): f32 =
reduce (+) 0.0 (map2 (*) u v)
-- vector * matrix
let lvecmul u b =
map (\rowOfTrB -> dotprod u rowOfTrB)
(transpose b)
-- matrix * matrix
entry matmul a b =
map(\rowOfA -> lvecmul rowOfA b) a
-- like numpy.exp
-- Calculate the exponential `e to the power of x` for all elements in the input array.
-- ==
-- entry: exp
-- input { [1f32, 2f32, 3f32] }
-- output { [2.71828183f32, 7.3890561f32, 20.08553692f32] }
entry exp (x: []f32) =
map (\ofX -> 2.71828182 ** ofX) x
-- Negate a list of float numbers
-- ==
-- entry: negate
-- input { [1f32, 2f32, 3f32] }
-- output { [ -1.0f32, -2.0f32, -3.0f32 ] }
entry negate (x: []f32) =
map (\ofX -> -ofX) x
-- divide a given number with each float number
-- ==
-- entry: divide
-- input { 0.5f32 [1f32, 2f32, 4f32] }
-- output { [0.5f32, 0.25f32, 0.125f32] }
entry divide (div: f32) (x: []f32) =
map (\ofX -> div / ofX) x
-- multiply (m) with a vector
-- ==
-- entry: multiply
-- input { [1f32, 2f32, 5f32] 3.5f32 }
-- output { [3.5f32, 7.0f32, 17.5f32] }
entry multiply (x: []f32) (m: f32) =
map (\ofX -> m * ofX) x
-- multiply two vectors
-- ==
-- entry: multiply2
-- input { [1.5f32, 4f32, 5.75f32] [1.6f32, 7f32, 5.6f32] }
-- output { [2.4f32, 28f32, 32.2f32] }
entry multiply2 (x: []f32) (y:[]f32) =
map2 (*) x y
-- add (s) to a vector
-- ==
-- entry: add
-- input { [1f32, 2f32, 3f32] 2.0f32 }
-- output { [3.0f32, 4.0f32, 5.0f32] }
entry add (x: []f32) (s: f32) =
map (\ofX -> ofX + s) x
-- add two vectors
-- ==
-- entry: add2
-- input { [1f32,2f32,1f32] [1f32,1f32,1f32] }
-- output { [2f32,3f32,2f32] }
entry add2 (x: []f32) (y: []f32) =
map2 (+) x y
-- substract (d) from a vector
-- ==
-- entry: substract
-- input { 4f32 [3f32, 5f32, 10f32] }
-- output { [1.0f32, -1f32, -6f32] }
entry substract (d: f32) (x: []f32) =
map (\ofX -> d - ofX) x
-- substract two vectors
-- ==
-- entry: substract2
-- input { [4f32, 67f32, 24f32] [8f32, 9.4f32, 12.1f32] }
-- output { [-4f32, 57.6f32, 11.9f32] }
entry substract2 (x: []f32) (y: []f32) =
map2 (-) x y
-- Python example: (1 / (1 + numpy.exp(-x)))
-- The Sigmoid Function
-- ==
-- entry: sigmoid
-- input { [0.1f32, 0.132f32, 0.213f32] }
-- output { [0.52497919f32, 0.53295217f32, 0.55304958f32] }
entry sigmoid (x: []f32) =
divide 1.0 (add (exp (negate x)) 1.0)
-- Reverse or permute the axes of an array (like numpy.transpose)
-- ==
-- entry: transp
-- input { [[1f32,2f32],[45f32,67f32]] }
-- output { [[1f32,45f32],[2f32,67f32]] }
entry transp (x: [][]f32) =
transpose x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment