-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.py
317 lines (255 loc) · 12.3 KB
/
session.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Run prelininary imports
import sys
import time
import numpy as np
import tensorflow as tf
from codecarbon import EmissionsTracker
from codecarbon import OfflineEmissionsTracker
from tensorflow.keras import datasets
from tensorflow.keras.callbacks import Callback
import argparse
import cct
import models
parser = argparse.ArgumentParser(description='Demo')
parser.add_argument('--sect', type=str, default='init', required=False, help='Selects the section to run')
args = parser.parse_args()
##############################################
############# Sections Outline ###############
##############################################
# 1a: Standard Run
# 1b: Basic use of CarbonTracker
# 1c: Energy is all we need: tracking energy
# 2a: Getting energy information when training
sections = ['1a', '1b', '1c',
'2a']
if args.sect not in sections:
print ('Incorrect section name. Please choose a section between 1[a-i] or 2[a-g]. Example: python session.py --sect 1c')
sys.exit()
# Read CIFAR-10 dataset
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
num_classes = len(class_names)
##############################################
######### Section 1a: Standard Run ###########
##############################################
if args.sect == '1a':
start_1a = time.time()
# Getting our simple convolutional model
model = models.get_simple_model()
# Compile and train the model
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# Running the training loop
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
# Evaluating the trained model
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
end_1a = time.time()
time_1a = end_1a - start_1a
# Printing some results
print('Session 1a completed')
print('Test accuracy: '+ str(test_acc))
print('Processing time: '+ str(time_1a) + ' seconds')
sys.exit()
##############################################
### Section 1b: Basic use of CarbonTracker ###
##############################################
elif args.sect == '1b':
start_1b = time.time()
# Getting our simple convolutional model
model = models.get_simple_model()
# Compile and train the model
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# Creating the tracker object
# Country ISO codes can be found on Wikipedia
# https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
# Tracker initialization parameters:
# https://github.com/mlco2/codecarbon/blob/96c1ce15dbf33eaaaa378d3104bde64bfc9f1416/codecarbon/emissions_tracker.py#L157
tracker = OfflineEmissionsTracker(country_iso_code='DEU', log_level='error')
# Start Tracking
tracker.start()
# Running the training loop
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
# Stop the tracking
# tracker.stop() returns CO2 emissions in kilograms
# Source: https://github.com/mlco2/codecarbon/blob/96c1ce15dbf33eaaaa378d3104bde64bfc9f1416/codecarbon/emissions_tracker.py#L408
emissions = tracker.stop()
# Evaluating the trained model
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
end_1b = time.time()
time_1b = end_1b - start_1b
# Printing some results
print('Session 1b completed')
print('Test accuracy: '+ str(test_acc))
print('Processing time: '+ str(time_1b) + ' seconds')
print('Emissions: '+ str(emissions) + ' KgCO2e')
## Q1: How much is the instrumentation (tracking) overhead?
# The overhead is how much additional time it takes to process the
# instrumented training, when compared to the same training without tracking
# this can be calculated by overhead = time_tracked / time_untracked
sys.exit()
##############################################
################ Section 1c: #################
### Energy is all we need: tracking energy ###
##############################################
elif args.sect == '1c':
start_1c = time.time()
# Getting our simple convolutional model
model = models.get_simple_model()
# Compile and train the model
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# Creating the tracker object
# Country ISO codes can be found on Wikipedia
# https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
tracker = OfflineEmissionsTracker(country_iso_code='DEU', log_level='error')
# Start Tracking
tracker.start()
# Running the training loop
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
# Stop the tracking
# tracker.stop() returns CO2 emissions in kilograms
# Source: https://github.com/mlco2/codecarbon/blob/96c1ce15dbf33eaaaa378d3104bde64bfc9f1416/codecarbon/emissions_tracker.py#L408
emissions = tracker.stop()
# Evaluating the trained model
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
end_1c = time.time()
time_1c = end_1c - start_1c
# Printing some results
print('Session 1c completed')
print('Test accuracy: '+ str(test_acc))
print('Processing time: '+ str(time_1c) + ' seconds')
print('Total CPU energy consumption: ' + str(tracker._total_cpu_energy.kWh) + ' kWh')
print('Total RAM energy consumption: ' + str(tracker._total_ram_energy.kWh) + ' kWh')
print('Total Energy consumption: ' + str(tracker._total_energy.kWh) + ' kWh')
print('Emissions by CarbonTracker: '+ str(emissions) + ' KgCO2e')
print('\nAnswer of questions Q2 and Q3')
## Q2: How to take into account the power usage efficiency (PUE) of a datacenter?
# Calculate a new total energy consumption taking the tracker's output as a base
# Use a PUE value of 1.1
power_usage_efficiency = 1.1
#### Q2 answer code goes here ####
q2_answer = np.NaN
##################################
print('My calculated total energy consumption (with PUE): ' + str(q2_answer) + ' kWh')
## Q3: How to properly calculate CO2 emissions using datacenter data?
# Calculate a new CO2 emissions taking the Q2 output as a base
# Use the below two use cases: dc1 and dc2 datacenters
# The carbon intensity (CI) is the grams (i.e., g, not Kg) of CO2 emitted by kilowatt hour
# CFE% stands for the percentage of carbon free energy
# a CFE of 10% means that 10% of the energy has a CI of 0
# the remaining 90% emits CO2 according to the corresponding CI
# A source of carbon intensity of electricity generation data:
# https://ourworldindata.org/grapher/carbon-intensity-electricity?time=latest
#Data center data
#Name CFE% CI (gCO2e/kWh)
#dc1 11% 746
#dc2 91% 127
#### Q3 answer code goes here ####
dc_data: dict = {
'dc1':{'CFE':11, 'CI':746},
'dc2':{'CFE':91, 'CI':127}
}
q3_answer_dc1 = np.NaN
q3_answer_dc2 = np.NaN
##################################
print('My calculated Emissions (dc1): '+ str(q3_answer_dc1) + ' KgCO2e')
print('My calculated Emissions (dc2): '+ str(q3_answer_dc2) + ' KgCO2e')
sys.exit()
##############################################
################ Section 2a: #################
## Getting energy information when training ##
##############################################
elif args.sect == '2a':
start_2a = time.time()
# Creating a callback class to collect data while training
class MyTrainingCallBack(Callback):
def __init__(self, codecarbon_tracker):
self.codecarbon_tracker = codecarbon_tracker
pass
## Q4: How to stop training in an epoch when we pass a energy cap?
# Use the energy measured at section 1b as an energy cap for the
# training
#
# Hint: variable to tell TF to stop training: self.model.stop_training
# (True or False)
def on_epoch_end(self, epoch, logs=None):
self.codecarbon_tracker.flush()
#### Q4 answer code goes here ####
# Be mindful of the identation level
##################################
## Q5: How to stop training in a **batch** when we pass a energy cap?
# Use the energy measured at section 1b as an energy cap for the
# training
#
# Useful resources: Custom callbacks:
# https://www.tensorflow.org/guide/keras/custom_callback
#
# Hint: use self.codecarbon_tracker._measure_power_and_energy() instead
# of self.codecarbon_tracker.flush() to avoid IO overhead
## Q6: What happens if you don't call _measure_power_and_energy() or flush()?
#### Q5 answer code goes here ####
# Be mindful of the identation level
##################################
# Small label reshape to fit the CCT model
train_labels = tf.keras.utils.to_categorical(train_labels, num_classes)
test_labels = tf.keras.utils.to_categorical(test_labels, num_classes)
# Model obtained from Hassani, Ali, et al. "Escaping the big data paradigm
# with compact transformers." arXiv preprint arXiv:2104.05704 (2021).
# https://github.com/keras-team/keras-io/blob/master/examples/vision/cct.py
# https://keras.io/examples/vision/cct/
model = cct.create_cct_model()
# Compile and train the model
model.compile(optimizer='adam',
loss=tf.keras.losses.CategoricalCrossentropy(
from_logits=True, label_smoothing=0.1
),
metrics=['accuracy'])
# Creating the tracker object
# Country ISO codes can be found on Wikipedia
# https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
tracker = OfflineEmissionsTracker(country_iso_code='DEU', log_level='error')
# Initializing my call back object to be used during training
my_callback = MyTrainingCallBack(tracker)
# Start Tracking
tracker.start()
# Running the training loop
history = model.fit(train_images, train_labels, epochs=30, batch_size=128,
validation_data=(test_images, test_labels), callbacks=[my_callback])
# Stop the tracking
# tracker.stop() returns CO2 emissions in kilograms
# Source: https://github.com/mlco2/codecarbon/blob/96c1ce15dbf33eaaaa378d3104bde64bfc9f1416/codecarbon/emissions_tracker.py#L408
emissions = tracker.stop()
# Evaluating the trained model
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
end_2a = time.time()
time_2a = end_2a - start_2a
# Printing some results
print('Session 2a completed')
print('Test accuracy: '+ str(test_acc))
print('Processing time: '+ str(time_2a) + ' seconds')
print('Total CPU energy consumption: ' + str(tracker._total_cpu_energy.kWh) + ' kWh')
print('Total RAM energy consumption: ' + str(tracker._total_ram_energy.kWh) + ' kWh')
print('Total Energy consumption: ' + str(tracker._total_energy.kWh) + ' kWh')
print('Emissions by CarbonTracker: '+ str(emissions) + ' KgCO2e')
sys.exit()