Stability-AI/generative-models · error · NotImplementedError

NotImplementedError

Error message

NotImplementedError

What it means

load_model_from_config only knows two checkpoint formats: legacy .ckpt files (torch pickles with a 'state_dict' key) and .safetensors files. Anything else (or an unsupported file type) hits the else branch and raises a bare NotImplementedError instead of a descriptive error.

Source

Thrown at sgm/util.py:212

    dims_to_append = target_dims - x.ndim
    if dims_to_append < 0:
        raise ValueError(
            f"input has {x.ndim} dims but target_dims is {target_dims}, which is less"
        )
    return x[(...,) + (None,) * dims_to_append]


def load_model_from_config(config, ckpt, verbose=True, freeze=True):
    print(f"Loading model from {ckpt}")
    if ckpt.endswith("ckpt"):
        pl_sd = torch.load(ckpt, map_location="cpu")
        if "global_step" in pl_sd:
            print(f"Global Step: {pl_sd['global_step']}")
        sd = pl_sd["state_dict"]
    elif ckpt.endswith("safetensors"):
        sd = load_safetensors(ckpt)
    else:
        raise NotImplementedError

    model = instantiate_from_config(config.model)

    m, u = model.load_state_dict(sd, strict=False)

    if len(m) > 0 and verbose:
        print("missing keys:")
        print(m)
    if len(u) > 0 and verbose:
        print("unexpected keys:")
        print(u)

    if freeze:
        for param in model.parameters():
            param.requires_grad = False

    model.eval()
    return model

View on GitHub (pinned to e8cd657656)

Solutions

  1. Convert/rename the checkpoint so the path ends with '.safetensors' and load it with safetensors.torch.load_file
  2. If it's a plain state_dict (.pt/.pth), load it yourself with torch.load and pass model.load_state_dict(sd, strict=False) manually
  3. Verify the file path/extension is correct and the file exists

Example fix

// before
model = load_model_from_config(config, "model.bin")  # NotImplementedError
// after
from safetensors.torch import load_file
sd = load_file("model.safetensors")
model = instantiate_from_config(config.model)
model.load_state_dict(sd, strict=False)
Defensive patterns

Strategy: validation

Validate before calling

import os
p = "model.safetensors"
assert os.path.isfile(p), f"checkpoint not found: {p}"
assert p.endswith("safetensors"), "only safetensors supported"

Type guard

def is_supported_ckpt(path: str) -> bool:
    return os.path.isfile(path) and (path.endswith("safetensors") or is_torch_ckpt(path))

Try / catch

try:
    model = load_model_from_config(config, ckpt)
except NotImplementedError:
    sd = safetensors.torch.load_file(ckpt)  # manual fallback
    model = instantiate_from_config(config.model)
    model.load_state_dict(sd, strict=False)

Prevention

When it happens

Trigger: Calling sgm.util.load_model_from_config(config, ckpt) where ckpt does not end with '.ckpt'-style torch-saved files handled above and does not end with 'safetensors' — e.g. a .pt/.pth/.bin file, a .safetensors path with wrong case, or a directory/URL path.

Common situations: Users downloading weights in .safetensors but passing a path with a different extension (e.g. '.safetensors.tmp'), passing HuggingFace .bin shards, or pointing at a diffusers-format folder rather than a single checkpoint file.

Related errors


AI-assisted analysis of Stability-AI/generative-models@e8cd657656 (2026-08-29). Data as JSON: /api/errors/0b23ef6de27d66ee. Report an issue: GitHub.