Skip to content

The current instantiation does not trigger the initialization of submodules #2273

Open
@dz1iang

Description

Currently, the instantiation method used in Torchtune's configuration does not trigger the initialization of its submodules. The instantiate function from hydra.utils does handle this, but it differs from the current approach in Torchtune. Is there an elegant way to trigger the initialization of submodules?

Below is a simple example of how hydra.utils successfully triggers the initialization of submodules.

  • config
main_component:
  _target_: __main__.MainComponent
  sub_component:
    _target_: __main__.SubComponent
    param_a: 42
    param_b: "example"
  • test code
from omegaconf import OmegaConf
from hydra.utils import instantiate

class SubComponent:
    def __init__(self, param_a, param_b):
        self.param_a = param_a
        self.param_b = param_b
        #breakpoint()
        self.param_c = 1

    def __repr__(self):
        return f"SubComponent(param_a={self.param_a}, param_b={self.param_b})"

class MainComponent:
    def __init__(self, sub_component):
        self.sub_component = sub_component
        self.param_c = self.sub_component.param_c

    def __repr__(self):
        return f"MainComponent(sub_component={self.sub_component})"

nested_config_path = "nested_config.yaml"
config = OmegaConf.load(nested_config_path)

main_component = instantiate(config.main_component)
print(main_component)
print(main_component.param_c)

Metadata

Assignees

Labels

discussionStart a discussiontriagedThis issue has been assigned an owner and appropriate label

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions