-
Notifications
You must be signed in to change notification settings - Fork 330
Fix AssertionError during eval when val set size is not divisible by train_batch_size #1589
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
rishithayenumula
wants to merge
5
commits into
NovaSky-AI:main
Choose a base branch
from
rishithayenumula:fix-eval-batch-assertion
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.
+121
−14
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7185ef1
Fix eval crash when val set size is not divisible by train_batch_size
rishithayenumula 4adaade
Fix test assertion placement and reduce eval partial-batch log noise
rishithayenumula 6d7f74c
Trigger CI rerun after review updates
rishithayenumula dbb781d
Restore jaxtyping tensor annotations in preprocess
rishithayenumula bde201e
fix formatting via pre-commit (black)
rishithayenumula 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 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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |||||||||||||||||||||||||||||
| from typing import List, Optional, Tuple | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| import torch | ||||||||||||||||||||||||||||||
| from jaxtyping import Float, Integer | ||||||||||||||||||||||||||||||
| from transformers import AutoTokenizer | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| logger = logging.getLogger(__name__) | ||||||||||||||||||||||||||||||
|
|
@@ -39,13 +38,13 @@ def convert_prompts_responses_to_batch_tensors( | |||||||||||||||||||||||||||||
| rollout_expert_indices: Optional[List[List[List[List[int]]]]] = None, | ||||||||||||||||||||||||||||||
| max_seq_len: Optional[int] = None, | ||||||||||||||||||||||||||||||
| ) -> Tuple[ | ||||||||||||||||||||||||||||||
| Float[torch.Tensor, "batch seq_len"], | ||||||||||||||||||||||||||||||
| Float[torch.Tensor, "batch seq_len"], | ||||||||||||||||||||||||||||||
| Float[torch.Tensor, "batch response_len"], | ||||||||||||||||||||||||||||||
| Float[torch.Tensor, "batch response_len"], | ||||||||||||||||||||||||||||||
| Float[torch.Tensor, "batch response_len"], | ||||||||||||||||||||||||||||||
| Optional[Float[torch.Tensor, "batch response_len"]], | ||||||||||||||||||||||||||||||
| Optional[Integer[torch.Tensor, "batch seq_len layer_num topk"]], | ||||||||||||||||||||||||||||||
| torch.Tensor, | ||||||||||||||||||||||||||||||
| torch.Tensor, | ||||||||||||||||||||||||||||||
| torch.Tensor, | ||||||||||||||||||||||||||||||
| torch.Tensor, | ||||||||||||||||||||||||||||||
| torch.Tensor, | ||||||||||||||||||||||||||||||
| Optional[torch.Tensor], | ||||||||||||||||||||||||||||||
| Optional[torch.Tensor], | ||||||||||||||||||||||||||||||
|
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. The detailed shape annotations for the return types were removed. It is recommended to keep these for better maintainability and readability.
Suggested change
|
||||||||||||||||||||||||||||||
| ]: | ||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||
| Convert prompts and responses to batch tensors for training. | ||||||||||||||||||||||||||||||
|
|
@@ -196,6 +195,7 @@ def compute_prompt_mini_batch_boundaries( | |||||||||||||||||||||||||||||
| train_batch_size: int, | ||||||||||||||||||||||||||||||
| is_stepwise: bool, | ||||||||||||||||||||||||||||||
| n_samples_per_prompt: int, | ||||||||||||||||||||||||||||||
| is_training: bool = True, | ||||||||||||||||||||||||||||||
| ) -> List[Tuple[int, int]]: | ||||||||||||||||||||||||||||||
| """Compute mini-batch ``(start, end)`` slices from a flat ``uids`` list. | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
@@ -206,10 +206,12 @@ def compute_prompt_mini_batch_boundaries( | |||||||||||||||||||||||||||||
| train_batch_size: Number of prompts in a training batch. For sanity check. | ||||||||||||||||||||||||||||||
| is_stepwise: Whether the training is step-wise. For sanity check. | ||||||||||||||||||||||||||||||
| n_samples_per_prompt: how many samples per prompt. For sanity check. | ||||||||||||||||||||||||||||||
| is_training: Whether this is a training batch (strict validation) or eval batch (allows partial batches). | ||||||||||||||||||||||||||||||
| Defaults to True for backward compatibility. | ||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||
| List of (start, end) indices of the mini-batches. The length of the list is the number of | ||||||||||||||||||||||||||||||
| mini-batches, guaranteed to be `train_batch_size // mini_batch_size` regardless of whether | ||||||||||||||||||||||||||||||
| the training is step-wise or not. | ||||||||||||||||||||||||||||||
| mini-batches, guaranteed to be `train_batch_size // mini_batch_size` during training, but may differ | ||||||||||||||||||||||||||||||
| during evaluation if the final batch is partial. | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Consecutive equal entries in ``uids`` belong to the same prompt. Each mini batch spans exactly | ||||||||||||||||||||||||||||||
| ``mini_batch_size`` prompts (the last may be smaller if the total prompt count is not divisible | ||||||||||||||||||||||||||||||
|
|
@@ -244,23 +246,35 @@ def compute_prompt_mini_batch_boundaries( | |||||||||||||||||||||||||||||
| prompt_end_indices.append(i) | ||||||||||||||||||||||||||||||
| prompt_end_indices.append(len(uids)) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # seen_uids should equal to the number of prompts and equal to `train_batch_size` | ||||||||||||||||||||||||||||||
| # Check that num_prompts matches expected batch size | ||||||||||||||||||||||||||||||
| num_prompts = len(prompt_end_indices) | ||||||||||||||||||||||||||||||
| assert num_prompts == train_batch_size and len(seen_uids) == train_batch_size | ||||||||||||||||||||||||||||||
| assert train_batch_size % mini_batch_size == 0 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Compute boundaries. | ||||||||||||||||||||||||||||||
| if is_training: | ||||||||||||||||||||||||||||||
| assert num_prompts == train_batch_size and len(seen_uids) == train_batch_size, ( | ||||||||||||||||||||||||||||||
| f"Expected {train_batch_size} prompts in training batch, got {num_prompts}." | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| assert train_batch_size % mini_batch_size == 0 | ||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||
| if num_prompts != train_batch_size: | ||||||||||||||||||||||||||||||
| logger.info( | ||||||||||||||||||||||||||||||
| f"Partial batch detected during eval: got {num_prompts} prompts but " | ||||||||||||||||||||||||||||||
| f"train_batch_size={train_batch_size}. Using actual batch size for mini-batch boundaries." | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Compute boundaries. Handle partial batches during eval. | ||||||||||||||||||||||||||||||
| boundaries: List[Tuple[int, int]] = [] | ||||||||||||||||||||||||||||||
| start_seq = 0 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| for i in range(0, num_prompts, mini_batch_size): | ||||||||||||||||||||||||||||||
| end_prompt_idx = i + mini_batch_size - 1 # i + mini_batch_size is next mini-batch's first prompt's end index | ||||||||||||||||||||||||||||||
| end_prompt_idx = min(i + mini_batch_size - 1, num_prompts - 1) | ||||||||||||||||||||||||||||||
| end_seq = prompt_end_indices[end_prompt_idx] | ||||||||||||||||||||||||||||||
| boundaries.append((start_seq, end_seq)) | ||||||||||||||||||||||||||||||
| start_seq = end_seq | ||||||||||||||||||||||||||||||
| assert len(boundaries) == train_batch_size // mini_batch_size | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if is_training: | ||||||||||||||||||||||||||||||
| assert len(boundaries) == train_batch_size // mini_batch_size | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Assert that the mini-batch boundaries are uniform for non-step-wise training. | ||||||||||||||||||||||||||||||
| if not is_stepwise: | ||||||||||||||||||||||||||||||
| if not is_stepwise and is_training: | ||||||||||||||||||||||||||||||
| expected_num_seq_in_mini_batch = n_samples_per_prompt * mini_batch_size | ||||||||||||||||||||||||||||||
| for i, (start, end) in enumerate(boundaries): | ||||||||||||||||||||||||||||||
| assert start == i * expected_num_seq_in_mini_batch | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
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
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
Oops, something went wrong.
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.
The
jaxtypingimports and associated type annotations were removed in this file. These annotations provide valuable documentation regarding tensor shapes and dtypes, which is particularly helpful in complex batching logic. Unless there is a specific reason for their removal, they should be retained to maintain code clarity and type safety.