aihwkit.simulator.tiles.inference_torch module
High level analog tiles (inference).
- class aihwkit.simulator.tiles.inference_torch.InputRangeForward(*args, **kwargs)[source]
Bases:
Function
Enable custom input range gradient computation using torch’s autograd.
- static backward(ctx, d_output)[source]
Define a formula for differentiating the operation with backward mode automatic differentiation.
This function is to be overridden by all subclasses. (Defining this function is equivalent to defining the
vjp
function.)It must accept a context
ctx
as the first argument, followed by as many outputs as theforward()
returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs toforward()
. Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.The context can be used to retrieve tensors saved during the forward pass. It also has an attribute
ctx.needs_input_grad
as a tuple of booleans representing whether each input needs gradient. E.g.,backward()
will havectx.needs_input_grad[0] = True
if the first input toforward()
needs gradient computed w.r.t. the output.- Parameters:
ctx (Any) –
d_output (Tensor) –
- Return type:
Tuple[Tensor, Tensor, None]
- static forward(ctx, x_input, input_range, ir_params)[source]
Define the forward of the custom autograd Function.
This function is to be overridden by all subclasses. There are two ways to define forward:
Usage 1 (Combined forward and ctx):
@staticmethod def forward(ctx: Any, *args: Any, **kwargs: Any) -> Any: pass
It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).
See combining-forward-context for more details
Usage 2 (Separate forward and ctx):
@staticmethod def forward(*args: Any, **kwargs: Any) -> Any: pass @staticmethod def setup_context(ctx: Any, inputs: Tuple[Any, ...], output: Any) -> None: pass
The forward no longer accepts a ctx argument.
Instead, you must also override the
torch.autograd.Function.setup_context()
staticmethod to handle setting up thectx
object.output
is the output of the forward,inputs
are a Tuple of inputs to the forward.See extending-autograd for more details
The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with
ctx.save_for_backward()
if they are intended to be used inbackward
(equivalently,vjp
) orctx.save_for_forward()
if they are intended to be used for injvp
.- Parameters:
ctx (Any) –
x_input (Tensor) –
input_range (Tensor) –
ir_params (InputRangeParameter) –
- Return type:
Tensor
- class aihwkit.simulator.tiles.inference_torch.TorchInferenceTile(out_size, in_size, rpu_config=None, bias=False, in_trans=False, out_trans=False)[source]
Bases:
TileModule
,InferenceTileWithPeriphery
,SimulatorTileWrapper
InferenceTile using a torch-based simulator tile (and not a tile from RPUCuda).
- Parameters:
out_size (int) –
in_size (int) –
rpu_config (TorchInferenceRPUConfig | None) –
bias (bool) –
in_trans (bool) –
out_trans (bool) –
- forward(x_input, tensor_view=None)[source]
Torch forward function that calls the analog forward
- Parameters:
x_input (Tensor) –
tensor_view (Tuple | None) –
- Return type:
Tensor
- get_forward_parameters()[source]
Get the additional parameters generated for the forward pass.
- Returns:
Dictionary of the forward parameters set.
- Return type:
Dict[str, Tensor]
- init_input_processing()[source]
Helper function to initialize the input processing.
Note
This method is called from the constructor.
- Returns:
whether input processing is enabled
- Return type:
bool
- Raises: ConfigError in case
manage_output_clipping
is enabled but not supported.
- post_update_step()[source]
Clip and remap weights after weights have been updated.
- Return type:
None
- pre_forward(x_input, dim, is_test=False, ctx=None)[source]
Operations before the actual forward step for pre processing.
By default, this is an no-op. However, it could be overridden in derived tile classes.
- Parameters:
x_input (Tensor) – input tensor for the analog MVM of the tile.
dim (int) – input channel dimension, ie the x_size dimension
is_test (bool) – whether in eval mode
ctx (Any | None) – torch auto-grad context [Optional]
- Returns:
Output tensor of the same shape
- Return type:
Tensor
- set_forward_parameters(dic=None, **kwargs)[source]
Set the additional parameters generated for the forward pass.
Currently only
out_noise_values
is implemented.- Parameters:
dic (Dict[str, Tensor] | None) – dictionary of parameters to set (from
get_forward_parameter()
)kwargs (Dict[str, Tensor]) – parameter names can alternatively given directly as keywords
- Raises:
ArgumentError – If size are mismatched or keyword unknown
- Return type:
None
- set_scales(scales)[source]
Set all scales with a new scale.
This will set the mapping scales to
scales
and set all other scales to 1.- Parameters:
scales (Tensor) – scales to set.
- Return type:
None
- supports_ddp: bool = True
- supports_indexed: bool = False