neurobench.benchmarks
Benchmark
- class neurobench.benchmarks.Benchmark(model: NeuroBenchModel, dataloader: torch.utils.data.DataLoader | None, preprocessors: List[NeuroBenchPreProcessor | Callable[[Tuple[torch.Tensor, torch.Tensor]], Tuple[torch.Tensor, torch.Tensor]]] | None, postprocessors: List[NeuroBenchPostProcessor | Callable[[torch.Tensor], torch.Tensor]] | None, metric_list: List[List[Type[StaticMetric | WorkloadMetric]]])[source]
Bases:
objectTop-level benchmark class for running benchmarks.
- __init__(model: NeuroBenchModel, dataloader: torch.utils.data.DataLoader | None, preprocessors: List[NeuroBenchPreProcessor | Callable[[Tuple[torch.Tensor, torch.Tensor]], Tuple[torch.Tensor, torch.Tensor]]] | None, postprocessors: List[NeuroBenchPostProcessor | Callable[[torch.Tensor], torch.Tensor]] | None, metric_list: List[List[Type[StaticMetric | WorkloadMetric]]])[source]
- Parameters:
model – A NeuroBenchModel.
dataloader – A PyTorch DataLoader.
preprocessors – A list of NeuroBenchPreProcessors or callable functions (e.g. lambda) with matching interfaces.
postprocessors – A list of NeuroBenchPostProcessors or callable functions (e.g. lambda) with matching interfaces.
metric_list – A list of lists of StaticMetric and WorkloadMetric classes of metrics to run. First item is StaticMetrics, second item is WorkloadMetrics.
- run(quiet: bool = False, verbose: bool = False, dataloader: torch.utils.data.DataLoader | None = None, preprocessors: NeuroBenchPreProcessor | Callable[[Tuple[torch.Tensor, torch.Tensor]], Tuple[torch.Tensor, torch.Tensor]] | None = None, postprocessors: NeuroBenchPostProcessor | Callable[[torch.Tensor], torch.Tensor] | None = None, device: str | None = None) Dict[str, Any][source]
Runs batched evaluation of the benchmark.
- Parameters:
dataloader (Optional) – override DataLoader for this run.
preprocessors (Optional) – override preprocessors for this run.
postprocessors (Optional) – override postprocessors for this run.
quiet (bool, default=False) – If True, output is suppressed.
verbose (bool, default=False) – If True, metrics for each bach will be printed. If False (default), metrics are accumulated and printed after all batches are processed.
device (Optional) – use device for this run (e.g. ‘cuda’ or ‘cpu’).
- Returns:
A dictionary of results.
- Return type:
Dict[str, Any]
- save_benchmark_results(file_path: str, file_format: Literal['json', 'csv', 'txt'] = 'json') None[source]
Save benchmark results to a specified file in the chosen format.
- Parameters:
file_path (str) – Path to the output file (excluding the extension). The method automatically appends the appropriate extension based on the chosen file format.
file_format (Literal["json", "csv", "txt"], default="json") –
The format in which the results should be saved. Supported formats:
”json”: Saves the results as a JSON file with formatted indentation.
”csv”: Saves the results as a CSV file with keys as headers and values as the first row.
”txt”: Saves the results as a plain text file with one key-value pair per line.
- Raises:
ValueError – If the provided file_format is not one of the supported formats (“json”, “csv”, “txt”).
- to_nir(dummy_input: torch.Tensor, filename: str, **kwargs) None[source]
Exports the model to the NIR (Neural Intermediate Representation) format.
- Parameters:
dummy_input (torch.Tensor) – A sample input tensor that matches the input shape of the model. This is required for tracing the model during export.
filename (str) – The file path where the exported NIR file will be saved.
**kwargs – Additional keyword arguments passed to the export_to_nir function for customization during the export process.
- Raises:
ValueError – If the installed version of snntorch is less than 0.9.0.
- to_onnx(dummy_input: torch.Tensor, filename: str, **kwargs) None[source]
Exports the model to the ONNX (Open Neural Network Exchange) format.
- Parameters:
dummy_input (torch.Tensor) – A sample input tensor that matches the input shape of the model. This tensor is required for tracing the model during the export process.
filename (str) – The file path where the ONNX model will be saved, including the .onnx extension.
**kwargs – Additional keyword arguments passed to the torch.onnx.export function for customization during the export process.
- Raises:
RuntimeError – If an error occurs during the ONNX export process.
Workload Metrics
- class neurobench.metrics.workload.ActivationSparsity[source]
Bases:
WorkloadMetricSparsity of model activations.
Calculated as the number of zero activations over the total number of activations, over all layers, timesteps, samples in data.
- class neurobench.metrics.workload.ActivationSparsityByLayer[source]
Bases:
AccumulatedMetricSparsity layer-wise of model activations.
Calculated as the number of zero activations over the number of activations layer by layer, over all timesteps, samples in data.
- class neurobench.metrics.workload.ClassificationAccuracy[source]
Bases:
WorkloadMetricClassification accuracy of the model predictions.
- class neurobench.metrics.workload.CocoMap[source]
Bases:
AccumulatedMetricCOCO mean average precision.
- Measured for event data based on Perot2020, Supplementary B (https://arxiv.org/abs/2009.13436)
Skips first 0.5s of each sequence
Bounding boxes with diagonal size smaller than 60 pixels are ignored
- __call__(model, preds, data)[source]
Accumulate predictions and ground truth detections over batches.
- Parameters:
model – A NeuroBenchModel.
preds – A tensor of model predictions.
data – A tuple of data and labels.
- Returns:
COCO mean average precision.
- Return type:
float
- __init__()[source]
Initialize the CocoMap metric.
- Raises:
ImportError – If the metavision_ml and metavision_sdk_core packages are not installed on a supported platform.
- class neurobench.metrics.workload.MSE[source]
Bases:
WorkloadMetricMean squared error of the model predictions.
- class neurobench.metrics.workload.MembraneUpdates[source]
Bases:
AccumulatedMetricMembrane potential updates metric.
This metric computes the number of membrane potential updates occurring during the forward pass of the model. The updates are tracked per neuron, per layer.
- __call__(model, preds, data)[source]
Accumulate the number of membrane updates for each model forward pass.
- Parameters:
model – A NeuroBenchModel.
preds – A tensor of model predictions.
data – A tuple of data and labels.
- Returns:
Number of membrane potential updates.
- Return type:
float
- class neurobench.metrics.workload.R2[source]
Bases:
AccumulatedMetricR2 Score of the model predictions.
Currently implemented for 2D output only.
- class neurobench.metrics.workload.SMAPE[source]
Bases:
WorkloadMetricSymmetric mean absolute percentage error of the model predictions.
- __call__(model, preds: torch.Tensor, data: torch.Tensor) float[source]
Compute symmetric mean absolute percentage error.
- Parameters:
model – A NeuroBenchModel.
preds – A tensor of model predictions.
data – A tuple of data and labels.
- Returns:
Symmetric mean absolute percentage error.
- Return type:
float
- class neurobench.metrics.workload.SynapticOperations[source]
Bases:
AccumulatedMetricNumber of synaptic operations.
This metric computes the number of Multiply-Accumulate operations (MACs) for Artificial Neural Networks (ANN) and Accumulation operations (ACs) for Spiking Neural Networks (SNN).
- __call__(model, preds, data)[source]
Accumulate the Multiply-Accumulate (MAC) operations or Accumulation (AC) operations during the forward pass.
This method accumulates the operations based on the model’s connections, and differentiates between ANN (MACs) and SNN (ACs) operations based on the spiking activity.
- Parameters:
model – A NeuroBenchModel.
preds – A tensor of model predictions.
data – A tuple of data and labels.
inputs – A tensor of model inputs.
- Returns:
Multiply-accumulates.
- Return type:
float
Static Metrics
- class neurobench.metrics.static.ConnectionSparsity[source]
Bases:
StaticMetricSparsity of model connections between layers.
Based on number of zeros in supported layers, other layers are not taken into account in the computation: Supported layers: Linear Conv1d, Conv2d, Conv3d RNN, RNNBase, RNNCell LSTM, LSTMBase, LSTMCell GRU, GRUBase, GRUCell
- class neurobench.metrics.static.Footprint[source]
Bases:
StaticMetricA metric that counts the memory footprint of a model.
- class neurobench.metrics.static.ParameterCount[source]
Bases:
StaticMetricA metric that counts the number of parameters in a model.