Important
You are viewing the NeMo 2.0 documentation. This release introduces significant changes to the API and a new library, NeMo Run. We are currently porting all features from NeMo 1.0 to 2.0. For documentation on previous versions or features not yet available in 2.0, please refer to the NeMo 24.07 documentation.
Helpers and Utils#
- nemo_run.api.autoconvert(
- fn: Callable[[P], Config[T]],
- *,
- partial: bool = False,
- nemo_run.api.autoconvert(
- fn: Callable[[P], Partial[T]],
- *,
- partial: bool = False,
- nemo_run.api.autoconvert(
- fn: Callable[[P], T],
- *,
- partial: bool = False,
- nemo_run.api.autoconvert(
- *,
- partial: Literal[True] = False,
- nemo_run.api.autoconvert(
- *,
- partial: Literal[False] = False,
The autoconvert function is a powerful and flexible decorator for Python functions that can modify the behavior of the function it decorates by converting the returned object in a nested manner to: run.Config (when partial is False) or run.Partial (when partial is True). This conversion is done by a provided conversion function to_buildable_fn, which defaults to default_autoconfig_buildable. Under the hood, it uses fiddle’s autoconfig to parse the function’s AST and convert objects to their run.Config/run.Partial counterparts.
You can use it in two different ways:
Directly as a decorator for a function you define:
@autoconvert def my_func(param1: int, param2: str) -> MyType: return MyType(param1=param1, param2=param2)
This will return run.Config(MyType, param1=param1, param2=param2) when called, assuming that partial=False (otherwise, it would be a run.Partial instance).
Indirectly, as a way to convert an existing function:
def my_func(param1: int, param2: str) -> MyType: return MyType(param1=param1, param2=param2) my_new_func = autoconvert(partial=True)(my_func)
Now, calling my_new_func will actually return run.Partial(MyType, param1=param1, param2=param2) rather than a MyType instance.
Parameters:
- fn:
The function to be decorated. This parameter is optional, and if not provided, autoconvert acts as a decorator factory. Defaults to None.
- partial:
A boolean flag that indicates whether the return type of fn should be converted to Partial[T] (if True) or Config[T] (if False). Defaults to False.
- to_buildable_fn:
The conversion function to be used for the desired output type. This function takes another function and any positional and keyword arguments and returns an instance of either Config[T] or Partial[T]. By default, it uses default_autoconfig_buildable.