-
Notifications
You must be signed in to change notification settings - Fork 69
Temporarily use no packing in SFT #614
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
base: main
Are you sure you want to change the base?
Changes from all commits
ad346cd
b7652a9
32c9814
bd52608
240abf0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,65 @@ | |
| from typing import Any, Callable | ||
|
|
||
| import torch | ||
| import torch.nn.functional as F | ||
|
|
||
| from forge.data.utils import CROSS_ENTROPY_IGNORE_IDX | ||
|
|
||
|
|
||
| def collate_padded(batch: list[dict[str, Any]]) -> dict[str, Any]: | ||
| """ | ||
| Collate function that pads sequences to the longest sample in the batch. | ||
| Pads 'tokens' with 0 and 'labels' with CROSS_ENTROPY_IGNORE_IDX (-100). | ||
| Non-tensor fields (like metrics) are collected into lists and flattened | ||
| if all items are lists. | ||
|
Comment on lines
+19
to
+21
Contributor
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. Is it common practice to assume |
||
| Args: | ||
| batch: List of samples, each containing 'tokens' and 'labels' tensors | ||
| Returns: | ||
| Batched dict with padded tensors | ||
| """ | ||
| if not batch: | ||
| return {} | ||
|
|
||
| # Find max length in batch | ||
| max_len = max(sample["tokens"].size(0) for sample in batch) | ||
|
|
||
| # Initialize lists for batched tensors | ||
| tokens_list = [] | ||
| labels_list = [] | ||
|
|
||
| # Pad each sample to max_len | ||
| for sample in batch: | ||
| seq_len = sample["tokens"].size(0) | ||
| pad_len = max_len - seq_len | ||
|
|
||
| # Pad tokens with 0 | ||
| padded_tokens = F.pad(sample["tokens"], (0, pad_len), value=0) | ||
| tokens_list.append(padded_tokens) | ||
|
|
||
| # Pad labels with CROSS_ENTROPY_IGNORE_IDX (-100) | ||
| padded_labels = F.pad( | ||
| sample["labels"], (0, pad_len), value=CROSS_ENTROPY_IGNORE_IDX | ||
| ) | ||
| labels_list.append(padded_labels) | ||
|
|
||
| # Stack into batch | ||
| result = { | ||
| "tokens": torch.stack(tokens_list), | ||
| "labels": torch.stack(labels_list), | ||
| } | ||
|
|
||
| # Collect non-tensor fields (like metrics) | ||
| for key in batch[0].keys(): | ||
| if key not in ["tokens", "labels"]: | ||
| result[key] = [sample[key] for sample in batch] | ||
| # Flatten if all are lists | ||
| if all(isinstance(item, list) for item in result[key]): | ||
| result[key] = [item for sublist in result[key] for item in sublist] | ||
|
Comment on lines
+64
to
+66
Contributor
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. Is this a common practice? Feels like unnecessary operation / tribal knowledge |
||
|
|
||
| return result | ||
|
|
||
|
|
||
| def collate_packed( | ||
|
|
||
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.
Objection to start a main issue tracking the nightly build?
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.
Sounds good! But can we first nail down the different subtasks via the Google Doc I just shared? Then we can translate to a GI.