Stability-AI/generative-models · error · NotImplementedError

NotImplementedError

Error message

NotImplementedError

What it means

AbstractDistribution is an interface: its sample() (and mode()) deliberately raise NotImplementedError. This error means you instantiated or called the abstract base directly instead of a concrete subclass such as DiagonalGaussianDistribution or DiracDistribution.

Source

Thrown at sgm/modules/distributions/distributions.py:7

import numpy as np
import torch


class AbstractDistribution:
    def sample(self):
        raise NotImplementedError()

    def mode(self):
        raise NotImplementedError()


class DiracDistribution(AbstractDistribution):
    def __init__(self, value):
        self.value = value

    def sample(self):
        return self.value

    def mode(self):
        return self.value


class DiagonalGaussianDistribution(object):
    def __init__(self, parameters, deterministic=False):

View on GitHub (pinned to e8cd657656)

Solutions

  1. Use a concrete subclass (e.g. DiagonalGaussianDistribution) and call .sample() or .mode() on it.
  2. If you wrote a subclass, implement sample(self) (and mode) to return a tensor draw.
  3. Check what first_stage_model.encode() returns and ensure it wraps the distribution in the intended concrete class.

Example fix

// before
class MyDist(AbstractDistribution):
    pass
MyDist().sample()  # NotImplementedError
// after
class MyDist(AbstractDistribution):
    def sample(self):
        return torch.randn_like(self.params)
    def mode(self):
        return self.mean
Defensive patterns

Strategy: type-guard

Validate before calling

dist = first_stage_model.encode(x)
if isinstance(dist, AbstractDistribution) and type(dist) is AbstractDistribution:
    raise TypeError("Got abstract AbstractDistribution; use a concrete subclass")

Type guard

def is_concrete_distribution(d) -> bool:
    return isinstance(d, AbstractDistribution) and type(d) is not AbstractDistribution \
        and callable(getattr(d, "sample", None)) and type(d).sample is not AbstractDistribution.sample

Try / catch

try:
    samples = dist.sample()
except NotImplementedError:
    logger.error("Distribution subclass does not implement sample(): %s", type(dist).__name__)
    raise

Prevention

When it happens

Trigger: Calling .sample() on an AbstractDistribution instance, or on a custom subclass that forgot to override sample(), or on first_stage_model's distribution when the subclass wiring is broken (e.g. encode returns the base class).

Common situations: Creating a new distribution subclass without implementing sample(), refactoring that changed which class encode() returns, or unit tests instantiating the base class directly.

Related errors


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