Skip to content

Simplify to_constant_volume_bins#562

Open
curtischong wants to merge 1 commit into
TorchSim:mainfrom
curtischong:clean-to_constant_volume_bins
Open

Simplify to_constant_volume_bins#562
curtischong wants to merge 1 commit into
TorchSim:mainfrom
curtischong:clean-to_constant_volume_bins

Conversation

@curtischong
Copy link
Copy Markdown
Collaborator

@curtischong curtischong commented May 14, 2026

Summary

By standardizing all of the different formats into a single datatype before running the algorithm, we no longer need isinstance checks, which simplifies the algorithm (since the algorithm is only handling one type of object)

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.

@curtischong curtischong marked this pull request as draft May 14, 2026 23:34
Comment thread torch_sim/autobatching.py

bins = [[]] if is_tuple_list else [{}]
else:
weights = sorted(items, key=lambda x: -x)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

all of the above logic to get this reverse weights list is simplified to entries.sort(key=lambda e: -e[0]) below

Comment thread torch_sim/autobatching.py
if not is_dict and not is_tuple_list and isinstance(items[0], (tuple, list)):
raise ValueError("Must provide weight_pos or key for tuple list")

weights = _get_bins(weights, valid_ndcs)
Copy link
Copy Markdown
Collaborator Author

@curtischong curtischong May 15, 2026

Choose a reason for hiding this comment

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

The above logic to get all of the valid indices is simplified to:

    if lower_bound is not None:
        entries = [e for e in entries if e[0] > lower_bound]
    if upper_bound is not None:
        entries = [e for e in entries if e[0] < upper_bound]

Comment thread torch_sim/autobatching.py
weights = _get_bins(weights, valid_ndcs)

if isinstance(items, dict):
keys = _get_bins(keys, valid_ndcs)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

this is no longer needed since we operate on the new tuple objects, which automatically carries the keys in the entries list

Comment thread torch_sim/autobatching.py
for weight, payload in entries:
# get all bin indices that can fit this payload
candidate_bin_indices = [
i for i, s in enumerate(bin_sums) if s + weight <= max_volume
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

one thing I noticed that was missing in the original implementation (I didn't add it here). but I think we need to have an assert to ensure that all weights are <= max_volume? bc if there contains an item that exceeds max_volume, we can never fit it into a bin, even a new bin

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

maybe we add this line around 120?

    # warn on oversized items — they'll still be placed in a bin by themselves,
    # but that bin will exceed max_volume
    for weight, payload in entries:
        if weight > max_volume:
            logger.warning(
                "item %r has weight %s > max_volume %s; placing in its own bin anyway",
                payload,
                weight,
                max_volume,
            )

Right now, we allow items to overflow the max bin size. some tests fail if we add this line and throw an exception

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I actually wanted this to be an exception, but I can see cases where people might want the weight to exceed the max volume so maybe it's not necessary to add this in.

@curtischong curtischong force-pushed the clean-to_constant_volume_bins branch from 32b395a to 10b47c4 Compare May 15, 2026 00:59
clean fn

cleanup ruff

add back len check

manual comments

backup

better logic

add max volume check

remove

fix ruff
@curtischong curtischong force-pushed the clean-to_constant_volume_bins branch from 10b47c4 to 0341adb Compare May 15, 2026 01:03
@curtischong curtischong changed the title Cleanup to_constant_volume_bins Simplify to_constant_volume_bins May 15, 2026
@curtischong curtischong marked this pull request as ready for review May 15, 2026 14:23
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.

1 participant