def test_combo_layers(self): p = fault_injection( self.model, self.BATCH_SIZE, layer_types=[torch.nn.Conv2d, torch.nn.Linear], # layer_types=[torch.nn.Conv2d], use_cuda=self.USE_GPU, ) (b, layer, C, H, W, err_val) = ( [0, 1], [1, 7], [5, 888], [5, None], [3, None], [20000, 10000], ) inj = p.declare_neuron_fi(batch=b, layer_num=layer, dim1=C, dim2=H, dim3=W, value=err_val) inj_output = inj(self.IMAGE) inj_softmax = self.softmax(inj_output) inj_label_1 = list(torch.argmax(inj_softmax, dim=1))[0].item() inj_label_2 = list(torch.argmax(inj_softmax, dim=1))[1].item() if p.get_total_layers() != 8: raise AssertionError if inj_label_1 != 695: raise AssertionError if inj_label_2 != 888: raise AssertionError
def setup_class(self): torch.manual_seed(5) self.C = 3 self.H = 224 self.W = 224 self.BATCH_SIZE = 4 self.IMAGE = torch.rand((self.BATCH_SIZE, self.C, self.H, self.W)) self.USE_GPU = False self.softmax = torch.nn.Softmax(dim=1) self.model = models.alexnet(pretrained=True) self.model.eval() # Error free inference to gather golden value self.output = self.model(self.IMAGE) self.golden_softmax = self.softmax(self.output) self.golden_label = list(torch.argmax(self.golden_softmax, dim=1))[0].item() self.p = fault_injection( self.model, self.BATCH_SIZE, input_shape=[self.C, self.H, self.W], use_cuda=self.USE_GPU, )
def setup_class(self): torch.manual_seed(5) c = 3 h = 224 w = 224 batch_size = 4 self.image = torch.rand((batch_size, c, h, w)) self.softmax = torch.nn.Softmax(dim=1) self.model = models.alexnet(pretrained=True) self.model.eval() # Error free inference to gather golden value self.output = self.model(self.image) self.golden_softmax = self.softmax(self.output) self.golden_label = list(torch.argmax(self.golden_softmax, dim=1))[0].item() self.p = fault_injection( self.model, batch_size, input_shape=[c, h, w], use_cuda=False, )
def test_single_linear_layer(self): p = fault_injection( self.model, self.BATCH_SIZE, input_shape=[3, self.H, self.W], layer_types=[torch.nn.Linear], use_cuda=self.USE_GPU, ) print("TOTAL LAYERS", p.get_total_layers()) print("LAYER DIM", p.get_layer_dim(2)) print("LAYER TYPE", p.get_layer_type(2)) print("Summary", p.print_pytorchfi_layer_summary()) print("------------------------------------")
def test_single_linear_layer(self): p = fault_injection( self.model, self.BATCH_SIZE, layer_types=[torch.nn.Linear], use_cuda=self.USE_GPU, ) if p.get_total_layers() != 3: raise AssertionError if p.get_layer_dim(2) != 2: raise AssertionError if p.get_layer_type(2) != torch.nn.Linear: raise AssertionError
def test_inj_all_layers(self): p = fault_injection( self.model, self.BATCH_SIZE, layer_types=["all"], use_cuda=self.USE_GPU, ) if p.get_total_layers() != 21: raise AssertionError if p.get_layer_dim(2) != 4: raise AssertionError if p.get_layer_type(2) != torch.nn.MaxPool2d: raise AssertionError if p.get_layer_type(20) != torch.nn.Linear: raise AssertionError
def test_single_linear_neuron_inj(self): p = fault_injection( self.model, self.BATCH_SIZE, layer_types=[torch.nn.Linear], use_cuda=self.USE_GPU, ) (b, layer, C, H, W, err_val) = (0, 2, 1, 192, 3000, 10000) inj = p.declare_neuron_fi(batch=[b], layer_num=[layer], dim1=[C], dim2=[H], dim3=[W], value=[err_val]) inj_output = inj(self.IMAGE) inj_softmax = self.softmax(inj_output) inj_label = list(torch.argmax(inj_softmax, dim=1))[0].item() if inj_label != 888: raise AssertionError
def test_single_conv_neuron(self): p = fault_injection( self.model, self.BATCH_SIZE, layer_types=[torch.nn.Conv2d], use_cuda=self.USE_GPU, ) (b, layer, C, H, W, err_val) = ([0], [3], [4], [2], [4], [10000]) inj = p.declare_neuron_fi(batch=b, layer_num=layer, dim1=C, dim2=H, dim3=W, value=err_val) inj_output = inj(self.IMAGE) inj_softmax = self.softmax(inj_output) inj_label = list(torch.argmax(inj_softmax, dim=1))[0].item() if inj_label != 578: raise AssertionError
def test_single_conv_neuron(self): p = fault_injection( self.model, self.BATCH_SIZE, input_shape=[3, self.H, self.W], layer_types=[torch.nn.Linear], use_cuda=self.USE_GPU, ) (b, layer, C, H, W, err_val) = ([0], [2], [None], [190], [3000], [10000]) inj = p.declare_neuron_fi(batch=b, layer_num=layer, c=C, h=H, w=W, value=err_val) inj_output = inj(self.IMAGE) inj_softmax = self.softmax(inj_output) inj_label = list(torch.argmax(inj_softmax, dim=1))[0].item() print("Inj Label", inj_label) print("------------------------------------")
USE_GPU = False # model, dataset = helper_setUp_CIFAR10_same(BATCH_SIZE, WORKERS) # dataiter = iter(dataset) torch.manual_seed(5) model = timm.create_model("vit_base_patch16_224", pretrained=True) model.eval() images = torch.rand((BATCH_SIZE, 3, IMG_SIZE, IMG_SIZE)) torch.no_grad() output = model(images) gold_label = list(torch.argmax(output, dim=1))[0].item() print("Golden Label:", gold_label) # p = fault_injection(model, IMG_SIZE, IMG_SIZE, BATCH_SIZE, use_cuda=USE_GPU) p = fault_injection( model, # IMG_SIZE, BATCH_SIZE, [3, 224, 224], layer_types=[torch.nn.Conv2d, torch.nn.Linear], use_cuda=False, ) (b, layer, C, H, W, err_val) = ([0], [2], [190], [3000], [None], [10000]) inj = p.declare_neuron_fi(batch=b, layer_num=layer, c=C, h=H, w=W, value=err_val) inj_output = inj(images) # print(p.print_pytorchfi_layer_summary())
random_image = torch.rand((batch_size, channels, height, width)) output = model(random_image) proper_label = list(torch.argmax(output, dim=1))[0].item() print("Error-free label:", proper_label) # Fickle the safe model safe_model = Pickled.load(pickle.dumps(model)) safe_props = safe_model.properties # Apply the fault injection injected_model = fault_injection( model, batch_size, input_shape=[channels, height, width], layer_types=[torch.nn.Conv2d], use_cuda=False, ) b, layer, C, H, W, err_val = [0], [2], [4], [2], [4], [10000] injected_model = injected_model.declare_neuron_fi( batch=b, layer_num=layer, dim1=C, dim2=H, dim3=W, value=err_val ) injected_output = injected_model(random_image) injected_label = list(torch.argmax(injected_output, dim=1))[0].item() print("Injected Label:", injected_label) # Fickle the model with the fault injection