evox.triton_kernels.kernels.lora_noise¶
LoRA (Low-Rank Adaptation) noise utility functions.
These pure-PyTorch helpers build on :mod:~evox.triton_kernels.kernels.philox
to generate deterministic, non-overlapping random low-rank perturbation streams.
They are not Triton kernels themselves — they call
- func:
~evox.triton_kernels.kernels.philox.philox_normalinternally, which dispatches to Triton on CUDA and to the PyTorch fallback elsewhere.
The functions support a batched / population first dimension so that, given
pop_size seeds, one call produces pop_size independent LoRA factor
samples. This is useful for population-based (evolutionary) perturbation of
neural-network weights, where each individual gets its own low-rank delta.
Key ideas:
- func:
compute_counter_offsetspartitions the Philox counter space so that different parameter blocks (or A/B sub-factors within a block) draw from non-overlapping streams.
- func:
generate_lora_factorsproduces the per-individual low-rank factors(A, B)(or a flat noise vector for 1-D weights).
- func:
lora_delta_outputapplies the low-rank delta to an input without ever materializing the full(out_features, in_features)weight delta.
- func:
lora_gradientestimates the gradient of a population objective with respect to a weight, given per-individual fitness and factors.
Module Contents¶
Functions¶
Flatten a weight shape to |
|
Round |
|
Compute non-overlapping Philox counter offsets for a list of parameter blocks. |
|
Generate deterministic LoRA factors for a batch of individuals. |
|
Apply a batched low-rank delta to an input without materializing the weight. |
|
Estimate the weight gradient from population fitness and LoRA factors. |
API¶
- evox.triton_kernels.kernels.lora_noise._flatten_to_2d(weight_shape: tuple) tuple[int, int][source]¶
Flatten a weight shape to
(d, k)wherekis the last dim.dis the product of all dimensions except the last;kis the last dimension. For a 1-D shape this returns(1, n)— but callers handle the 1-D case separately, so this is only invoked forlen >= 2.- Parameters:
weight_shape – The original weight tensor shape.
- Returns:
(d, k)whered = prod(shape[:-1])andk = shape[-1].
- evox.triton_kernels.kernels.lora_noise._ceil_div4(x: int) int[source]¶
Round
xup to the next multiple of 4 (Philox yields 4 values per call).- Parameters:
x – An element count.
- Returns:
ceil(x / 4) * 4.
- evox.triton_kernels.kernels.lora_noise.compute_counter_offsets(param_shapes: list[tuple], lora_rank: int) list[int][source]¶
Compute non-overlapping Philox counter offsets for a list of parameter blocks.
For each parameter block we compute how many Philox elements it consumes, then round up to a multiple of 4 (Philox produces 4 values per call). The offsets are cumulative so every sub-stream occupies a disjoint counter range.
1-D block
(n,): consumesnelements.2-D or >2-D block
(d, k)(flattened): consumeslora_rank * k(factorA) plusd * lora_rank(factorB).
- Parameters:
param_shapes – List of weight shapes (each a tuple of ints).
lora_rank – The LoRA rank
r.
- Returns:
A list of starting counter values, one per block (cumulative).
- evox.triton_kernels.kernels.lora_noise.generate_lora_factors(seeds: torch.Tensor, weight_shape: tuple, rank: int, counter: int) torch.Tensor | tuple[torch.Tensor, torch.Tensor][source]¶
Generate deterministic LoRA factors for a batch of individuals.
1-D weight
(n,): returns a flat noise tensor of shape(pop_size, n)drawn from :func:philox_normal.2-D / >2-D weight
(d, k)(flattened,d = prod(shape[:-1]),k = shape[-1]): returns a tuple(A, B)whereAhas shape(pop_size, rank, k)(the “down” projection),Bhas shape(pop_size, d, rank)(the “up” projection),
drawn from two non-overlapping Philox sub-streams. The product
B @ Areconstructs a low-rank delta of shape(d, k)per individual.
- Parameters:
seeds – 1-D
int64tensor of per-individual seeds.weight_shape – The target weight tensor shape.
rank – The LoRA rank
r.counter – Starting Philox counter for this block.
- Returns:
Flat noise tensor (1-D weight) or
(A, B)tuple (≥2-D weight).
- evox.triton_kernels.kernels.lora_noise.lora_delta_output(x: torch.Tensor, A: torch.Tensor, B: torch.Tensor, sigma: float) torch.Tensor[source]¶
Apply a batched low-rank delta to an input without materializing the weight.
Given a batched input
xand per-individual LoRA factors(A, B), computesigma * (x @ Aᵀ) @ Bᵀ. This is mathematically equivalent tosigma * x @ (B @ A)ᵀ(whereB @ Ais the low-rank weight delta) but avoids materializing the potentially huge(out_features, in_features)matrix.- Parameters:
x – Input tensor of shape
(pop_size, batch, in_features).A – Factor
Aof shape(pop_size, rank, in_features).B – Factor
Bof shape(pop_size, out_features, rank).sigma – Scaling factor applied to the delta.
- Returns:
Output of shape
(pop_size, batch, out_features).
- evox.triton_kernels.kernels.lora_noise.lora_gradient(fitness: torch.Tensor, A: torch.Tensor, B: torch.Tensor | None, pop_size: int, sigma: float, weight_shape: tuple) torch.Tensor[source]¶
Estimate the weight gradient from population fitness and LoRA factors.
This implements the population-based gradient estimate used in zeroth-order / evolution-strategies style optimization. For each individual
ithe perturbation applied to the weight issigma * delta_W_iwheredelta_W_i = B_i @ A_i(2-D) or the flat noise (1-D). The gradient is the fitness-weighted average of these perturbations, normalized bypop_size * sigma.1-D (
B is None):grad = (fitness / (pop_size * sigma)) @ noise, reshaped toweight_shape.2-D / >2-D:
grad = einsum('i,ijk,ikl->jl', fitness, B, A) / (pop_size * sigma), reshaped toweight_shape.
- Parameters:
fitness – 1-D tensor of per-individual fitness values
(pop_size,).A – For 2-D, factor
Aof shape(pop_size, rank, k); for 1-D, the flat noise of shape(pop_size, n).B – For 2-D, factor
Bof shape(pop_size, d, rank); for 1-D,None.pop_size – The population size (number of individuals).
sigma – The perturbation scaling factor used during evaluation.
weight_shape – The target weight tensor shape for the output gradient.
- Returns:
Gradient tensor reshaped to
weight_shape.