From 4ce267388476bd880a66d8a02e79e5a2d93316d1 Mon Sep 17 00:00:00 2001 From: Ali Asgari Date: Mon, 11 Apr 2022 07:14:19 -0700 Subject: [PATCH] Add memory overhead to artifact description --- ad_overhead.sh | 5 +- injection.py | 2 +- linearcode/detection_overhead.py | 57 ++++++++++++++++ linearcode/memory_overhead.py | 40 ++++++++++++ linearcode/overhead.py | 108 ------------------------------- requirements.txt | 2 +- 6 files changed, 103 insertions(+), 111 deletions(-) create mode 100755 linearcode/detection_overhead.py create mode 100644 linearcode/memory_overhead.py delete mode 100755 linearcode/overhead.py diff --git a/ad_overhead.sh b/ad_overhead.sh index 9d73afa..9d1e238 100755 --- a/ad_overhead.sh +++ b/ad_overhead.sh @@ -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 \ No newline at end of file + +#python memory_overhead.py +python detection_overhead.py +#python correction_overhead.py diff --git a/injection.py b/injection.py index dadc485..ca03d44 100755 --- a/injection.py +++ b/injection.py @@ -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): diff --git a/linearcode/detection_overhead.py b/linearcode/detection_overhead.py new file mode 100755 index 0000000..a5fa936 --- /dev/null +++ b/linearcode/detection_overhead.py @@ -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() diff --git a/linearcode/memory_overhead.py b/linearcode/memory_overhead.py new file mode 100644 index 0000000..33dfdde --- /dev/null +++ b/linearcode/memory_overhead.py @@ -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) diff --git a/linearcode/overhead.py b/linearcode/overhead.py deleted file mode 100755 index 5245432..0000000 --- a/linearcode/overhead.py +++ /dev/null @@ -1,108 +0,0 @@ -# sudo sh -c 'echo -1 >/proc/sys/kernel/perf_event_paranoid' -from time import time - -import torch -import torchvision.models -from torch import FloatTensor -from pypapi import papi_high -from pypapi import events as papi_events - -from common.models import MODEL_CLASSES -from injection import convert, StructuralCodedConv2d, StructuralCodedLinear, ClipperReLU - -# for i in range(32, 257): -# inp = torch.randn(16, 32, 229, 229) -# w = torch.randn(i, 32, 4, 5) -# papi_high.start_counters([papi_events.PAPI_FP_OPS]) -# torch.conv2d(inp, w) -# print(i, papi_high.stop_counters()[0], sep=',') -# exit() -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 count_flops(input_image, _model): - papi_high.start_counters([papi_events.PAPI_DP_OPS]) - # papi_high.start_counters([papi_events.PAPI_SP_OPS]) - _model.forward(input_image) - return papi_high.stop_counters()[0] - - -def measure_time(input_image, _model): - count = 10 - start = time() - for _ in range(count): - _model.forward(input_image) - return (time() - start) / count - - -measure = measure_time - - -for model_name, model_class in MODEL_CLASSES: - model = model_class() - print(sum(m.weight.nelement() for m in model.modules() if hasattr(m, 'weight')) / sum(p.nelement() for p in model.parameters())) - -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: - model = model_class() - sc_normalized_model = PROTECTIONS['before_quantization']['sc'](model, None) - sc_correction_model = PROTECTIONS['after_quantization']['sc'](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: - model = model_class() - sc_normalized_model = PROTECTIONS['before_quantization']['milr'](model, None) - sc_correction_model = PROTECTIONS['after_quantization']['milr'](sc_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) - -# exit() -for protection in ( - 'sc', - 'milr', - 'tmr', - '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: - 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(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(image, sc_detection_model) - - print(model_name, - 100 * (sc_detection_flops / baseline_flops - 1), file=detection_file) - detection_file.flush() - print() diff --git a/requirements.txt b/requirements.txt index 72f3629..a5fc610 100755 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ galois==0.0.24 -torchvision==0.9.0 +torchvision==0.10.1 matplotlib==3.5.1