evox.problems.neuroevolution#

Submodules#

Package Contents#

Classes#

VirtualLoRAProblem

Virtual LoRA-based neuroevolution problem.

API#

class evox.problems.neuroevolution.VirtualLoRAProblem(model: torch.nn.Sequential, data_loader: torch.utils.data.DataLoader, criterion: torch.nn.Module, lora_rank: int, n_batch_per_eval: int = 1, device: torch.device | None = None, reduction: str = 'mean')[source]#

Bases: evox.core.Problem

Virtual LoRA-based neuroevolution problem.

Evaluates a population of LoRA-perturbed neural networks without materializing the full perturbed population. For each individual, the weight perturbation is generated on-demand from a seed using the Philox PRNG + LoRA low-rank factorization.

Instead of receiving a full (pop_size, dim) population, the

Meth:

evaluate method receives a tuple (center_flat, seeds, sigma) and applies LoRA perturbations layer-by-layer during the forward pass. Activations are kept at (pop_size, batch, features) — far smaller than the full perturbed population (pop_size, total_params).

Supports nn.Sequential models containing nn.Linear layers and common activation functions (ReLU, Tanh, Sigmoid, GELU, LeakyReLU, ELU, Softmax, Identity).

Warning

This problem does NOT support HPO wrapper (problems.hpo_wrapper.HPOProblemWrapper).

Initialization

Initialize the VirtualLoRAProblem.

Parameters:
  • model – The neural network model. Must be an nn.Sequential whose children are nn.Linear layers and/or supported activation modules.

  • data_loader – The data loader providing the dataset for evaluation.

  • criterion – The loss function used to evaluate the parameters’ performance. Use reduction='none' so that per-sample losses are returned and this problem handles the aggregation. If a scalar criterion is provided, a warning is emitted.

  • lora_rank – The LoRA rank r used for the low-rank perturbation factorization.

  • n_batch_per_eval – The number of batches to be evaluated in a single evaluation. When set to -1, will go through the whole dataset. Defaults to 1.

  • device – The device to run the computations on. Defaults to the current default device.

  • reduction – The reduction method for aggregating per-sample losses across the batch (and across multiple batches). 'mean' | 'sum'. Defaults to 'mean'.

_virtual_forward(inputs: torch.Tensor, center_params: Dict[str, torch.nn.Parameter], seeds: torch.Tensor, sigma: float, pop_size: int) torch.Tensor[source]#

Layer-by-layer forward pass with on-demand LoRA perturbations.

For each layer:

  • nn.Linear: compute the base output (center weight) plus the LoRA delta (from per-individual low-rank factors). Both weight and bias are perturbed. The weight delta uses the low-rank factorization sigma * (h @ A^T) @ B^T (via :func:lora_delta_output), which avoids materializing the (out_features, in_features) weight delta. The bias delta is direct Gaussian noise (1-D weight).

  • Activation modules: applied element-wise.

Activations are (pop_size, batch, features) — much smaller than the full perturbed population (pop_size, total_params).

Parameters:
  • inputs – Input batch of shape (batch, in_features).

  • center_params – Parameter dict of the center (un-perturbed) model.

  • seeds – 1-D int64 tensor of per-individual seeds (pop_size,).

  • sigma – Noise standard deviation.

  • pop_size – The population size (number of individuals).

Returns:

Output activations of shape (pop_size, batch, out_features).

evaluate(payload) torch.Tensor[source]#

Evaluate the fitness of a virtual LoRA-perturbed population.

Parameters:

payload

A tuple (center_flat, seeds, sigma) where:

  • center_flat: (dim,) float32 tensor — flat center vector.

  • seeds: (pop_size,) int64 tensor — per-individual seeds.

  • sigma: Python float — noise standard deviation.

Returns:

A tensor of shape (pop_size,) containing the fitness of each individual.