rllib/agents/trainer.py
def validate_config(self, config: TrainerConfigDict) -> None:
        """Validates a given config dict for this Trainer.

        Users should override this method to implement custom validation
        behavior. It is recommended to call `super().validate_config()` in
        this override.

        Args:
            config: The given config dict to check.

        Raises:
            ValueError: If there is something wrong with the config.
        """
        model_config = config.get("model")
        if model_config is None:
            config["model"] = model_config = {}

        # Monitor should be replaced by `record_env`.
        if config.get("monitor", DEPRECATED_VALUE) != DEPRECATED_VALUE:
            deprecation_warning("monitor", "record_env", error=False)
            config["record_env"] = config.get("monitor", False)
        # Empty string would fail some if-blocks checking for this setting.
        # Set to True instead, meaning: use default output dir to store
        # the videos.
        if config.get("record_env") == "":
            config["record_env"] = True

        # Use DefaultCallbacks class, if callbacks is None.
        if config["callbacks"] is None:
            config["callbacks"] = DefaultCallbacks
        # Check, whether given `callbacks` is a callable.
        if not callable(config["callbacks"]):
            raise ValueError(
                "`callbacks` must be a callable method that "
                "returns a subclass of DefaultCallbacks, got "
                f"{config['callbacks']}!"
            )

        # Multi-GPU settings.
        simple_optim_setting = config.get("simple_optimizer", DEPRECATED_VALUE)
        if simple_optim_setting != DEPRECATED_VALUE:
            deprecation_warning(old="simple_optimizer", error=False)

        # Validate "multiagent" sub-dict and convert policy 4-tuples to
        # PolicySpec objects.
        policies, is_multi_agent = check_multi_agent(config)

        framework = config.get("framework")
        # Multi-GPU setting: Must use MultiGPUTrainOneStep.
        if config.get("num_gpus", 0) > 1:
            if framework in ["tfe", "tf2"]:
                raise ValueError(
                    "`num_gpus` > 1 not supported yet for "
                    "framework={}!".format(framework)
                )
            elif simple_optim_setting is True:
                raise ValueError(
                    "Cannot use `simple_optimizer` if `num_gpus` > 1! "
                    "Consider not setting `simple_optimizer` in your config."
                )
            config["simple_optimizer"] = False
        # Auto-setting: Use simple-optimizer for tf-eager or multiagent,
        # otherwise: MultiGPUTrainOneStep (if supported by the algo's execution
        # plan).
        elif simple_optim_setting == DEPRECATED_VALUE:
            # tf-eager: Must use simple optimizer.
            if framework not in ["tf", "torch"]:
                config["simple_optimizer"] = True
            # Multi-agent case: Try using MultiGPU optimizer (only
            # if all policies used are DynamicTFPolicies or TorchPolicies).
            elif is_multi_agent:
                from ray.rllib.policy.dynamic_tf_policy import DynamicTFPolicy
                from ray.rllib.policy.torch_policy import TorchPolicy

                default_policy_cls = self.get_default_policy_class(config)
                if any(
                    (p[0] or default_policy_cls) is None
                    or not issubclass(
                        p[0] or default_policy_cls, (DynamicTFPolicy, TorchPolicy)
                    )
                    for p in config["multiagent"]["policies"].values()
                ):
                    config["simple_optimizer"] = True
                else:
                    config["simple_optimizer"] = False
            else:
                config["simple_optimizer"] = False

        # User manually set simple-optimizer to False -> Error if tf-eager.
        elif simple_optim_setting is False:
            if framework in ["tfe", "tf2"]:
                raise ValueError(
                    "`simple_optimizer=False` not supported for "
                    "framework={}!".format(framework)
                )

        # Offline RL settings.
        if isinstance(config["input_evaluation"], tuple):
            config["input_evaluation"] = list(config["input_evaluation"])
        elif not isinstance(config["input_evaluation"], list):
            raise ValueError(
                "`input_evaluation` must be a list of strings, got {}!".format(
                    config["input_evaluation"]
                )
            )

        # Check model config.
        # If no preprocessing, propagate into model's config as well
        # (so model will know, whether inputs are preprocessed or not).
        if config["_disable_preprocessor_api"] is True:
            model_config["_disable_preprocessor_api"] = True
        # If no action flattening, propagate into model's config as well
        # (so model will know, whether action inputs are already flattened or
        # not).
        if config["_disable_action_flattening"] is True:
            model_config["_disable_action_flattening"] = True

        # Prev_a/r settings.
        prev_a_r = model_config.get("lstm_use_prev_action_reward", DEPRECATED_VALUE)
        if prev_a_r != DEPRECATED_VALUE:
            deprecation_warning(
                "model.lstm_use_prev_action_reward",
                "model.lstm_use_prev_action and model.lstm_use_prev_reward",
                error=False,
            )
            model_config["lstm_use_prev_action"] = prev_a_r
            model_config["lstm_use_prev_reward"] = prev_a_r

        # Check batching/sample collection settings.
        if config["batch_mode"] not in ["truncate_episodes", "complete_episodes"]:
            raise ValueError(
                "`batch_mode` must be one of [truncate_episodes|"
                "complete_episodes]! Got {}".format(config["batch_mode"])
            )

        # Store multi-agent batch count mode.
        self._by_agent_steps = (
            self.config["multiagent"].get("count_steps_by") == "agent_steps"
        )

        # Metrics settings.
        if config["metrics_smoothing_episodes"] != DEPRECATED_VALUE:
            deprecation_warning(
                old="metrics_smoothing_episodes",
                new="metrics_num_episodes_for_smoothing",
                error=False,
            )
            config["metrics_num_episodes_for_smoothing"] = config[
                "metrics_smoothing_episodes"
            ]
        if config["min_iter_time_s"] != DEPRECATED_VALUE:
            deprecation_warning(
                old="min_iter_time_s",
                new="min_time_s_per_reporting",
                error=False,
            )
            config["min_time_s_per_reporting"] = config["min_iter_time_s"]

        if config["collect_metrics_timeout"] != DEPRECATED_VALUE:
            # TODO: Warn once all algos use the `training_iteration` method.
            # deprecation_warning(
            #     old="collect_metrics_timeout",
            #     new="metrics_episode_collection_timeout_s",
            #     error=False,
            # )
            config["metrics_episode_collection_timeout_s"] = config[
                "collect_metrics_timeout"
            ]

        if config["timesteps_per_iteration"] != DEPRECATED_VALUE:
            # TODO: Warn once all algos use the `training_iteration` method.
            # deprecation_warning(
            #     old="timesteps_per_iteration",
            #     new="min_sample_timesteps_per_reporting",
            #     error=False,
            # )
            config["min_sample_timesteps_per_reporting"] = config[
                "timesteps_per_iteration"
            ]

        # Evaluation settings.

        # Deprecated setting: `evaluation_num_episodes`.
        if config["evaluation_num_episodes"] != DEPRECATED_VALUE:
            deprecation_warning(
                old="evaluation_num_episodes",
                new="`evaluation_duration` and `evaluation_duration_unit=episodes`",
                error=False,
            )
            config["evaluation_duration"] = config["evaluation_num_episodes"]
            config["evaluation_duration_unit"] = "episodes"
            config["evaluation_num_episodes"] = DEPRECATED_VALUE

        # If `evaluation_num_workers` > 0, warn if `evaluation_interval` is
        # None (also set `evaluation_interval` to 1).
        if config["evaluation_num_workers"] > 0 and not config["evaluation_interval"]:
            logger.warning(
                f"You have specified {config['evaluation_num_workers']} "
                "evaluation workers, but your `evaluation_interval` is None! "
                "Therefore, evaluation will not occur automatically with each"
                " call to `Trainer.train()`. Instead, you will have to call "
                "`Trainer.evaluate()` manually in order to trigger an "
                "evaluation run."
            )
        # If `evaluation_num_workers=0` and
        # `evaluation_parallel_to_training=True`, warn that you need
        # at least one remote eval worker for parallel training and
        # evaluation, and set `evaluation_parallel_to_training` to False.
        elif config["evaluation_num_workers"] == 0 and config.get(
            "evaluation_parallel_to_training", False
        ):
            logger.warning(
                "`evaluation_parallel_to_training` can only be done if "
                "`evaluation_num_workers` > 0! Setting "
                "`evaluation_parallel_to_training` to False."
            )
            config["evaluation_parallel_to_training"] = False

        # If `evaluation_duration=auto`, error if
        # `evaluation_parallel_to_training=False`.
        if config["evaluation_duration"] == "auto":
            if not config["evaluation_parallel_to_training"]:
                raise ValueError(
                    "`evaluation_duration=auto` not supported for "
                    "`evaluation_parallel_to_training=False`!"
                )
        # Make sure, it's an int otherwise.
        elif (
            not isinstance(config["evaluation_duration"], int)
            or config["evaluation_duration"] <= 0
        ):
            raise ValueError(
                "`evaluation_duration` ({}) must be an int and "
                ">0!".format(config["evaluation_duration"])
            )
Validates a given config dict for this Trainer.

        Users should override this method to implement custom validation
        behavior. It is recommended to call `super().validate_config()` in
        this override.

        Args:
            config: The given config dict to check.

        Raises:
            ValueError: If there is something wrong with the config.
        
Validates a given config dict for this Trainer.
Users should override this method to implement custom validation
behavior. It is recommended to call `super().validate_config()` in
this override.
Validates a given config dict for this Trainer . Users should override this method to implement custom validation behavior . It is recommended to call ` super () . validate_config () ` in this override .
def validate_config(self, config: TrainerConfigDict) -> None:
        model_config = config.get("model")
        if model_config is None:
            config["model"] = model_config = {}
        if config.get("monitor", DEPRECATED_VALUE) != DEPRECATED_VALUE:
            deprecation_warning("monitor", "record_env", error=False)
            config["record_env"] = config.get("monitor", False)
        if config.get("record_env") == "":
            config["record_env"] = True
        if config["callbacks"] is None:
            config["callbacks"] = DefaultCallbacks
        if not callable(config["callbacks"]):
            raise ValueError(
                "`callbacks` must be a callable method that "
                "returns a subclass of DefaultCallbacks, got "
                f"{config['callbacks']}!"
            )
        simple_optim_setting = config.get("simple_optimizer", DEPRECATED_VALUE)
        if simple_optim_setting != DEPRECATED_VALUE:
            deprecation_warning(old="simple_optimizer", error=False)
        policies, is_multi_agent = check_multi_agent(config)
        framework = config.get("framework")
        if config.get("num_gpus", 0) > 1:
            if framework in ["tfe", "tf2"]:
                raise ValueError(
                    "`num_gpus` > 1 not supported yet for "
                    "framework={}!".format(framework)
                )
            elif simple_optim_setting is True:
                raise ValueError(
                    "Cannot use `simple_optimizer` if `num_gpus` > 1! "
                    "Consider not setting `simple_optimizer` in your config."
                )
            config["simple_optimizer"] = False
        elif simple_optim_setting == DEPRECATED_VALUE:
            if framework not in ["tf", "torch"]:
                config["simple_optimizer"] = True
            elif is_multi_agent:
                from ray.rllib.policy.dynamic_tf_policy import DynamicTFPolicy
                from ray.rllib.policy.torch_policy import TorchPolicy
                default_policy_cls = self.get_default_policy_class(config)
                if any(
                    (p[0] or default_policy_cls) is None
                    or not issubclass(
                        p[0] or default_policy_cls, (DynamicTFPolicy, TorchPolicy)
                    )
                    for p in config["multiagent"]["policies"].values()
                ):
                    config["simple_optimizer"] = True
                else:
                    config["simple_optimizer"] = False
            else:
                config["simple_optimizer"] = False
        elif simple_optim_setting is False:
            if framework in ["tfe", "tf2"]:
                raise ValueError(
                    "`simple_optimizer=False` not supported for "
                    "framework={}!".format(framework)
                )
        if isinstance(config["input_evaluation"], tuple):
            config["input_evaluation"] = list(config["input_evaluation"])
        elif not isinstance(config["input_evaluation"], list):
            raise ValueError(
                "`input_evaluation` must be a list of strings, got {}!".format(
                    config["input_evaluation"]
                )
            )
        if config["_disable_preprocessor_api"] is True:
            model_config["_disable_preprocessor_api"] = True
        if config["_disable_action_flattening"] is True:
            model_config["_disable_action_flattening"] = True
        prev_a_r = model_config.get("lstm_use_prev_action_reward", DEPRECATED_VALUE)
        if prev_a_r != DEPRECATED_VALUE:
            deprecation_warning(
                "model.lstm_use_prev_action_reward",
                "model.lstm_use_prev_action and model.lstm_use_prev_reward",
                error=False,
            )
            model_config["lstm_use_prev_action"] = prev_a_r
            model_config["lstm_use_prev_reward"] = prev_a_r
        if config["batch_mode"] not in ["truncate_episodes", "complete_episodes"]:
            raise ValueError(
                "`batch_mode` must be one of [truncate_episodes|"
                "complete_episodes]! Got {}".format(config["batch_mode"])
            )
        self._by_agent_steps = (
            self.config["multiagent"].get("count_steps_by") == "agent_steps"
        )
        if config["metrics_smoothing_episodes"] != DEPRECATED_VALUE:
            deprecation_warning(
                old="metrics_smoothing_episodes",
                new="metrics_num_episodes_for_smoothing",
                error=False,
            )
            config["metrics_num_episodes_for_smoothing"] = config[
                "metrics_smoothing_episodes"
            ]
        if config["min_iter_time_s"] != DEPRECATED_VALUE:
            deprecation_warning(
                old="min_iter_time_s",
                new="min_time_s_per_reporting",
                error=False,
            )
            config["min_time_s_per_reporting"] = config["min_iter_time_s"]
        if config["collect_metrics_timeout"] != DEPRECATED_VALUE:
            config["metrics_episode_collection_timeout_s"] = config[
                "collect_metrics_timeout"
            ]
        if config["timesteps_per_iteration"] != DEPRECATED_VALUE:
            config["min_sample_timesteps_per_reporting"] = config[
                "timesteps_per_iteration"
            ]
        if config["evaluation_num_episodes"] != DEPRECATED_VALUE:
            deprecation_warning(
                old="evaluation_num_episodes",
                new="`evaluation_duration` and `evaluation_duration_unit=episodes`",
                error=False,
            )
            config["evaluation_duration"] = config["evaluation_num_episodes"]
            config["evaluation_duration_unit"] = "episodes"
            config["evaluation_num_episodes"] = DEPRECATED_VALUE
        if config["evaluation_num_workers"] > 0 and not config["evaluation_interval"]:
            logger.warning(
                f"You have specified {config['evaluation_num_workers']} "
                "evaluation workers, but your `evaluation_interval` is None! "
                "Therefore, evaluation will not occur automatically with each"
                " call to `Trainer.train()`. Instead, you will have to call "
                "`Trainer.evaluate()` manually in order to trigger an "
                "evaluation run."
            )
        elif config["evaluation_num_workers"] == 0 and config.get(
            "evaluation_parallel_to_training", False
        ):
            logger.warning(
                "`evaluation_parallel_to_training` can only be done if "
                "`evaluation_num_workers` > 0! Setting "
                "`evaluation_parallel_to_training` to False."
            )
            config["evaluation_parallel_to_training"] = False
        if config["evaluation_duration"] == "auto":
            if not config["evaluation_parallel_to_training"]:
                raise ValueError(
                    "`evaluation_duration=auto` not supported for "
                    "`evaluation_parallel_to_training=False`!"
                )
        elif (
            not isinstance(config["evaluation_duration"], int)
            or config["evaluation_duration"] <= 0
        ):
            raise ValueError(
                "`evaluation_duration` ({}) must be an int and "
                ">0!".format(config["evaluation_duration"])
            )
def validate_config ( self , config : TrainerConfigDict ) -> None : model_config = config . get ( "model" ) if model_config is None : config [ "model" ] = model_config = { } if config . get ( "monitor" , DEPRECATED_VALUE ) != DEPRECATED_VALUE : deprecation_warning ( "monitor" , "record_env" , error = False ) config [ "record_env" ] = config . get ( "monitor" , False ) if config . get ( "record_env" ) == "" : config [ "record_env" ] = True if config [ "callbacks" ] is None : config [ "callbacks" ] = DefaultCallbacks if not callable ( config [ "callbacks" ] ) : raise ValueError ( "`callbacks` must be a callable method that " "returns a subclass of DefaultCallbacks, got " f"{config['callbacks']}!" ) simple_optim_setting = config . get ( "simple_optimizer" , DEPRECATED_VALUE ) if simple_optim_setting != DEPRECATED_VALUE : deprecation_warning ( old = "simple_optimizer" , error = False ) policies , is_multi_agent = check_multi_agent ( config ) framework = config . get ( "framework" ) if config . get ( "num_gpus" , 0 ) > 1 : if framework in [ "tfe" , "tf2" ] : raise ValueError ( "`num_gpus` > 1 not supported yet for " "framework={}!" . format ( framework ) ) elif simple_optim_setting is True : raise ValueError ( "Cannot use `simple_optimizer` if `num_gpus` > 1! " "Consider not setting `simple_optimizer` in your config." ) config [ "simple_optimizer" ] = False elif simple_optim_setting == DEPRECATED_VALUE : if framework not in [ "tf" , "torch" ] : config [ "simple_optimizer" ] = True elif is_multi_agent : from ray . rllib . policy . dynamic_tf_policy import DynamicTFPolicy from ray . rllib . policy . torch_policy import TorchPolicy default_policy_cls = self . get_default_policy_class ( config ) if any ( ( p [ 0 ] or default_policy_cls ) is None or not issubclass ( p [ 0 ] or default_policy_cls , ( DynamicTFPolicy , TorchPolicy ) ) for p in config [ "multiagent" ] [ "policies" ] . values ( ) ) : config [ "simple_optimizer" ] = True else : config [ "simple_optimizer" ] = False else : config [ "simple_optimizer" ] = False elif simple_optim_setting is False : if framework in [ "tfe" , "tf2" ] : raise ValueError ( "`simple_optimizer=False` not supported for " "framework={}!" . format ( framework ) ) if isinstance ( config [ "input_evaluation" ] , tuple ) : config [ "input_evaluation" ] = list ( config [ "input_evaluation" ] ) elif not isinstance ( config [ "input_evaluation" ] , list ) : raise ValueError ( "`input_evaluation` must be a list of strings, got {}!" . format ( config [ "input_evaluation" ] ) ) if config [ "_disable_preprocessor_api" ] is True : model_config [ "_disable_preprocessor_api" ] = True if config [ "_disable_action_flattening" ] is True : model_config [ "_disable_action_flattening" ] = True prev_a_r = model_config . get ( "lstm_use_prev_action_reward" , DEPRECATED_VALUE ) if prev_a_r != DEPRECATED_VALUE : deprecation_warning ( "model.lstm_use_prev_action_reward" , "model.lstm_use_prev_action and model.lstm_use_prev_reward" , error = False , ) model_config [ "lstm_use_prev_action" ] = prev_a_r model_config [ "lstm_use_prev_reward" ] = prev_a_r if config [ "batch_mode" ] not in [ "truncate_episodes" , "complete_episodes" ] : raise ValueError ( "`batch_mode` must be one of [truncate_episodes|" "complete_episodes]! Got {}" . format ( config [ "batch_mode" ] ) ) self . _by_agent_steps = ( self . config [ "multiagent" ] . get ( "count_steps_by" ) == "agent_steps" ) if config [ "metrics_smoothing_episodes" ] != DEPRECATED_VALUE : deprecation_warning ( old = "metrics_smoothing_episodes" , new = "metrics_num_episodes_for_smoothing" , error = False , ) config [ "metrics_num_episodes_for_smoothing" ] = config [ "metrics_smoothing_episodes" ] if config [ "min_iter_time_s" ] != DEPRECATED_VALUE : deprecation_warning ( old = "min_iter_time_s" , new = "min_time_s_per_reporting" , error = False , ) config [ "min_time_s_per_reporting" ] = config [ "min_iter_time_s" ] if config [ "collect_metrics_timeout" ] != DEPRECATED_VALUE : config [ "metrics_episode_collection_timeout_s" ] = config [ "collect_metrics_timeout" ] if config [ "timesteps_per_iteration" ] != DEPRECATED_VALUE : config [ "min_sample_timesteps_per_reporting" ] = config [ "timesteps_per_iteration" ] if config [ "evaluation_num_episodes" ] != DEPRECATED_VALUE : deprecation_warning ( old = "evaluation_num_episodes" , new = "`evaluation_duration` and `evaluation_duration_unit=episodes`" , error = False , ) config [ "evaluation_duration" ] = config [ "evaluation_num_episodes" ] config [ "evaluation_duration_unit" ] = "episodes" config [ "evaluation_num_episodes" ] = DEPRECATED_VALUE if config [ "evaluation_num_workers" ] > 0 and not config [ "evaluation_interval" ] : logger . warning ( f"You have specified {config['evaluation_num_workers']} " "evaluation workers, but your `evaluation_interval` is None! " "Therefore, evaluation will not occur automatically with each" " call to `Trainer.train()`. Instead, you will have to call " "`Trainer.evaluate()` manually in order to trigger an " "evaluation run." ) elif config [ "evaluation_num_workers" ] == 0 and config . get ( "evaluation_parallel_to_training" , False ) : logger . warning ( "`evaluation_parallel_to_training` can only be done if " "`evaluation_num_workers` > 0! Setting " "`evaluation_parallel_to_training` to False." ) config [ "evaluation_parallel_to_training" ] = False if config [ "evaluation_duration" ] == "auto" : if not config [ "evaluation_parallel_to_training" ] : raise ValueError ( "`evaluation_duration=auto` not supported for " "`evaluation_parallel_to_training=False`!" ) elif ( not isinstance ( config [ "evaluation_duration" ] , int ) or config [ "evaluation_duration" ] <= 0 ) : raise ValueError ( "`evaluation_duration` ({}) must be an int and " ">0!" . format ( config [ "evaluation_duration" ] ) )
Validates a given config dict for this Trainer.
Validates a given config dict for this Trainer .
"""Validates a given config dict for this Trainer.

        Users should override this method to implement custom validation
        behavior. It is recommended to call `super().validate_config()` in
        this override.

        Args:
            config: The given config dict to check.

        Raises:
            ValueError: If there is something wrong with the config.
        """ # Monitor should be replaced by `record_env`. # Empty string would fail some if-blocks checking for this setting. # Set to True instead, meaning: use default output dir to store # the videos. # Use DefaultCallbacks class, if callbacks is None. # Check, whether given `callbacks` is a callable. # Multi-GPU settings. # Validate "multiagent" sub-dict and convert policy 4-tuples to # PolicySpec objects. # Multi-GPU setting: Must use MultiGPUTrainOneStep. # Auto-setting: Use simple-optimizer for tf-eager or multiagent, # otherwise: MultiGPUTrainOneStep (if supported by the algo's execution # plan). # tf-eager: Must use simple optimizer. # Multi-agent case: Try using MultiGPU optimizer (only # if all policies used are DynamicTFPolicies or TorchPolicies). # User manually set simple-optimizer to False -> Error if tf-eager. # Offline RL settings. # Check model config. # If no preprocessing, propagate into model's config as well # (so model will know, whether inputs are preprocessed or not). # If no action flattening, propagate into model's config as well # (so model will know, whether action inputs are already flattened or # not). # Prev_a/r settings. # Check batching/sample collection settings. # Store multi-agent batch count mode. # Metrics settings. # TODO: Warn once all algos use the `training_iteration` method. # deprecation_warning( #     old="collect_metrics_timeout", #     new="metrics_episode_collection_timeout_s", #     error=False, # ) # TODO: Warn once all algos use the `training_iteration` method. # deprecation_warning( #     old="timesteps_per_iteration", #     new="min_sample_timesteps_per_reporting", #     error=False, # ) # Evaluation settings. # Deprecated setting: `evaluation_num_episodes`. # If `evaluation_num_workers` > 0, warn if `evaluation_interval` is # None (also set `evaluation_interval` to 1). # If `evaluation_num_workers=0` and # `evaluation_parallel_to_training=True`, warn that you need # at least one remote eval worker for parallel training and # evaluation, and set `evaluation_parallel_to_training` to False. # If `evaluation_duration=auto`, error if # `evaluation_parallel_to_training=False`. # Make sure, it's an int otherwise.
If there is something wrong with the config.
If there is something wrong with the config .
ValueError
self
config
TrainerConfigDict
The given config dict to check.
The given config dict to check .