Skip to content

Optimize tuple unpacking in skyrl_gym_generator.generate()#138

Merged
tyler-griggs merged 2 commits intoNovaSky-AI:mainfrom
davenpi:optimize-generator-tuple-unpacking
Aug 15, 2025
Merged

Optimize tuple unpacking in skyrl_gym_generator.generate()#138
tyler-griggs merged 2 commits intoNovaSky-AI:mainfrom
davenpi:optimize-generator-tuple-unpacking

Conversation

@davenpi
Copy link
Contributor

@davenpi davenpi commented Aug 9, 2025

Hey! 👋🏾

Love what you're building with SkyRL - the modular architecture makes the system level challenges super clear!

I was exploring the codebase and noticed an opportunity to optimize the tuple unpacking in skyrl_gym_generator.py. The current implementation iterates through all_outputs 5 separate times and uses sum() for list concatenation, which has O(n²) complexity.

What I Changed

Replaced the multiple iterations with a single zip(*all_outputs) call to transpose the data in one pass.

Before:

responses = sum([[output[0]] for output in all_outputs], [])
rewards = sum([[output[1]] for output in all_outputs], [])
# ... 3 more similar lines

After:

responses, rewards, stop_reasons, loss_masks, prompt_token_ids = map(list, zip(*all_outputs))

Performance Impact

I wrote a quick benchmark script and the speedup scales nicely with batch size:

  • Small batches (10 items): ~5x faster
  • Large batches (1000 items): ~50x faster (2.51ms → 0.05ms)

The optimization maintains identical functionality - just more efficient execution. Happy to discuss any questions!

Replace sum() concatenation with zip() transposition.
Reduces execution time by ~98% on large batches (48x speedup).
Single iteration instead of 5 separate passes through data.
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.

Summary of Changes

Hello @davenpi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

I've submitted a pull request to enhance the performance of the skyrl_gym_generator.py module. The core change involves refactoring the tuple unpacking process within the generate function. Previously, this was handled by multiple inefficient sum() calls, leading to quadratic time complexity for list concatenation. My update replaces this with a single, optimized line utilizing zip(*all_outputs) and map(list, ...), which transposes the data in a single pass. This modification not only simplifies the code but also delivers substantial speed improvements, particularly noticeable with larger data batches.

Highlights

  • Performance Optimization: I've optimized the tuple unpacking logic within the generate method of skyrl_gym_generator.py.
  • Code Simplification: The previous implementation iterated through all_outputs five separate times and used sum() for list concatenation, which has O(n²) complexity. I've replaced this with a single, more Pythonic line using map(list, zip(*all_outputs)) to transpose the data efficiently.
  • Performance Gains: This change yields significant performance improvements, with benchmarks showing a ~5x speedup for small batches and a ~50x speedup for large batches (1000 items), reducing processing time from 2.51ms to 0.05ms.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

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 provides a great performance optimization by replacing an inefficient tuple unpacking method with a more Pythonic and faster approach using zip. My review focuses on an edge case where the new implementation could fail. I've identified that an empty input list would cause a ValueError and have provided a suggestion to handle this case gracefully, making the implementation more robust.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Copy link
Member

@tyler-griggs tyler-griggs left a comment

Choose a reason for hiding this comment

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

Hi @davenpi , thanks for your interest in SkyRL! I'm glad you've enjoyed using it and have had a chance to start digging into the codebase.

LGTM

@tyler-griggs tyler-griggs merged commit bb4cb94 into NovaSky-AI:main Aug 15, 2025
3 checks passed
SumanthRH added a commit that referenced this pull request Aug 15, 2025
…e() " (#150)

# What does this PR do?


Reverts #138 since it's incorrect after #145 - `rollout_logprobs` is
also returned now and it's dtype is `Optional[List[float]]`.

Also simplifies original approach a bit

---------

Signed-off-by: SumanthRH <sumanthrh99@gmail.com>
dzorlu referenced this pull request in fleet-ai/SkyRL Feb 4, 2026
Hey! 👋🏾

Love what you're building with SkyRL - the modular architecture makes
the system level challenges super clear!

I was exploring the codebase and noticed an opportunity to optimize the
tuple unpacking in `skyrl_gym_generator.py`. The current implementation
iterates through `all_outputs` 5 separate times and uses `sum()` for
list concatenation, which has O(n²) complexity.

## What I Changed
Replaced the multiple iterations with a single `zip(*all_outputs)` call
to transpose the data in one pass.

**Before:**
```python
responses = sum([[output[0]] for output in all_outputs], [])
rewards = sum([[output[1]] for output in all_outputs], [])
# ... 3 more similar lines
```

**After:**
```python
responses, rewards, stop_reasons, loss_masks, prompt_token_ids = map(list, zip(*all_outputs))
```

## Performance Impact
I wrote a quick benchmark script and the speedup scales nicely with
batch size:
- Small batches (10 items): ~5x faster
- Large batches (1000 items): ~50x faster (2.51ms → 0.05ms)

The optimization maintains identical functionality - just more efficient
execution. Happy to discuss any questions!

---------

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
dzorlu referenced this pull request in fleet-ai/SkyRL Feb 4, 2026
…e() " (#150)

# What does this PR do?


Reverts #138 since it's incorrect after #145 - `rollout_logprobs` is
also returned now and it's dtype is `Optional[List[float]]`.

Also simplifies original approach a bit

---------

Signed-off-by: SumanthRH <sumanthrh99@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants