-
Notifications
You must be signed in to change notification settings - Fork 436
Improving packed sequences SFT for large datasets #2395
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
Merged
cuichenx
merged 6 commits into
NVIDIA-NeMo:main
from
shaltielshmid:feature/improved_sft_packing
Mar 16, 2026
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
399dd7f
Enables packed sequence dataset in mmap format
shaltielshmid c02660b
Removed memmap implementation, left optimizations
shaltielshmid e91e771
Updated test
shaltielshmid 96fde10
Addressed coderabbit comments
shaltielshmid f3dd874
pre-commit run --all-files
shaltielshmid 071b97a
Merge branch 'main' into feature/improved_sft_packing
yaoyu-33 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 |
|---|---|---|
|
|
@@ -15,6 +15,10 @@ | |
| import logging | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from multiprocessing import Pool | ||
| import multiprocessing as mp | ||
| from tqdm import tqdm | ||
| from typing import Literal, List | ||
|
|
||
| import numpy as np | ||
| from megatron.core.msc_utils import MultiStorageClientFeature | ||
|
|
@@ -26,6 +30,20 @@ | |
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| _shared_dataset = None | ||
| def _tokenize_get_item(i): | ||
| return _shared_dataset[i] | ||
|
|
||
| def _tokenize_init_worker(dataset): | ||
| global _shared_dataset | ||
| _shared_dataset = dataset | ||
|
|
||
| def _retrieve_tokenized(dataset, num_workers): | ||
| if num_workers == 1: | ||
| return np.array([dataset[i] for i in tqdm(range(len(dataset)))]) | ||
| num_workers = num_workers if num_workers > 0 else mp.cpu_count() | ||
| with Pool(num_workers, initializer=_tokenize_init_worker, initargs=(dataset,)) as pool: | ||
| return np.array(list(tqdm(pool.imap(_tokenize_get_item, range(len(dataset))), total=len(dataset)))) | ||
|
|
||
| def tokenize_dataset( | ||
| path: Path, | ||
|
|
@@ -34,6 +52,7 @@ def tokenize_dataset( | |
| seed: int, | ||
| dataset_kwargs: dict | None = None, | ||
| pad_seq_to_mult: int | None = 1, | ||
| num_tokenizer_workers: int = -1, | ||
| ): | ||
| """ | ||
| Tokenizes a dataset from the provided path using the specified tokenizer | ||
|
|
@@ -88,7 +107,7 @@ def tokenize_dataset( | |
| pad_id = dataset.tokenizer.eod | ||
| pad_seq_length_to_mult = dataset.pad_seq_length_to_mult | ||
| max_seq_length = dataset.max_seq_length | ||
| dataset = np.array([dataset[i] for i in range(len(dataset))]) | ||
| dataset = _retrieve_tokenized(dataset, num_tokenizer_workers) | ||
|
|
||
| if pad_seq_to_mult > 1: | ||
|
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. Potential TypeError when If Proposed fix- if pad_seq_to_mult > 1:
+ if pad_seq_to_mult is not None and pad_seq_to_mult > 1:🤖 Prompt for AI Agents |
||
|
|
||
|
|
@@ -120,7 +139,6 @@ def pre_pad_dataset(data, max_seq_length, max_length_to_pad, pad_id): | |
|
|
||
| return dataset | ||
|
|
||
|
|
||
| def prepare_packed_sequence_data( | ||
| input_path: Path, | ||
| output_path: Path, | ||
|
|
@@ -132,6 +150,7 @@ def prepare_packed_sequence_data( | |
| packing_algorithm: str = "first_fit_shuffle", | ||
| dataset_kwargs: dict | None = None, | ||
| pad_seq_to_mult: int | None = 1, | ||
| num_tokenizer_workers: int = -1, | ||
| ): | ||
| """ | ||
| Prepares a packed sequence dataset from a given input file and saves it to an output file. | ||
|
|
@@ -162,6 +181,7 @@ def prepare_packed_sequence_data( | |
| seed, | ||
| dataset_kwargs, | ||
| pad_seq_to_mult=pad_seq_to_mult, | ||
| num_tokenizer_workers=num_tokenizer_workers | ||
| ) | ||
| sequences, histogram = create_hist(dataset, max_seq_length) | ||
|
|
||
|
|
@@ -220,6 +240,12 @@ class PackedSequenceSpecs: | |
| This field is set by llm.finetune api. | ||
| """ | ||
|
|
||
| num_tokenizer_workers: int = -1 | ||
| """ | ||
| The number of worker processes to use for tokenization when preparing the packed sequence dataset. | ||
| If -1, the number of workers will be set to the number of CPU cores available | ||
| """ | ||
|
|
||
| packed_train_data_path: str = None | ||
| """ | ||
| If specified, use this file for the packed training dataset instead of the default path. | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.