@@ -962,3 +962,103 @@ def as_remote(cls, remote_config: dict[str, Any] | None = None):
962962 remote_collector = ray .remote (** remote_config )(cls )
963963 remote_collector .is_remote = True
964964 return remote_collector
965+
966+
967+ def get_ray_default_runtime_env () -> dict [str , Any ]:
968+ """Get the default Ray runtime environment configuration for TorchRL.
969+
970+ This function returns a runtime environment configuration that excludes
971+ large directories and files that should not be uploaded to Ray workers.
972+ This helps prevent issues with Ray's working_dir size limits (512MB default).
973+
974+ Returns:
975+ dict: A dictionary containing the default runtime_env configuration with
976+ excludes for common large directories.
977+
978+ Examples:
979+ >>> import ray
980+ >>> from torchrl._utils import get_ray_default_runtime_env
981+ >>> ray_init_config = {"num_cpus": 4}
982+ >>> ray_init_config["runtime_env"] = get_ray_default_runtime_env()
983+ >>> ray.init(**ray_init_config)
984+
985+ Note:
986+ The excludes list includes:
987+ - Virtual environments (.venv/, venv/, etc.)
988+ - Test files and caches
989+ - Documentation builds
990+ - Benchmarks
991+ - Examples and tutorials
992+ - CI/CD configurations
993+ - IDE configurations
994+
995+ """
996+ return {
997+ "excludes" : [
998+ ".venv/" ,
999+ "venv/" ,
1000+ "env/" ,
1001+ "ENV/" ,
1002+ "env.bak/" ,
1003+ "venv.bak/" ,
1004+ "test/" ,
1005+ "tests/" ,
1006+ "docs/" ,
1007+ "benchmarks/" ,
1008+ "tutorials/" ,
1009+ "examples/" ,
1010+ ".github/" ,
1011+ ".pytest_cache/" ,
1012+ ".mypy_cache/" ,
1013+ ".ruff_cache/" ,
1014+ "__pycache__/" ,
1015+ "*.pyc" ,
1016+ "*.pyo" ,
1017+ "*.egg-info/" ,
1018+ ".idea/" ,
1019+ ".vscode/" ,
1020+ "dev/" ,
1021+ "main/" ,
1022+ "*.html" ,
1023+ ]
1024+ }
1025+
1026+
1027+ def merge_ray_runtime_env (ray_init_config : dict [str , Any ]) -> dict [str , Any ]:
1028+ """Merge user-provided ray_init_config with default runtime_env excludes.
1029+
1030+ This function ensures that the default TorchRL runtime_env excludes are applied
1031+ to prevent large directories from being uploaded to Ray workers, while preserving
1032+ any user-provided configuration.
1033+
1034+ Args:
1035+ ray_init_config (dict): The ray init configuration dictionary to merge.
1036+
1037+ Returns:
1038+ dict: The merged configuration with default runtime_env excludes applied.
1039+
1040+ Examples:
1041+ >>> from torchrl._utils import merge_ray_runtime_env
1042+ >>> ray_init_config = {"num_cpus": 4}
1043+ >>> ray_init_config = merge_ray_runtime_env(ray_init_config)
1044+ >>> ray.init(**ray_init_config)
1045+
1046+ """
1047+ default_runtime_env = get_ray_default_runtime_env ()
1048+ runtime_env = ray_init_config .setdefault ("runtime_env" , {})
1049+
1050+ if not isinstance (runtime_env , dict ):
1051+ runtime_env = dict (runtime_env )
1052+ ray_init_config ["runtime_env" ] = runtime_env
1053+
1054+ # Merge excludes lists
1055+ excludes = runtime_env .get ("excludes" , [])
1056+ runtime_env ["excludes" ] = list (set (default_runtime_env ["excludes" ] + excludes ))
1057+
1058+ # Ensure env_vars exists
1059+ if "env_vars" not in runtime_env :
1060+ runtime_env ["env_vars" ] = {}
1061+ elif not isinstance (runtime_env ["env_vars" ], dict ):
1062+ runtime_env ["env_vars" ] = dict (runtime_env ["env_vars" ])
1063+
1064+ return ray_init_config
0 commit comments