# Parse additional arguments to the program when running the wandb agent command from the command line

**URL:** <https://community.wandb.ai/t/parse-additional-arguments-to-the-program-when-running-the-wandb-agent-command-from-the-command-line/4010>\
**Category:** W&B Help\
**Tags:** sweeps, wandb\
**Created:** [March 6, 2023, 8:09pm UTC](https://community.wandb.ai/t/parse-additional-arguments-to-the-program-when-running-the-wandb-agent-command-from-the-command-line/4010 "2023-03-06T20:09:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![liu97](https://avatars.discourse-cdn.com/v4/letter/l/d2c977/32.png) [@liu97](https://community.wandb.ai/u/liu97)\
**Post date:** [March 6, 2023, 8:09pm UTC](https://community.wandb.ai/t/parse-additional-arguments-to-the-program-when-running-the-wandb-agent-command-from-the-command-line/4010/1 "2023-03-06T20:09:58Z")

</div>

Is it possible to parse additional arguments to the program when running the wandb agent command from the command line?

For example, suppose I have a script `train.py` that takes a `--gpu_idx` argument to specify the GPU index, and I want to run the script with different GPUs using the WandB agent. Can I pass the `--gpu_idx` argument as a key-value pair when running the `wandb agent` command?

`wandb agent <ID> --gpu_idx 1`

In the training script, I have something like:

```auto
import wandb
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--gpu_idx', type=int, default=1)
args = parser.parse_args()

wandb.init()
wandb.config.update(args)

# train model with the learning rate

```

---

<div class="post-metadata">

**Author:** ![thanos-wandb](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@thanos-wandb](https://community.wandb.ai/u/thanos-wandb)\
**Post date:** [March 9, 2023, 11:24pm UTC](https://community.wandb.ai/t/parse-additional-arguments-to-the-program-when-running-the-wandb-agent-command-from-the-command-line/4010/2 "2023-03-09T23:24:18Z")

</div>

Hi @liu97 thank you for writing in! You could parse arguments to your `train.py` script, you will need to edit your `yaml` file such that it contains the `command` field too as follows:

```auto
program:
  train.py
method: grid
parameters:
  batch_size:
    value: 8
  lr:
    value: 0.0001
command:
  - ${env}
  - python
  - ${program}
  - "--gpu_idx=1"

```

Would this work for you? Please also check our [documentation here](https://docs.wandb.ai/guides/sweeps/define-sweep-configuration#command-) on the `command`.

---

<div class="post-metadata">

**Author:** ![liu97](https://avatars.discourse-cdn.com/v4/letter/l/d2c977/32.png) [@liu97](https://community.wandb.ai/u/liu97)\
**Post date:** [March 10, 2023, 1:45am UTC](https://community.wandb.ai/t/parse-additional-arguments-to-the-program-when-running-the-wandb-agent-command-from-the-command-line/4010/3 "2023-03-10T01:45:38Z")

</div>

Thank you very much! I will try it out.

---

<div class="post-metadata">

**Author:** ![thanos-wandb](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@thanos-wandb](https://community.wandb.ai/u/thanos-wandb)\
**Post date:** [March 15, 2023, 10:10am UTC](https://community.wandb.ai/t/parse-additional-arguments-to-the-program-when-running-the-wandb-agent-command-from-the-command-line/4010/4 "2023-03-15T10:10:28Z")

</div>

Hi @liu97 just checking in here to see if that resolved the issue, and if you had any other questions? thanks

---

<div class="post-metadata">

**Author:** ![liu97](https://avatars.discourse-cdn.com/v4/letter/l/d2c977/32.png) [@liu97](https://community.wandb.ai/u/liu97)\
**Post date:** [March 15, 2023, 2:52pm UTC](https://community.wandb.ai/t/parse-additional-arguments-to-the-program-when-running-the-wandb-agent-command-from-the-command-line/4010/5 "2023-03-15T14:52:46Z")

</div>

Hi, I didn’t run hyper-parameter search with wandb sweep recently, so did not have the chance to test it. I will post updates here.

Just a question, after creating a sweep agent using the above example yaml file, it will use --gpu\_idx=1 to sweep through hyper parameters by default. If I run this agent on other machines, where GPU 0 instead of GPU 1 is available, would I be able to parse this --gpu\_idx=0 when running `wandb agent sweep_id --gpu_idx=0`?

---

<div class="post-metadata">

**Author:** ![thanos-wandb](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@thanos-wandb](https://community.wandb.ai/u/thanos-wandb)\
**Post date:** [March 20, 2023, 8:37pm UTC](https://community.wandb.ai/t/parse-additional-arguments-to-the-program-when-running-the-wandb-agent-command-from-the-command-line/4010/6 "2023-03-20T20:37:48Z")

</div>

Hi @liu97 thanks for the additional context. In that case you won’t be able to do this as you will be using the same `yaml` for all agents. What you could do though in a multi-gpu environment is to specify the GPU as follows:

```auto
CUDA_VISIBLE_DEVICES=0 wandb agent sweep_ID

```

Would this work for you? Please also check [this docs page](https://docs.wandb.ai/guides/sweeps/parallelize-agents#parallelize-on-a-multi-gpu-machine) for more information.

---

<div class="post-metadata">

**Author:** ![thanos-wandb](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@thanos-wandb](https://community.wandb.ai/u/thanos-wandb)\
**Post date:** [March 24, 2023, 10:22am UTC](https://community.wandb.ai/t/parse-additional-arguments-to-the-program-when-running-the-wandb-agent-command-from-the-command-line/4010/7 "2023-03-24T10:22:03Z")

</div>

Hi @liu97 I wanted to follow up with you and see if you’ve managed to run the `wandb agent` in multiple GPUs? and if there are any further questions that we could help with.

---

<div class="post-metadata">

**Author:** ![liu97](https://avatars.discourse-cdn.com/v4/letter/l/d2c977/32.png) [@liu97](https://community.wandb.ai/u/liu97)\
**Post date:** [March 29, 2023, 6:31pm UTC](https://community.wandb.ai/t/parse-additional-arguments-to-the-program-when-running-the-wandb-agent-command-from-the-command-line/4010/8 "2023-03-29T18:31:47Z")

</div>

Hi Thanos, thank you very much for the help all the way! I appreciate it. Setting CUDA\_VISIBLE\_DEVICES is a viable way for me.

---

<div class="post-metadata">

**Author:** ![thanos-wandb](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@thanos-wandb](https://community.wandb.ai/u/thanos-wandb)\
**Post date:** [April 2, 2023, 1:32pm UTC](https://community.wandb.ai/t/parse-additional-arguments-to-the-program-when-running-the-wandb-agent-command-from-the-command-line/4010/9 "2023-04-02T13:32:45Z")

</div>

Hi @liu97 perfect, glad to hear this! I will now close this ticket, and please feel free to contact us again if you have any other questions.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/wandb/original/1X/366b649231631dbab896843020da0056074ac79d.png) [@system](https://community.wandb.ai/u/system)\
**Post date:** [June 1, 2023, 1:33pm UTC](https://community.wandb.ai/t/parse-additional-arguments-to-the-program-when-running-the-wandb-agent-command-from-the-command-line/4010/10 "2023-06-01T13:33:20Z")

</div>

Closed on June 1, 2023, 1:33pm UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
