-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofile_memory.py
115 lines (104 loc) · 2.78 KB
/
profile_memory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import json
import subprocess
from pathlib import Path
# from pytorch_memlab import LineProfiler, MemReporter
def profile_per_num_layers(n_layers: int):
hidden_size = 2000
lr = 0.03
device = "cuda:0"
# device = "cpu"
epochs = 1
batch_size = 5000
theta = 2.0
# reporter_backprop = MemReporter(run_backprop_mnist)
profiled_values = {}
CMD = [
"python",
"train_base.py",
"--n_layers",
str(n_layers),
"--hidden_size",
str(hidden_size),
"--lr",
str(lr),
"--device",
device,
"--epochs",
str(epochs),
"--batch_size",
str(batch_size),
"--theta",
str(theta),
"--save_memory_profile",
f"base_ff.txt",
]
subprocess.run(CMD)
with open(f"base_ff.txt", "r") as f:
string_profile = f.read()
profiled_values["base_ff"] = float(string_profile.strip())
Path(f"base_ff.txt").unlink()
CMD = [
"python",
"train_recurrent.py",
"--n_layers",
str(n_layers),
"--hidden_size",
str(hidden_size),
"--lr",
str(lr),
"--device",
device,
"--epochs",
str(epochs),
"--batch_size",
str(batch_size),
"--theta",
str(theta),
"--save_memory_profile",
f"recurrent_ff.txt",
]
subprocess.run(CMD)
with open("recurrent_ff.txt", "r") as f:
string_profile = f.read()
profiled_values["recurrent_ff"] = float(string_profile.strip())
Path(f"recurrent_ff.txt").unlink()
CMD = [
"python",
"train_backprop.py",
"--n_layers",
str(n_layers),
"--hidden_size",
str(hidden_size),
"--lr",
str(lr),
"--device",
device,
"--epochs",
str(epochs),
"--batch_size",
str(batch_size),
"--save_memory_profile",
f"backprop.txt",
]
subprocess.run(CMD)
with open(f"backprop.txt", "r") as f:
string_profile = f.read()
profiled_values["backprop"] = float(string_profile.strip())
Path(f"backprop.txt").unlink()
print("##############################################")
print("RESULTS ON MEMORY CONSUMPTION")
print(profiled_values)
return profiled_values
def main(max_layers: int):
all_results = {}
for n_layers in range(2, max_layers + 1, 5):
profile_result = profile_per_num_layers(n_layers)
all_results[n_layers] = profile_result
with open("results.json", "w") as f:
json.dump(all_results, f)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--max_layers", type=int, default=50)
args = parser.parse_args()
main(args.max_layers)