aihwkit.simulator.tiles.base module

High level analog tiles (base).

class aihwkit.simulator.tiles.base.BaseTile(*args, **kwds)

Bases: typing.Generic

Base class for tiles.

Parameters
  • out_size – output size

  • in_size – input size

  • rpu_config – resistive processing unit configuration.

  • bias – whether to add a bias column to the tile.

  • in_trans – Whether to assume an transposed input (batch first)

  • out_trans – Whether to assume an transposed output (batch first)

backward(d_input)

Perform the backward pass.

Parameters

d_input (torch.Tensor) – [N, out_size] tensor. If out_trans is set, transposed.

Returns

[N, in_size] tensor. If in_trans is set, transposed.

Return type

torch.Tensor

backward_indexed(d_input)

Perform the backward pass for convolutions.

Depending on the input tensor size it performs the backward pass for a 2D image or a 3D one.

Parameters

d_input (torch.Tensor) – [N, out_size] tensor. If out_trans is set, transposed.

Returns

[N, in_size] tensor. If in_trans is set, transposed.

Return type

torch.Tensor

Raises

TileError – if the indexed tile has not been initialized, or if self.images_sizes does not have a valid dimennion.

cpu()

Return a copy of this tile in CPU memory.

Return type

aihwkit.simulator.tiles.base.BaseTile

cuda(device=None)

Return a copy of this tile in CUDA memory.

Parameters

device (Optional[Union[torch.device, str, int]]) –

Return type

aihwkit.simulator.tiles.base.BaseTile

decay_weights(alpha=1.0)

Decays the weights once according to the decay parameters of the tile.

Parameters

alpha – additional decay scale (such as LR). The base decay rate is set during tile init.

Returns

None.

Return type

None

diffuse_weights()

Diffuses the weights once according to the diffusion parameters of the tile.

The base diffusion rate is set during tile init.

Returns

None

Return type

None

drift_weights(delta_t=1.0)

Drifts the weights once according to the drift parameters of the tile.

See also DriftParameter.

Parameters

delta_t – Time since last drift call.

Returns

None.

Return type

None

forward(x_input, is_test=False)

Perform the forward pass.

Parameters
  • x_input[N, in_size] tensor. If in_trans is set, transposed.

  • is_test – whether to assume testing mode.

Returns

[N, out_size] tensor. If out_trans is set, transposed.

Return type

torch.Tensor

forward_indexed(x_input, is_test=False)

Perform the forward pass for convolutions.

Depending on the input tensor size it performs the forward pass for a 2D image or a 3D one.

Parameters
  • x_input[N, in_size] tensor. If in_trans is set, transposed.

  • is_test – whether to assume testing mode.

Returns

[N, out_size] tensor. If out_trans is set, transposed.

Return type

torch.Tensor

Raises

TileError – if the indexed tile has not been initialized, or if self.images_sizes does not have a valid dimennion.

get_hidden_parameters()

Get the hidden parameters of the tile.

Returns

Ordered dictionary of hidden parameter tensors.

Return type

collections.OrderedDict

get_hidden_update_index()

Get the current updated device index of the hidden devices.

Usually this is 0 as only one device is present per cross-point for many tile RPU configs. However, some RPU configs maintain internally multiple devices per cross-point (e.g. VectorUnitCell).

Returns

The next mini-batch updated device index.

Return type

int

Note

Depending on the update and learning policy implemented in the tile, updated devices might switch internally as well.

get_learning_rate()

Return the tile learning rate.

Returns

the tile learning rate.

Return type

float

get_weights(realistic=False)

Get the tile weights (and biases).

Gets the tile weights and extracts the mathematical weight matrix and biases (if present, by determined by the self.bias parameter).

Note

By default this is not hardware realistic. Use set realistic to True for a realistic transfer.

Parameters

realistic (bool) – Whether to use the forward pass to read out the tile weights iteratively, using get_weights_realistic().

Returns

a tuple where the first item is the [out_size, in_size] weight matrix; and the second item is either the [out_size] bias vector or None if the tile is set not to use bias.

Return type

Tuple[torch.Tensor, Optional[torch.Tensor]]

get_weights_scaled(realistic=False)

Get the tile weights (and biases) and applies the current alpha scale to it.

Gets the tile weights and extracts the mathematical weight matrix and biases (if present, by determined by the self.bias parameter).

Note

By default this is not hardware realistic. Use set realistic to True for a realistic transfer.

Parameters

realistic (bool) – Whether to use the forward pass to read out the tile weights iteratively, using get_weights_realistic().

Returns

a tuple where the first item is the [out_size, in_size] weight matrix; and the second item is either the [out_size] bias vector or None if the tile is set not to use bias. Both have the alpha scale applied.

Return type

Tuple[torch.Tensor, Optional[torch.Tensor]]

is_cuda = False
post_update_step()

Operators that need to be called once per mini-batch.

Return type

None

reset_columns(start_column_idx=0, num_columns=1, reset_prob=1.0)

Reset (a number of) columns according to the reset parameters of the tile.

Resets the weights with device-to-device and cycle-to-cycle variability (depending on device type), typically:

\[W_{ij} = \xi*\sigma_\text{reset} + b^\text{reset}_{ij}\]

The reset parameters are set during tile init.

Parameters
  • start_column_idx – a start index of columns (0..x_size-1)

  • num_columns – how many consecutive columns to reset (with circular warping)

  • reset_prob – individual probability of reset.

Returns

None

Return type

None

set_hidden_parameters(ordered_parameters)

Set the hidden parameters of the tile.

Caution

Usually the hidden parameters are drawn according to the parameter definitions (those given in the RPU config). If the hidden parameters are arbitrary set by the user, then this correspondence might be broken. This might cause problems in the learning, in particular, the weight granularity (usually dw_min, depending on the device) is needed for the dynamic adjustment of the bit length (update_bl_management, see UpdateParameters).

Currently, the new dw_min parameter is tried to be estimated from the average of hidden parameters if the discrepancy with the dw_min from the definition is too large.

Parameters

ordered_parameters (collections.OrderedDict) – Ordered dictionary of hidden parameter tensors.

Return type

None

set_hidden_update_index(index)

set the current updated hidden device index.

Usually this is ignored and fixed to 0 as only one device is present per cross-point. Other devices, might not allow explicit setting as it would interfere with the implemented learning However rule. However, some tiles have internally multiple devices per cross-point (eg. unit cell) that can be chosen depending on the update policy.

Parameters

index (int) – device index to be updated in the next mini-batch

Return type

None

Note

Depending on the update and learning policy implemented in the tile, updated devices might switch internally as well.

set_indexed(indices, image_sizes)

Sets the index matrix for convolutions ans switches to indexed forward/backward/update versions.

Parameters
  • indices (torch.Tensor) – torch.tensor with int indices

  • image_sizes (List) – [C_in, H_in, W_in, H_out, W_out] sizes

Raises
  • ValueError – if image_sizes does not have valid dimensions.

  • TileError – if the tile uses transposition.

Return type

None

set_learning_rate(learning_rate)

Set the tile learning rate.

Set the tile learning rate to -learning_rate. Note that the learning rate is always taken to be negative (because of the meaning in gradient descent) and positive learning rates are not supported.

Parameters

learning_rate (float) – the desired learning rate.

Returns

None.

Return type

None

set_weights(weights, biases=None, realistic=False, n_loops=10)

Set the tile weights (and biases).

Sets the internal tile weights to the specified values, and also the internal tile biases if the tile was set to use bias (via self.bias).

Note

By default this is not hardware realistic. You can set the realistic parameter to True for a realistic transfer.

Parameters
  • weights (torch.Tensor) – [out_size, in_size] weight matrix.

  • biases (Optional[torch.Tensor]) – [out_size] bias vector. This parameter is required if self.bias is True, and ignored otherwise.

  • realistic (bool) – whether to use the forward and update pass to program the weights iteratively, using set_weights_realistic().

  • n_loops (int) – number of times the columns of the weights are set in a closed-loop manner. A value of 1 means that all columns in principle receive enough pulses to change from w_min to w_max.

Returns

None.

Raises

ValueError – if the tile has bias but bias has not been specified.

Return type

None

set_weights_scaled(weights, biases=None, realistic=False, n_loops=10, omega=1.0)

Set the tile weights (and biases) in a scaled fashion.

Similar to set_weights(), however, additionally scales the weights by a global scale \(\alpha\), that is then applied in digital at the output of forward and backward pass, and the learning rate for this tile is adjusted accordingly.

The weights are scaled by \(\omega/\max_{ij} |w_ij|\) and the global digital factor \(alpha\) is set to \(\max_{ij} |w_ij|/\omega\).

It can be shown that such a constant factor greatly improves the SNR and training accuracy as the full weight range of the analog devices are used. See also Rasch, Gokmen & Haensch (2019) for more details.

Caution

Using get_weights will now retrieve the true analog weights without applying the global factor. To get the true weights, use get_weights and scale it by the \(\alpha\) of this layer which can be retrieved by get_alpha_scale().

Parameters
  • weights (torch.Tensor) – [out_size, in_size] weight matrix.

  • biases (Optional[torch.Tensor]) – [out_size] bias vector. This parameter is required if self.bias is True, and ignored otherwise.

  • realistic (bool) – whether to use the forward and update pass to program the weights iteratively, using set_weights_realistic().

  • n_loops (int) – number of times the columns of the weights are set in a closed-loop manner. A value of 1 means that all columns in principle receive enough pulses to change from w_min to w_max.

  • omega (float) – where the weight max should be mapped in terms of the weight range. Note that for omega larger than the maximal weight of the device, weights will get clipped for most devices.

Returns

None.

Raises

ValueError – if the tile has bias but bias has not been specified.

Return type

None

update(x_input, d_input)

Perform the update pass.

Parameters
  • x_input (torch.Tensor) – [N, in_size] tensor. If in_trans is set, transposed.

  • d_input (torch.Tensor) – [N, out_size] tensor. If out_trans is set, transposed.

Returns

None

Return type

None

update_indexed(x_input, d_input)

Perform the update pass for convolutions.

Parameters
  • x_input (torch.Tensor) – [N, in_size] tensor. If in_trans is set, transposed.

  • d_input (torch.Tensor) – [N, out_size] tensor. If out_trans is set, transposed.

Returns

None

Return type

None

aihwkit.simulator.tiles.base.empty(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) → Tensor

Returns a tensor filled with uninitialized data. The shape of the tensor is defined by the variable argument size.

Parameters

size (int...) – a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.

Keyword Arguments
  • out (Tensor, optional) – the output tensor.

  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()).

  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.

  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

  • memory_format (torch.memory_format, optional) – the desired memory format of returned Tensor. Default: torch.contiguous_format.

Example:

>>> torch.empty(2, 3)
tensor(1.00000e-08 *
       [[ 6.3984,  0.0000,  0.0000],
        [ 0.0000,  0.0000,  0.0000]])