{"record":{"id":"bc5c2fe404a8f9fa","repo":"Stability-AI/generative-models","slug":"notimplementederror-bc5c2f","errorCode":null,"errorMessage":"NotImplementedError","messagePattern":"NotImplementedError","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"sgm/modules/distributions/distributions.py","lineNumber":7,"sourceCode":"import numpy as np\nimport torch\n\n\nclass AbstractDistribution:\n    def sample(self):\n        raise NotImplementedError()\n\n    def mode(self):\n        raise NotImplementedError()\n\n\nclass DiracDistribution(AbstractDistribution):\n    def __init__(self, value):\n        self.value = value\n\n    def sample(self):\n        return self.value\n\n    def mode(self):\n        return self.value\n\n\nclass DiagonalGaussianDistribution(object):\n    def __init__(self, parameters, deterministic=False):","sourceCodeStart":1,"sourceCodeEnd":25,"githubUrl":"https://github.com/Stability-AI/generative-models/blob/e8cd657656fa5d61688191730d0e03242bf4ed44/sgm/modules/distributions/distributions.py#L1-L25","documentation":"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.","triggerScenarios":"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).","commonSituations":"Creating a new distribution subclass without implementing sample(), refactoring that changed which class encode() returns, or unit tests instantiating the base class directly.","solutions":["Use a concrete subclass (e.g. DiagonalGaussianDistribution) and call .sample() or .mode() on it.","If you wrote a subclass, implement sample(self) (and mode) to return a tensor draw.","Check what first_stage_model.encode() returns and ensure it wraps the distribution in the intended concrete class."],"exampleFix":"// before\nclass MyDist(AbstractDistribution):\n    pass\nMyDist().sample()  # NotImplementedError\n// after\nclass MyDist(AbstractDistribution):\n    def sample(self):\n        return torch.randn_like(self.params)\n    def mode(self):\n        return self.mean","handlingStrategy":"type-guard","validationCode":"dist = first_stage_model.encode(x)\nif isinstance(dist, AbstractDistribution) and type(dist) is AbstractDistribution:\n    raise TypeError(\"Got abstract AbstractDistribution; use a concrete subclass\")","typeGuard":"def is_concrete_distribution(d) -> bool:\n    return isinstance(d, AbstractDistribution) and type(d) is not AbstractDistribution \\\n        and callable(getattr(d, \"sample\", None)) and type(d).sample is not AbstractDistribution.sample","tryCatchPattern":"try:\n    samples = dist.sample()\nexcept NotImplementedError:\n    logger.error(\"Distribution subclass does not implement sample(): %s\", type(dist).__name__)\n    raise","preventionTips":["Never instantiate AbstractDistribution directly; always use DiagonalGaussianDistribution etc.","When writing subclasses, implement both sample() and mode().","Add a unit test asserting encode() returns a concrete distribution type."],"tags":["pytorch","notimplementederror","abstract-class"],"backgroundTag":"abstract-method-not-implemented","analyzedSha":"e8cd657656fa5d61688191730d0e03242bf4ed44","analyzedAt":"2026-08-29T11:23:43.234Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}