Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non-lora PEFT state into intermediate checkpoint saving. #1813

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions llava/train/llava_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ def get_mm_adapter_state_maybe_zero_3(named_params, keys_to_match):
return to_return


def get_peft_state_non_lora_maybe_zero_3(named_params, require_grad_only=True):
to_return = {k: t for k, t in named_params if "lora_" not in k}
if require_grad_only:
to_return = {k: t for k, t in to_return.items() if t.requires_grad}
to_return = {k: maybe_zero_3(v, ignore_status=True).cpu() for k, v in to_return.items()}
return to_return


def split_to_even_chunks(indices, lengths, num_chunks):
"""
Split a list of indices into `chunks` chunks of roughly equal lengths.
Expand Down Expand Up @@ -248,6 +256,14 @@ def _save_checkpoint(self, model, trial, metrics=None):
else:
super(LLaVATrainer, self)._save_checkpoint(model, trial, metrics)

# Save non-lora peft state
if getattr(self.args, 'lora_enable', False):
non_lora_state_dict = get_peft_state_non_lora_maybe_zero_3(
self.model.named_parameters()
)
if self.args.local_rank == 0 or self.args.local_rank == -1:
torch.save(non_lora_state_dict, os.path.join(output_dir, 'non_lora_trainables.bin'))

def _save(self, output_dir: Optional[str] = None, state_dict=None):
if getattr(self.args, 'tune_mm_mlp_adapter', False):
pass
Expand Down