Skip to content
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

internal change #706

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions grain/_src/python/dataset/transformations/packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,35 @@ def __iter__(self) -> dataset.DatasetIterator:
meta_features=self._meta_features,
)

@classmethod
def first_row_to_pack_element(
cls,
element_feature_lengths: PyTree[int],
num_packing_bins: int,
length_struct: PyTree[int],
first_free_cell_per_row: PyTree[int],
) -> int | None:
"""Returns the first row to pack an element into or None if it can't fit.

The logic is the same as the one used in the packing iterator.

Args:
element_feature_lengths: The lengths of each feature in the element.
num_packing_bins: The number of packing bins.
length_struct: The max length of each feature.
first_free_cell_per_row: The first free cell per row.
"""
row_or_failing_component = packing_packed_batch.PackedBatch.can_add_at_row(
element_feature_lengths,
num_packing_bins,
length_struct,
first_free_cell_per_row,
)
if row_or_failing_component.row is not None:
return row_or_failing_component.row
else:
return None


class FirstFitPackDatasetIterator(dataset.DatasetIterator):
"""Iterator for the first-fit packing transformation."""
Expand Down
18 changes: 18 additions & 0 deletions grain/_src/python/dataset/transformations/packing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,24 @@ def test_nested_feature(self, mark_as_meta_feature: bool):
):
_ = next(iter(ld))

def test_first_row_to_pack_element(self):
element = {"a": 1, "b": 1}
num_packing_bins = 2
length_struct = {"a": 10, "b": 10}
first_free_cell_per_row = {
"a": np.zeros(num_packing_bins, dtype=np.int64),
"b": np.zeros(num_packing_bins, dtype=np.int64),
}
self.assertEqual(
packing.FirstFitPackIterDataset.first_row_to_pack_element(
element,
num_packing_bins,
length_struct,
first_free_cell_per_row,
),
0,
)


if __name__ == "__main__":
absltest.main()
Loading