[Python] RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn...

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    This answer did not solve my problem.

    I was trying to fine tune llama model locally using LoRA, peft. Here is my code:

    import torch
    from datasets import load_dataset
    from transformers import (
    AutoModelForCausalLM,
    AutoTokenizer,
    BitsAndBytesConfig,
    TrainingArguments,
    pipeline,
    logging,
    )
    from peft import LoraConfig, PeftModel, get_peft_model, prepare_model_for_kbit_training
    from trl import SFTTrainer

    base_model_id = "/ml/models/Meta-Llama-3.1-8B-Instruct"
    new_model = "./models/Meta-llama3-8b-SFT2"
    dataset = load_dataset('json', data_files='./datasets/done.jsonl', split='train')

    def preprocess_function(examples):
    examples['text'] = [f"question: {q}, answer: {a}, url: {u}" for q, a, u in zip(examples['question'], examples['answer'], examples['url'])]
    return {'text': examples['text']}

    preprocessed_dataset = dataset.map(preprocess_function, batched=True)
    preprocessed_dataset = preprocessed_dataset.remove_columns(['chunk_id', 'question', 'answer', 'url'])

    bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_use_double_quant=True,
    )

    model = AutoModelForCausalLM.from_pretrained(base_model_id, quantization_config=bnb_config, device_map="auto")
    tokenizer = AutoTokenizer.from_pretrained(base_model_id, add_eos_token=True, add_bos_token=True)
    tokenizer.pad_token = tokenizer.eos_token

    peft_config = LoraConfig(task_type="CAUSAL_LM", r=2, lora_alpha=16, lora_dropout=0.01)
    model = prepare_model_for_kbit_training(model)
    model = get_peft_model(model, peft_config)
    model.print_trainable_parameters()

    training_arguments = TrainingArguments(
    output_dir="./results",
    num_train_epochs=1,
    per_device_train_batch_size=1,
    gradient_accumulation_steps=16,
    optim="paged_adamw_32bit",
    save_steps=100,
    logging_steps=5,
    learning_rate=2e-4,
    weight_decay=0.001,
    bf16=True,
    max_grad_norm=0.3,
    max_steps=500,
    warmup_ratio=0.03,
    group_by_length=True,
    gradient_checkpointing=True,
    report_to="tensorboard",
    )

    trainer = SFTTrainer(
    model=model,
    train_dataset=preprocessed_dataset,
    tokenizer=tokenizer,
    args=training_arguments,
    )

    trainer.train()
    trainer.model.save_pretrained(new_model)


    However, this code throws an error:

    ---------------------------------------------------------------------------
    RuntimeError Traceback (most recent call last)
    Cell In[2], line 68
    60 trainer = SFTTrainer(
    61 model=model,
    62 train_dataset=dataset,
    63 tokenizer=tokenizer,
    64 args=training_arguments,
    65 )
    67 # Train the model
    ---> 68 trainer.train()
    70 # Save the trained model
    71 trainer.model.save_pretrained(new_model)

    File ~/venvs/fine_tuning_llama/lib/python3.11/site-packages/transformers/trainer.py:2164, in Trainer.train(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs)
    2162 hf_hub_utils.enable_progress_bars()
    2163 else:
    -> 2164 return inner_training_loop(
    2165 args=args,
    2166 resume_from_checkpoint=resume_from_checkpoint,
    2167 trial=trial,
    2168 ignore_keys_for_eval=ignore_keys_for_eval,
    2169 )

    File ~/venvs/fine_tuning_llama/lib/python3.11/site-packages/transformers/trainer.py:2522, in Trainer._inner_training_loop(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval)
    2516 context = (
    2517 functools.partial(self.accelerator.no_sync, model=model)
    2518 if i != len(batch_samples) - 1
    2519 else contextlib.nullcontext
    2520 )
    2521 with context():
    -> 2522 tr_loss_step = self.training_step(model, inputs, num_items_in_batch)
    2524 if (
    2525 args.logging_nan_inf_filter
    2526 and not is_torch_xla_available()
    2527 and (torch.isnan(tr_loss_step) or torch.isinf(tr_loss_step))
    2528 ):
    2529 # if loss is nan or inf simply add the average of previous logged losses
    2530 tr_loss = tr_loss + tr_loss / (1 + self.state.global_step - self._globalstep_last_logged)

    File ~/venvs/fine_tuning_llama/lib/python3.11/site-packages/transformers/trainer.py:3688, in Trainer.training_step(***failed resolving arguments***)
    3686 scaled_loss.backward()
    3687 else:
    -> 3688 self.accelerator.backward(loss, **kwargs)
    3689 # Finally we need to normalize the loss for reporting
    3690 if num_items_in_batch is None:

    File ~/venvs/fine_tuning_llama/lib/python3.11/site-packages/accelerate/accelerator.py:2248, in Accelerator.backward(self, loss, **kwargs)
    2246 self.lomo_backward(loss, learning_rate)
    2247 else:
    -> 2248 loss.backward(**kwargs)

    File ~/venvs/fine_tuning_llama/lib/python3.11/site-packages/torch/_tensor.py:581, in Tensor.backward(self, gradient, retain_graph, create_graph, inputs)
    571 if has_torch_function_unary(self):
    572 return handle_torch_function(
    573 Tensor.backward,
    574 (self,),
    (...)
    579 inputs=inputs,
    580 )
    --> 581 torch.autograd.backward(
    582 self, gradient, retain_graph, create_graph, inputs=inputs
    583 )

    File ~/venvs/fine_tuning_llama/lib/python3.11/site-packages/torch/autograd/__init__.py:347, in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)
    342 retain_graph = create_graph
    344 # The reason we repeat the same comment below is that
    345 # some Python versions print out the first line of a multi-line function
    346 # calls in the traceback and some print out the last line
    --> 347 _engine_run_backward(
    348 tensors,
    349 grad_tensors_,
    350 retain_graph,
    351 create_graph,
    352 inputs,
    353 allow_unreachable=True,
    354 accumulate_grad=True,
    355 )

    File ~/venvs/fine_tuning_llama/lib/python3.11/site-packages/torch/autograd/graph.py:825, in _engine_run_backward(t_outputs, *args, **kwargs)
    823 unregister_hooks = _register_logging_hooks_on_whole_graph(t_outputs)
    824 try:
    --> 825 return Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass
    826 t_outputs, *args, **kwargs
    827 ) # Calls into the C++ engine to run the backward pass
    828 finally:
    829 if attach_logging_hooks:

    RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn


    What should I do to resolve it?

    Continue reading...

Compartilhe esta Página