aihwkit.utils.visualization module
Visualization utilities.
This module includes plotting and visualization utilities for aihwkit
.
Using this module has extra dependencies that can be installed via the
extras mechanism:
pip install aihwkit[visualization]
- aihwkit.utils.visualization.compute_pulse_response(analog_tile, direction, use_forward=False)[source]
Compute the pulse response of a given device configuration.
- Parameters:
analog_tile (TileModule) – Base tile used for computing the weight traces
direction (ndarray) – numpy vector of directions to sequentially apply (-1 or 1)
use_forward (bool) – Whether to use the (noisy) forward pass to read out the weights (otherwise returns exact weight value).
- Returns:
An numpy array
w_trace
of dimensionslen(direction) x out_size x in_size
- Return type:
ndarray
- aihwkit.utils.visualization.compute_pulse_statistics(w_nodes, w_trace, direction, up_direction, smoothness=0.5)[source]
Compute the statistics of the step trace from
compute_pulse_response()
.- Parameters:
w_nodes (ndarray) – weight range vector to estimate the step histogram
w_trace (ndarray) – weight trace from
compute_pulse_response()
direction (ndarray) – direction vector used to generate the weight traces
up_direction (bool) – whether and plot to compute the statistics for up or down direction
smoothness (float) – value for smoothing the estimation of the statistical step response curves
- Returns:
Tuple of
(dw_mean, dw_std)
.- Return type:
Tuple[ndarray, ndarray]
- aihwkit.utils.visualization.estimate_n_steps(rpu_config)[source]
Estimate the n_steps.
Note
The estimate of the number of update pulses needed to drive from smallest to largest conductance. The estimation just assumes linear behavior, thus only be a rough estimate for non-linear response curves.
- Parameters:
rpu_config (SingleRPUConfig | UnitCellRPUConfig) – RPU Configuration to use for plotting
- Returns:
Guessed number of steps
- Raises:
ConfigError – If rpu_config.device does not have the w_min attribute (which is only ensured for
PulseDevice
)- Return type:
int
- aihwkit.utils.visualization.get_tile_for_plotting(rpu_config, n_traces, use_cuda=False, noise_free=False, w_init=None)[source]
Return an analog tile for plotting the response curve.
- Parameters:
rpu_config (SingleRPUConfig | UnitCellRPUConfig) – RPU Configuration to use for plotting
n_traces (int) – Number of traces to plot
use_cuda (bool) – Whether to use the CUDA implementation (if available)
noise_free (bool) – Whether to turn-off cycle-to-cycle noises (if possible)
w_init (float | None) – init value if given otherwise
w_min
is taken
- Returns:
Instantiated tile.
- Return type:
- aihwkit.utils.visualization.plot_device(device, w_noise=0.0, **kwargs)[source]
Plot the step response figure for a given device (preset).
Note
It will use an amount of read weight noise
w_noise
for reading the weights.- Parameters:
device (PulsedDevice | UnitCell) – PulsedDevice parameters
w_noise (float) – Weight noise standard deviation during read
kwargs (Any) – for other parameters, see
plot_response_overview()
- Return type:
None
- aihwkit.utils.visualization.plot_device_compact(device, w_noise=0.0, n_steps=None, n_traces=3, n_loops=2, w_init=None, use_cuda=False, axes=None, **plot_kwargs)[source]
Plot a compact step response figure for a given device (preset).
Note
It will use an amount of read weight noise
w_noise
for reading the weights.- Parameters:
device (PulsedDevice | UnitCell) –
PulsedDevice
orUnitCell
parametersw_noise (float) – Weight noise standard deviation during read
n_steps (int | None) – Number of up/down steps per cycle. If not given, will be tried to be estimated (only for
PulsedDevice
possible otherwise defaults to 1000 ifn_steps=None
).n_traces (int) – Number of traces to plot (for device-to-device variation)
n_loops (int) – Number of cycles
w_init (float | None) – init weight values if given, otherwise
w_min
is takenuse_cuda (bool) – Whether to use CUDA for the computation
axes (Axes | None) – axis to plot on. If given the statistics are not computed and only one axes is returned
plot_kwargs (Any) – additional plotting keywords for the main plot
- Returns:
the compact step response figure or the axes if given as argument.
- Return type:
Figure | Axes
- aihwkit.utils.visualization.plot_device_symmetry(device, w_noise=0.0, n_pulses=10000, n_traces=3, use_cuda=False, w_init=1.0)[source]
Plot the response figure for a given device (preset).
It will show the response to alternating up down pulses.
Note
It will use an amount of read weight noise
w_noise
for reading the weights.- Parameters:
device (PulsedDevice) – PulsedDevice parameters
n_pulses (int) – total number of pulses
w_noise (float) – Weight noise standard deviation during read
n_traces (int) – Number of device traces
use_cuda (bool) – Whether to use CUDA,
w_init (float) – Initial value of the weights
- Return type:
None
- aihwkit.utils.visualization.plot_model_programming_error(analog_model, n_bins=51, apply_weight_scaling=False, axes=None, **kwargs)[source]
Plot weight programming error between the programmed weights and the target weight values.
The target values are read out from the internal reference structure and drift compensation and any analog aspect is turned off.
Weight of the analog_model are read out using realistic read, that is with a estimation of the operator that takes into account all the analog noise sources and non-idealities.
- Parameters:
analog_model (AnalogLayerBase) – Programmed (and drifted) analog model
n_bins (int) – Number of bins for plotting
apply_weight_scaling (bool) – Whether to apply the weight scaling factors when combining the weights
axes (Axes | None) – Plot axes (default current axis)
kwargs (Any) – additional plotting arguments such as label
- Returns:
the axis used for the plot.
- Raises:
TileError – In case not inference tile with programmed weights is used.
- Return type:
Axes
- aihwkit.utils.visualization.plot_programming_error(config, w_range=(-1.0, 1.0), w_init=None, n_rows=100, realistic_read=False, n_bins=101, use_cuda=False, label=None, axes=None, t_inference=None, **kwargs)[source]
Plot weight programming error with weight value.
Programming of weights will be done by calling the
program_weights
method and read out can also optionally be done withread_weights
. This means that for analog training device configurations, programming will be done using SGD on random vectors and stochastic pulsing or as defined in theRPUConfig
if given explicitely. Read is done perfectly unlessrealistic_read
is set.It plots mean with plus/minus standard error of the mean as confidence interval.
Note
If RPUConfig is not directly given, it will use the
PresetIOParameters
configuration by default.- Parameters:
config (PulsedDevice | UnitCell | RPUConfigBase) –
PulsedDevice
,UnitCell
parameters or directly aRPUConfig
w_range (Tuple[float, float]) – Range of weights.
w_init (ndarray | None) – Weight matrix to program (default n_rows x n_rows uniform in
w_range
)n_rows (int) – Number of rows (and columns) of the decault weight matrix
realistic_read (bool) – Whether to use
read_weights
estimation instead of a perfect read.n_bins (int) – Number of bins for plotting
use_cuda (bool) – Whether to use CUDA for the computation
label (str | None) – Plotting curve label
axes (Axes | None) – Plot axes (default current axis)
t_inference (int | None) – Setting of the inference time (only in case of
InferenceRPUConfig
)kwargs (Any) – Additional arguments for
program_weights
call.
- Returns:
the axis used for the plot.
- Return type:
Axes
- aihwkit.utils.visualization.plot_pulse_response(analog_tile, direction, use_forward=False)[source]
Plot the pulse response of a direction vector for each weight of the analog tile.
- Parameters:
analog_tile (TileModule) – Base tile used for computing the weight traces
direction (ndarray) – vector of directions to sequentially apply (-1 or 1)
use_forward (bool) – Whether to use the (noisy) forward pass to read out the weights (otherwise returns exact weight value).
- Returns:
- Return type:
w_trace
- aihwkit.utils.visualization.plot_pulse_statistics(w_trace, direction, up_direction, num_nodes=100, smoothness=0.5)[source]
Plot the dG-G curve from a given weight trace and direction vector.
- Parameters:
w_trace (ndarray) – weight trace from
compute_pulse_response()
direction (ndarray) – direction vector used to generate
w_trace
up_direction (bool) – whether and plot to compute the statistics for up or down direction
num_nodes (int) – number of nodes for estimation of the step histogram
smoothness (float) – value for smoothing the estimation of the statistical step response curves
- Returns:
A tuple (w_nodes, dw_mean, dw_std) from
compute_pulse_statistics()
- Return type:
Tuple[ndarray, ndarray, ndarray]
- aihwkit.utils.visualization.plot_response_overview(rpu_config, n_loops=5, n_steps=None, n_traces=5, use_cuda=False, smoothness=0.1)[source]
Plot the step response and statistics of a given device configuration.
- Parameters:
rpu_config (SingleRPUConfig | UnitCellRPUConfig) – RPU Configuration to use for plotting
n_loops (int) – How many hyper-cycles (up/down pulse sequences) to plot
n_steps (int | None) – Number of up/down steps per cycle. If not given, will be tried to be estimated (only for
PulsedDevice
possible otherwise defaults to 1000 ifn_steps=None
).n_traces (int) – Number of traces to plot
use_cuda (bool) – Whether to use the CUDA implementation (if available)
smoothness (float) – value for smoothing the estimation of the statistical step response curves
- Return type:
None
- aihwkit.utils.visualization.plot_weight_drift(noise_model=None, t_inference_list=None, w_inits=None, n_repeats=25)[source]
Plots the weight drift behavior of a given noise model over time.
- Parameters:
noise_model (BaseNoiseModel | None) – Noise model of derived from
BaseNoiseModel
t_inference_list (ndarray | None) – Numpy array of times of inference after programming at time 0 (in seconds)
w_inits (ndarray | None) – Numpy array of target weights to program
n_repeats (int) – How many repeats to estimate the standard deviation
- Return type:
None