Skip to content

Commit

Permalink
Add memory overhead to artifact description
Browse files Browse the repository at this point in the history
  • Loading branch information
altostratous committed Apr 11, 2022
1 parent 638bcd4 commit 4ce2673
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 111 deletions.
5 changes: 4 additions & 1 deletion ad_overhead.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ cd linearcode

sudo sh -c 'echo -1 >/proc/sys/kernel/perf_event_paranoid'
sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'
python correction_overhead.py

#python memory_overhead.py
python detection_overhead.py
#python correction_overhead.py
2 changes: 1 addition & 1 deletion injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ def forward(self, input: Tensor) -> Tensor:
return self._conv_forward(input, recovered, self.bias)


MILR_BATCH_SIZE = 4
MILR_BATCH_SIZE = 16 * 9


class MILRLinear(torch.nn.Linear):
Expand Down
57 changes: 57 additions & 0 deletions linearcode/detection_overhead.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# sudo sh -c 'echo -1 >/proc/sys/kernel/perf_event_paranoid'
# sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'

from time import time

import torch
from common.models import MODEL_CLASSES
from linearcode.protection import PROTECTIONS
from storage import get_storage_filename

torch.random.manual_seed(0)
imagenet_image = torch.rand((16, 3, 299, 299))
e2e_image = torch.rand((1, 3, 200, 66))
n = 256


def measure_time(input_image, _model):
count = 10
start = time()
for _ in range(count):
_model.forward(input_image)
return (time() - start) / count


for protection in (
'sc',
'milr',
'radar',
):
detection_filename = get_storage_filename({'fig': 'detection_time_overhead', 'protection': protection},
extension='.tex', storage='../thesis/data/')

with open(detection_filename, mode='w') as detection_file:
for model_name, model_class in MODEL_CLASSES:
if model_name in ('e2e', 'vgg19'):
continue
with torch.no_grad():

if model_name == 'e2e':
image = e2e_image
else:
image = imagenet_image
now = time()
model = model_class(pretrained=True)
print(time() - now)
model = model
image = image

baseline_flops = measure_time(image, model)
sc_normalized_model = PROTECTIONS['before_quantization'][protection](model, None)
sc_detection_model = PROTECTIONS['after_quantization'][protection](sc_normalized_model, {'flips': 1, 'n': 2048})
sc_detection_flops = measure_time(image, sc_detection_model)

print(model_name,
100 * (sc_detection_flops / baseline_flops - 1), file=detection_file)
detection_file.flush()
print()
40 changes: 40 additions & 0 deletions linearcode/memory_overhead.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from common.models import MODEL_CLASSES
from linearcode.protection import PROTECTIONS
from storage import get_storage_filename

memory_filename = get_storage_filename({'fig': 'memory_overhead', 'protection': 'sc'},
extension='.tex', storage='../thesis/data/')

with open(memory_filename, mode='w') as memory_file:
for model_name, model_class in MODEL_CLASSES:
if model_name in ('e2e', 'vgg19'):
continue
model = model_class()
normalized_model = PROTECTIONS['before_quantization']['sc'](model, None)
sc_correction_model = PROTECTIONS['after_quantization']['sc'](normalized_model, {'flips': 32})
correction_size = sum(p.nelement() for p in sc_correction_model.parameters())
model_size = sum(p.nelement() for p in model.parameters())
print(model_name, round(100 * (correction_size / model_size - 1), 2), file=memory_file)

memory_filename = get_storage_filename({'fig': 'memory_overhead', 'protection': 'milr'},
extension='.tex', storage='../thesis/data/')

with open(memory_filename, mode='w') as memory_file:
for model_name, model_class in MODEL_CLASSES:
if model_name in ('e2e', 'vgg19'):
continue
model = model_class()
normalized_model = PROTECTIONS['before_quantization']['milr'](model, None)
sc_correction_model = PROTECTIONS['after_quantization']['milr'](normalized_model, {'flips': 32})
model_size = sum(p.nelement() for p in model.parameters())
correction_size = sum(
m.checkpoint.nelement() for m in sc_correction_model.modules() if hasattr(m, 'checkpoint')) + model_size
print(model_name, round(100 * (correction_size / model_size - 1), 2), file=memory_file)

memory_filename = get_storage_filename({'fig': 'memory_overhead', 'protection': 'radar'},
extension='.tex', storage='../thesis/data/')
with open(memory_filename, mode='w') as memory_file:
for model_name, model_class in MODEL_CLASSES:
if model_name in ('e2e', 'vgg19'):
continue
print(model_name, 25, file=memory_file)
108 changes: 0 additions & 108 deletions linearcode/overhead.py

This file was deleted.

2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
galois==0.0.24
torchvision==0.9.0
torchvision==0.10.1
matplotlib==3.5.1

0 comments on commit 4ce2673

Please sign in to comment.