# DeepSpeed Zero3 and Peft LoRA fp16 issue

**URL:** <https://discuss.huggingface.co/t/deepspeed-zero3-and-peft-lora-fp16-issue/32571>\
**Category:** Intermediate\
**Created:** [February 26, 2023, 7:25pm UTC](https://discuss.huggingface.co/t/deepspeed-zero3-and-peft-lora-fp16-issue/32571 "2023-02-26T19:25:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![conceptofmind](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/conceptofmind/32/55565_2.png) [@conceptofmind](https://discuss.huggingface.co/u/conceptofmind)\
**Post date:** [February 26, 2023, 7:25pm UTC](https://discuss.huggingface.co/t/deepspeed-zero3-and-peft-lora-fp16-issue/32571/1 "2023-02-26T19:25:34Z")

</div>

Hi all,

I am having an issue when running Peft LoRA with DeepSpeed Zero3.

Error:

```bash
ValueError: fp16 is enabled but the following parameters have dtype that is not fp16: base_model.model.gpt_neox.layers.0.attention.query_key_value.lora_A.weight, 
base_model.model.gpt_neox.layers.0.attention.query_key_value.lora_B.weight, base_model.model.gpt_neox.layers.1.attention.query_key_value.lora_A.weight, 
base_model.model.gpt_neox.layers.1.attention.query_key_value.lora_B.weight, base_model.model.gpt_neox.layers.2.attention.query_key_value.lora_A.weight, 
base_model.model.gpt_neox.layers.2.attention.query_key_value.lora_B.weight, base_model.model.gpt_neox.layers.3.attention.query_key_value.lora_A.weight,

```

How to reproduce:

CLI:

```bash
deepspeed finetune_pythia.py --per_device_train_batch_size 1 --output_dir /home/training_scripts/pythia-1.4b --fp16 --deepspeed configs/ds_z3_config.json

```

Code:

```python
from transformers import AutoTokenizer, GPTNeoXForCausalLM
from peft import get_peft_model, LoraConfig, TaskType

peft_config = LoraConfig(
  task_type=TaskType.CAUSAL_LM, 
  inference_mode=False, 
  r=16, 
  lora_alpha=32, 
  lora_dropout=0.1,
)

tokenizer = AutoTokenizer.from_pretrained("EleutherAI/pythia-1.4b")
model = GPTNeoXForCausalLM.from_pretrained("EleutherAI/pythia-1.4b")
model = get_peft_model(model, peft_config)
model.print_trainable_parameters()

```

DeepSpeed Zero 3 config:

```json
{
    "fp16": {
        "enabled": "auto",
        "loss_scale": 0,
        "loss_scale_window": 1000,
        "initial_scale_power": 16,
        "hysteresis": 2,
        "min_loss_scale": 1
    },

    "optimizer": {
        "type": "AdamW",
        "params": {
            "lr": "auto",
            "betas": "auto",
            "eps": "auto",
            "weight_decay": "auto"
        }
    },

    "scheduler": {
        "type": "WarmupLR",
        "params": {
            "warmup_min_lr": "auto",
            "warmup_max_lr": "auto",
            "warmup_num_steps": "auto"
        }
    },

    "zero_optimization": {
        "stage": 3,
        "offload_optimizer": {
            "device": "cpu",
            "pin_memory": true
        },
        "offload_param": {
            "device": "cpu",
            "pin_memory": true
        },
        "overlap_comm": true,
        "contiguous_gradients": true,
        "sub_group_size": 1e9,
        "reduce_bucket_size": "auto",
        "stage3_prefetch_bucket_size": "auto",
        "stage3_param_persistence_threshold": "auto",
        "stage3_max_live_parameters": 1e9,
        "stage3_max_reuse_distance": 1e9,
        "stage3_gather_16bit_weights_on_model_save": true
    },

    "gradient_accumulation_steps": "auto",
    "gradient_clipping": "auto",
    "steps_per_print": 2000,
    "train_batch_size": "auto",
    "train_micro_batch_size_per_gpu": "auto",
    "wall_clock_breakdown": false
}

```

Any help would be greatly appreciated.

Thank you,

Enrico

---

<div class="post-metadata">

**Author:** ![wuwuha](https://avatars.discourse-cdn.com/v4/letter/w/a9adbd/32.png) [@wuwuha](https://discuss.huggingface.co/u/wuwuha)\
**Post date:** [March 23, 2023, 7:21am UTC](https://discuss.huggingface.co/t/deepspeed-zero3-and-peft-lora-fp16-issue/32571/2 "2023-03-23T07:21:20Z")

</div>

Have solve it？ I meet same issue.

---

<div class="post-metadata">

**Author:** ![iwisher](https://avatars.discourse-cdn.com/v4/letter/i/50afbb/32.png) [@iwisher](https://discuss.huggingface.co/u/iwisher)\
**Post date:** [May 15, 2023, 1:21am UTC](https://discuss.huggingface.co/t/deepspeed-zero3-and-peft-lora-fp16-issue/32571/3 "2023-05-15T01:21:12Z")

</div>

Wrap your trainer with autocast,  
with torch.autocast(“cuda”):  
for epoch in range(args.epochs):

---

<div class="post-metadata">

**Author:** ![appvoid](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/appvoid/32/37041_2.png) [@appvoid](https://discuss.huggingface.co/u/appvoid)\
**Post date:** [May 24, 2023, 8:37pm UTC](https://discuss.huggingface.co/t/deepspeed-zero3-and-peft-lora-fp16-issue/32571/4 "2023-05-24T20:37:55Z")

</div>

**You saved my life!**
