memtorch.mn¶
Memristive torch.nn equivalent submodule.
memtorch.mn.Module¶
Encapsulates memtorch.bmn.Module.patch_model
, which can be used to convert torch.nn models.
import copy
import Net
from memtorch.mn.Module import patch_model
from memtorch.map.Parameter import naive_map
from memtorch.map.Input import naive_scale
model = Net()
reference_memristor = memtorch.bh.memristor.VTEAM
patched_model = patch_model(copy.deepcopy(model),
memristor_model=reference_memristor,
memristor_model_params={},
module_parameters_to_patch=[torch.nn.Linear, torch.nn.Conv2d],
mapping_routine=naive_map,
transistor=True,
programming_routine=None,
tile_shape=(128, 128),
max_input_voltage=0.3,
scaling_routine=naive_scale,
ADC_resolution=8,
ADC_overflow_rate=0.,
quant_method='linear')
Warning
It is strongly suggested to copy the original model using copy.deepcopy prior to conversion, as some values are overriden by-reference.
-
memtorch.mn.Module.
patch_model
(model, memristor_model, memristor_model_params, module_parameters_to_patch={}, mapping_routine=<function naive_map>, transistor=True, programming_routine=None, programming_routine_params={'rel_tol': 0.1}, p_l=None, scheme=<Scheme.DoubleColumn: 2>, tile_shape=None, max_input_voltage=None, scaling_routine=<function naive_scale>, scaling_routine_params={}, source_resistance=None, line_resistance=None, ADC_resolution=None, ADC_overflow_rate=0.0, quant_method=None, use_bindings=True, random_crossbar_init=False, verbose=True, **kwargs)[source]¶ Method to convert a torch.nn model to a memristive model.
Parameters: - model (torch.nn.Module) – torch.nn.Module to patch.
- memristor_model (memtorch.bh.memristor.Memristor.Memristor) – Memristor model.
- memristor_model_params (**kwargs) – Memristor model keyword arguments.
- module_parameters_to_patch (module_paramter_patches) – Model parameters to patch.
- mapping_routine (function) – Mapping routine to use.
- transistor (bool) – Used to determine if a 1T1R (True) or 1R arrangement (False) is simulated.
- programming_routine (function) – Programming routine to use.
- programming_routine_params (**kwargs) – Programming routine keyword arguments.
- p_l (float) – If not None, the proportion of weights to retain.
- scheme (memtorch.bh.Scheme) – Weight representation scheme.
- tile_shape ((int, int)) – Tile shape to use to store weights. If None, modular tiles are not used.
- max_input_voltage (float) – Maximum input voltage used to encode inputs. If None, inputs are unbounded.
- scaling_routine (function) – Scaling routine to use in order to scale batch inputs.
- scaling_routine_params (**kwargs) – Scaling routine keyword arguments.
- source_resistance (float) – The resistance between word/bit line voltage sources and crossbar(s).
- line_resistance (float) – The interconnect line resistance between adjacent cells.
- ADC_resolution (int) – ADC resolution (bit width). If None, quantization noise is not accounted for.
- ADC_overflow_rate (float) – Overflow rate threshold for linear quanitzation (if ADC_resolution is not None).
- quant_method – Quantization method. Must be in [‘linear’, ‘log’, ‘log_minmax’, ‘minmax’, ‘tanh’], or None.
- use_bindings (bool) – Used to determine if C++/CUDA bindings are used (True) or not (False).
- random_crossbar_init (bool) – Determines if the crossbar is to be initialized at random values in between Ron and Roff
- verbose (bool) – Used to determine if verbose output is enabled (True) or disabled (False).
Returns: Patched torch.nn.Module.
Return type:
The following layer/module types are currently supported:
memtorch.mn.Linear¶
torch.nn.Linear equivalent.
-
class
memtorch.mn.Linear.
Linear
(linear_layer, memristor_model, memristor_model_params, mapping_routine=<function naive_map>, transistor=True, programming_routine=None, programming_routine_params={}, p_l=None, scheme=<Scheme.DoubleColumn: 2>, tile_shape=None, max_input_voltage=None, scaling_routine=<function naive_scale>, scaling_routine_params={}, source_resistance=None, line_resistance=None, ADC_resolution=None, ADC_overflow_rate=0.0, quant_method=None, use_bindings=True, random_crossbar_init=False, verbose=True, *args, **kwargs)[source]¶ Bases:
torch.nn.modules.linear.Linear
nn.Linear equivalent.
Parameters: - linear_layer (torch.nn.Linear) – Linear layer to patch.
- memristor_model (memtorch.bh.memristor.Memristor.Memristor) – Memristor model.
- memristor_model_params (**kwargs) – Memristor model keyword arguments.
- mapping_routine (function) – Mapping routine to use.
- transistor (bool) – Used to determine if a 1T1R (True) or 1R arrangement (False) is simulated.
- programming_routine (function) – Programming routine to use.
- programming_routine_params (**kwargs) – Programming routine keyword arguments.
- p_l (float) – If not None, the proportion of weights to retain.
- scheme (memtorch.bh.Scheme) – Weight representation scheme.
- tile_shape ((int, int)) – Tile shape to use to store weights. If None, modular tiles are not used.
- max_input_voltage (float) – Maximum input voltage used to encode inputs. If None, inputs are unbounded.
- scaling_routine (function) – Scaling routine to use in order to scale batch inputs.
- scaling_routine_params (**kwargs) – Scaling routine keyword arguments.
- source_resistance (float) – The resistance between word/bit line voltage sources and crossbar(s).
- line_resistance (float) – The interconnect line resistance between adjacent cells.
- ADC_resolution (int) – ADC resolution (bit width). If None, quantization noise is not accounted for.
- ADC_overflow_rate (float) – Overflow rate threshold for linear quanitzation (if ADC_resolution is not None).
- quant_method (string) – Quantization method. Must be in [‘linear’, ‘log’, ‘log_minmax’, ‘minmax’, ‘tanh’], or None.
- use_bindings (bool) – Used to determine if C++/CUDA bindings are used (True) or not (False).
- random_crossbar_init (bool) – Determines if the crossbar is to be initialized at random values in between Ron and Roff
- verbose (bool) – Used to determine if verbose output is enabled (True) or disabled (False).
-
forward
(input)[source]¶ Method to perform forward propagations.
Parameters: input (torch.Tensor) – Input tensor. Returns: Output tensor. Return type: torch.Tensor
memtorch.mn.Conv1d¶
torch.nn.Conv1d equivalent.
-
class
memtorch.mn.Conv1d.
Conv1d
(convolutional_layer, memristor_model, memristor_model_params, mapping_routine=<function naive_map>, transistor=True, programming_routine=None, programming_routine_params={}, p_l=None, scheme=<Scheme.DoubleColumn: 2>, tile_shape=None, max_input_voltage=None, scaling_routine=<function naive_scale>, scaling_routine_params={}, source_resistance=None, line_resistance=None, ADC_resolution=None, ADC_overflow_rate=0.0, quant_method=None, use_bindings=True, random_crossbar_init=False, verbose=True, *args, **kwargs)[source]¶ Bases:
torch.nn.modules.conv.Conv1d
nn.Conv1d equivalent.
Parameters: - convolutional_layer (torch.nn.Conv1d) – Convolutional layer to patch.
- memristor_model (memtorch.bh.memristor.Memristor.Memristor) – Memristor model.
- memristor_model_params (**kwargs) – Memristor model keyword arguments.
- mapping_routine (function) – Mapping routine to use.
- transistor (bool) – Used to determine if a 1T1R (True) or 1R arrangement (False) is simulated.
- programming_routine (function) – Programming routine to use.
- programming_routine_params (**kwargs) – Programming routine keyword arguments.
- p_l (float) – If not None, the proportion of weights to retain.
- scheme (memtorch.bh.Scheme) – Weight representation scheme.
- tile_shape ((int, int)) – Tile shape to use to store weights. If None, modular tiles are not used.
- max_input_voltage (float) – Maximum input voltage used to encode inputs. If None, inputs are unbounded.
- scaling_routine (function) – Scaling routine to use in order to scale batch inputs.
- scaling_routine_params (**kwargs) – Scaling routine keyword arguments.
- source_resistance (float) – The resistance between word/bit line voltage sources and crossbar(s).
- line_resistance (float) – The interconnect line resistance between adjacent cells.
- ADC_resolution (int) – ADC resolution (bit width). If None, quantization noise is not accounted for.
- ADC_overflow_rate (float) – Overflow rate threshold for linear quanitzation (if ADC_resolution is not None).
- quant_method (string) – Quantization method. Must be in [‘linear’, ‘log’, ‘log_minmax’, ‘minmax’, ‘tanh’], or None.
- use_bindings (bool) – Used to determine if C++/CUDA bindings are used (True) or not (False).
- random_crossbar_init (bool) – Determines if the crossbar is to be initialized at random values in between Ron and Roff
- verbose (bool) – Used to determine if verbose output is enabled (True) or disabled (False).
-
forward
(input)[source]¶ Method to perform forward propagations.
Parameters: input (torch.Tensor) – Input tensor. Returns: Output tensor. Return type: torch.Tensor
memtorch.mn.Conv2d¶
torch.nn.Conv2d equivalent.
-
class
memtorch.mn.Conv2d.
Conv2d
(convolutional_layer, memristor_model, memristor_model_params, mapping_routine=<function naive_map>, transistor=True, programming_routine=None, programming_routine_params={}, p_l=None, scheme=<Scheme.DoubleColumn: 2>, tile_shape=None, max_input_voltage=None, scaling_routine=<function naive_scale>, scaling_routine_params={}, source_resistance=None, line_resistance=None, ADC_resolution=None, ADC_overflow_rate=0.0, quant_method=None, use_bindings=True, random_crossbar_init=False, verbose=True, *args, **kwargs)[source]¶ Bases:
torch.nn.modules.conv.Conv2d
nn.Conv2d equivalent.
Parameters: - convolutional_layer (torch.nn.Conv2d) – Convolutional layer to patch.
- memristor_model (memtorch.bh.memristor.Memristor.Memristor) – Memristor model.
- memristor_model_params (**kwargs) – Memristor model keyword arguments.
- mapping_routine (function) – Mapping routine to use.
- transistor (bool) – Used to determine if a 1T1R (True) or 1R arrangement (False) is simulated.
- programming_routine (function) – Programming routine to use.
- programming_routine_params (**kwargs) – Programming routine keyword arguments.
- p_l (float) – If not None, the proportion of weights to retain.
- scheme (memtorch.bh.Scheme) – Weight representation scheme.
- tile_shape ((int, int)) – Tile shape to use to store weights. If None, modular tiles are not used.
- max_input_voltage (float) – Maximum input voltage used to encode inputs. If None, inputs are unbounded.
- scaling_routine (function) – Scaling routine to use in order to scale batch inputs.
- scaling_routine_params (**kwargs) – Scaling routine keyword arguments.
- source_resistance (float) – The resistance between word/bit line voltage sources and crossbar(s).
- line_resistance (float) – The interconnect line resistance between adjacent cells.
- ADC_resolution (int) – ADC resolution (bit width). If None, quantization noise is not accounted for.
- ADC_overflow_rate (float) – Overflow rate threshold for linear quanitzation (if ADC_resolution is not None).
- quant_method (string) – Quantization method. Must be in [‘linear’, ‘log’, ‘log_minmax’, ‘minmax’, ‘tanh’], or None.
- use_bindings (bool) – Used to determine if C++/CUDA bindings are used (True) or not (False).
- random_crossbar_init (bool) – Determines if the crossbar is to be initialized at random values in between Ron and Roff
- verbose (bool) – Used to determine if verbose output is enabled (True) or disabled (False).
-
forward
(input)[source]¶ Method to perform forward propagations.
Parameters: input (torch.Tensor) – Input tensor. Returns: Output tensor. Return type: torch.Tensor
memtorch.mn.Conv3d¶
torch.nn.Conv3d equivalent.
-
class
memtorch.mn.Conv3d.
Conv3d
(convolutional_layer, memristor_model, memristor_model_params, mapping_routine=<function naive_map>, transistor=True, programming_routine=None, programming_routine_params={}, p_l=None, scheme=<Scheme.DoubleColumn: 2>, tile_shape=None, max_input_voltage=None, scaling_routine=<function naive_scale>, scaling_routine_params={}, source_resistance=None, line_resistance=None, ADC_resolution=None, ADC_overflow_rate=0.0, quant_method=None, use_bindings=True, random_crossbar_init=False, verbose=True, *args, **kwargs)[source]¶ Bases:
torch.nn.modules.conv.Conv3d
nn.Conv3d equivalent.
Parameters: - convolutional_layer (torch.nn.Conv3d) – Convolutional layer to patch.
- memristor_model (memtorch.bh.memristor.Memristor.Memristor) – Memristor model.
- memristor_model_params (**kwargs) – Memristor model keyword arguments.
- mapping_routine (function) – Mapping routine to use.
- transistor (bool) – Used to determine if a 1T1R (True) or 1R arrangement (False) is simulated.
- programming_routine (function) – Programming routine to use.
- programming_routine_params (**kwargs) – Programming routine keyword arguments.
- p_l (float) – If not None, the proportion of weights to retain.
- scheme (memtorch.bh.Scheme) – Weight representation scheme.
- tile_shape ((int, int)) – Tile shape to use to store weights. If None, modular tiles are not used.
- max_input_voltage (float) – Maximum input voltage used to encode inputs. If None, inputs are unbounded.
- scaling_routine (function) – Scaling routine to use in order to scale batch inputs.
- scaling_routine_params (**kwargs) – Scaling routine keyword arguments.
- source_resistance (float) – The resistance between word/bit line voltage sources and crossbar(s).
- line_resistance (float) – The interconnect line resistance between adjacent cells.
- ADC_resolution (int) – ADC resolution (bit width). If None, quantization noise is not accounted for.
- ADC_overflow_rate (float) – Overflow rate threshold for linear quanitzation (if ADC_resolution is not None).
- quant_method (string) – Quantization method. Must be in [‘linear’, ‘log’, ‘log_minmax’, ‘minmax’, ‘tanh’], or None.
- use_bindings (bool) – Used to determine if C++/CUDA bindings are used (True) or not (False).
- random_crossbar_init (bool) – Determines if the crossbar is to be initialized at random values in between Ron and Roff
- verbose (bool) – Used to determine if verbose output is enabled (True) or disabled (False).
-
forward
(input)[source]¶ Method to perform forward propagations.
Parameters: input (torch.Tensor) – Input tensor. Returns: Output tensor. Return type: torch.Tensor
memtorch.mn.RNN¶
torch.nn.RNN equivalent.
-
class
memtorch.mn.RNN.
RNN
(rnn_layer, memristor_model, memristor_model_params, mapping_routine=<function naive_map>, transistor=True, programming_routine=None, programming_routine_params={}, p_l=None, scheme=<Scheme.DoubleColumn: 2>, tile_shape=None, max_input_voltage=None, scaling_routine=<function naive_scale>, scaling_routine_params={}, source_resistance=None, line_resistance=None, ADC_resolution=None, ADC_overflow_rate=0.0, quant_method=None, use_bindings=True, random_crossbar_init=False, verbose=True, *args, **kwargs)[source]¶ Bases:
torch.nn.modules.rnn.RNN
nn.RNN equivalent.
Parameters: - rnn_layer (torch.nn.RNN) – RNN layer to patch.
- memristor_model (memtorch.bh.memristor.Memristor.Memristor) – Memristor model.
- memristor_model_params (**kwargs) – Memristor model keyword arguments.
- mapping_routine (function) – Mapping routine to use.
- transistor (bool) – Used to determine if a 1T1R (True) or 1R arrangement (False) is simulated.
- programming_routine (function) – Programming routine to use.
- programming_routine_params (**kwargs) – Programming routine keyword arguments.
- p_l (float) – If not None, the proportion of weights to retain.
- scheme (memtorch.bh.Scheme) – Weight representation scheme.
- tile_shape ((int, int)) – Tile shape to use to store weights. If None, modular tiles are not used.
- max_input_voltage (float) – Maximum input voltage used to encode inputs. If None, inputs are unbounded.
- scaling_routine (function) – Scaling routine to use in order to scale batch inputs.
- scaling_routine_params (**kwargs) – Scaling routine keyword arguments.
- source_resistance (float) – The resistance between word/bit line voltage sources and crossbar(s).
- line_resistance (float) – The interconnect line resistance between adjacent cells.
- ADC_resolution (int) – ADC resolution (bit width). If None, quantization noise is not accounted for.
- ADC_overflow_rate (float) – Overflow rate threshold for linear quanitzation (if ADC_resolution is not None).
- quant_method (string) – Quantization method. Must be in [‘linear’, ‘log’, ‘log_minmax’, ‘minmax’, ‘tanh’], or None.
- use_bindings (bool) – Used to determine if C++/CUDA bindings are used (True) or not (False).
- random_crossbar_init (bool) – Determines if the crossbar is to be initialized at random values in between Ron and Roff
- verbose (bool) – Used to determine if verbose output is enabled (True) or disabled (False).
-
forward
(input, h_0=None)[source]¶ Method to perform forward propagations.
Parameters: input (torch.Tensor) – Input tensor.
- h_0 : torch.Tensor
- The initial hidden state for the input sequence batch
Returns: Output tensor. Return type: torch.Tensor