Replace shape-based empty batch handling inside DPDataLoader with structure-aware approach (#806)#812
Closed
iden-kalemaj wants to merge 1 commit into
Closed
Conversation
…tructure-aware approach (meta-pytorch#806) Summary: ## Types of changes - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [x] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Docs change / refactoring / dependency upgrade ## Motivation and Context / Related issue Replaces unstable shape-based empty batch handling with a stateful approach that learns and replicates the actual output structure from `collate_fn`. This fixes a critical bug where custom collate functions returning non-list structures (dicts, custom classes) were incompatible with Poisson sampling. The old implementation inspected `dataset[0]` to pre-compute shapes, then hardcoded empty batches as lists: ```python def collate(batch, collate_fn, sample_empty_shapes, dtypes): if len(batch) > 0: return collate_fn(batch) # Could return dict, custom class, etc. else: return [torch.zeros(shape, dtype=dtype) for ...] # Always list! ``` Bug -> if `collate_fn` returns a dict, non-empty batches are dicts but empty batches are lists -> type mismatch crash Existing, related issue: meta-pytorch#534 ### Solution: New `CollateFnWithEmpty` learns the structure from the first non-empty batch: ```python class CollateFnWithEmpty: def __call__(self, batch): if len(batch) > 0: output = self.wrapped_collator_fn(batch) if self.first_batch is None: self.first_batch = copy.deepcopy(output) # Learn structure else: output = self._make_empty_batch(self.first_batch) # Replicate structure return output ``` Now empty batches match the structure of non-empty batches, regardless of what `collate_fn` returns. If the first non-empty batch is actually the first batch, then it returns an error: ```python if self.first_batch is None: raise ValueError( "First sampled batch cannot be empty. Please ensure your dataset " "has sufficient samples or increase sample_rate." ) ``` ### Key Changes - Removed: `shape_safe()`, `dtype_safe()`, hardcoded list return - Added: `CollateFnWithEmpty` class with recursive structure replication - Changed: `wrap_collate_with_empty()` signature: `(collate_fn, sample_empty_shapes, dtype)` -> `(collate_fn, batch_first, rand_on_empty)` It is compatible with existing API. A small disclosure: for small percentage of users who hacked around empty batches handling, it might cause problems but in majority of cases it should be compatible. ## How Has This Been Tested (if it applies) - We used this approach to fine-tune `Qwen 7B` model using `trl` library for model alignment - Tested on `Mellum` 5B parameter model fine-tuning ## Checklist - [ ] The documentation is up-to-date with the changes I made. - [] I have read the **CONTRIBUTING** document and completed the CLA (see **CONTRIBUTING**). - [x] All tests passed, and additional code has been covered with new tests. Test Plan: Imported from GitHub, without a `Test Plan:` line. Unit tests Differential Revision: D98312879 Pulled By: iden-kalemaj
|
@iden-kalemaj has exported this pull request. If you are a Meta employee, you can view the originating Diff in D98312879. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary:
Types of changes
Motivation and Context / Related issue
Replaces unstable shape-based empty batch handling with a stateful approach that learns and replicates the actual output structure from
collate_fn. This fixes a critical bug where custom collate functions returning non-list structures (dicts, custom classes) were incompatible with Poisson sampling.The old implementation inspected
dataset[0]to pre-compute shapes, then hardcoded empty batches as lists:Bug -> if
collate_fnreturns a dict, non-empty batches are dicts but empty batches are lists -> type mismatch crashExisting, related issue: #534
Solution:
New
CollateFnWithEmptylearns the structure from the first non-empty batch:Now empty batches match the structure of non-empty batches, regardless of what
collate_fnreturns.If the first non-empty batch is actually the first batch, then it returns an error:
Key Changes
shape_safe(),dtype_safe(), hardcoded list returnCollateFnWithEmptyclass with recursive structure replicationwrap_collate_with_empty()signature:(collate_fn, sample_empty_shapes, dtype)->(collate_fn, batch_first, rand_on_empty)It is compatible with existing API.
A small disclosure: for small percentage of users who hacked around empty batches handling, it might cause problems but in majority of cases it should be compatible.
How Has This Been Tested (if it applies)
Qwen 7Bmodel usingtrllibrary for model alignmentMellum5B parameter model fine-tuningChecklist
Test Plan:
Imported from GitHub, without a
Test Plan:line.Unit tests
Differential Revision: D98312879
Pulled By: iden-kalemaj