# Freezing layers with SFTTrainer

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffreezing-layers-with-sfttrainer%2F88951%3E%5C
**Category:** Intermediate\
**Created:** [May 28, 2024, 10:56am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffreezing-layers-with-sfttrainer%2F88951 "2024-05-28T10:56:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NofarSachs](https://avatars.discourse-cdn.com/v4/letter/n/b782af/32.png) [@NofarSachs](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FNofarSachs)%5C
**Post date:** [May 28, 2024, 10:56am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffreezing-layers-with-sfttrainer%2F88951%2F1 "2024-05-28T10:56:56Z")

</div>

Hi there!  
I try to freeze the embedding layer and the first transformer’s layer of Phi-3-mini-128k-instruct, and get the following error:

```auto
File "/home/user/anaconda3/envs/JupyterSystemEnv/lib/python3.10/site-packages/torch/autograd/ __init__.py", line 266, in backward
    Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

```

Code snippet:

```auto
    ################
    # Model & Tokenizer
    ################

    # Tokenizer        
    tokenizer = AutoTokenizer.from_pretrained(script_args.model_id, token=script_args.token, trust_remote_code=True, use_fast=True)
    tokenizer.pad_token = tokenizer.eos_token
    tokenizer.padding_side = 'right'

    # Model    
    torch_dtype = torch.bfloat16
    quant_storage_dtype = torch.bfloat16

    quantization_config = BitsAndBytesConfig(
            load_in_4bit=True,
            bnb_4bit_use_double_quant=True,
            bnb_4bit_quant_type="nf4",
            bnb_4bit_compute_dtype=torch_dtype,
            bnb_4bit_quant_storage=quant_storage_dtype,
        )

    model = AutoModelForCausalLM.from_pretrained(
        script_args.model_id,
        token=script_args.token,
        trust_remote_code=True,
        quantization_config=quantization_config,
        #attn_implementation="sdpa", # use sdpa, alternatively use "flash_attention_2"
        torch_dtype=quant_storage_dtype,
        use_cache=False if training_args.gradient_checkpointing else True, # this is needed for gradient checkpointing
    )
    
    if training_args.gradient_checkpointing:
        model.gradient_checkpointing_enable()
    ################
    # PEFT
    ################

    # LoRA config based on QLoRA paper & Sebastian Raschka experiment
    peft_config = LoraConfig(
        lora_alpha=8,
        lora_dropout=0.05,
        r=16,
        bias="none",
        target_modules='all-linear',
        task_type="CAUSAL_LM",
        # modules_to_save = ["lm_head", "embed_tokens"] # add if you want to use the Llama 3 instruct template
    )
    
    peft_model = PeftModel(model, peft_config)
    print(peft_model)
    for param in peft_model.base_model.model.model.layers[0].parameters():
          param.requires_grad = False
    for param in peft_model.base_model.model.model.embed_tokens.parameters():
          param.requires_grad = False
    peft_model.print_trainable_parameters()
    

    ################
    # Training
    ################
    trainer = SFTTrainer(
        model=peft_model,
        args=training_args,
        train_dataset=train_dataset,
        dataset_text_field="content",
        # peft_config=peft_config,
        max_seq_length=script_args.max_seq_length,
        tokenizer=tokenizer,
        packing=True,
        dataset_kwargs={
            "add_special_tokens": False, # We template with special tokens
            "append_concat_token": False, # No need to add additional separator token
        },
    )
    if trainer.accelerator.is_main_process:
        trainer.model.print_trainable_parameters()

    ##########################
    # Train model
    ##########################
    checkpoint = None
    if training_args.resume_from_checkpoint is not None:
        checkpoint = training_args.resume_from_checkpoint
    trainer.train(resume_from_checkpoint=checkpoint)

```

Any help will be appreciated 🙂

---

<div class="post-metadata">

**Author:** ![HenryLiu0820](https://avatars.discourse-cdn.com/v4/letter/h/94ad74/32.png) [@HenryLiu0820](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FHenryLiu0820)%5C
**Post date:** [March 8, 2025, 12:20am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffreezing-layers-with-sfttrainer%2F88951%2F2 "2025-03-08T00:20:01Z")

</div>

Hi. I had the exact same issue as yours. Have you found a solution?

I also found that freezing the first embed\_tokens layer will cause this problem, but freezing other layers doesn’t

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [March 8, 2025, 5:50am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffreezing-layers-with-sfttrainer%2F88951%2F3 "2025-03-08T05:50:53Z")

</div>

Similar issue?

> <https://github.com/huggingface/peft/issues/137>
>
> Hi!
> 
> I tried to use peft model with Seq2SeqTrainer, got the next error:
> \`\`\`
> …Variable.\_execution\_engine.run\_backward( # Calls into the C++ engine to run the backward pass
> RuntimeError: element 0 of tensors does not require grad and does not have a grad\_fn
> \`\`\`
> 
> Code:
> 
> \`\`\`
> tokenizer = AutoTokenizer.from\_pretrained(args.model\_id)
> model = AutoModelForSeq2SeqLM.from\_pretrained(
> "google/flan-t5-large",
> use\_cache=False if args.gradient\_checkpointing else True, 
> )
> 
> lora\_config = LoraConfig(
> r=16,
> lora\_alpha=32,
> target\_modules=\["q", "v"\],
> lora\_dropout=0.05,
> bias="none",
> task\_type="SEQ\_2\_SEQ\_LM"
> )
> 
> model = get\_peft\_model(model, lora\_config)
> 
> 
> trainer = Seq2SeqTrainer(
> model=model,
> args=training\_args,
> train\_dataset=train\_dataset,
> eval\_dataset=eval\_dataset,
> )
> \`\`\`
