From cf544ed92fab227c9dab83690b8f72c4f427cd5e Mon Sep 17 00:00:00 2001 From: Adam Paszke Date: Mon, 22 Feb 2021 16:49:25 +0000 Subject: [PATCH] Update some benchmarks to the latest language version --- benchmarks/parboil/mriq.dx | 17 ++++++----- benchmarks/prepare-executables.py | 10 +++---- benchmarks/rodinia/backprop.dx | 7 ++--- .../rodinia/{backprop-ad.dx => backpropad.dx} | 19 ++++++------- benchmarks/rodinia/hotspot.dx | 12 ++++---- benchmarks/rodinia/kmeans.dx | 28 ++++++++----------- benchmarks/rodinia/pathfinder.dx | 7 +++-- 7 files changed, 46 insertions(+), 54 deletions(-) rename benchmarks/rodinia/{backprop-ad.dx => backpropad.dx} (66%) diff --git a/benchmarks/parboil/mriq.dx b/benchmarks/parboil/mriq.dx index 5de1cc616..8db90ae2c 100644 --- a/benchmarks/parboil/mriq.dx +++ b/benchmarks/parboil/mriq.dx @@ -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 diff --git a/benchmarks/prepare-executables.py b/benchmarks/prepare-executables.py index 850924ec9..be5398273 100644 --- a/benchmarks/prepare-executables.py +++ b/benchmarks/prepare-executables.py @@ -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] @@ -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')), @@ -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') diff --git a/benchmarks/rodinia/backprop.dx b/benchmarks/rodinia/backprop.dx index bb412dad9..5cf98d74d 100644 --- a/benchmarks/rodinia/backprop.dx +++ b/benchmarks/rodinia/backprop.dx @@ -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) @@ -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) @@ -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 diff --git a/benchmarks/rodinia/backprop-ad.dx b/benchmarks/rodinia/backpropad.dx similarity index 66% rename from benchmarks/rodinia/backprop-ad.dx rename to benchmarks/rodinia/backpropad.dx index 50ca21b55..fd061ec71 100644 --- a/benchmarks/rodinia/backprop-ad.dx +++ b/benchmarks/rodinia/backpropad.dx @@ -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 = @@ -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 diff --git a/benchmarks/rodinia/hotspot.dx b/benchmarks/rodinia/hotspot.dx index 61f7a682f..4a7e4bbcc 100644 --- a/benchmarks/rodinia/hotspot.dx +++ b/benchmarks/rodinia/hotspot.dx @@ -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' diff --git a/benchmarks/rodinia/kmeans.dx b/benchmarks/rodinia/kmeans.dx index f0e369b12..3f4eaeb23 100644 --- a/benchmarks/rodinia/kmeans.dx +++ b/benchmarks/rodinia/kmeans.dx @@ -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) @@ -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 diff --git a/benchmarks/rodinia/pathfinder.dx b/benchmarks/rodinia/pathfinder.dx index d8831cf09..a73a868fe 100644 --- a/benchmarks/rodinia/pathfinder.dx +++ b/benchmarks/rodinia/pathfinder.dx @@ -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)))