{"record":{"id":"ac5b68eddc82d846","repo":"Lightning-AI/pytorch-lightning","slug":"materialization-requires-that-the-type-module","errorCode":null,"errorMessage":"Materialization requires that the `{type(module).__name__}.reset_parameters` method is implemented. This method is used to initialize any children parameters or buffers in this module.","messagePattern":"Materialization requires that the `(.+?)\\.reset_parameters` method is implemented\\. This method is used to initialize any children parameters or buffers in this module\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/utilities/init.py","lineNumber":66,"sourceCode":"        types: Sequence,\n        args: Sequence[Any] = (),\n        kwargs: Optional[dict] = None,\n    ) -> Any:\n        kwargs = kwargs or {}\n        if not self.enabled:\n            return func(*args, **kwargs)\n        if getattr(func, \"__module__\", None) == \"torch.nn.init\":\n            if \"tensor\" in kwargs:\n                return kwargs[\"tensor\"]\n            return args[0]\n        return func(*args, **kwargs)\n\n\ndef _materialize(module: Module, device: _DEVICE) -> None:\n    \"\"\"Materialize a module.\"\"\"\n    module.to_empty(device=device, recurse=False)\n    if not hasattr(module, \"reset_parameters\"):\n        raise TypeError(\n            f\"Materialization requires that the `{type(module).__name__}.reset_parameters` method is implemented.\"\n            \" This method is used to initialize any children parameters or buffers in this module.\"\n        )\n    if callable(module.reset_parameters):\n        module.reset_parameters()\n\n\ndef _materialize_meta_tensors(module: Module, device: _DEVICE) -> None:\n    \"\"\"Materialize all tensors in a given module.\"\"\"\n    for module in module.modules():\n        if _has_meta_device_parameters_or_buffers(module, recurse=False):\n            _materialize(module, device)\n\n\ndef _materialize_distributed_module(module: Module, device: torch.device) -> None:\n    # Reference: https://github.com/pytorch/torchtitan/blob/main/docs/fsdp.md#meta-device-initialization\n    # TODO: Introduce `Fabric.materialize(module)` to give user control when materialization should happen\n    # TODO: Make `torchmetrics.Metric` compatible with the `to_empty()` + `reset_parameters()` semantics","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/utilities/init.py#L48-L84","documentation":"When moving a module off the meta device, Lightning calls `to_empty` and then relies on `reset_parameters()` to re-initialize the newly allocated (empty) tensors. If the module (or the object you passed to `Fabric.setup`/`init_module`) does not implement `reset_parameters`, there is no way to initialize the parameters, so Lightning raises a TypeError. Custom modules created on the meta device must implement this method.","triggerScenarios":"Using `Fabric`/`Trainer` with a device that materializes modules lazily (meta device init, `init_module`, or FSDP/`materialize_distributed_module`) where the user's custom `nn.Module` subclasses something without `reset_parameters` (e.g. a plain Module wrapper) and does not define it itself.","commonSituations":"Custom model classes used with `with fabric.init_module():` on GPU/meta-device workflows; wrapping pretrained models that lack `reset_parameters`; upgrading Lightning to versions where meta-device init became the default path.","solutions":["Implement `reset_parameters(self)` on your custom module that re-initializes parameters/buffers (e.g. call `nn.init` functions or children's `reset_parameters`)","If the weights come from a checkpoint, implement a no-op or loading `reset_parameters` and load weights after materialization","Avoid meta-device initialization (skip `init_module`/`to_empty`) if you cannot modify the module"],"exampleFix":"// before\nclass MyModel(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.lin = nn.Linear(4, 4)\n\n// after\nclass MyModel(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.lin = nn.Linear(4, 4)\n\n    def reset_parameters(self) -> None:\n        self.lin.reset_parameters()","handlingStrategy":"type-guard","validationCode":"hasattr(model, \"reset_parameters\") and callable(model.reset_parameters)","typeGuard":"def supports_materialization(m) -> bool:\n    return isinstance(m, torch.nn.Module) and callable(getattr(m, \"reset_parameters\", None))","tryCatchPattern":null,"preventionTips":["Implement reset_parameters on every custom module used with init_module/meta-device","Add a startup check that all meta-initialized modules have reset_parameters"],"tags":["lightning","meta-device","model-initialization"],"backgroundTag":"missing-interface-method","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}