fairseq2.recipe.composition

The documentation for this module is work in progress.

Functions

fairseq2.recipe.composition.register_recipe_assets(container: DependencyContainer, rel_path: Path | str, *, stack_level: int = 1) None[source]

Registers asset cards located under the specified path, relative to the calling module’s path (i.e. module.__path__).

The specified path should be either a YAML file or a directory containing YAML files. If a directory is specified, it will be scanned recursively. Each YAML file must define one or more asset cards, which are used by AssetStore to represent assets such as models, datasets, and tokenizers.

Check out the Assets concept documentation to learn more about assets and asset cards.

This function is intended to be called within the register() method of a recipe, to register assets that should always be available alongside the recipe.

from fairseq2.runtime.dependency import DependencyContainer
from fairseq2.recipe import TrainRecipe
from fairseq2.recipe.composition import register_recipe_assets

class MyRecipe(TrainRecipe):
    def register(self, container: DependencyContainer) -> None:
        register_recipe_assets(container, "configs/assets")

stack_level can be used to specify the actual calling module. For example, if register_recipe_assets is invoked within a wrapper helper function, setting stack_level to 2 indicates that the caller module is the one that called the helper function.

A helper function in a Python file named “my_helpers.py”
from fairseq2.recipe.composition import register_recipe_assets
from fairseq2.runtime.dependency import DependencyContainer

def my_recipe_helper_function(container: DependencyContainer) -> None:
    # Note that `stack_level` is set to 2, so that the actual calling
    # module, and not "my_helpers.py", is used to resolve the path.
    register_recipe_assets(container, "path/to/assets", stack_level=2)

Note that this function is primarily intended for use by recipe authors. Users of a recipe can specify additional paths to search for asset cards by using the AssetsConfig.extra_paths configuration option.

A YAML recipe configuration
common:
  assets:
    extra_paths:
      - /path/to/extra/recipes1
      - /path/to/extra/recipes2
Raises:
  • ValueError – If rel_path specifies an absolute path

  • ValueError – If rel_path is a string and does not represent a valid path

  • ValueError – If stack_level is less than 1 or larger than the size of the call stack.