-
Notifications
You must be signed in to change notification settings - Fork 927
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
madroidmaq
wants to merge
1
commit into
ml-explore:main
Choose a base branch
from
madroidmaq:sort-flag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -2,7 +2,6 @@ | |||||||
|
||||||||
import time | ||||||||
from dataclasses import dataclass, field | ||||||||
from functools import partial | ||||||||
from pathlib import Path | ||||||||
|
||||||||
import mlx.core as mx | ||||||||
|
@@ -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): | ||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
# 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||||||||
|
||||||||
|
@@ -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 | ||||||||
|
@@ -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) | ||||||||
|
@@ -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) | ||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
?