# SFTTrainer checkpointing

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsfttrainer-checkpointing%2F50462%3E%5C
**Category:** 🤗Transformers\
**Created:** [August 11, 2023, 6:05pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsfttrainer-checkpointing%2F50462 "2023-08-11T18:05:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![samlhuillier](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@samlhuillier](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsamlhuillier)%5C
**Post date:** [August 11, 2023, 6:05pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsfttrainer-checkpointing%2F50462%2F1 "2023-08-11T18:05:35Z")

</div>

Hey I’m trying to finetune Llama 2 and I can’t see where the checkpoints are getting saved. I am using the following code:

```auto
base_model_name = "meta-llama/Llama-2-7b-hf"

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.float16,
)

device_map = "auto"

base_model = AutoModelForCausalLM.from_pretrained(
    base_model_name,
    quantization_config=bnb_config,
    device_map=device_map,
    trust_remote_code=True,
    use_auth_token=True
)
base_model.config.use_cache = False

# More info: https://github.com/huggingface/transformers/pull/24906
base_model.config.pretraining_tp = 1 

peft_config = LoraConfig(
    lora_alpha=16,
    lora_dropout=0.1,
    r=64,
    bias="none",
    task_type="CAUSAL_LM",
)

tokenizer = AutoTokenizer.from_pretrained(base_model_name, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token

output_dir = "./Llama-2-7b-hf-qlora"

training_args = TrainingArguments(
    output_dir=output_dir,
    per_device_train_batch_size=4,
    gradient_accumulation_steps=4,
    learning_rate=2e-4,
    logging_steps=5,
    max_steps=400,
    evaluation_strategy="steps", # Evaluate the model every logging step
    logging_dir="./logs", # Directory for storing logs
    save_strategy="steps", # Save the model checkpoint every logging step
    eval_steps=5, # Evaluate and save checkpoints every 10 steps
    do_eval=True # Perform evaluation at the end of training
)
class PeftSavingCallback(TrainerCallback):
    def on_save(self, args, state, control, **kwargs):
        checkpoint_path = os.path.join(args.output_dir, f"checkpoint-{state.global_step}")
        kwargs["model"].save_pretrained(checkpoint_path)

        if "pytorch_model.bin" in os.listdir(checkpoint_path):
            os.remove(os.path.join(checkpoint_path, "pytorch_model.bin"))

callbacks = [PeftSavingCallback()]
max_seq_length = 512
trainer = SFTTrainer(
    model=base_model,
    train_dataset=train_dataset,
    eval_dataset=eval_dataset, # Add this line
    peft_config=peft_config,
    dataset_text_field="text",
    max_seq_length=max_seq_length,
    tokenizer=tokenizer,
    args=training_args,
    callbacks=callbacks
)

trainer.train()

```

(I added in the callback stuff based on this guide [Supervised Fine-tuning Trainer](https://huggingface.co/docs/trl/sft_trainer))  
How do I get a checkpoint saved every 5/10 steps?

---

<div class="post-metadata">

**Author:** ![samlhuillier](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@samlhuillier](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsamlhuillier)%5C
**Post date:** [August 11, 2023, 7:20pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsfttrainer-checkpointing%2F50462%2F2 "2023-08-11T19:20:05Z")

</div>

Worked this out…Fairly simple in the end: just adding save\_steps to TrainingArguments does the trick!

---

<div class="post-metadata">

**Author:** ![gaurav-imp](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gaurav-imp](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fgaurav-imp)%5C
**Post date:** [August 13, 2023, 6:01am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsfttrainer-checkpointing%2F50462%2F3 "2023-08-13T06:01:24Z")

</div>

Though you have answered the question, just adding a snippet for sake of completeness.

```auto

# Set training parameters
training_arguments = TrainingArguments(
    output_dir=output_dir,
    num_train_epochs=num_train_epochs,
    per_device_train_batch_size=per_device_train_batch_size,
    gradient_accumulation_steps=gradient_accumulation_steps,
    optim=optim,
    save_steps=save_steps, # saves the model at specified step....
    logging_steps=logging_steps,
    learning_rate=learning_rate,
    weight_decay=weight_decay,
    fp16=fp16,
    bf16=bf16,
    max_grad_norm=max_grad_norm,
    max_steps=max_steps,
    warmup_ratio=warmup_ratio,
    group_by_length=group_by_length,
    lr_scheduler_type=lr_scheduler_type,
    report_to="tensorboard"
)

trainer = SFTTrainer(
    model=model,
    train_dataset=dataset,
    peft_config=peft_config,
    dataset_text_field="text",
    max_seq_length=max_seq_length,
    tokenizer=tokenizer,
    args=training_arguments,
    packing=packing,
)

```

---

<div class="post-metadata">

**Author:** ![AirinElizabath](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@AirinElizabath](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FAirinElizabath)%5C
**Post date:** [August 23, 2023, 12:43pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsfttrainer-checkpointing%2F50462%2F4 "2023-08-23T12:43:09Z")

</div>

Are you able to resume training from the checkpoint or able to load the model from checkpoint. I could not do either. I used SFTTrainer? Once the model is trained, I could not find config.json file as well.

---

<div class="post-metadata">

**Author:** ![samlhuillier](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@samlhuillier](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsamlhuillier)%5C
**Post date:** [August 25, 2023, 1:39pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsfttrainer-checkpointing%2F50462%2F5 "2023-08-25T13:39:38Z")

</div>

do this: trainer.train(resume\_from\_checkpoint=true)

---

<div class="post-metadata">

**Author:** ![AirinElizabath](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@AirinElizabath](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FAirinElizabath)%5C
**Post date:** [August 25, 2023, 2:06pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsfttrainer-checkpointing%2F50462%2F6 "2023-08-25T14:06:24Z")

</div>

Thank you. The actual problem was that, I was using an older version of transformer. The problem is solved in the second last release.

---

<div class="post-metadata">

**Author:** ![RadAlienware](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/radalienware/32/26288_2.png) [@RadAlienware](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FRadAlienware)%5C
**Post date:** [January 21, 2024, 8:19pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsfttrainer-checkpointing%2F50462%2F7 "2024-01-21T20:19:00Z")

</div>

Hey, I am trying to run mistral model from checkpoint. But the saving checkpoints are missing a file like pytorch\_model.bin.index.json is not saving. Any idea of that?
