evox.problems.neuroevolution.virtual_lora_problem#

模块内容#

#

VirtualLoRAProblem

Virtual LoRA-based neuroevolution problem.

API#

class evox.problems.neuroevolution.virtual_lora_problem.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')[源代码]#

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).

警告

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

初始化

Initialize the VirtualLoRAProblem.

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

  • data_loader -- 用于评估的数据加载器提供数据集。

  • 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 -- 每次评估中要计算的批次数。当设置为 -1 时,将遍历整个数据集。默认值为 1。

  • 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[源代码]#

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).

参数:
  • 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).

返回:

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

evaluate(payload) torch.Tensor[源代码]#

Evaluate the fitness of a virtual LoRA-perturbed population.

参数:

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.

返回:

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