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

LoRA: add sort data flag #611

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions llms/mlx_lm/lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ def build_parser():
type=str,
help="Directory with {train, valid, test}.jsonl files",
)
parser.add_argument(
"--sort-by-data-length",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind changing this to --sort-data-by-length?

action="store_true",
help="Sorts sequences by length to reduce padding and enhance efficiency.",
)
parser.add_argument(
"--lora-layers",
type=int,
Expand Down Expand Up @@ -196,6 +201,7 @@ def run(args, training_callback: TrainingCallback = None):
adapter_file=args.adapter_file,
max_seq_length=args.max_seq_length,
grad_checkpoint=args.grad_checkpoint,
sort_by_data_length=args.sort_by_data_length,
)

model.train()
Expand Down
50 changes: 38 additions & 12 deletions llms/mlx_lm/tuner/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import time
from dataclasses import dataclass, field
from functools import partial
from pathlib import Path

import mlx.core as mx
Expand Down Expand Up @@ -61,6 +60,12 @@ class TrainingArgs:
default=False,
metadata={"help": "Use gradient checkpointing to reduce memory use."},
)
sort_by_data_length: bool = field(
default=False,
metadata={
"help": "Sorts sequences by length to reduce padding and enhance efficiency."
},
)


def default_loss(model, inputs, targets, lengths):
Expand All @@ -76,19 +81,37 @@ def default_loss(model, inputs, targets, lengths):
return ce, ntoks


def iterate_batches(dataset, tokenizer, batch_size, max_seq_length, train=False):
# Sort by length:
idx = sorted(range(len(dataset)), key=lambda idx: len(dataset[idx]))

# Make the batches:
batch_idx = [
idx[i : i + batch_size] for i in range(0, len(idx) - batch_size + 1, batch_size)
]
def iterate_batches(
dataset,
tokenizer,
batch_size,
max_seq_length,
train=False,
sort_by_data_length=False,
):
if sort_by_data_length:
# Sort by length
idx = sorted(range(len(dataset)), key=lambda idx: len(dataset[idx]))
# Make batches
batch_idx = [
idx[i : i + batch_size]
for i in range(0, len(idx) - batch_size + 1, batch_size)
]
else:
# Shuffle indices
indices = np.arange(len(dataset))
indices = np.random.permutation(indices)
Comment on lines +102 to +103
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
indices = np.arange(len(dataset))
indices = np.random.permutation(indices)
indices = np.random.permutation(len(dataset))

# Make batches
batch_idx = [
indices[i : i + batch_size]
for i in range(0, len(indices) - batch_size + 1, batch_size)
]
Comment on lines +104 to +108
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you pull this out of the if statement and remove the duplicate one above?


while True:
indices = np.random.permutation(len(batch_idx))
for i in indices:
# Encode batch
# Randomize batch order
batch_indices = np.random.permutation(len(batch_idx))

for i in batch_indices:
batch = [tokenizer.encode(dataset[j]) for j in batch_idx[i]]
lengths = [len(x) for x in batch]

Expand Down Expand Up @@ -129,6 +152,7 @@ def evaluate(
max_seq_length=2048,
loss: callable = default_loss,
iterate_batches: callable = iterate_batches,
sort_by_data_length: bool = False,
):
all_losses = []
ntokens = 0
Expand All @@ -139,6 +163,7 @@ def evaluate(
tokenizer=tokenizer,
batch_size=batch_size,
max_seq_length=max_seq_length,
sort_by_data_length=sort_by_data_length,
),
):
losses, toks = loss(model, *batch)
Expand Down Expand Up @@ -213,6 +238,7 @@ def step(batch):
batch_size=args.batch_size,
max_seq_length=args.max_seq_length,
train=True,
sort_by_data_length=args.sort_by_data_length,
),
):
lvalue, toks = step(batch)
Expand Down