AUTOMATIC1111/stable-diffusion-webui · error · KeyError

Key {weight_init} is not defined as initialization!

Error message

Key {weight_init} is not defined as initialization!

What it means

During HypernetworkModule weight initialization, weight_init is matched against the literals 'Zero', 'XavierUniform', 'XavierNormal', 'KaimingUniform', 'KaimingNormal'; any other value falls through to raise KeyError. It indicates an unrecognized weight initialization scheme string in the hypernetwork's training parameters.

Source

Thrown at modules/hypernetworks/hypernetwork.py:97

                if type(layer) == torch.nn.Linear or type(layer) == torch.nn.LayerNorm:
                    w, b = layer.weight.data, layer.bias.data
                    if weight_init == "Normal" or type(layer) == torch.nn.LayerNorm:
                        normal_(w, mean=0.0, std=0.01)
                        normal_(b, mean=0.0, std=0)
                    elif weight_init == 'XavierUniform':
                        xavier_uniform_(w)
                        zeros_(b)
                    elif weight_init == 'XavierNormal':
                        xavier_normal_(w)
                        zeros_(b)
                    elif weight_init == 'KaimingUniform':
                        kaiming_uniform_(w, nonlinearity='leaky_relu' if 'leakyrelu' == activation_func else 'relu')
                        zeros_(b)
                    elif weight_init == 'KaimingNormal':
                        kaiming_normal_(w, nonlinearity='leaky_relu' if 'leakyrelu' == activation_func else 'relu')
                        zeros_(b)
                    else:
                        raise KeyError(f"Key {weight_init} is not defined as initialization!")
        devices.torch_npu_set_device()
        self.to(devices.device)

    def fix_old_state_dict(self, state_dict):
        changes = {
            'linear1.bias': 'linear.0.bias',
            'linear1.weight': 'linear.0.weight',
            'linear2.bias': 'linear.1.bias',
            'linear2.weight': 'linear.1.weight',
        }

        for fr, to in changes.items():
            x = state_dict.get(fr, None)
            if x is None:
                continue

            del state_dict[fr]
            state_dict[to] = x

View on GitHub (pinned to 82a973c043)

Solutions

  1. Use exactly one of: 'Zero', 'XavierUniform', 'XavierNormal', 'KaimingUniform', 'KaimingNormal'.
  2. If you need another initializer, select the closest supported one and re-initialize manually after module creation.
  3. Audit any template/state used to build the hypernetwork for a stray weight_init value before re-running.

Example fix

# before
weight_init = 'xavier_uniform'  # KeyError

# after
weight_init = 'XavierUniform'   # exact supported literal
Defensive patterns

Strategy: validation

Validate before calling

SUPPORTED_INITS = {'Zero', 'XavierUniform', 'XavierNormal', 'KaimingUniform', 'KaimingNormal'}

if weight_init not in SUPPORTED_INITS:
    raise ValueError(f'weight_init must be one of {sorted(SUPPORTED_INITS)}, got {weight_init!r}')

Type guard

def is_supported_weight_init(name: str) -> bool:
    return name in {'Zero', 'XavierUniform', 'XavierNormal', 'KaimingUniform', 'KaimingNormal'}

Try / catch

try:
    module = HypernetworkModule(..., weight_init=weight_init)
except KeyError as e:
    if 'not defined as initialization' in str(e):
        weight_init = 'KaimingUniform'  # safe default; log the substitution
        module = HypernetworkModule(..., weight_init=weight_init)
    else:
        raise

Prevention

When it happens

Trigger: Creating/training a hypernetwork whose weight_init parameter is not one of the five supported names — e.g. 'orthogonal', 'xavier_uniform' (underscored), or a mis-typed 'Kaiming Unifom'.

Common situations: API/script calls that pass a PyTorch-style initializer name instead of the webui's label; hypernetworks saved by newer forks that added initializer aliases; case/copy-paste mistakes from the UI dropdown.

Related errors


AI-assisted analysis of AUTOMATIC1111/stable-diffusion-webui@82a973c043 (2026-08-14). Data as JSON: /api/errors/73c9943b9eb2648e. Report an issue: GitHub.