specsr.models.blocks¶
Building blocks shared across the SR1, ZHead and SR2 architectures.
Attribute names and numerical behaviour here are load-bearing: published checkpoints are keyed on them, so changing a submodule name breaks weight loading. Keep signatures stable and add new behaviour behind defaults.
Functions
|
Split parameters into decayed and non-decayed groups. |
|
Resolve an activation by name. |
|
Remove the smooth component, leaving small-scale structure. |
|
Largest |
|
Clamp |
|
Moving-average smooth along the last axis with reflect padding. |
Classes
|
Pre-activation 1D residual block with a scaled residual branch. |
- class specsr.models.blocks.ResidualBlock1D(*args, **kwargs)[source]¶
Bases:
ModulePre-activation 1D residual block with a scaled residual branch.
The residual is scaled by
alpharather than added at unit weight, which keeps activations well conditioned when many blocks are stacked.- forward(x)[source]¶
- Parameters:
x (torch.Tensor)
- Return type:
- specsr.models.blocks.get_activation(name)[source]¶
Resolve an activation by name.
eluis the Exponential Linear Unit:xforx > 0andalpha * (exp(x) - 1)otherwise. Unlike ReLU it has non-zero gradient for negative inputs, which avoids dead units, and its output mean sits closer to zero.- Parameters:
name (str)
- Return type:
- specsr.models.blocks.build_param_groups(model, lr, weight_decay)[source]¶
Split parameters into decayed and non-decayed groups.
Weight decay is applied only to multi-dimensional weight tensors. Biases and normalisation parameters are excluded, which is standard practice: decaying them tends to hurt without regularising anything meaningful.
- Parameters:
model (torch.nn.Module)
lr (float)
weight_decay (float)
- Return type:
- specsr.models.blocks.smooth1d(x, k=31)[source]¶
Moving-average smooth along the last axis with reflect padding.
kis forced odd and clipped to the sequence length, so the output always has the same length as the input.- Parameters:
x (torch.Tensor)
k (int)
- Return type:
- specsr.models.blocks.highpass(x, k=51)[source]¶
Remove the smooth component, leaving small-scale structure.
- Parameters:
x (torch.Tensor)
k (int)
- Return type:
- specsr.models.blocks.largest_divisor_at_most(channels, groups=8)[source]¶
Largest
g <= groupsthat divideschannelsexactly.GroupNormrequires the channel count to be divisible by the group count. Hyperparameter sweeps produce channel widths that are not multiples of 8 (e.g. 108), so the group count is chosen adaptively rather than fixed: 108 resolves to 6 groups (108/6 = 18) while 96 keeps 8 (96/8 = 12).
- specsr.models.blocks.odd_kernel(k, length)[source]¶
Clamp
kto an odd kernel size that fits in a sequence oflength.Reflect-padding by
k // 2on each side and pooling with stride 1 returns the input length only whenkis odd. The original code had two inconsistent versions of this clamp — one rounding evenkdown (k -= 1) and one rounding up (k | 1) — and the rounding-up variant could leavekeven after being clipped to the sequence length, silently returning a sequence one sample longer than the input.Both agree for odd
k, which is what every shipped config uses, so this did not affect published results. It is fixed here so it cannot.