Skip to content

Fix 'standardize_data_formats' when using iterable datasets #126

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

Open
wants to merge 1 commit 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
32 changes: 20 additions & 12 deletions unsloth_zoo/dataset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,10 @@ def standardize_data_formats(
if "conversations" not in column_names:
return dataset

convos = dataset[:10]["conversations"]
examples = itertools.islice(dataset, 10)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this specifically for iterable datasets? Ie itertools.islice works for iterable datasets whilst dataset[:10] does not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly, you can't use an index or in this case [:10] on iterators

uniques = collections.defaultdict(list)
for convo in convos:
for message in convo:
for example in examples:
for message in example["conversations"]:
Copy link
Contributor

Choose a reason for hiding this comment

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

The main reason why I did "conversations" outside is to make it somewhat faster, but I guess since its 10 examples, no big deal

for key, value in message.items():
if type(value) is not str:
raise RuntimeError("Unsloth: Cannot standardize non text datasets!")
Expand Down Expand Up @@ -464,15 +464,23 @@ def _standardize_dataset(examples):
return { "conversations" : all_convos, }
pass

from multiprocessing import cpu_count
num_proc = cpu_count()

return dataset.map(
_standardize_dataset,
batched = True,
desc = "Unsloth: Standardizing formats",
num_proc = num_proc,
)
if isinstance(dataset, IterableDataset):
return dataset.map(
_standardize_dataset,
batched = True,
batch_size = dataset._ex_iterable.batch_size,
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting on ._ex_iterable - I actually am not super interested with this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason I did this is to pass the batch_size, if you are using a iterable dataset, you might have a resource restrained system and by default it would use a batch size of 1000, but this way it will keep the same batch size it had before the _standardize_datasetformatting.

desc = "Unsloth: Standardizing formats"
)
else:
from multiprocessing import cpu_count
num_proc = cpu_count()

return dataset.map(
_standardize_dataset,
batched = True,
desc = "Unsloth: Standardizing formats",
num_proc = num_proc,
)
pass


Expand Down