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

ensure_shared_weights(shared_weights=None)

Ensure that the shared_weights is set properly.

No-op if shared weights is not used.

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_analog_ctx()

Return the analog context of the tile to be used in AnalogFunction.

Return type

aihwkit.optim.context.AnalogContext

get_brief_info()

Return short info about the underlying C++ tile.

Return type

str

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]]

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

reset_delta_weights()

Reset the weight grad tensor to default update behavior (i.e. adding the update directly to the weight).

No-op if shared weights is not used.

Return type

None

set_delta_weights(delta_weights=None)

Set the weight grad tensor and set the update to.

No-op if shared weights is not used.

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.

Raises

TileError – In case the ordered dict keys do not conform with the current rpu config tile structure of the hidden parameters

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)

Set 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