pytorch-lightning icon indicating copy to clipboard operation
pytorch-lightning copied to clipboard

Lazy object instantiation via config file

Open rob-hen opened this issue 6 months ago • 11 comments

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:

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

rob-hen avatar Jan 11 '24 13:01 rob-hen

Excellent PR.

shivan-s avatar Nov 12 '23 12:11 shivan-s

not sure why this was changed, I opened #26 (PR #29) for Linux installation and by the time my PR was merged, both MacOS and Linux instructions used bash ./util/install_XXX.sh instead. This PR needs to be merged, and if you could please also fix the Linux instructions the same way I'd be grateful.

brunofin avatar Nov 17 '23 09:11 brunofin

those changes were introduced by @idan here https://github.com/githubnext/monaspace/pull/29/commits/45c9887744307edea7b289eee4320280a59e93c6 upon merging my PR.

brunofin avatar Nov 17 '23 09:11 brunofin

Someone fixed the Linux instructions in #104 — there are already a bunch of duplicates so I'll just leave it separate.

zanieb avatar Nov 17 '23 16:11 zanieb