Argparse failed in trainable function

I have a tuning task using an imported parameter via argparse in trainable function. The task crashes complaining the argument is not provided. It works fine If I use it outside the trainable function. Any help is appreciated. The script being imported called “”:

import sys, argparse

parser = argparse.ArgumentParser(description='')
parser.add_argument('--ttt', type=int, required=True, help='anything > 1')
args = parser.parse_args()

ttt = args.ttt

The tuning task code is named as ‘’:

import os
from ray import tune, air
from hyperopt import hp
from import HyperOptSearch
import input_param as input_param

def trainable(config):
    #print('!! ttt = ', input_param.ttt)
    score = config["a"] ** 2 + config["b"]

search_space = {
    "a": hp.uniform("a", 0, 1),
    "b": hp.uniform("b", 0, 1)

raw_log_dir = "./ray_log"
raw_log_name = "example"

algorithm = HyperOptSearch(search_space, metric="SCORE", mode="max", n_initial_points=1)

tuner = tune.Tuner(trainable,
        tune_config = tune.TuneConfig(
            num_samples = 10,
        run_config = air.RunConfig(local_dir = raw_log_dir, name = raw_log_name) #

print('!! ttt = ', input_param.ttt)
results =
print(results.get_best_result(metric="SCORE", mode="max").config)

I run the task via the following command:

py --ttt 99

The following is part of the error:

(pid=19560) error: the following arguments are required: --ttt
(pid=19560) 2022-11-23 20:45:01,769     ERROR -- Worker exits with an exit code 2.

Each Ray worker spawns a separate process, therefore you will run into such issues. You should do all the argument parsing outside the trainable function, and pass the values in config or through tune.with_parameters.

1 Like