Repository navigation
Studio: truncate completion_mask / assistant_masks with pre-tokenized input_ids - #14884
Conversation
…ith pre-tokenized input_ids
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 27ea8e4a5c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| for name in _LOSS_COLUMNS: | ||
| if name not in table.column_names: | ||
| continue | ||
| if not pc.all(pc.equal(lengths, pc.list_value_length(table.column(name)))).as_py(): |
There was a problem hiding this comment.
Validate loss-mask lengths for streamed datasets
When the pre-tokenized input is a datasets.IterableDataset (the supported dataset_streaming path), it has no Arrow .data, so _check_arrow_table is never called and this new length check never runs. _truncate then slices the masks independently; a short mask can still reach TRL with a different length and fail during collation, while some overlong malformed masks can be silently made equal by truncation. Validate each streamed batch against input_ids before slicing so the promised early ValueError applies consistently.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in f16c640: the streamed path now checks each batch's masks against input_ids before slicing, with the same ValueError. Test test_prepare_checks_streamed_loss_masks.
| for name in _LOSS_COLUMNS: | ||
| if name not in table.column_names: | ||
| continue | ||
| if not pc.all(pc.equal(lengths, pc.list_value_length(table.column(name)))).as_py(): |
There was a problem hiding this comment.
Reject null values in loss-mask columns
When only some Arrow rows contain a mask, Hugging Face represents the missing values as nulls; pc.equal therefore returns null for those rows, and pc.all skips nulls by default. A column such as [[0, 1, 1], None] consequently passes this check and is returned unchanged when no truncation is needed, but TRL later attempts torch.tensor(None) and fails during collation. Treat a null comparison as a mismatch (or explicitly reject null list values) so every row is actually checked.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in f16c640: the mask comparison uses skip_nulls = False, so a row without the mask fails the check. Test test_prepare_rejects_a_row_without_its_loss_mask.
| for name in _LOSS_COLUMNS: | ||
| if name not in table.column_names: | ||
| continue | ||
| if not pc.all(pc.equal(lengths, pc.list_value_length(table.column(name)))).as_py(): |
There was a problem hiding this comment.
Skip loss-mask checks when explicit labels take precedence
For pre-tokenized datasets that already contain labels, Studio treats those labels as the complete objective (has_custom_labels disables response masking), and Unsloth's SFT preparation selects the labels-based collator rather than consuming completion_mask or assistant_masks. This unconditional loop newly rejects such datasets when an otherwise-unused mask metadata column has a different length, even though the same dataset trained correctly before this commit. Only require mask alignment when the corresponding mask can actually be used instead of explicit labels.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in f16c640: the mask checks only run when the dataset has no labels; with user labels the masks are unused and a dataset that trained before still does. Test test_prepare_ignores_unused_masks_next_to_user_labels. Truncation still applies to every token column.
…t missing rows, check streamed batches [skip ci]
|
@codex review |
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f16c64041d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| "completion_mask", | ||
| "assistant_masks", |
There was a problem hiding this comment.
Exclude unused masks from labeled truncation
When an explicitly labeled dataset has an overlong row and an unused mask column whose Arrow type is null (for example, every completion_mask value is None) or scalar metadata, _checked_masks correctly skips validation but _truncate_arrow still passes that column to pc.list_slice, which raises ArrowNotImplementedError and aborts preparation. Such mask columns are ignored by the labels-based training path and were not sliced before this change, so omit them from the truncation columns whenever labels is present.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in f6723ae: with labels present the mask columns are left out of the truncation columns entirely, so a null or scalar mask column next to labels is passed through untouched as before. Test test_prepare_leaves_unused_null_masks_next_to_labels_alone (raised ArrowNotImplementedError before). The formatter bot's commit had also reformatted test_diffusion_learned_peaks.py, which this PR does not touch; that file is restored.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f6723aef1c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| def _checked_masks(columns) -> tuple: | ||
| # User labels are the whole objective: the masks are then unused and not checked. | ||
| return () if "labels" in columns else tuple(c for c in _MASK_COLUMNS if c in columns) |
There was a problem hiding this comment.
Respect completion-only mode before checking completion masks
When a pre-tokenized dataset has no labels but carries a completion_mask, this treats that mask as active unconditionally. Studio does not pass completion_only_loss into SFTConfig (studio/backend/core/training/trainer.py:4948-4964,5104-5113), so TRL resolves it to false for input_ids-only language-modeling data; the repository's own supervision logic therefore gates completion_mask on that flag (unsloth/models/rl.py:1555-1566). Consequently, stale or malformed completion-mask metadata that downstream ignores—and that previously trained on the full sequence—now raises here or fails during list_slice; pass the resolved mode into preparation or leave this column untouched when completion-only loss is disabled.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, fixed in 92a2835. TRL only applies completion_mask when completion_only_loss resolves true, and with Studio leaving it unset that never happens for input_ids-only rows (TRL resolves None to "has prompt and completion columns", the same in 0.22.2 and 1.15). So checking the mask unconditionally rejected datasets that train fine. The mask checks now follow the same effective completion_only_loss the trainer uses: Studio passes its SFTConfig value in, an unset value resolves the way TRL resolves it, assistant_masks is still checked whenever present, and a mask the trainer ignores is left as it is. New tests cover stale completion masks with the flag unset or False (normal and streamed) and assistant_masks without the flag; tests/test_pretokenized_dataset.py: 34 passed.
…pplies it [skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 92a2835714
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if "labels" in columns: | ||
| return () |
There was a problem hiding this comment.
Remove or align masks beside explicit labels
When pre-tokenized rows contain explicit labels plus assistant_masks (or an enabled completion_mask), this early return excludes the active mask from both validation and truncation. TRL's collator constructs labels from the explicit column and then applies these masks on top, so truncating an overlong row can produce four-token IDs/labels alongside a six-token mask and fail during boolean indexing. Fresh evidence in the current tree is unsloth/models/rl.py:2991-3001, which likewise treats labels and active masks as intersecting supervision signals; either keep such masks aligned or remove them before they reach the collator.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed for TRL before 1.7, fixed in 9e31207. Up to 1.6 the collator takes the row's labels and then applies assistant_masks, and completion_mask under completion_only_loss, on top of them (trl v0.22.2 sft_trainer.py 200-250), so truncating ids and labels without the mask breaks the boolean index. From 1.7 the collator only reads labels, and TRL folds the masks in only when a dataset has no labels column (v1.15.0 sft_trainer.py 172 and 1405-1431), so there the masks beside user labels are unused. The masks beside user labels are now checked and truncated with the ids whenever the installed TRL's collator applies them, and left as they are on TRL 1.7 and later. New tests cover both mask names, in memory and streamed, plus the 1.7+ case; tests/test_pretokenized_dataset.py: 39 passed.
…ator applies them [skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9e31207c6b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| names = _column_names(dataset) | ||
| masks = _checked_masks(names, completion_only_loss) |
There was a problem hiding this comment.
Ignore mask columns hidden by the dataset format
When a Hugging Face dataset uses set_format(columns=["input_ids"], output_all_columns=False), column_names still includes backing columns that examples do not expose. Consequently, a hidden stale assistant_masks column is treated as active here and a length mismatch newly raises ValueError, even though TRL never receives or applies that mask. Determine active masks from the dataset's effective formatted columns rather than all backing columns.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, fixed in b1417d9. Dataset.format["columns"] with output_all_columns = False is what the trainer's rows contain, while column_names still lists the backing columns (datasets 3.6.0 arrow_dataset.py 2437-2443, 4.8.5 2814-2820, 5.1.0 2806-2812). The pre-tokenized checks now read the active columns from the format, so a hidden mask or labels column is neither checked nor treated as active. The truncation pass also kept only the format type, which dropped the column selection and re-exposed hidden columns; it now restores the full format. With output_all_columns = True a mask is still checked, and IterableDataset has no column selection, so streaming is unchanged. New tests cover a hidden stale mask with truncation and the output_all_columns = True case; tests/test_pretokenized_dataset.py: 41 passed.
Active columns follow Dataset.format (columns / output_all_columns), so a hidden stale mask or labels column is neither checked nor applied, and the truncation restores the full format so hidden columns stay hidden. [skip ci]
…ck masks hidden by the format TRL resolves an unset completion_only_loss once from the train split and uses it for eval too. Hidden mask columns are still applied by TRL 0.22-0.26 (with_transform resets the column selection) and 1.7+ (dataset.column_names plus map), so they are checked and truncated as well. [skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 79c6466e75
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| # with_transform, and 1.7+ reads dataset.column_names and builds labels with map(), which sees them all. | ||
| masks = _checked_masks(getattr(dataset, "column_names", None) or names, completion_only_loss) | ||
| # A mask the trainer will not apply is left as it is (it may not even be a list). | ||
| columns = [c for c in _TOKEN_COLUMNS if (c in names and c not in _MASK_COLUMNS) or c in masks] |
There was a problem hiding this comment.
Keep hidden labels aligned with truncated input IDs
When a map-style dataset uses set_format(columns=["input_ids"], output_all_columns=False) while retaining a backing labels column, this filters labels out of columns, so an overlong row gets four-token IDs but keeps its original labels. The TRL paths described immediately above re-expose backing columns, and its collator consumes labels whenever present, producing different input and target lengths and failing during training. Either remove labels that are intended to remain hidden before passing the dataset to TRL, or validate and truncate the backing label column alongside input_ids.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
A labels column hidden by set_format never reaches the collator next to truncated input_ids. Studio trains through Unsloth's SFTTrainer, whose _prepare_dataset (unsloth_zoo sft_prepare_dataset) reads the formatted row (column_names = set(next(iter(dataset)).keys())) and does not call with_transform, so dataset[i] still yields only input_ids after prepare_pretokenized_dataset and the Trainer's remove_columns. When the zoo builds labels with map() on TRL 1.7+, it overwrites the hidden column from the already truncated input_ids, and the column stays hidden. Stock TRL behaves the same way. In 0.22.2, with_transform (sft_trainer.py:858-859) exposes the column, but truncate_dataset (sft_trainer.py:1015, data_utils.py:722-727) then slices every list column, labels included. In 1.x, the hidden labels column skips label building (sft_trainer.py:1411-1412) and is truncated together with input_ids (sft_trainer.py:1451-1454). No path pairs truncated input_ids with untruncated labels, so the column is left as it is.
… error pc.list_value_length has no kernel for a null or scalar column, so such a mask raised ArrowNotImplementedError instead of the documented ValueError. Check the column is list-typed first. [skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f04c52eb04
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| completion_only_loss = resolve_completion_only_loss(dataset, completion_only_loss) | ||
| # Masks are checked even when set_format hides them: TRL 0.22-0.26 re-exposes every column through | ||
| # with_transform, and 1.7+ reads dataset.column_names and builds labels with map(), which sees them all. | ||
| masks = _checked_masks(getattr(dataset, "column_names", None) or names, completion_only_loss) |
There was a problem hiding this comment.
Use effective labels when deciding whether masks are active
Under supported TRL >= 1.7, a map-style dataset can hide a backing labels column with set_format while still exposing assistant_masks. Passing the unfiltered backing column_names here makes _checked_masks see those hidden labels and skip the active mask, whereas Unsloth's dataset preparation inspects the formatted row, sees no labels, and builds labels from that mask. A short mask therefore bypasses validation and produces labels shorter than input_ids, failing during collation or training; use the effective names for the labels decision while discovering mask columns separately.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changed. When set_format hides a labels column, the masks are not applied on TRL 1.7+ on either preparation path, so line 152 reading the backing column_names matches what training does. Unsloth's sft_prepare_dataset (unsloth_zoo/dataset_utils.py:3033-3051 on main) does build labels from the mask when the formatted row has no labels, but it writes them with dataset.map, and datasets' transmit_format (arrow_dataset.py:549 and :564 in datasets 3.6.0) keeps any column that was not formatted hidden after the map. So the rebuilt labels land in the hidden labels column and stay hidden, and the mask is removed. Reproduced: with input_ids [1,2,3,4], hidden labels and a 2-long assistant_masks, the row after that block is {'input_ids': [1, 2, 3, 4]}, and the short labels exist only in the hidden backing column. The collator then builds labels from input_ids (TRL v1.16.0 sft_trainer.py:171), so no short labels reach collation. TRL's own path (v1.16.0 sft_trainer.py:1411-1412) reads get_dataset_column_names, sees the hidden labels and skips the masks too. On TRL < 1.7 the labels check does not apply, since the collator applies the masks on top of labels (v0.22.2 sft_trainer.py:200-248). Using the effective names here would reject or rewrite a mask that no trainer reads.
| for ids, mask in zip(batch["input_ids"], batch[name]): | ||
| if mask is None or ids is None or len(mask) != len(ids): | ||
| raise _mask_error(name) |
There was a problem hiding this comment.
Reject scalar masks explicitly in streamed batches
For an IterableDataset whose active mask column contains scalar values, such as assistant_masks: 0, evaluating len(mask) raises an unhandled TypeError instead of the mask-specific ValueError produced for the same schema on the Arrow path. Validate that each streamed mask is list-like before taking its length so malformed streamed datasets receive the promised early, actionable error.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in aee074a. The streamed check (pretokenized.py:169-178) now turns the TypeError from len() on a scalar mask (None, int, float, bool, numpy scalar, 0-d array or tensor) into the same "'assistant_masks' must have the same length as 'input_ids'" ValueError the Arrow path raises. Lists, tuples, 1-d numpy arrays and torch tensors are still accepted and truncated. test_prepare_rejects_streamed_scalar_masks_with_the_mask_error covers scalar masks unformatted and under numpy and torch format and fails without the change; test_prepare_truncates_streamed_array_masks checks that numpy and torch masks still truncate.
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…#15018) `tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding[8]` fails on `main`, for example in `Repo tests (CPU, rest)` on #15009 and #15010: ``` studio/backend/tests/test_pretokenized_dataset.py:313: read_text() ``` #14884 added a read of `core/training/trainer.py` with the platform default encoding. This passes `encoding = "utf-8"`. ## Verification - On `main`: `-k checked_in_file_reads` gives 1 failed, 15 passed. - On this branch: 16 passed. - `studio/backend/tests/test_pretokenized_dataset.py`: 56 passed.
Drops the earlier auto-format of unrelated unsloth/ and tests/ files (AST-equal) so the PR only touches Studio. [skip ci]
Pre-tokenized datasets (#13816) are truncated to
max_seq_lengthcolumn by column, but onlyinput_ids,attention_mask,labelsandtoken_type_idswere truncated. Rows withoutlabelskeep TRL's collator when they carrycompletion_maskorassistant_masks, and TRL applies those masks to the labels, so a row longer thanmax_seq_lengthfailed on its first step:completion_maskandassistant_masksare now truncated withinput_ids.labels, they must have the same length asinput_idsin every row; a mismatch is a clearValueErrorbefore training instead of the error above.Tests: 4 new cases in
test_pretokenized_dataset.py(truncation and length check for both masks) fail on main and pass here; the pre-tokenized and token preview suites pass (52).