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. #675

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
12 changes: 12 additions & 0 deletions grain/_src/python/dataset/transformations/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ py_test(
],
)

# copybara: strip_begin
py_test(
name = "fast_packing_test",
srcs = ["fast_packing_test.py"],
srcs_version = "PY3",
deps = [
":packing",
"//grain/_src/python/dataset",
],
)
# copybara: strip_end

py_library(
name = "packing_packed_batch",
srcs = ["packing_packed_batch.py"],
Expand Down
5 changes: 4 additions & 1 deletion grain/_src/python/dataset/transformations/packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
from collections.abc import Sequence
import copy
from typing import Any, Optional
from absl import logging
from grain._src.cpp.transformations.packing.python import packing
from grain._src.python.dataset import dataset
from grain._src.python.dataset import stats as dataset_stats
from grain._src.python.dataset.transformations import packing_packed_batch
from jaxtyping import PyTree # pylint: disable=g-importing-member
import numpy as np
import tree
Expand Down Expand Up @@ -289,6 +290,7 @@ def __init__(
num_packing_bins: int,
shuffle_bins: bool = True,
meta_features: Sequence[str] = (),
use_cc_version: bool = False,
):
"""Creates a dataset that packs sequences from the parent dataset.

Expand All @@ -300,6 +302,7 @@ def __init__(
shuffle_bins: Whether to shuffle bins after packing.
meta_features: Meta features that do not need *_segment_ids and
*_positions features.
use_cc_version: Whether to use the faster C++ implementation.
"""
super().__init__(parent)
self._length_struct = length_struct
Expand Down
55 changes: 36 additions & 19 deletions grain/_src/python/dataset/transformations/packing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ def _common_test_body(
shuffle_bins=shuffle_bins,
)
actual_elements = list(ld)
print("got", actual_elements)
print("want", expected_elements)
np.testing.assert_equal(len(actual_elements), len(expected_elements))

def _check_equivalence(path, actual_val, expected_val):
Expand All @@ -390,10 +392,8 @@ def _check_equivalence(path, actual_val, expected_val):
class FirstFitPackIterDatasetTest(parameterized.TestCase):
"""Tests for FirstFitPackIterDataset."""

@parameterized.parameters(
{"num_packing_bins": 1},
{"num_packing_bins": 2},
{"num_packing_bins": 3},
@parameterized.product(
num_packing_bins=[1, 2, 3],
)
def test_pack_sequences_length_3(self, num_packing_bins: int):
input_elements = [
Expand Down Expand Up @@ -447,11 +447,13 @@ def test_pack_sequences_length_3(self, num_packing_bins: int):
num_packing_bins=num_packing_bins,
)

@parameterized.parameters(
{"num_packing_bins": 3},
{"num_packing_bins": 5},
@parameterized.product(
num_packing_bins=[3, 5],
)
def test_pack_sequences_length_shuffle_bins(self, num_packing_bins: int):
def test_pack_sequences_length_shuffle_bins(
self,
num_packing_bins: int,
):
input_elements = [
{
"inputs": [1, 2, 3],
Expand Down Expand Up @@ -541,7 +543,10 @@ def test_pack_sequences_length_4(self):
]

_common_test_body(
input_elements, expected_elements, length_struct, num_packing_bins=2
input_elements,
expected_elements,
length_struct,
num_packing_bins=2,
)

def test_pack_sequences_length_5(self):
Expand Down Expand Up @@ -581,7 +586,10 @@ def test_pack_sequences_length_5(self):
]

_common_test_body(
input_elements, expected_elements, length_struct, num_packing_bins=2
input_elements,
expected_elements,
length_struct,
num_packing_bins=2,
)

def test_pack_sequences_length_6(self):
Expand Down Expand Up @@ -611,7 +619,10 @@ def test_pack_sequences_length_6(self):
}]

_common_test_body(
input_elements, expected_elements, length_struct, num_packing_bins=2
input_elements,
expected_elements,
length_struct,
num_packing_bins=2,
)

def test_pack_sequences_length_7(self):
Expand Down Expand Up @@ -641,7 +652,10 @@ def test_pack_sequences_length_7(self):
}]

_common_test_body(
input_elements, expected_elements, length_struct, num_packing_bins=1
input_elements,
expected_elements,
length_struct,
num_packing_bins=1,
)

def test_pack_sequences_different_lengths(self):
Expand Down Expand Up @@ -688,7 +702,10 @@ def test_pack_sequences_different_lengths(self):
},
]
_common_test_body(
input_elements, expected_elements, length_struct, num_packing_bins=3
input_elements,
expected_elements,
length_struct,
num_packing_bins=3,
)

def test_pack_sequences_two_dimensional_features(self):
Expand Down Expand Up @@ -738,14 +755,14 @@ def test_pack_sequences_two_dimensional_features(self):
]

_common_test_body(
input_elements, expected_elements, length_struct, num_packing_bins=2
input_elements,
expected_elements,
length_struct,
num_packing_bins=2,
)

@parameterized.parameters(
{"restore_at_step": 0},
{"restore_at_step": 1},
{"restore_at_step": 2},
{"restore_at_step": 3},
@parameterized.product(
restore_at_step=[0, 1, 2, 3],
)
def test_checkpointing(self, restore_at_step: int):
input_elements = [
Expand Down
Loading