I’m on Linux Ubuntu. I’m confused that, if I initialize ray outside of main:
import ray
ray.init()
Then ray can run all trials in parallel. However, if I do not call init() or I call it inside main:
import ray
def main():
ray.init()
if __name__ == __main__:
main()
Then ray will only use 2 CPUs to run all trials.
What is the correct / best practices way of using ray.init()?
------------------- Update ------------------------------
It doesn’t seem to relate with main . It seems to relate to importing order:
Below will work
1 from utils_ray import load_config
2 ppo_config = load_config(path='./ts_ppo_config.yaml')
3 import ray
4 from ray import air
5 from ray import tune
6 from datetime import datetime
7 import os
8 import optuna
9 from myoptuna import OptunaSearch
10 from ray.tune.schedulers import ASHAScheduler
11
12
13 ray.init(
14 # so docker can listen to host port
15 dashboard_host=ppo_config.dashboard_host,
16 # redirect worker logs to driver stdout/err
17 log_to_driver=True,
18 logging_level=ppo_config.log_level)
19
20 # This is a customized Trainable class, I imported packages like torch in there
22 from ppo.ray_ppo import Policy
Below NOT work
1 from utils_ray import load_config
2 ppo_config = load_config(path='./ts_ppo_config.yaml')
3 import ray
4 from ray import air
5 from ray import tune
6 from datetime import datetime
7 import os
8 import optuna
9 from myoptuna import OptunaSearch
10 from ray.tune.schedulers import ASHAScheduler
22 from ppo.ray_ppo import Policy
11
12
13 ray.init(
14 # so docker can listen to host port
15 dashboard_host=ppo_config.dashboard_host,
16 # redirect worker logs to driver stdout/err
17 log_to_driver=True,
18 logging_level=ppo_config.log_level)
19
20 # This is a customized Trainable class, I imported packages like torch in there
I would love to know what’s the best practice of ray.init() in this case?