Skip to content

Commit

Permalink
Update some benchmarks to the latest language version
Browse files Browse the repository at this point in the history
  • Loading branch information
apaszke committed Mar 1, 2021
1 parent 9e1ed74 commit cf544ed
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 54 deletions.
17 changes: 8 additions & 9 deletions benchmarks/parboil/mriq.dx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ def mriq (kx : ks=>Float) (ky : ks=>Float) (kz : ks=>Float)
(phiR : ks=>Float) (phiI : ks=>Float)
: (cs=>Float & cs=>Float) =
unzip $ for i.
snd $ withAccum \ref.
qr = fstRef ref
qi = sndRef ref
for j.
phiMag = phiR.j * phiR.j + phiI.j * phiI.j
expArg = kx.j * x.i + ky.j * y.i + kz.j * z.i
x = 2.0 * pi * expArg * phiMag
qr += cos x
qi += sin x
runAccum (AddMonoid Float) \qi.
yieldAccum (AddMonoid Float) \qr.
for j.
phiMag = phiR.j * phiR.j + phiI.j * phiI.j
expArg = kx.j * x.i + ky.j * y.i + kz.j * z.i
t = 2.0 * pi * expArg * phiMag
qr += cos t
qi += sin t
10 changes: 5 additions & 5 deletions benchmarks/prepare-executables.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ def prepare_rodinia_hotspot():
with open(case_exe_path, 'w') as f:
emit_dex(f, 'rodinia', 'hotspot', [
('numIterations', 360),
('T', format_matrix(ts)),
('P', format_matrix(ps))
('T', random_mat(f'(Fin {size})=>(Fin {size})=>Float')), # format_matrix(ts)),
('P', random_mat(f'(Fin {size})=>(Fin {size})=>Float')), # format_matrix(ps))
])
print(f'Created {case_exe_path}')

def prepare_rodinia_backprop():
exe_path = RODINIA_EXE_ROOT / 'backprop'
exe_path.mkdir(parents=True, exist_ok=True)
exe_path_ad = RODINIA_EXE_ROOT / 'backprop-ad'
exe_path_ad = RODINIA_EXE_ROOT / 'backpropad'
exe_path_ad.mkdir(parents=True, exist_ok=True)
in_features = [128, 1048576]

Expand All @@ -85,7 +85,7 @@ def prepare_rodinia_backprop():
hidf = 16
case_exe_path = (exe_path_ad if use_ad else exe_path) / f'{inf}_{hidf}_{outf}.dx'
with open(case_exe_path, 'w') as f:
emit_dex(f, 'rodinia', 'backprop', [
emit_dex(f, 'rodinia', ('backpropad' if use_ad else 'backprop'), [
('input', random_vec('in=>Float')),
('target', random_vec('out=>Float')),
('inputWeights', random_mat('{ b: Unit | w: in }=>hid=>Float')),
Expand Down Expand Up @@ -196,7 +196,7 @@ def emit_dex(f, suite, name, params, *, preamble=[]):
for n, v in params:
f.write(f'{n} = {v}\n')
f.write('\n')
f.write(f'include "{suite}/{name}.dx"\n')
f.write(f'import {name}\n')
f.write('\n')
f.write(f'%bench "{name}"\n')
f.write(f'result = {name} {(" ".join(n for n, v in params))}\n')
Expand Down
7 changes: 3 additions & 4 deletions benchmarks/rodinia/backprop.dx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ def layerForward (input : in=>Float)
(params : { b: Unit| w: in }=>out=>Float)
: out=>Float =
bias = params.{| b=() |}
-- TODO: Push the j loop into the sum or not?
total = (for j:out. sum $ for i:in. params.{| w=i |}.j * input.i) + bias
total = (sum $ for i:in j:out. params.{| w=i |}.j * input.i) + bias
for i. squash total.i

def adjustWeights (delta : out=>Float)
Expand All @@ -27,7 +26,7 @@ def adjustWeights (delta : out=>Float)
def outputError (target : out=>Float)
(output : out=>Float)
: (Float & out=>Float) =
swap $ withAccum \err.
swap $ runAccum (AddMonoid Float) \err.
for i.
o = output.i
d = o * (1.0 - o) * (target.i - o)
Expand All @@ -38,7 +37,7 @@ def hiddenError (outputDelta : out=>Float)
(hiddenWeights : { b: Unit | w: hid }=>out=>Float)
(hidden : hid=>Float)
: (Float & hid=>Float) =
swap $ withAccum \err.
swap $ runAccum (AddMonoid Float) \err.
for i:hid.
mult = sum $ for j. outputDelta.j * hiddenWeights.{| w = i |}.j
r = hidden.i * (1.0 - hidden.i) * mult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ def layerForward (input : in=>Float)
(params : { b: Unit| w: in }=>out=>Float)
: out=>Float =
bias = params.{| b=() |}
-- TODO: Push the j loop into the sum or not?
total = (for j:out. sum $ for i:in. params.{| w=i |}.j * input.i) + bias
total = (sum $ for i:in j:out. params.{| w=i |}.j * input.i) + bias
for i. squash total.i

def lossForward (input : n=>Float) (target : n=>Float) : Float =
Expand All @@ -22,14 +21,14 @@ def adjustWeights (gradWeight : { b: Unit | w: in }=>out=>Float)
d = ETA * gradWeight.k.j + MOMENTUM * oldWeight.k.j
weight.k.j + d

def backprop (input : in=>Float)
(target : out=>Float)
(inputWeights : { b: Unit | w: in }=>hid=>Float)
(hiddenWeights : { b: Unit | w: hid }=>out=>Float)
(oldInputWeights : { b: Unit | w: in }=>hid=>Float)
(oldHiddenWeights : { b: Unit | w: hid }=>out=>Float)
: ( { b: Unit | w: in }=>hid=>Float
& { b: Unit | w: hid }=>out=>Float) =
def backpropad (input : in=>Float)
(target : out=>Float)
(inputWeights : { b: Unit | w: in }=>hid=>Float)
(hiddenWeights : { b: Unit | w: hid }=>out=>Float)
(oldInputWeights : { b: Unit | w: in }=>hid=>Float)
(oldHiddenWeights : { b: Unit | w: hid }=>out=>Float)
: ( { b: Unit | w: in }=>hid=>Float
& { b: Unit | w: hid }=>out=>Float) =
(gradInputWeights, gradHiddenWeights) =
flip grad (inputWeights, hiddenWeights) \(iw, hw).
hidden = layerForward input inputWeights
Expand Down
12 changes: 6 additions & 6 deletions benchmarks/rodinia/hotspot.dx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def hotspot (numIterations: Int)
Rz = tChip / (kSI * gridHeight * gridWidth)
maxSlope = maxPD / (factorChip * tChip * specHeatSI)
step = precision / maxSlope
snd $ withState tsInit $ \ts.
yieldState tsInit $ \tsRef.
for _:(Fin numIterations).
ts' = for r c.
t = get ts!r!c
dc = (get ts!r!(c +| 1) + get ts!r!(c -| 1) - 2.0 * t) / Rx
dr = (get ts!(r +| 1)!c + get ts!(r -| 1)!c - 2.0 * t) / Ry
ts = get tsRef
tsRef := for r c.
t = ts.r.c
dc = (ts.r.(c +| 1) + ts.r.(c -| 1) - 2.0 * t) / Rx
dr = (ts.(r +| 1).c + ts.(r -| 1).c - 2.0 * t) / Ry
d = (step / cap) * (p.r.c + dc + dr + (tAmb - t) / Rz)
t + d
ts := ts'
28 changes: 11 additions & 17 deletions benchmarks/rodinia/kmeans.dx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

def dist (x : d=>Float) (y : d=>Float) : Float =
d = x - y
sum $ for i. d.i * d.i

def centroidsOf (points : n=>d=>Float) (membership : n=>k) : k=>d=>Float =
clusterSums = snd $ withAccum \clusterSums.
clusterSums = yieldAccum (AddMonoid Float) \clusterSums.
for i. clusterSums!(membership.i) += points.i
clusterSizes = snd $ withAccum \clusterSizes.
clusterSizes = yieldAccum (AddMonoid Float) \clusterSizes.
for i. clusterSizes!(membership.i) += 1.0
for i. clusterSums.i / (max clusterSizes.i 1.0)

Expand All @@ -20,18 +19,13 @@ def kmeans (points : n=>d=>Float)
: (Fin k)=>d=>Float =
initCentroids = for i:(Fin k). points.(ordinal i@_)
initMembership = for c:n. ((ordinal c `mod` k)@_)
initDelta = threshold + 1
final = snd $ withState (initMembership, initCentroids, initDelta, 0) \ref.
(while (\().
(_, _, delta, i) = get ref
delta > threshold && i < maxIterations)
(\().
(membership, centroids, _, i) = get ref
membership' = for i. argminBy (dist points.i) centroids
centroids' = centroidsOf points membership'
delta' = sum $ for i. BToI $ membership.i /= membership'.i
ref := (membership', centroids', delta', i + 1)
()))
()
(_, centroids, _, _) = final
final = yieldState (initMembership, initCentroids, 0) \ref.
while do
(membership, centroids, i) = get ref
membership' = for i. argminBy (dist points.i) centroids
centroids' = centroidsOf points membership'
delta = sum $ for i. BToI $ membership.i /= membership'.i
ref := (membership', centroids', i + 1)
delta > threshold && i < maxIterations
(_, centroids, _) = final
centroids
7 changes: 4 additions & 3 deletions benchmarks/rodinia/pathfinder.dx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def (-|) (i:n) (off:Int) : n =
False -> i

def pathfinder (world : rows=>cols=>Int) : cols=>Int =
snd $ withState zero $ \costs.
yieldState zero $ \costsRef.
for r.
costs := for c. world.r.c + (min (get costs!c) $ (min (get costs!(c -| 1))
(get costs!(c +| 1))))
costs = get costsRef
costsRef := for c. world.r.c + (min costs.c $ (min costs.(c -| 1)
costs.(c +| 1)))

0 comments on commit cf544ed

Please sign in to comment.