def test_cycle_variability(debug_patched_networks, std=10): for parallelize in [True, False]: patched_networks = debug_patched_networks for patched_network in patched_networks: for i, (name, m) in enumerate(list(patched_network.named_modules())): if type(m) in supported_module_parameters.values(): if 'cpu' not in memtorch.__version__ and len( name.split('.')) > 1: name = name.split('.')[1] if hasattr(patched_network, 'module'): with pytest.raises(Exception): setattr( patched_network.module, name, apply_cycle_variability( m, parallelize=parallelize, r_off_kwargs={'invalid_arg': None}, r_on_kwargs={'invalid_arg': None})) setattr( patched_network.module, name, apply_cycle_variability( m, parallelize=parallelize, r_off_kwargs={ 'loc': m.crossbars[0].r_off_mean, 'scale': std * 2 }, r_on_kwargs={ 'loc': m.crossbars[0].r_on_mean, 'scale': std })) else: with pytest.raises(Exception): setattr( patched_network, name, apply_cycle_variability( m, parallelize=parallelize, r_off_kwargs={'invalid_arg': None}, r_on_kwargs={'invalid_arg': None})) setattr( patched_network, name, apply_cycle_variability( m, parallelize=parallelize, r_off_kwargs={ 'loc': m.crossbars[0].r_off_mean, 'scale': std * 2 }, r_on_kwargs={ 'loc': m.crossbars[0].r_on_mean, 'scale': std }))
def model_degradation(model, cycle_count, v_stop): for i, (name, m) in enumerate(list(model.named_modules())): if type(m) in supported_module_parameters.values(): setattr(model, name, model_sudden(m, cycle_count, v_stop)) return model
def model_degradation(model, time, tempurature): for i, (name, m) in enumerate(list(model.named_modules())): if type(m) in supported_module_parameters.values(): setattr(model, name, model_sudden(m, time, tempurature)) return model
def test_cycle_variability( debug_patched_networks, tile_shape, parallelize, quant_method, std=10 ): patched_networks = debug_patched_networks(tile_shape, quant_method) for patched_network in patched_networks: for i, (name, m) in enumerate(list(patched_network.named_modules())): if type(m) in supported_module_parameters.values(): if "cpu" not in memtorch.__version__ and len(name.split(".")) > 1: name = name.split(".")[1] if hasattr(patched_network, "module"): with pytest.raises(Exception): setattr( patched_network.module, name, apply_cycle_variability( m, parallelize=parallelize, r_off_kwargs={"invalid_arg": None}, r_on_kwargs={"invalid_arg": None}, ), ) setattr( patched_network.module, name, apply_cycle_variability( m, parallelize=parallelize, r_off_kwargs={ "loc": m.crossbars[0].r_off_mean, "scale": std * 2, }, r_on_kwargs={"loc": m.crossbars[0].r_on_mean, "scale": std}, ), ) else: with pytest.raises(Exception): setattr( patched_network, name, apply_cycle_variability( m, parallelize=parallelize, r_off_kwargs={"invalid_arg": None}, r_on_kwargs={"invalid_arg": None}, ), ) setattr( patched_network, name, apply_cycle_variability( m, parallelize=parallelize, r_off_kwargs={ "loc": m.crossbars[0].r_off_mean, "scale": std * 2, }, r_on_kwargs={"loc": m.crossbars[0].r_on_mean, "scale": std}, ), )
def apply_nonidealities(model, non_idealities, **kwargs): """Method to apply non-idealities to a torch.nn.Module instance with memristive layers. Parameters ---------- model : torch.nn.Module torch.nn.Module instance. nonidealities : memtorch.bh.nonideality.NonIdeality.NonIdeality, tuple Non-linearitites to model. Returns ------- torch.nn.Module Patched instance. """ for i, (name, m) in enumerate(list(model.named_modules())): if type(m) in supported_module_parameters.values(): if "cpu" not in memtorch.__version__ and len(name.split(".")) > 1: name = name.split(".")[1] for non_ideality in non_idealities: if non_ideality == NonIdeality.FiniteConductanceStates: required( kwargs, ["conductance_states"], "memtorch.bh.nonideality.NonIdeality.FiniteConductanceStates", ) if hasattr(model, "module"): setattr( model.module, name, apply_finite_conductance_states( m, kwargs["conductance_states"]), ) else: setattr( model, name, apply_finite_conductance_states( m, kwargs["conductance_states"]), ) elif non_ideality == NonIdeality.DeviceFaults: required( kwargs, [ "lrs_proportion", "hrs_proportion", "electroform_proportion" ], "memtorch.bh.nonideality.NonIdeality.DeviceFaults", ) if hasattr(model, "module"): setattr( model.module, name, apply_device_faults( m, kwargs["lrs_proportion"], kwargs["hrs_proportion"], kwargs["electroform_proportion"], ), ) else: setattr( model, name, apply_device_faults( m, kwargs["lrs_proportion"], kwargs["hrs_proportion"], kwargs["electroform_proportion"], ), ) elif non_ideality == NonIdeality.NonLinear: if "simulate" in kwargs: if kwargs["simulate"] == True: if hasattr(model, "module"): setattr( model.module, name, apply_non_linear(m, simulate=True), ) else: setattr(model, name, apply_non_linear(m, simulate=True)) else: required( kwargs, [ "sweep_duration", "sweep_voltage_signal_amplitude", "sweep_voltage_signal_frequency", ], "memtorch.bh.nonideality.NonIdeality.NonLinear", ) if hasattr(model, "module"): setattr( model.module, name, apply_non_linear( m, kwargs["sweep_duration"], kwargs[ "sweep_voltage_signal_amplitude"], kwargs[ "sweep_voltage_signal_frequency"], ), ) else: setattr( model, name, apply_non_linear( m, kwargs["sweep_duration"], kwargs[ "sweep_voltage_signal_amplitude"], kwargs[ "sweep_voltage_signal_frequency"], ), ) else: required( kwargs, [ "sweep_duration", "sweep_voltage_signal_amplitude", "sweep_voltage_signal_frequency", ], "memtorch.bh.nonideality.NonIdeality.NonLinear", ) if hasattr(model, "module"): setattr( model.module, name, apply_non_linear( m, kwargs["sweep_duration"], kwargs["sweep_voltage_signal_amplitude"], kwargs["sweep_voltage_signal_frequency"], ), ) else: setattr( model, name, apply_non_linear( m, kwargs["sweep_duration"], kwargs["sweep_voltage_signal_amplitude"], kwargs["sweep_voltage_signal_frequency"], ), ) elif non_ideality == NonIdeality.Endurance: required( kwargs, ["x", "endurance_model", "endurance_model_kwargs"], "memtorch.bh.nonideality.Endurance", ) if hasattr(model, "module"): setattr( model.module, name, apply_endurance_model( layer=m, x=kwargs["x"], endurance_model=kwargs["endurance_model"], **kwargs["endurance_model_kwargs"]), ) else: setattr( model, name, apply_endurance_model( layer=m, x=kwargs["x"], endurance_model=kwargs["endurance_model"], **kwargs["endurance_model_kwargs"]), ) elif non_ideality == NonIdeality.Retention: required( kwargs, ["time", "retention_model", "retention_model_kwargs"], "memtorch.bh.nonideality.Retention", ) if hasattr(model, "module"): setattr( model.module, name, apply_retention_model( layer=m, time=kwargs["time"], retention_model=kwargs["retention_model"], **kwargs["retention_model_kwargs"]), ) else: setattr( model, name, apply_retention_model( layer=m, time=kwargs["time"], retention_model=kwargs["retention_model"], **kwargs["retention_model_kwargs"]), ) return model
def apply_nonidealities(model, non_idealities, **kwargs): """Method to apply non-idealities to a torch.nn.Module instance with memristive layers. Parameters ---------- model : torch.nn.Module torch.nn.Module instance. nonidealities : memtorch.bh.nonideality.NonIdeality.NonIdeality, tuple Non-linearitites to model. Returns ------- torch.nn.Module Patched instance. """ for i, (name, m) in enumerate(list(model.named_modules())): if type(m) in supported_module_parameters.values(): if 'cpu' not in memtorch.__version__ and len(name.split('.')) > 1: name = name.split('.')[1] for non_ideality in non_idealities: if non_ideality == NonIdeality.FiniteConductanceStates: required( kwargs, ['conductance_states'], 'memtorch.bh.nonideality.NonIdeality.FiniteConductanceStates' ) if hasattr(model, 'module'): setattr( model.module, name, apply_finite_conductance_states( m, kwargs['conductance_states'])) else: setattr( model, name, apply_finite_conductance_states( m, kwargs['conductance_states'])) if non_ideality == NonIdeality.DeviceFaults: required( kwargs, [ 'lrs_proportion', 'hrs_proportion', 'electroform_proportion' ], 'memtorch.bh.nonideality.NonIdeality.DeviceFaults') if hasattr(model, 'module'): setattr( model.module, name, apply_device_faults( m, kwargs['lrs_proportion'], kwargs['hrs_proportion'], kwargs['electroform_proportion'])) else: setattr( model, name, apply_device_faults( m, kwargs['lrs_proportion'], kwargs['hrs_proportion'], kwargs['electroform_proportion'])) if non_ideality == NonIdeality.NonLinear: if 'simulate' in kwargs: if kwargs['simulate'] == True: if hasattr(model, 'module'): setattr(model.module, name, apply_non_linear(m, simulate=True)) else: setattr(model, name, apply_non_linear(m, simulate=True)) else: required( kwargs, [ 'sweep_duration', 'sweep_voltage_signal_amplitude', 'sweep_voltage_signal_frequency' ], 'memtorch.bh.nonideality.NonIdeality.NonLinear' ) if hasattr(model, 'module'): setattr( model.module, name, apply_non_linear( m, kwargs['sweep_duration'], kwargs[ 'sweep_voltage_signal_amplitude'], kwargs[ 'sweep_voltage_signal_frequency'])) else: setattr( model, name, apply_non_linear( m, kwargs['sweep_duration'], kwargs[ 'sweep_voltage_signal_amplitude'], kwargs[ 'sweep_voltage_signal_frequency'])) else: required( kwargs, [ 'sweep_duration', 'sweep_voltage_signal_amplitude', 'sweep_voltage_signal_frequency' ], 'memtorch.bh.nonideality.NonIdeality.NonLinear') if hasattr(model, 'module'): setattr( model.module, name, apply_non_linear( m, kwargs['sweep_duration'], kwargs['sweep_voltage_signal_amplitude'], kwargs['sweep_voltage_signal_frequency'])) else: setattr( model, name, apply_non_linear( m, kwargs['sweep_duration'], kwargs['sweep_voltage_signal_amplitude'], kwargs['sweep_voltage_signal_frequency'])) return model