Skip to content

dew.nn.backbones.edm2

EDM2’s magnitude-preserving U-Net.

The architecture is Figure 21 of Karras et al. 2024. The code follows NVlabs/edm2’s training/networks_edm2.py UNet, channels last, built from dew.nn.mp: every convolution and dense layer is an MPConv, the time features are MPFourier, branches join with mp_sum and skips with mp_cat, and there are no biases or gains. Train it under the edm preset with OptimConfig.forced_weight_normalization. In place of the one-hot class label, it embeds the mask-weighted mean of the textcontext states at unit magnitude through the same dense layer.

NameSummary
BlockRuns one encoder or decoder block, with optional self-attention.
EDM2UNetRuns EDM2’s U-Net.

Flax module source

class Block(
flavor: str = 'enc',
resample: str = 'keep',
attention: bool = False,
channels_per_head: int = 64,
dropout: float = 0.0,
res_balance: float = 0.3,
attn_balance: float = 0.3,
clip_act: float | None = 256.0,
attention_impl: AttentionImpl = 'auto',
)

Runs one encoder or decoder block, with optional self-attention.

def spatial_shape(shape: tuple[int, ...]) -> tuple[int, ...]
def model_flops(lengths, input_features: int, emb_features: int) -> float

The block’s residual, time and skip maps and optional spatial attention.

Flax module source

class EDM2UNet(
output_channels: int = 3,
model_channels: int = 192,
channel_mult: Sequence[int] = (1, 2, 3, 4),
channel_mult_noise: int | None = None,
channel_mult_emb: int | None = None,
num_blocks: int = 3,
attn_resolutions: Sequence[int] = (16, 8),
label_balance: float = 0.5,
concat_balance: float = 0.5,
channels_per_head: int = 64,
dropout: float = 0.0,
res_balance: float = 0.3,
attn_balance: float = 0.3,
clip_act: float | None = 256.0,
dtype: Dtype | None = None,
attention_impl: AttentionImpl = 'auto',
)

Runs EDM2’s U-Net.

The fields are the reference’s, and output_channels is its img_channels. attn_resolutions are feature-map sizes, compared with the input’s height halved once per level.

def condition_lengths(lengths: np.ndarray, spans: np.ndarray) -> np.ndarray
def model_flops(lengths) -> float
def spatial_flops(
sample_shape: tuple[int, ...],
rows: int,
context_lengths=None,
text_features: int | None = None,
) -> float

Count the shared magnitude-preserving stage walk on spatial shapes.

def spatial_walk(x, emb, train, operations)