open-mmlab / open-mmlab/mmengine
[Feature] Instantiation from config using path to class / function
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 464
- PR merge metrics
- No merged PRs in 30d
Description
What is the feature?
For new users (as I am) it might be hard to navigate the registry - it is not immediately obvious where and how a component is instantiated. Also, it might be challenging to maintain multiple registries in large projects.
An alternative approach to instantiation is to use the python path to a component (class, function, ...) as e.g. done here https://github.com/Farama-Foundation/HighwayEnv/blob/master/highway_env/utils.py#L38
Applying the approach from highway-env, instantiating from config could look as follows
cfg = dict(type="my_package.subdir.ComponentClass", one_param=-2., another_param="a_param")
obj_from_config = instantiate_from_config(cfg)
with
def instantiate_from_config(cfg: dict) -> Any:
cfg_copy = copy(cfg)
cls = class_from_path(path=cfg_copy.pop("type"))
if cls.hasattr("from_config"):
return cls.from_config(cfg_copy)
return cls(**cfg_copy)
This object instantiation approach does not require any registration and can instantiate any object from any library that is installed in the same python environment.
Additionally, it can be extended to handle partial functions and many more.
What is your opinion on that? I can implement a small example demoing this instantiation
Any other context?
No response
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 reviewing MMEngine's current registry-based instantiation flow, then compare it with the linked HighwayEnv utils.py example and the proposed instantiate_from_config and class_from_path functions. Done would mean an agreed API and a small working example showing instantiation from a dotted Python path without registration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- developer-experience
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100