Skip to content

Commit 51b2d6b

Browse files
authoredJul 10, 2024
Merge pull request #25 from clackary/refactor/benchmark-cleanup
refactor: Benchmark cleanup
2 parents 2819b63 + 190e749 commit 51b2d6b

File tree

4 files changed

+54
-48
lines changed

4 files changed

+54
-48
lines changed
 

Diff for: ‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ DerivedData
99
.build/
1010
.swift-version
1111
*.swp
12-
main
12+
main
13+
SwiftBenchmark

Diff for: ‎Benchmarks/BuildingSimulation/PyTorch/PyTorchSimulator.py

+22-21
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import torch
22

3-
# Definitions
4-
3+
# Simulation parameters
4+
trials = 100
5+
timesteps = 20
6+
warmup = 3
57
dTime = 0.1
6-
π = 3.14159265359
8+
printGradToCompare = False
79

10+
# Definitions
11+
π = 3.14159265359
812

913
# TubeType and other custom object holding primitives will be represented with a 1D Tensor,
1014
# and SimParams will compose them into a 2D tensor
@@ -70,18 +74,18 @@ class SimParamsIndices:
7074

7175
def computeResistance(floor, tube, quanta):
7276
geometry_coeff = 10.0
73-
77+
7478
tubingSurfaceArea = (floor[SlabTypeIndices.iarea] / tube[TubeTypeIndices.itubeSpacing]) * π * tube[TubeTypeIndices.idiameter]
7579
resistance_abs = tube[TubeTypeIndices.iresistivity] * tube[TubeTypeIndices.ithickness] / tubingSurfaceArea
76-
80+
7781
resistance_corrected = resistance_abs * geometry_coeff
7882

7983
return resistance_corrected
8084

8185

8286
def computeLoadPower(floor, tube, quanta):
8387
resistance_abs = computeResistance(floor, tube, quanta)
84-
88+
8589
conductance = 1/resistance_abs
8690
dTemp = floor[SlabTypeIndices.itemp] - quanta[QuantaIndices.itemp]
8791
power = dTemp * conductance
@@ -146,14 +150,14 @@ def simulate(simParams):
146150
quanta = tankAndQuanta[1]
147151

148152
quanta = updateQuanta(quanta)
149-
153+
150154
quantaAndPower = computeLoadPower(slab, pexTube, quanta)
151155
quanta = quantaAndPower[0]
152156
powerToBuilding = quantaAndPower[1]
153157
quanta = updateQuanta(quanta)
154-
158+
155159
slab = updateBuildingModel(powerToBuilding, slab)
156-
160+
157161
return slab[SlabTypeIndices.itemp]
158162

159163
import time
@@ -174,16 +178,12 @@ def fullPipe(simParams):
174178
totalForwardTime = 0
175179
totalGradientTime = 0
176180

177-
timesteps = 20
178-
trials = 30
179-
warmup = 3
180-
printGradToCompare = False
181181

182-
for i in range(trials):
183-
182+
for i in range(trials + warmup):
183+
184184
inputs = SimParamsConstant
185185
forwardTime, forwardOutput = measure(fullPipe, inputs)
186-
186+
187187
simParams = SimParamsConstant
188188
def getGradient(simParams):
189189
gradient = torch.autograd.grad(forwardOutput, inputs)
@@ -194,15 +194,16 @@ def getGradient(simParams):
194194

195195
if printGradToCompare:
196196
print(gradient)
197-
197+
198198
if i >= warmup:
199199
totalForwardTime += forwardTime
200200
totalGradientTime += gradientTime
201201

202202

203-
averageForwardTime = totalForwardTime / (trials - warmup)
204-
averageGradientTime = totalGradientTime / (trials - warmup)
203+
averageForwardTime = totalForwardTime / trials
204+
averageGradientTime = totalGradientTime / trials
205205

206-
print("timesteps:", timesteps)
207206
print("trials:", trials)
208-
print("average forward and backwards pass (gradient) time", averageGradientTime)
207+
print("timesteps:", timesteps)
208+
print(f"average forward only time: {averageForwardTime} seconds")
209+
print(f"average forward and backwards (gradient) time: {averageGradientTime} seconds")

Diff for: ‎Benchmarks/BuildingSimulation/Swift/main.swift

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import _Differentiation
22
import Foundation
33

4-
// Definitions
5-
4+
// Simulation parameters
5+
let trials = 100
6+
let timesteps = 20
67
let dTime: Float = 0.1
8+
let printGradToCompare = false
9+
10+
// Definitions
711
let π = Float.pi
812

913
struct SimParams: Differentiable {
@@ -216,11 +220,8 @@ func fullPipe(simParams: SimParams) -> Float {
216220
return loss
217221
}
218222

219-
var trials = 30
220-
var timesteps = 20
221223
var totalPureForwardTime: Double = 0
222224
var totalGradientTime: Double = 0
223-
let printGradToCompare = false
224225

225226
for _ in 0 ..< trials {
226227
let (forwardOnly, _) = try measure {
@@ -244,6 +245,7 @@ for _ in 0 ..< trials {
244245
let averagePureForward = totalPureForwardTime / Double(trials)
245246
let averageGradient = totalGradientTime / Double(trials)
246247

247-
print("timesteps:", timesteps)
248-
print("trials:", trials)
249-
print("average forward and back (gradient) time:", averageGradient)
248+
print("trials: \(trials)")
249+
print("timesteps: \(timesteps)")
250+
print("average forward only time: \(averagePureForward) seconds")
251+
print("average forward and back (gradient) time: \(averageGradient) seconds")

Diff for: ‎Benchmarks/BuildingSimulation/TensorFlow/TensorFlowSimulator.py

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11

22
import tensorflow as tf
33

4+
# Simulation parameters
5+
trials = 100
6+
timesteps = 20
7+
warmup = 3
8+
dTime = 0.1
9+
printGradToCompare = False
10+
411
@tf.function
512
def doMath(a):
613
return a * 2
714

815
# Definitions
916

10-
dTime = 0.1
1117
π = 3.14159265359
1218

1319

@@ -74,10 +80,10 @@ class SimParamsIndices:
7480
@tf.function
7581
def computeResistance(floor, tube, quanta):
7682
geometry_coeff = 10.0
77-
83+
7884
tubingSurfaceArea = (floor[SlabTypeIndices.iarea] / tube[TubeTypeIndices.itubeSpacing]) * π * tube[TubeTypeIndices.idiameter]
7985
resistance_abs = tube[TubeTypeIndices.iresistivity] * tube[TubeTypeIndices.ithickness] / tubingSurfaceArea
80-
86+
8187
resistance_corrected = resistance_abs * geometry_coeff
8288

8389
return resistance_corrected
@@ -86,7 +92,7 @@ def computeResistance(floor, tube, quanta):
8692
@tf.function
8793
def computeLoadPower(floor, tube, quanta):
8894
resistance_abs = computeResistance(floor, tube, quanta)
89-
95+
9096
conductance = 1/resistance_abs
9197
dTemp = floor[SlabTypeIndices.itemp] - quanta[QuantaIndices.itemp]
9298
power = dTemp * conductance
@@ -193,14 +199,14 @@ def simulate(simParams):
193199
quanta = tankAndQuanta[1]
194200

195201
quanta = updateQuanta(quanta)
196-
202+
197203
quantaAndPower = computeLoadPower(slab, pexTube, quanta)
198204
quanta = quantaAndPower[0]
199205
powerToBuilding = quantaAndPower[1]
200206
quanta = updateQuanta(quanta)
201-
207+
202208
slab = updateBuildingModel(powerToBuilding, slab)
203-
209+
204210
return slab[SlabTypeIndices.itemp]
205211

206212

@@ -224,15 +230,10 @@ def fullPipe(simParams):
224230
totalForwardTime = 0
225231
totalGradientTime = 0
226232

227-
timesteps = 20
228-
trials = 30
229-
warmup = 3
230-
printGradToCompare = False
231-
232233
for i in range(trials + warmup):
233-
234+
234235
forwardTime, forwardOutput = measure(fullPipe, SimParamsConstant)
235-
236+
236237
simParams = tf.Variable(SimParamsConstant)
237238
def getGradient(simParams):
238239
with tf.GradientTape() as tape:
@@ -252,9 +253,10 @@ def getGradient(simParams):
252253
totalGradientTime += gradientTime
253254

254255

255-
averageForwardTime = totalForwardTime / (trials - warmup)
256-
averageGradientTime = totalGradientTime / (trials - warmup)
256+
averageForwardTime = totalForwardTime / trials
257+
averageGradientTime = totalGradientTime / trials
257258

258-
print("timesteps:", timesteps)
259259
print("trials:", trials)
260-
print("average forward and backwards pass (gradient) time", averageGradientTime)
260+
print("timesteps:", timesteps)
261+
print(f"average forward only time: {averageForwardTime} seconds")
262+
print(f"average forward and backwards (gradient) time: {averageGradientTime} seconds")

0 commit comments

Comments
 (0)