Lightning-AI/pytorch-lightning · error · ValueError

`max_depth` can be -1, 0 or > 0, got {max_depth}.

Error message

`max_depth` can be -1, 0 or > 0, got {max_depth}.

What it means

ModelSummary validates its max_depth argument: it must be an int and >= -1 (-1 means unlimited depth). Passing a non-int (e.g. float or bool misuse) or a value < -1 raises this ValueError at construction.

Source

Thrown at src/lightning/pytorch/utilities/model_summary/model_summary.py:217

        0 | net   | Sequential  | 132 K  | train | 2.6 M | [10, 256] | [10, 512]
        1 | net.0 | Linear      | 131 K  | train | 2.6 M | [10, 256] | [10, 512]
        2 | net.1 | BatchNorm1d | 1.0 K  | train | 0     | [10, 512] | [10, 512]
        ------------------------------------------------------------------------------
        132 K     Trainable params
        0         Non-trainable params
        132 K     Total params
        0.530     Total estimated model params size (MB)
        3         Modules in train mode
        0         Modules in eval mode
        2.6 M     Total Flops

    """

    def __init__(self, model: "pl.LightningModule", max_depth: int = 1) -> None:
        self._model = model

        if not isinstance(max_depth, int) or max_depth < -1:
            raise ValueError(f"`max_depth` can be -1, 0 or > 0, got {max_depth}.")

        # The max-depth needs to be plus one because the root module is already counted as depth 0.
        self._flop_counter = FlopCounterMode(display=False, depth=max_depth + 1)

        self._max_depth = max_depth
        self._layer_summary = self.summarize()
        # 1 byte -> 8 bits
        # TODO: how do we compute precision_megabytes in case of mixed precision?
        precision_to_bits = {
            "64": 64,
            "32": 32,
            "16": 16,
            "bf16": 16,
            "16-true": 16,
            "bf16-true": 16,
            "32-true": 32,
            "64-true": 64,
        }

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Pass an integer: use -1 for unlimited, 0 for none, or a positive int
  2. Coerce config values: int(max_depth)
  3. Validate depth >= -1 before constructing

Example fix

# before
summary = ModelSummary(model, max_depth=-2)
# after
summary = ModelSummary(model, max_depth=-1)
Defensive patterns

Strategy: validation

Validate before calling

def valid_depth(d):
    return isinstance(d, int) and not isinstance(d, bool) and d >= -1
assert valid_depth(max_depth), 'max_depth must be int >= -1'

Type guard

def is_valid_max_depth(d) -> bool:
    return isinstance(d, int) and not isinstance(d, bool) and d >= -1

Prevention

When it happens

Trigger: ModelSummary(model, max_depth=-2) or ModelSummary(model, max_depth=1.0); also Trainer(summary=...) misconfigured with a bad depth value.

Common situations: Computing max_depth dynamically (e.g. -len(layers) going below -1), passing a float from config files (YAML parses 1.0 as float).

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/76d903fc43ecc267. Report an issue: GitHub.