Exemple #1
0
    def set_package_header(self, package_header: Optional[str]) -> None:
        assert self.__dict__["package_header"] is None

        if package_header is None:
            return

        if "_group_" in package_header or "_name_" in package_header:
            path = self.get_config_path()
            url = "https://hydra.cc/docs/next/upgrades/1.0_to_1.1/changes_to_package_header"
            deprecation_warning(
                message=dedent(
                    f"""\
                    In '{path}': Usage of deprecated keyword in package header '# @package {package_header}'.
                    See {url} for more information"""
                ),
            )

        if package_header == "_group_":
            return

        # package header is always interpreted as absolute.
        # if it does not have a _global_ prefix, add it.
        if package_header != "_global_" and not package_header.startswith("_global_."):
            if package_header == "":
                package_header = "_global_"
            else:
                package_header = f"_global_.{package_header}"

        package_header = package_header.replace("_group_", self.get_default_package())
        self.__dict__["package_header"] = package_header
Exemple #2
0
    def __init__(
        self,
        config_path: Optional[str] = _UNSPECIFIED_,
        job_name: Optional[str] = None,
        caller_stack_depth: int = 1,
    ) -> None:
        self._gh_backup = get_gh_backup()

        # DEPRECATED: remove in 1.2
        # in 1.2, the default config_path should be changed to None
        if config_path is _UNSPECIFIED_:
            url = "https://hydra.cc/docs/next/upgrades/1.0_to_1.1/changes_to_hydra_main_config_path"
            deprecation_warning(
                message=dedent(f"""\
                config_path is not specified in hydra.initialize().
                See {url} for more information."""),
                stacklevel=2,
            )
            config_path = "."

        if config_path is not None and os.path.isabs(config_path):
            raise HydraException(
                "config_path in initialize() must be relative")
        calling_file, calling_module = detect_calling_file_or_module_from_stack_frame(
            caller_stack_depth + 1)
        if job_name is None:
            job_name = detect_task_name(calling_file=calling_file,
                                        calling_module=calling_module)

        Hydra.create_main_hydra_file_or_module(
            calling_file=calling_file,
            calling_module=calling_module,
            config_path=config_path,
            job_name=job_name,
        )
Exemple #3
0
 def validate(self) -> None:
     if self.package is not None and "_name_" in self.package:
         # DEPRECATED: remove in 1.2
         url = "https://hydra.cc/docs/next/upgrades/1.0_to_1.1/changes_to_package_header"
         deprecation_warning(message=dedent(f"""\
                 In override {self.input_line}: _name_ keyword is deprecated in packages, see {url}
                 """), )
Exemple #4
0
 def _normalize_file_name(filename: str) -> str:
     if filename.endswith(".yml"):
         # DEPRECATED: remove in 1.2
         deprecation_warning(
             "Support for .yml files is deprecated. Use .yaml extension for Hydra config files"
         )
     if not any(filename.endswith(ext) for ext in [".yaml", ".yml"]):
         filename += ".yaml"
     return filename
Exemple #5
0
    def __init__(self, config_dir: str, job_name: str = "app") -> None:
        from hydra import initialize_config_dir as real_initialize_config_dir

        deprecation_warning(
            message=
            "hydra.experimental.initialize_config_dir() is no longer experimental."
            " Use hydra.initialize_config_dir().", )

        self.delegate = real_initialize_config_dir(config_dir=config_dir,
                                                   job_name=job_name)
Exemple #6
0
 def _normalize_file_name(filename: str) -> str:
     supported_extensions = [".yaml"]
     if not version.base_at_least("1.2"):
         supported_extensions.append(".yml")
         if filename.endswith(".yml"):
             deprecation_warning(
                 "Support for .yml files is deprecated. Use .yaml extension for Hydra config files"
             )
     if not any(filename.endswith(ext) for ext in supported_extensions):
         filename += ".yaml"
     return filename
    def resolve_interpolation(self, known_choices: DictConfig) -> None:
        name = self.get_name()
        if name is not None:
            # DEPRECATED: remove in 1.2
            if re.match(_legacy_interpolation_pattern, name) is not None:
                msg = dedent(f"""
Defaults list element '{self.get_override_key()}={name}' is using a deprecated interpolation form.
See http://hydra.cc/docs/next/upgrades/1.0_to_1.1/defaults_list_interpolation for migration information."""
                             )
                deprecation_warning(message=msg, )

            self.value = self._resolve_interpolation_impl(known_choices, name)
Exemple #8
0
def _get_callbacks_for_run_job(
        hydra_context: Optional[HydraContext]) -> "Callbacks":
    if hydra_context is None:
        # DEPRECATED: remove in 1.2
        # hydra_context will be required in 1.2
        deprecation_warning(message=dedent("""
                run_job's signature has changed in Hydra 1.1. Please pass in hydra_context.
                Support for the old style will be removed in Hydra 1.2.
                For more info, check https://github.com/facebookresearch/hydra/pull/1581."""
                                           ), )
        from hydra._internal.callbacks import Callbacks

        callbacks = Callbacks()
    else:
        callbacks = hydra_context.callbacks

    return callbacks
Exemple #9
0
    def __init__(
        self,
        config_path: Optional[str] = _UNSPECIFIED_,
        job_name: Optional[str] = None,
        caller_stack_depth: int = 1,
    ) -> None:
        from hydra import initialize as real_initialize

        deprecation_warning(
            message="hydra.experimental.initialize() is no longer experimental."
            " Use hydra.initialize()", )

        self.delegate = real_initialize(
            config_path=config_path,
            job_name=job_name,
            caller_stack_depth=caller_stack_depth + 1,
        )
Exemple #10
0
    def _setup_plugin(
        plugin: Any,
        task_function: TaskFunction,
        config: DictConfig,
        config_loader: Optional[ConfigLoader] = None,
        hydra_context: Optional[HydraContext] = None,
    ) -> Any:
        """
        With HydraContext introduced in #1581, we need to set up the plugins in a way
        that's compatible with both Hydra 1.0 and Hydra 1.1 syntax.
        This method should be deleted in the next major release.
        """
        assert isinstance(plugin, Sweeper) or isinstance(plugin, Launcher)
        assert (config_loader is not None or hydra_context is not None
                ), "config_loader and hydra_context cannot both be None"

        param_keys = signature(plugin.setup).parameters.keys()

        if "hydra_context" not in param_keys:
            # DEPRECATED: remove in 1.2
            # hydra_context will be required in 1.2
            deprecation_warning(message=dedent("""
                    Plugin's setup() signature has changed in Hydra 1.1.
                    Support for the old style will be removed in Hydra 1.2.
                    For more info, check https://github.com/facebookresearch/hydra/pull/1581."""
                                               ), )
            config_loader = (
                config_loader if config_loader is not None else
                hydra_context.config_loader  # type: ignore
            )
            plugin.setup(  # type: ignore
                config=config,
                config_loader=config_loader,
                task_function=task_function,
            )
        else:
            if hydra_context is None:
                # hydra_context could be None when an incompatible Sweeper instantiates a compatible Launcher
                assert config_loader is not None
                hydra_context = HydraContext(config_loader=config_loader,
                                             callbacks=Callbacks())
            plugin.setup(config=config,
                         hydra_context=hydra_context,
                         task_function=task_function)
        return plugin
Exemple #11
0
def compose(
    config_name: Optional[str] = None,
    overrides: List[str] = [],
    return_hydra_config: bool = False,
    strict: Optional[bool] = None,
) -> DictConfig:
    from hydra import compose as real_compose

    deprecation_warning(
        message="hydra.experimental.compose() is no longer experimental."
        " Use hydra.compose()",
    )
    return real_compose(
        config_name=config_name,
        overrides=overrides,
        return_hydra_config=return_hydra_config,
        strict=strict,
    )
Exemple #12
0
def my_app(cfg: DictConfig) -> None:
    deprecation_warning("Feature FooBar is deprecated")
Exemple #13
0
def run_job(
    task_function: TaskFunction,
    config: DictConfig,
    job_dir_key: str,
    job_subdir_key: Optional[str],
    configure_logging: bool = True,
    hydra_context: Optional[HydraContext] = None,
) -> "JobReturn":
    callbacks = _get_callbacks_for_run_job(hydra_context)

    old_cwd = os.getcwd()
    orig_hydra_cfg = HydraConfig.instance().cfg

    output_dir = str(OmegaConf.select(config, job_dir_key))
    if job_subdir_key is not None:
        # evaluate job_subdir_key lazily.
        # this is running on the client side in sweep and contains things such as job:id which
        # are only available there.
        subdir = str(OmegaConf.select(config, job_subdir_key))
        output_dir = os.path.join(output_dir, subdir)

    with read_write(config.hydra.runtime):
        with open_dict(config.hydra.runtime):
            config.hydra.runtime.output_dir = os.path.abspath(output_dir)

    HydraConfig.instance().set_config(config)
    _chdir = None
    try:
        ret = JobReturn()
        task_cfg = copy.deepcopy(config)
        with read_write(task_cfg):
            with open_dict(task_cfg):
                del task_cfg["hydra"]

        ret.cfg = task_cfg
        hydra_cfg = copy.deepcopy(HydraConfig.instance().cfg)
        assert isinstance(hydra_cfg, DictConfig)
        ret.hydra_cfg = hydra_cfg
        overrides = OmegaConf.to_container(config.hydra.overrides.task)
        assert isinstance(overrides, list)
        ret.overrides = overrides
        # handle output directories here
        Path(str(output_dir)).mkdir(parents=True, exist_ok=True)

        _chdir = hydra_cfg.hydra.job.chdir

        if _chdir is None:
            url = "https://hydra.cc/docs/upgrades/1.1_to_1.2/changes_to_job_working_dir"
            deprecation_warning(
                message=dedent(f"""\
                    Hydra 1.3 will no longer change working directory at job runtime by default.
                    See {url} for more information."""),
                stacklevel=2,
            )
            _chdir = True

        if _chdir:
            os.chdir(output_dir)
            ret.working_dir = output_dir
        else:
            ret.working_dir = os.getcwd()

        if configure_logging:
            configure_log(config.hydra.job_logging, config.hydra.verbose)

        if config.hydra.output_subdir is not None:
            hydra_output = Path(config.hydra.runtime.output_dir) / Path(
                config.hydra.output_subdir)
            _save_config(task_cfg, "config.yaml", hydra_output)
            _save_config(hydra_cfg, "hydra.yaml", hydra_output)
            _save_config(config.hydra.overrides.task, "overrides.yaml",
                         hydra_output)

        with env_override(hydra_cfg.hydra.job.env_set):
            callbacks.on_job_start(config=config)
            try:
                ret.return_value = task_function(task_cfg)
                ret.status = JobStatus.COMPLETED
            except Exception as e:
                ret.return_value = e
                ret.status = JobStatus.FAILED

        ret.task_name = JobRuntime.instance().get("name")

        _flush_loggers()

        callbacks.on_job_end(config=config, job_return=ret)

        return ret
    finally:
        HydraConfig.instance().cfg = orig_hydra_cfg
        if _chdir:
            os.chdir(old_cwd)