Different behaviors of ray.init() in/outside of __main__ on Linux

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?

This seems weird. Could you share what’s the Policy there? I don’t expect this to happen, so it’ll be nice if we can have a minimal reproducible script.

Yes, of course. The complete project is here: GitHub - spacegoing/ray_ppo_trainable

Running scripts including optuna_mvp.py or pbt_mvp.py. Either one is ready to run and can reproduce that bug.

The trainable is implemented in
ppo/
ray_ppo.py

You need to pip install tianshou for running this.

env.yaml is simply for FYI. Please do not directly install it :stuck_out_tongue:

Please tell me if I can provide anything else.