Lightning-AI/pytorch-lightning · warning

The total number of parameters detected may be inaccurate be

Error message

The total number of parameters detected may be inaccurate because the model contains an instance of `UninitializedParameter`. To get an accurate number, set `self.example_input_array` in your LightningModule.

What it means

Lightning's ModelSummary counts parameters by iterating model tensors, but torch.nn.parameter.UninitializedParameter (used by LazyLinear/LazyConv layers and NNs with late-initialized weights) has no shape until a forward pass materializes it. The warning tells you the reported total/trainable parameter counts may be wrong and suggests providing example_input_array so Lightning can run a dry forward to initialize the lazy modules before counting. DTensor parameters are excluded because their shapes are known.

Source

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

    labels = PARAMETER_NUM_UNITS
    num_digits = int(math.floor(math.log10(number)) + 1 if number > 0 else 1)
    num_groups = int(math.ceil(num_digits / 3))
    num_groups = min(num_groups, len(labels))  # don't abbreviate beyond trillions
    shift = -3 * (num_groups - 1)
    number = number * (10**shift)
    index = num_groups - 1
    if index < 1 or number >= 100:
        return f"{int(number):,d} {labels[index]}"

    return f"{number:,.1f} {labels[index]}"


def _tensor_has_shape(p: Tensor) -> bool:
    from torch.nn.parameter import UninitializedParameter

    # DTensor is a subtype of `UninitializedParameter`, but the shape is known
    if isinstance(p, UninitializedParameter) and not _is_dtensor(p):
        warning_cache.warn(
            "The total number of parameters detected may be inaccurate because the model contains"
            " an instance of `UninitializedParameter`. To get an accurate number, set `self.example_input_array`"
            " in your LightningModule."
        )
        return True
    return False


def summarize(lightning_module: "pl.LightningModule", max_depth: int = 1) -> ModelSummary:
    """Summarize the LightningModule specified by `lightning_module`.

    Args:
        lightning_module: `LightningModule` to summarize.

        max_depth: The maximum depth of layer nesting that the summary will include. A value of 0 turns the
            layer summary off. Default: 1.

    Return:

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Set self.example_input_array = torch.rand(B, ...)` (matching real input shape) in your LightningModule so ModelSummary runs a forward and materializes lazy parameters before counting.
  2. Alternatively initialize the lazy modules yourself before summarizing: run a dummy batch through model.apply(lambda m: m.reset_parameters() if hasattr(m,'reset_parameters') else None) or a single forward pass, then build the summary.
  3. Replace nn.Lazy* layers with explicitly shaped layers once input dimensions are known, removing UninitializedParameter entirely.
  4. If exact counts don't matter (e.g. quick prototyping), ignore the warning — training will still initialize the parameters on the first real forward.

Example fix

# before
class MyModel(L.LightningModule):
    def __init__(self):
        super().__init__()
        self.net = nn.Sequential(nn.LazyLinear(128), nn.ReLU(), nn.LazyLinear(10))

# after
class MyModel(L.LightningModule):
    def __init__(self):
        super().__init__()
        self.net = nn.Sequential(nn.LazyLinear(128), nn.ReLU(), nn.LazyLinear(10))
        self.example_input_array = torch.randn(32, 1024)  # materializes lazy params for summary
Defensive patterns

Strategy: type-guard

Validate before calling

import torch.nn as nn

def model_has_uninitialized_params(model) -> bool:
    return any(
        isinstance(p, nn.UninitializedParameter) for p in model.parameters()
    )

if model_has_uninitialized_params(model):
    with torch.no_grad():
        model(torch.zeros(1, *input_shape))  # materialize lazy layers before summary

Type guard

def model_has_uninitialized_params(model: nn.Module) -> bool:
    import torch.nn.parameter as P
    return any(
        isinstance(p, P.UninitializedParameter) and not _is_dtensor(p)
        for p in model.parameters()
    )

Prevention

When it happens

Trigger: Instantiating ModelSummary / calling model.summary() (or trainer printing the summary at fit start, or accessing ModelSummary(model).total_parameters / trainable_parameters / average_shard_parameters) on a model containing lazy modules (nn.LazyLinear, nn.LazyConv2d, nn.LazyBatchNorm, or UninitializedParameter assigned manually) while self.example_input_array is None, so _tensor_has_shape flags the uninitialized parameter.

Common situations: Using nn.Lazy* layers to defer inferring input dimensions; models built for variable input shapes; FSDP/DeepSpeed setups where the summary is printed before initialization; reading total_parameters in tests and getting 0 or an unexpectedly tiny number.

Related errors


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