Lightning-AI / Lightning-AI/pytorch-lightning
Lazy object instantiation via config file
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 31.4k
- Forks
- 3.8k
- Avg merge
- 6d 7h
- Merged PRs (30d)
- 6
Description
### Description & Motivation
As far as I can see, there is no way currently to do lazy instantiation.
Let say, we want to create an object of class `path.to.B` that we configure from the yaml file. However, its creation might depend on a property of another object, that we know only during runtime. So it would be great if, based on the config file, we get a constructor for an object of class `path.to.B` , where all parameters are already set from the config file. In the example below, the constructor would already set `u=1, v=2`, e.g. using `functools.partial`. So instead of getting the instantiated object I get a preconfigured constructor.
See the example below
Module:
```python
class Model(pl.LightningModule):
def __init__(self,model):
super().__init__()
obj_A = model.A
obj_B_constructor = model.B
obj_B = obj_B_constructor(extern_param=obj_A.param) # should correspond to path.to.B_class(u=1, v=2, extern_param=obj_A)
```
config file:
```
model:
A:
class_path: path.to.A_class
init_args:
a: 1
b: 2
B:
class_path: path.to.B_class
lazy_init: True
init_args:
u: 1
v: 2
```
### Pitch
By setting an additional key in the config file, we are not getting the instantiate object, but the constructor for the object, with all parameters already set.
### Alternatives
_No response_
### Additional context
_No response_
cc @borda @carmocca @mauvilsa
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing how configuration entries with class_path and init_args are instantiated, then inspect the related tests if available. Define the expected behavior for lazy_init, including how the returned constructor accepts extern_param and how existing eager instantiation remains unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100