{"record":{"id":"dd1296a579d9edcc","repo":"Lightning-AI/pytorch-lightning","slug":"expected-torch-nn-module-or-torch-optim-optimiz","errorCode":null,"errorMessage":"Expected `torch.nn.Module` or `torch.optim.Optimizer`, got: {type(obj).__name__}","messagePattern":"Expected `torch\\.nn\\.Module` or `torch\\.optim\\.Optimizer`, got: (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/utilities/init.py","lineNumber":115,"sourceCode":"        else:\n            uninitialized_modules.add(type(submodule).__name__)\n\n    if uninitialized_modules:\n        rank_zero_warn(\n            \"Parameter initialization incomplete. The following modules have parameters or buffers with uninitialized\"\n            \" memory because they don't define a `reset_parameters()` method for re-initialization:\"\n            f\" {', '.join(uninitialized_modules)}\"\n        )\n\n\ndef _has_meta_device_parameters_or_buffers(obj: Union[Module, Optimizer], recurse: bool = True) -> bool:\n    if isinstance(obj, Optimizer):\n        return any(\n            t.is_meta for param_group in obj.param_groups for t in param_group[\"params\"] if isinstance(t, Parameter)\n        )\n    if isinstance(obj, Module):\n        return any(t.is_meta for t in itertools.chain(obj.parameters(recurse=recurse), obj.buffers(recurse=recurse)))\n    raise TypeError(f\"Expected `torch.nn.Module` or `torch.optim.Optimizer`, got: {type(obj).__name__}\")\n","sourceCodeStart":97,"sourceCodeEnd":116,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/utilities/init.py#L97-L116","documentation":"Lightning's meta-tensor detection helper `_has_meta_device_parameters_or_buffers` only accepts `torch.nn.Module` or `torch.optim.Optimizer`. It inspects parameters/buffers (or optimizer param_groups) for tensors on the meta device to decide whether materialization is needed. Passing any other type (raw tensor, list, dict, custom object) to code paths like `setup`, `_validate_setup`, or `_materialize_meta_tensors` triggers this TypeError.","triggerScenarios":"Calling `fabric.setup(obj)`, `fabric.setup_module(obj)`, or `fabric.setup_optimizers(obj)` with something that is neither an nn.Module nor an Optimizer (e.g. a raw tensor, a tuple of models, a LightningModule where a bare attribute was passed, or a custom class).","commonSituations":"Passing `(model, optimizer)` unpacked incorrectly, passing dataloaders or raw tensors to setup, wrapping objects in a way Lightning can't introspect, version changes that made this check stricter.","solutions":["Pass only `torch.nn.Module` instances to `setup`/`setup_module` and `torch.optim.Optimizer` instances to `setup_optimizers`","If you have multiple objects, call setup on each individually or use the tuple form `fabric.setup(model, optimizer)` supported by the API","For raw tensors, move them with `.to(fabric.device)` instead of setup"],"exampleFix":"// before\nmodel = fabric.setup(model.parameters())  # not a Module\n\n// after\nmodel = fabric.setup(model)  # torch.nn.Module\noptimizer = fabric.setup_optimizers(optimizer)","handlingStrategy":"type-guard","validationCode":"assert isinstance(obj, (torch.nn.Module, torch.optim.Optimizer)), type(obj)","typeGuard":"def is_setuppable(obj) -> bool:\n    return isinstance(obj, (torch.nn.Module, torch.optim.Optimizer))","tryCatchPattern":"try:\n    fabric.setup(obj)\nexcept TypeError as e:\n    if \"Expected `torch.nn.Module`\" in str(e):\n        raise TypeError(f\"setup got unsupported object {type(obj)}\") from e\n    raise","preventionTips":["Pass Modules to setup/setup_module and Optimizers to setup_optimizers","Avoid passing raw tensors, lists, or dataloaders to setup"],"tags":["lightning","type-error","setup"],"backgroundTag":"unsupported-argument-type","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}