{"record":{"id":"9035c5699c513d32","repo":"Lightning-AI/pytorch-lightning","slug":"the-total-number-of-parameters-detected-may-be-ina","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/lightning/pytorch/utilities/model_summary/model_summary.py","lineNumber":528,"sourceCode":"    labels = PARAMETER_NUM_UNITS\n    num_digits = int(math.floor(math.log10(number)) + 1 if number > 0 else 1)\n    num_groups = int(math.ceil(num_digits / 3))\n    num_groups = min(num_groups, len(labels))  # don't abbreviate beyond trillions\n    shift = -3 * (num_groups - 1)\n    number = number * (10**shift)\n    index = num_groups - 1\n    if index < 1 or number >= 100:\n        return f\"{int(number):,d} {labels[index]}\"\n\n    return f\"{number:,.1f} {labels[index]}\"\n\n\ndef _tensor_has_shape(p: Tensor) -> bool:\n    from torch.nn.parameter import UninitializedParameter\n\n    # DTensor is a subtype of `UninitializedParameter`, but the shape is known\n    if isinstance(p, UninitializedParameter) and not _is_dtensor(p):\n        warning_cache.warn(\n            \"The total number of parameters detected may be inaccurate because the model contains\"\n            \" an instance of `UninitializedParameter`. To get an accurate number, set `self.example_input_array`\"\n            \" in your LightningModule.\"\n        )\n        return True\n    return False\n\n\ndef summarize(lightning_module: \"pl.LightningModule\", max_depth: int = 1) -> ModelSummary:\n    \"\"\"Summarize the LightningModule specified by `lightning_module`.\n\n    Args:\n        lightning_module: `LightningModule` to summarize.\n\n        max_depth: The maximum depth of layer nesting that the summary will include. A value of 0 turns the\n            layer summary off. Default: 1.\n\n    Return:","sourceCodeStart":510,"sourceCodeEnd":546,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/utilities/model_summary/model_summary.py#L510-L546","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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.","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.","Replace nn.Lazy* layers with explicitly shaped layers once input dimensions are known, removing UninitializedParameter entirely.","If exact counts don't matter (e.g. quick prototyping), ignore the warning — training will still initialize the parameters on the first real forward."],"exampleFix":"# before\nclass MyModel(L.LightningModule):\n    def __init__(self):\n        super().__init__()\n        self.net = nn.Sequential(nn.LazyLinear(128), nn.ReLU(), nn.LazyLinear(10))\n\n# after\nclass MyModel(L.LightningModule):\n    def __init__(self):\n        super().__init__()\n        self.net = nn.Sequential(nn.LazyLinear(128), nn.ReLU(), nn.LazyLinear(10))\n        self.example_input_array = torch.randn(32, 1024)  # materializes lazy params for summary","handlingStrategy":"type-guard","validationCode":"import torch.nn as nn\n\ndef model_has_uninitialized_params(model) -> bool:\n    return any(\n        isinstance(p, nn.UninitializedParameter) for p in model.parameters()\n    )\n\nif model_has_uninitialized_params(model):\n    with torch.no_grad():\n        model(torch.zeros(1, *input_shape))  # materialize lazy layers before summary","typeGuard":"def model_has_uninitialized_params(model: nn.Module) -> bool:\n    import torch.nn.parameter as P\n    return any(\n        isinstance(p, P.UninitializedParameter) and not _is_dtensor(p)\n        for p in model.parameters()\n    )","tryCatchPattern":null,"preventionTips":["Set self.example_input_array in every LightningModule that uses nn.Lazy* layers.","Run one dummy forward before calling model.summary() or reading total_parameters.","Prefer explicitly-shaped layers once input dimensions are fixed to avoid lazy state entirely."],"tags":["pytorch-lightning","model-summary","lazy-layers","uninitialized-parameter","parameter-counting"],"backgroundTag":"lazy-module-uninitialized-parameters","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}