xbtorch.deployment.metrics

Error computation and weight encoding utilities for crossbar simulation.

This module provides functions to evaluate how well software weight matrices are represented on simulated crossbar arrays. It includes tools to compute mapping errors between software weights and their corresponding hardware conductance matrices, as well as utilities for reading and processing encoded weight matrices.

Functions

  • compute_error() : Compute hardware mapping errors for all mapped layers in a model.

  • error_mapping() : Compute the error between a software weight matrix and its crossbar representation.

Notes

Accurate error computation is essential for validating hardware-aware training and mapping strategies. The module assumes that layers have predefined array mappings and that the accelerator supports reading conductance values.

Functions

compute_error(model)

Compute mapping errors for all layers of a model that have crossbar mappings.

error_mapping(accelerator, sw_weight[, ...])

Compute the error between a software weight matrix and its crossbar representation.

xbtorch.deployment.metrics.compute_error(model)[source]

Compute mapping errors for all layers of a model that have crossbar mappings.

Iterates over the layers of the model and computes the percentage error between the software weight matrices and their corresponding mapped conductance matrices on the hardware accelerator.

Parameters:

model (object) – The model object containing layers with potential crossbar mappings. Each layer must have a _array_mappings attribute with keys: ‘Gpos’, ‘Gneg’, and ‘output_polling_mode’.

Returns:

  • List of errors (percentage) for each mapped layer.

  • List of pairs [cmapped, cideal] for each layer, where cmapped is the crossbar-mapped matrix and cideal is the original software weight matrix.

Return type:

tuple[list[float], list[list[np.ndarray]]]

xbtorch.deployment.metrics.error_mapping(accelerator, sw_weight, pos_idxs=[], neg_idxs=[], pos_matrices=None, neg_matrices=None, polling_mode='avg')[source]

Compute the error between a software weight matrix and its crossbar representation.

Converts a software weight matrix into one or more conductance matrices using positive and negative subarrays, then calculates the normalized error compared to the ideal software weight.

Parameters:
  • accelerator (GenericAccelerator) – The accelerator instance containing chip dimensions and read/write methods.

  • sw_weight (torch.Tensor) – The software weight matrix to encode.

  • pos_idxs (list[tuple[int, int]], optional) – List of starting indices for positive conductance subarrays (default: []).

  • neg_idxs (list[tuple[int, int]], optional) – List of starting indices for negative conductance subarrays (default: []).

  • pos_matrices (list[np.ndarray], optional) – Precomputed positive conductance matrices (default: None).

  • neg_matrices (list[np.ndarray], optional) – Precomputed negative conductance matrices (default: None).

  • polling_mode (str, optional) – Method to combine subarray outputs: ‘avg’ for averaging, ‘sum’ for summation (default: ‘avg’).

Returns:

  • List of percentage errors for each conductance matrix mapping.

  • List of [cmapped, cideal] pairs for each mapping.

Return type:

tuple[list[float], list[list[np.ndarray]]]

Notes

  • gnorm is computed as g_max - g_min of the accelerator.

  • The final mapped matrix is normalized and compared to the ideal software weight.

  • Supports both precomputed matrices and reading directly from the accelerator.