aihwkit.nn.modules.base module

Base class for analog Modules.

class aihwkit.nn.modules.base.AnalogModuleBase

Bases: torch.nn.modules.module.Module

Base class for analog Modules.

Base Module for analog layers that use analog tiles. When subclassing, please note:

  • the _setup_tile() method is expected to be called by the subclass constructor, and it does not only create a tile, but also sets some instance attributes that are needed by the analog features (optimizer and others).

  • the weight and bias Parameters are not guaranteed to be in sync with the tile weights and biases during the lifetime of the instance, for performance reasons. The canonical way of reading and writing weights is via the set_weights() and get_weights() as opposed to using the attributes directly.

TILE_CLASS_ANALOG

alias of aihwkit.simulator.tiles.analog.AnalogTile

TILE_CLASS_FLOATING_POINT

alias of aihwkit.simulator.tiles.floating_point.FloatingPointTile

TILE_CLASS_INFERENCE

alias of aihwkit.simulator.tiles.inference.InferenceTile

cuda(device=None)

Moves all model parameters, buffers and tiles to the GPU.

This also makes associated parameters and buffers different objects. So it should be called before constructing optimizer if the module will live on GPU while being optimized.

Parameters

device (int, optional) – if specified, all parameters will be copied to that GPU device

Return type

aihwkit.nn.modules.base.AnalogModuleBase

drift_analog_weights(t_inference=0.0)

(Program) and drift the analog weights.

Parameters

t_inference (float) – assumed time of inference (in sec)

Return type

None

get_weights(force_exact=False)

Get the weight (and bias) tensors.

This uses an realistic read if the property realistic_read_write of the layer is set, unless it is overwritten by force_exact.

Note

This is the recommended way for setting the weight/bias matrix from the analog tile, as it will correctly fetch the weights from the internal memory. Accessing self.weight and self.bias might yield wrong results as they are not always in sync with the analog tile library, for performance reasons.

Parameters

force_exact (bool) – forces an exact read to the analog tiles

Returns

weight matrix, bias vector

Return type

tuple

load_state_dict(state_dict, strict=True)

Copy parameters and buffers into this module and descendants.

Parameters
  • state_dict (Dict) –

  • strict (bool) –

Return type

NamedTuple

program_analog_weights()

Program the analog weights.

Return type

None

set_weights(weight, bias=None, force_exact=False)

Sets the weight (and bias) with given Tensors.

This uses an realistic write if the property realistic_read_write of the layer is set, unless it is overwritten by force_exact.

Note

This is the recommended way for setting the weight/bias matrix of the analog tile, as it will correctly store the weights into the internal memory. Directly writing to self.weight and self.bias might yield wrong results as they are not always in sync with the analog tile Parameters, for performance reasons.

Parameters
  • weight (torch.Tensor) – weight matrix

  • bias (Optional[torch.Tensor]) – bias vector

  • force_exact (bool) – forces an exact write to the analog tiles

Return type

None

state_dict(destination=None, prefix='', keep_vars=False)

Returns a dictionary containing a whole state of the module.

Parameters
  • destination (Any) –

  • prefix (str) –

  • keep_vars (bool) –

Return type

Dict