Lightning-AI/pytorch-lightning · error · AttributeError
'{type(self).__name__}' object has no attribute '{name}'
Error message
'{type(self).__name__}' object has no attribute '{name}' What it means
This AttributeError comes from `_UnloadedTensor` (Lightning's lazy checkpoint loading via `_lazy_load`), which wraps tensors that live on disk in a checkpoint instead of memory. It forwards most attribute access to the underlying meta tensor or materializes the tensor for a few special names (`contiguous`, `cuda`, `half`, `data`, `to`), but any other tensor attribute/method on an unloaded tensor cannot be served and raises.
Source
Thrown at src/lightning/fabric/utilities/load.py:176
"grad_fn",
"is_meta",
"layout",
"names",
"ndim",
"output_nr",
"requires_grad",
"retains_grad",
"size",
"shape",
"volatile",
}:
return getattr(self.metatensor, name)
# materializing these is needed for quantization (see lit-gpt)
if name in {"contiguous", "cuda", "half", "data", "to"}:
return getattr(self._load_tensor(), name)
raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
def __repr__(self) -> str:
return f"{self.__class__.__name__}({repr(self.metatensor)})"
# Modified from https://github.com/lernapparat/torchhacks by Thomas Viehmann
class _LazyLoadingUnpickler(pickle.Unpickler):
def __init__(self, file: IO, file_reader: torch.PyTorchFileReader) -> None:
super().__init__(file)
self.file_reader = file_reader
@override
def find_class(self, module: str, name: str) -> Any:
if module == "torch._utils" and name == "_rebuild_tensor_v2":
return partial(_NotYetLoadedTensor.rebuild_tensor_v2, archiveinfo=self)
if module == "torch._tensor" and name == "_rebuild_from_type_v2":
return partial(_NotYetLoadedTensor.rebuild_from_type_v2, archiveinfo=self)
if module == "torch._utils" and name == "_rebuild_parameter":View on GitHub (pinned to 9fed5c27d2)
Solutions
- Materialize the tensor first via `.to(torch.float32)`, `.cuda`, `.contiguous()`, or `.data`, which are handled by the wrapper
- Load the checkpoint fully (non-lazy `torch.load`) if you need arbitrary tensor operations
- Restructure code to call `.to(...)`/`.data` before any other tensor method
Example fix
// before w = state["model"]["proj.weight"] x = w.float() # AttributeError on _UnloadedTensor // after w = state["model"]["proj.weight"] w = w.to(torch.float32) # materializes x = w.float()
Defensive patterns
Strategy: fallback
Validate before calling
def materialize(t):
return t.to(t.metatensor.dtype) if hasattr(t, "metatensor") else t Type guard
def is_unloaded_tensor(t) -> bool:
return hasattr(t, "metatensor") and hasattr(t, "_load_tensor") Prevention
- Materialize lazily loaded tensors with .to/.data/.contiguous before arbitrary ops
- Keep heavy inspection code paths on fully loaded state dicts
When it happens
Trigger: Calling arbitrary tensor methods/attributes (e.g. `.shape` is fine via metatensor, but e.g. `.float()`, `.view(...)`, `.t()`, `.numpy()`) directly on tensors obtained from a lazily-loaded checkpoint (`_lazy_load`, quantized/lazy checkpoint workflows like lit-gpt).
Common situations: Inspecting or manipulating weights right after lazy-loading a large/quantized checkpoint without materializing it; code written for regular tensors reused against lazily loaded state dicts.
Related errors
- Path {str(filename)!r} does not exist or is not a file.
- Received multiple values for {', '.join(duplicated_plugin_ke
- Received both `precision={precision_input}` and `plugins={se
- accelerator set through both strategy class and accelerator
- precision set through both strategy class and plugins, choos
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/d6be95307d5d134d.
Report an issue: GitHub.