Skip to content

Conversation

@hypdeb
Copy link
Contributor

@hypdeb hypdeb commented Dec 5, 2025

Purpose

Sampling randomly directly from a tokenizer for benchmarking creates data that is not ideal to benchmark when using speculative decoding or expert parallelism.

On the other hand, random datasets are very flexible and offer complete control on the input and output sequence lengths, which is desirable to create reproducible benchmarks.

This PR introduces a new type of benchmarking dataset called TxtSlicesDataset which offers a compromise between the flexibility of a random dataset and the fidelity of a real dataset. It allows sampling slices from a user-provided txt file.

Content

  • The implementation of TxtSlicesDataset
  • Fixes to typing in datasets.py
  • Factored out internal utils from datasets.py in an attempt to bring the file to a more manageable size
  • A unit test for the new dataset type

Julien Debache and others added 2 commits December 5, 2025 13:13
…al utils to another file, added basic test

Signed-off-by: Julien Debache <jdebache@cpu-0007.cm.cluster>

Signed-off-by:  <>
@mergify mergify bot added the performance Performance-related issues label Dec 5, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces TxtSlicesDataset for benchmarking, which samples data from a text file. It also includes significant refactoring by moving utility functions from datasets.py to a new dataset_utils.py file and improving typing throughout. The changes are well-structured. My review focuses on improving the robustness and reproducibility of the new TxtSlicesDataset and its tests. I've pointed out a resource leak in the tests and potential for non-reproducible behavior due to the use of the global random module. I've also identified a missing check that could lead to a crash with certain input files.

Comment on lines +28 to +32
def test_txt_slices(hf_tokenizer: PreTrainedTokenizerBase) -> None:
# Write the text content to a temporary file
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:
f.write(text_content)
temp_file_path = f.name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The test creates a temporary file using tempfile.NamedTemporaryFile with delete=False but never cleans it up. This will leave temporary files on the system after the test suite runs, which can accumulate and consume disk space. It's better to use pytest's built-in tmp_path fixture, which automatically manages the lifecycle of temporary directories and files for tests.

With this change, you will also need to update the TxtSlicesDataset instantiation on line 34 to use str(temp_file_path).

def test_txt_slices(hf_tokenizer: PreTrainedTokenizerBase, tmp_path) -> None:
    # Write the text content to a temporary file
    temp_file_path = tmp_path / "test.txt"
    temp_file_path.write_text(text_content)

if len(self.text) == 0:
raise ValueError("The text file is empty and cannot be sampled from.")

random.seed(self.random_seed)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using random.seed() seeds the global random number generator, which can lead to non-reproducible benchmarks if other parts of the code also use the global random module. To ensure reproducibility and avoid side effects, it's better to use a dedicated random.Random instance for this class. You will also need to update generate_prompt to use this instance.

Suggested change
random.seed(self.random_seed)
self.rng = random.Random(self.random_seed)

num_available_tokens = len(token_ids)

# Randomly select a start position
start_pos = random.randint(0, num_available_tokens - 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

To complete the change to using a dedicated random number generator instance, this call should use self.rng.randint() instead of the global random.randint() to ensure benchmark reproducibility.

Suggested change
start_pos = random.randint(0, num_available_tokens - 1)
start_pos = self.rng.randint(0, num_available_tokens - 1)

**kwargs,
) -> list[SampleRequest]:
# Tokenize the entire text content
token_ids = self.get_token_ids(tokenizer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The code tokenizes the text but doesn't handle the case where the tokenization results in an empty list of tokens (e.g., if the text file contains only whitespace). This will lead to a ValueError in generate_prompt from random.randint(0, -1) or a ZeroDivisionError from the modulo operation if len(token_ids) is 0. An explicit check should be added after tokenization to prevent this crash.

Suggested change
token_ids = self.get_token_ids(tokenizer)
token_ids = self.get_token_ids(tokenizer)
if not token_ids:
raise ValueError("Tokenized text is empty and cannot be sampled from.")

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +1775 to +1776
input_len=args.txt_slices_input_len,
output_len=args.txt_slices_output_len,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Define txt-slices CLI args used in get_samples

The txt-slices branch of get_samples reads args.txt_slices_input_len and args.txt_slices_output_len, but add_dataset_parser only defines the random input/output options and never creates these txt-slices attributes. Passing --dataset-name txt-slices will therefore raise an AttributeError before sampling. Please add parser definitions for these arguments or reuse the existing random lengths when wiring the dataset.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant