# Main code executed twice per process. Normal behaviour?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmain-code-executed-twice-per-process-normal-behaviour%2F11863%3E%5C
**Category:** 🤗Accelerate\
**Created:** [November 16, 2021, 10:01am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmain-code-executed-twice-per-process-normal-behaviour%2F11863 "2021-11-16T10:01:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pzurrer](https://avatars.discourse-cdn.com/v4/letter/p/a3d4f5/32.png) [@pzurrer](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fpzurrer)%5C
**Post date:** [November 16, 2021, 10:01am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmain-code-executed-twice-per-process-normal-behaviour%2F11863%2F1 "2021-11-16T10:01:19Z")

</div>

Hello everyone. I am just getting started with accelerate and distributed training in general. To test the number of GPUs used I created a simple script containing a simple main function:

```auto
def main():
    
    deepspeed_plugin = DeepSpeedPlugin(zero_stage=3, gradient_accumulation_steps=1)
    accelerator = Accelerator(fp16=True, deepspeed_plugin=deepspeed_plugin)

    print(f'Num Processes: {accelerator.num_processes}; Device: {accelerator.device}; Process Index: {accelerator.process_index}')

```

I launch this with `CUDA_VISIBLE_DEVICES=0,1 accelerate launch --config_file accelerate_config.yaml accelerate_test.py`. I get the following output:

```auto
Num Processes: 2; Device: cuda:0; Process Index: 0
Num Processes: 2; Device: cuda:0; Process Index: 0
Num Processes: 2; Device: cuda:1; Process Index: 1
Num Processes: 2; Device: cuda:1; Process Index: 1

```

It seems like `main` is executed twice for each process. My question is whether this is expected behaviour?

`accelerate_config.yaml` contains:

```auto
compute_environment: LOCAL_MACHINE
deepspeed_config:
  gradient_accumulation_steps: 1
  offload_optimizer_device: cpu
  zero_stage: 3
distributed_type: DEEPSPEED
fp16: false
machine_rank: 0
main_process_ip: null
main_process_port: null
main_training_function: main
num_machines: 1
num_processes: 2

```

Thank you.

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsgugger)%5C
**Post date:** [November 16, 2021, 12:38pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmain-code-executed-twice-per-process-normal-behaviour%2F11863%2F2 "2021-11-16T12:38:37Z")

</div>

This is very weird, it’s not supposed to happen indeed, you should only see two print statements.

---

<div class="post-metadata">

**Author:** ![pzurrer](https://avatars.discourse-cdn.com/v4/letter/p/a3d4f5/32.png) [@pzurrer](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fpzurrer)%5C
**Post date:** [November 16, 2021, 10:23pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmain-code-executed-twice-per-process-normal-behaviour%2F11863%2F3 "2021-11-16T22:23:05Z")

</div>

Hmm yeah. Not really sure how to debug what is happening under the hood. I use Pytorch 1.8.0 and I am running the newest versions of both accelerate and deepspeed. Is this something you can reproduce on your end as well or could it be related to my setup?

---

<div class="post-metadata">

**Author:** ![pzurrer](https://avatars.discourse-cdn.com/v4/letter/p/a3d4f5/32.png) [@pzurrer](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fpzurrer)%5C
**Post date:** [November 17, 2021, 10:01am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmain-code-executed-twice-per-process-normal-behaviour%2F11863%2F4 "2021-11-17T10:01:00Z")

</div>

Ah I rechecked and figured it out. I made a stupid formatting error and main was accidentally called twice in my script. Everything works as expected. Sorry for the inconveniences.
