don't use no_sync when deepspeed doesn't support it for certain zero stages
Author: winglian
Pull request: yes

# What does this PR do?
Deepspeed 0.16 has assertions preventing the use of no_sync with zero 2/3. see https://github.com/microsoft/DeepSpeed/blob/master/deepspeed/runtime/engine.py#L1986-L2004

it seems people are reporting this here https://github.com/microsoft/DeepSpeed/issues/6793, and I'm assuming that everyone is using accelerate/transformers as downgrading to deepspeed 0.15.4 makes it "work" for them.

## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

@muellerzr 

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker
- vision models: @amyeroberts, @qubvel
- speech models: @ylacombe, @eustlb
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Rocketknight1
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @SunMarc
- chat templates: @Rocketknight1

Integrations:

- deepspeed: HF Trainer/Accelerate: @muellerzr
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc @MekkCyber

Documentation: @stevhliu

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @sanchit-gandhi
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


Comments:
might have to broaden deepspeed for all zero cases? https://github.com/microsoft/DeepSpeed/blob/master/deepspeed/runtime/engine.py#L2208-L2209

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_35157). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

can you just run `make fixup` 

Strangely, this issue still exists on `deepspeed==0.16.2`, has this fix been pushed to a stable release yet?

This is on 4.48 !

With transformers==4.48.0, accelerate==1.2.1 and deepspeed==0.16.3, still see this issue

Hi @ArthurZucker @muellerzr , we still face the same issue with transformers==4.48.0/4.49.0, accelerate==1.2.1 and deepspeed==0.16.3. Could you check this?

```text
error: no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3
```

Can you share the full traceback @jianguoz and with a minimal reproducer @jianguoz thanks ! 

Hi @SunMarc @ArthurZucker  @muellerzr  , Below is the my zero 3 config and  error output when fine-tuning a mistral_small_24b or llama model on 8 GPUs. It can only work with `gradient_accumulation=1`. I believe people will face same issue when training a model using the latest `transformer>=4.48.0` and `deepspeed>=0.16.0` and `accelerate==1.2.1/1.4.0`. 

```
[2025-03-04 19:03:35,551] [INFO] [config.py:991:print_user_config]   json = {
    "bf16": {
        "enabled": true
    }, 
    "optimizer": {
        "type": "AdamW", 
        "params": {
            "lr": 2e-05, 
            "weight_decay": 0
        }
    }, 
    "scheduler": {
        "type": "WarmupDecayLR", 
        "params": {
            "warmup_min_lr": 1e-06, 
            "warmup_max_lr": 2e-05, 
            "warmup_num_steps": 100, 
            "total_num_steps": 1.259000e+03
        }
    }, 
    "zero_optimization": {
        "stage": 3, 
        "overlap_comm": true, 
        "contiguous_gradients": true, 
        "sub_group_size": 1.000000e+09, 
        "reduce_bucket_size": 2.621440e+07, 
        "stage3_prefetch_bucket_size": 0, 
        "stage3_param_persistence_threshold": 5.120000e+04, 
        "stage3_max_live_parameters": 1.000000e+09, 
        "stage3_max_reuse_distance": 1.000000e+09, 
        "stage3_gather_16bit_weights_on_model_save": true
    }, 
    "gradient_accumulation_steps": 3, 
    "gradient_clipping": 1.0, 
    "steps_per_print": inf, 
    "train_batch_size": 48, 
    "train_micro_batch_size_per_gpu": 2, 
    "wall_clock_breakdown": false, 
    "fp16": {
        "enabled": false
    }
}
```

output 
```
step = 0 -- rank 0: error: no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3
  0%|                                                                                                                                     | 1/10072 [01:15<210:10:00, 75.13s/it]step = 1 -- rank 0: error: step = 0 -- rank 1: error: no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3
no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3
  0%|                                                                                                                                     | 1/10072 [01:15<210:30:16, 75.25s/it]step = 0 -- rank 2: error:step = 0 -- rank 5: error:  step = 0 -- rank 3: error:no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3step = 0 -- rank 6: error: 
step = 0 -- rank 7: error:
no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3  
no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3

step = 0 -- rank 4: error: no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3
step = 1 -- rank 1: error: no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3
  0%|                                                                                                                                     | 1/10072 [01:15<210:23:32, 75.21s/it]step = 1 -- rank 2: error: step = 1 -- rank 5: error:no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3 
no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3
step = 1 -- rank 4: error: no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3
step = 1 -- rank 7: error:step = 1 -- rank 6: error:  no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3no_sync context manager is incompatible with gradient partitioning logic of ZeRO stage 3
```

Hi @SunMarc , do we have updates for this issue? I saw others have same issues on #34984

cc @XuehaiPan 

Hi @SunMarc , any update for this incompatible issues between Huggingface and Deepspeed?