Skip to content

Conversation

kianpu34593
Copy link
Contributor

@kianpu34593 kianpu34593 commented Jul 20, 2025

Summary

This is related to the recent pull request #219. In addition to what @t-reents was suggesting, I found there is an improvement can be done in InFlightAutoBatcher during the initiation of batch.

  • The current batch initialization uses memory from the first_state to estimate the max_memory_scaler. After that, states are added using states = self._get_next_states(), based on the estimated max_memory_scaler. After this, max_memory_scaler is being estimated again using estimate_max_memory_scaler. This function results in a higher max_memory_scaler. This means that more states can be added in the first batch.

What I added is these two lines:

newer_states = self._get_next_states()
states = [*states, *newer_states]

This allows more states to be added based on the new max_memory_scaler in the first batch.

Checklist

Before a pull request can be merged, the following items must be checked:

  • Doc strings have been added in the Google docstring format.
    Run ruff on your code.
  • Tests have been added for any new functionality or bug fixes.

Summary by CodeRabbit

  • Improvements
    • Enhanced batching logic to include more states in the initial batch, potentially improving performance and efficiency for users processing large datasets.

Copy link

cla-bot bot commented Jul 20, 2025

We require contributors to sign our Contributor License Agreement (CLA), and we don't have record of your signature. In order for us to review and merge your code, please sign the CLA.

Copy link

coderabbitai bot commented Jul 20, 2025

Walkthrough

The _get_first_batch method in the InFlightAutoBatcher class was updated to call _get_next_states() twice. After estimating and adjusting the max_memory_scaler using the initial states, the method fetches additional states and appends them before forming the initial batch, thus increasing the batch size.

Changes

File Change Summary
torch_sim/autobatching.py Modified _get_first_batch to call _get_next_states() twice, increasing initial batch size.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant InFlightAutoBatcher
    participant Iterator

    User->>InFlightAutoBatcher: Initialize and call _get_first_batch()
    InFlightAutoBatcher->>Iterator: _get_next_states()
    Iterator-->>InFlightAutoBatcher: Return initial states
    InFlightAutoBatcher->>InFlightAutoBatcher: Estimate and adjust max_memory_scaler
    InFlightAutoBatcher->>Iterator: _get_next_states() (second call)
    Iterator-->>InFlightAutoBatcher: Return more states
    InFlightAutoBatcher->>InFlightAutoBatcher: Append new states to previous
    InFlightAutoBatcher->>InFlightAutoBatcher: Concatenate all states for first batch
Loading

Poem

A batcher with memory, clever and spry,
Now gathers more states as time passes by.
Twice it will fetch, before the first run,
Ensuring its batch is second to none.
With extra states gathered, it hops on ahead—
A rabbit’s delight, more data to be fed! 🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 317985c and 4423fb9.

📒 Files selected for processing (1)
  • torch_sim/autobatching.py (1 hunks)
🔇 Additional comments (1)
torch_sim/autobatching.py (1)

955-956: LGTM! Smart optimization to maximize batch utilization.

This change effectively leverages the improved max_memory_scaler estimate from estimate_max_memory_scaler() to include additional states in the first batch. The logic is sound:

  1. Initial rough estimate fills the batch partially
  2. More accurate estimation typically yields a higher limit
  3. Second call to _get_next_states() fills remaining capacity

The _get_next_states() method already has built-in safeguards to prevent exceeding memory limits, making this a safe optimization.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@curtischong
Copy link
Collaborator

Thank you for your contribution and similar to the other PR I didn't work on the autobatcher so I'm not too familiar with it at the moment. I'll have more free time on the weekend and will have better things to say then!

Copy link
Collaborator

@orionarcher orionarcher left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution @kianpu34593! This is a smart and common sense fix for maximizing the first batch memory. I was frustrated by the difference and wish I'd thought of it myself! LGTM.

@cla-bot cla-bot bot added the cla-signed Contributor license agreement signed label Jul 25, 2025
@CompRhys CompRhys merged commit 5371cb6 into Radical-AI:main Jul 25, 2025
3 checks passed
@CompRhys
Copy link
Collaborator

Thanks for this smart fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed Contributor license agreement signed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants