huggingface / transformers

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
https://huggingface.co/transformers
Apache License 2.0
134.64k stars 26.92k forks source link

Error using evaluation in run_clm.py #15898

Closed andstor closed 2 years ago

andstor commented 2 years ago

Environment info

Who can help

@stas00, @patil-suraj

Information

Model I am using: GPT-J-6B Running on GPU cluster with 10 x NVIDIA A100 40G

The problem arises when using:

The tasks I am working on is:

Purpose is to fine-tune GPT-J for generating smart contract code.

To reproduce

Steps to reproduce the behavior:

Run example training script transformers/examples/pytorch/language-modeling/run_clm.py.

HF launch script:

deepspeed --num_gpus=10 ./examples/pytorch/language-modeling/run_clm.py \
--deepspeed ./ds_config.json \
--model_name_or_path EleutherAI/gpt-j-6B \
--run_name gpt-j \
--dataset_name andstor/smart_contracts \
--dataset_config_name andstor--smart_contracts \
--output_dir ./finetuned \
--save_steps 100 \
--report_to all \
--logging_first_step \
--logging_steps 5 \
--evaluation_strategy steps \
--eval_steps 5 \
--block_size 1024 \
--do_train \
--do_eval \
--fp16 true \
--num_train_epochs 2 \
--gradient_accumulation_steps 2 \
--eval_accumulation_steps 2 \
--per_device_train_batch_size 1 \
--per_device_eval_batch_size 1

DeepSpeed config:

{
    "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": 2,
        "allgather_partitions": true,
        "allgather_bucket_size": 2e8,
        "overlap_comm": true,
        "reduce_scatter": true,
        "reduce_bucket_size": 2e8,
        "contiguous_gradients": true,
        "cpu_offload": true
    },
    "gradient_accumulation_steps": "auto",
    "gradient_clipping": "auto",
    "train_batch_size": "auto",
    "train_micro_batch_size_per_gpu": "auto",
    "wall_clock_breakdown": false
}

Fails with this error:

Traceback (most recent call last): 
  File "./examples/pytorch/language-modeling/run_clm.py", line 541, in <module> 
    main() 
  File "./examples/pytorch/language-modeling/run_clm.py", line 489, in main 
    train_result = trainer.train(resume_from_checkpoint=checkpoint) 
  File "/cluster/home/andstorh/transformers/.venv/lib/python3.8/site-packages/transformers/trainer.py", line 1473, in train 
    self._maybe_log_save_evaluate(tr_loss, model, trial, epoch, ignore_keys_for_eval) 
  File "/cluster/home/andstorh/transformers/.venv/lib/python3.8/site-packages/transformers/trainer.py", line 1600, in _maybe_log_save_evaluate 
    metrics = self.evaluate(ignore_keys=ignore_keys_for_eval) 
  File "/cluster/home/andstorh/transformers/.venv/lib/python3.8/site-packages/transformers/trainer.py", line 2255, in evaluate 
    output = eval_loop( 
  File "/cluster/home/andstorh/transformers/.venv/lib/python3.8/site-packages/transformers/trainer.py", line 2446, in evaluation_loop 
    logits = self.preprocess_logits_for_metrics(logits, labels) 
  File "./examples/pytorch/language-modeling/run_clm.py", line 457, in preprocess_logits_for_metrics 
    return logits.argmax(dim=-1) 
AttributeError: 'tuple' object has no attribute 'argmax' 

The model runs fine without evaluation turned on.

Expected behavior

The example script should run without producing an error.

stas00 commented 2 years ago

Most likely this PR https://github.com/huggingface/transformers/pull/15473 introduced the breakage, since this code didn't exist before:

https://github.com/huggingface/transformers/blob/130b987880a9b1ade5c76dc1413c12c8924fda50/examples/pytorch/language-modeling/run_clm.py#L456-L457

Most likely the new code has been merged w/o a test exercising this particular code path and your use case triggered the issue.

pinging the author: @davidleonfdez and the reviewer: @sgugger

to unblock you, @andstor, until this is sorted out please switch to a commit before that PR, that is:

git clone https://github.com/huggingface/transformers
cd transformers
git checkout 4f5faaf04407d4
sgugger commented 2 years ago

Indeed, the problem comes from the model returning more than one logit (it has use_cache set to True in its config) which we didn't anticipate in that PR. I will send a fix when I have time.

andstor commented 2 years ago

Thanks ❤️ Switch to the commit before that PR did do the trick 👌

davidleonfdez commented 2 years ago

Indeed, the problem comes from the model returning more than one logit (it has use_cache set to True in its config) which we didn't anticipate in that PR. I will send a fix when I have time.

Sorry, maybe I wasn't as careful with the examples as I should have been 😞. I've just learned about past_key_values. I had tested the example with GPT2, whose config has keys_to_ignore_at_inference = ["past_key_values"], so it doesn't return a tuple. I can try to fix it.

sgugger commented 2 years ago

@davidleonfdez If you want to work on a fix, look at how the compute_metrics in the run_glue script is defined. I believe you just need to add a similar test as this one at the beginning of the preprocess_logits_for_metrics function for the case where the model returns more than one logit.

davidleonfdez commented 2 years ago

@davidleonfdez If you want to work on a fix, look at how the compute_metrics in the run_glue script is defined. I believe you just need to add a similar test as this one at the beginning of the preprocess_logits_for_metrics function for the case where the model returns more than one logit.

Thanks!